Skip to content

Commit

Permalink
Rename and PathsToVector to clarify assumptions about its FST parameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjansche committed Mar 20, 2016
1 parent e61ded3 commit 3c9c06e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
17 changes: 8 additions & 9 deletions festus/runtime/fst-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#ifndef FESTUS_RUNTIME_FST_UTIL_H__
#define FESTUS_RUNTIME_FST_UTIL_H__

#include <cmath>
#include <cstddef>
#include <iterator>
#include <limits>
Expand Down Expand Up @@ -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 <class F>
void PathsToVector(
std::vector<std::pair<string, float>> ShortestPathsToVector(
const F &paths_fst,
std::vector<std::pair<string, float>> *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<std::pair<string, float>> 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<fst::VectorFst<Arc>> MyArcIterator;
Expand All @@ -329,10 +329,9 @@ void PathsToVector(
CHECK((iter.Next(), iter.Done()));
}
CHECK(fst::ArcIterator<fst::VectorFst<Arc>>(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;
Expand Down
5 changes: 4 additions & 1 deletion festus/runtime/g2p.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,10 @@ bool G2P<Arc>::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;
}
Expand Down

0 comments on commit 3c9c06e

Please sign in to comment.