Example #1
0
 bool inputTransformInPlace(IHypergraph<Arc> const& hg, int) {
   if (empty.empty)
     outProperty("EMPTY", Hypergraph::empty(hg));
   if (empty.fsm)
     outProperty("FSM", hg.isFsm());
   if (empty.graph)
     outProperty("GRAPH", hg.isGraph());
   return true;
 }
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]);
    }
  }
}
void floydWarshall(IHypergraph<Arc> const& hg, Util::Matrix<typename ArcWtFn::Weight>* distances,
                   ArcWtFn const& arcWtFn) {
  if (!hg.isFsm())
    SDL_THROW_LOG(Hypergraph, ConfigException, "Current floydWarshall implementation needs FSM input");
  floydWarshallInit(hg, distances, arcWtFn);
  floydWarshallOverMatrix(distances);
}
Example #4
0
 bool prepare(IHypergraph<A> const& h, IMutableHypergraph<A>& m) const {
   Prepare const* p = impl();
   if (clearOut && !isInplace(h, m)) {
     if (samePropertiesOut)
       m.clear(h.properties());
     else
       m.clear();
   }
   StateIdMapping* map = p->mapping(h, m);
   if (map) {
     this->stateRemap.resetNew(map);
     p->preparePost(h, m);
     return true;
   } else
     return false;
 }
Example #5
0
void printDistances(IHypergraph<Arc> const& hg, bool allPairs, bool dag, StateIdTranslation& stateRemap,
                    StateId partBoundary) {
  typedef typename Arc::Weight Weight;
  if (allPairs) {
    // TODO: test
    assert(partBoundary != kNoState);
    StateId n = hg.size();
    assert(partBoundary <= n);
    Util::Matrix<Weight> D(partBoundary, partBoundary, Weight::zero());
    if (dag) {
      AllPairsSortedDag<IHypergraph<Arc>> compute(hg, D);
      for (StateId i = 0; i < n; ++i) {  // state in input
        StateId topi = stateRemap.existingState(i);  // actual state in hg, modified by topo sort
        if (topi == kNoState || topi >= partBoundary) continue;
        for (StateId j = 0; j < n; ++j) {
          if (i == j) continue;
          StateId topj = stateRemap.existingState(j);
          if (topj == kNoState || topj >= partBoundary) continue;
          Weight const& wij = D(topi, topj);
          if (!isZero(wij)) {
            std::cout << i << " -> " << j << " = " << wij << "\n";
          }
        }
      }
    } else {
      floydWarshall(hg, &D);
      for (StateId i = 0; i < n; ++i)
        for (StateId j = 0; j < n; ++j) {
          if (i == j) continue;
          Weight const& wij = D(i, j);
          if (!isZero(wij)) std::cout << i << " -> " << j << " = " << wij << "\n";
        }
    }
  } else {
    boost::ptr_vector<Weight> weights;
    insideAlgorithm(hg, &weights);
    std::size_t i = 0;
    for (Weight w : weights) {
      std::cout << i++ << '\t' << w << '\n';
    }
  }
}
Example #6
0
void print(std::ostream& o, Sym sym, IHypergraph<Arc> const& hg, SymbolQuotation quote = kQuoted) {

  Hypergraph::writeLabel(o, sym, hg.getVocabulary(), quote);
}
Example #7
0
 bool checkInputs(IHypergraph<A> const& h) const {
   return h.isFsm();
 }
Example #8
0
 void inout(IHypergraph<A> const& h, IMutableHypergraph<A>* o) const {
   if (h.prunedEmpty()) return;
   prepare(h, *o);
   Restrict<A>::inout(h, o);
   complete();
 }
Example #9
0
 TokenSplitPolicy(IHypergraph<Arc> const& hg, Util::Utf8RangePred pred, bool spaceBetween)
     : hg_(hg), pVoc_(hg.getVocabulary()), pred_(pred), spaceBetween_(spaceBetween) {
   assert(hg.isGraph());
   assert(hg.hasAtMostOneLexicalTail());
 }
Example #10
0
ArcIdIterator searchLabel(const IHypergraph<A>& hg, StateId sid, Sym label) {
  ArcIdRange arcIdsRange = hg.outArcIds(sid);
  // binary search:
  return boost::lower_bound(arcIdsRange, fakeArcId, CmpInputLabelWithSearchLabel<A>(hg, sid, label));
}