From 3c9c06e25b481917fe473046d1360e609eb9c9bf Mon Sep 17 00:00:00 2001 From: Martin Jansche Date: Sun, 20 Mar 2016 20:14:49 +0000 Subject: [PATCH] Rename and PathsToVector to clarify assumptions about its FST parameter. --- festus/runtime/fst-util.h | 17 ++++++++--------- festus/runtime/g2p.h | 5 ++++- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/festus/runtime/fst-util.h b/festus/runtime/fst-util.h index fd9b6a3..7a606d8 100644 --- a/festus/runtime/fst-util.h +++ b/festus/runtime/fst-util.h @@ -21,7 +21,6 @@ #ifndef FESTUS_RUNTIME_FST_UTIL_H__ #define FESTUS_RUNTIME_FST_UTIL_H__ -#include #include #include #include @@ -295,18 +294,19 @@ void DeterminizeConvertWeight( } // Converts the output FST of ShortestPath() into vector form. +// Returns a vector holding the output string and weight of each successful +// path in the FST. template -void PathsToVector( +std::vector> ShortestPathsToVector( const F &paths_fst, - std::vector> *paths, typename F::Arc::Weight total_weight = F::Arc::Weight::One()) { typedef typename F::Arc Arc; typedef typename F::StateId StateId; typedef typename F::Weight Weight; - paths->clear(); + std::vector> paths; const StateId start = paths_fst.Start(); - if (start == fst::kNoStateId) return; - paths->reserve(paths_fst.NumArcs(start)); + if (start == fst::kNoStateId) return paths; + paths.reserve(paths_fst.NumArcs(start)); const fst::SymbolTable *symbols = paths_fst.OutputSymbols(); CHECK(symbols != nullptr); typedef fst::ArcIterator> MyArcIterator; @@ -329,10 +329,9 @@ void PathsToVector( CHECK((iter.Next(), iter.Done())); } CHECK(fst::ArcIterator>(paths_fst, state).Done()); - weight = Times(weight, paths_fst.Final(state)); - weight = Divide(weight, total_weight); - paths->emplace_back(std::move(str), std::exp(-weight.Value())); + paths.emplace_back(std::move(str), weight.Value()); } + return paths; } constexpr uint64 kConnected = fst::kAccessible | fst::kCoAccessible; diff --git a/festus/runtime/g2p.h b/festus/runtime/g2p.h index 72add67..189d534 100644 --- a/festus/runtime/g2p.h +++ b/festus/runtime/g2p.h @@ -255,7 +255,10 @@ bool G2P::Pronounce(const string &spelling, } VLOG(2) << "8. Convert shortest paths to pronunciations."; - PathsToVector(paths, &result->pronunciations, total_weight.Value()); + result->pronunciations = ShortestPathsToVector(paths, total_weight.Value()); + for (auto &pron : result->pronunciations) { + pron.second = std::exp(-pron.second); + } result->error.clear(); return true; }