예제 #1
0
void floydWarshallInit(IHypergraph<Arc> const& hg, Util::Matrix<typename ArcWtFn::Weight>* pdistances,
                       ArcWtFn arcWtFn) {
  typedef typename ArcWtFn::Weight Weight;
  Util::Matrix<Weight>& dist = *pdistances;
  dist.setDiagonal(Weight::one(), Weight::zero());
  for (StateId tail = 0, numStates = (StateId)dist.getNumRows(); tail < numStates; ++tail) {
    Weight* rowTail = dist.row(tail);
    for (ArcId aid : hg.outArcIds(tail)) {
      Arc* arc = hg.outArc(tail, aid);
      StateId head = arc->head();
      Hypergraph::plusBy(arcWtFn(arc), rowTail[head]);
    }
  }
}
예제 #2
0
void stringPairToFst(Strings const& inputTokens, std::vector<std::string> const& outputTokens,
                     IMutableHypergraph<Arc>* pHgResult,
                     StringToHypergraphOptions const& opts = StringToHypergraphOptions()) {
  if (inputTokens.size() != outputTokens.size()) {
    SDL_THROW_LOG(Hypergraph.stringPairToFst, IndexException,
                  "The two strings must have same number of words");
  }

  // 1. Create simple FSA from input tokens:
  stringToHypergraph(inputTokens, pHgResult, opts);

  // 2. Insert output tokens:
  IVocabularyPtr pVoc = pHgResult->getVocabulary();
  std::vector<std::string>::const_iterator it = outputTokens.begin();
  StateId stateId = pHgResult->start();
  const StateId finalId = pHgResult->final();
  while (stateId != finalId) {
    Arc* arc = pHgResult->outArc(stateId, 0);
    const Sym sym = opts.terminalMaybeUnk(pVoc.get(), *it);
    setFsmOutputLabel(pHgResult, *arc, sym);
    ++it;
    stateId = arc->head();
  }
}