20 #ifndef FST_EXTENSIONS_LINEAR_LINEAR_FST_DATA_H_ 21 #define FST_EXTENSIONS_LINEAR_LINEAR_FST_DATA_H_ 37 class LinearFstDataBuilder;
54 typedef typename A::Label
Label;
65 : max_future_size_(0), max_input_label_(1), input_attribs_(1) {}
80 template <
class Iterator>
81 void TakeTransition(Iterator buffer_end, Iterator trie_state_begin,
82 Iterator trie_state_end, Label ilabel, Label olabel,
83 std::vector<Label> *next, Weight *weight)
const;
86 template <
class Iterator>
87 Weight
FinalWeight(Iterator trie_state_begin, Iterator trie_state_end)
const;
91 return groups_[group_id]->Start();
97 Label olabel, Weight *weight)
const;
101 return groups_[group_id]->FinalWeight(trie_state);
119 std::pair<typename std::vector<Label>::const_iterator,
120 typename std::vector<Label>::const_iterator>
124 std::ostream &
Write(std::ostream &strm)
const;
128 struct InputAttribute {
129 size_t output_begin, output_length;
131 std::istream &
Read(std::istream &strm);
132 std::ostream &
Write(std::ostream &strm)
const;
141 Label FindFeature(
size_t group, Label word)
const;
143 size_t max_future_size_;
144 Label max_input_label_;
145 std::vector<std::unique_ptr<const FeatureGroup<A>>> groups_;
146 std::vector<InputAttribute> input_attribs_;
147 std::vector<Label> output_pool_, output_set_;
155 template <
class Iterator>
157 Iterator trie_state_begin,
158 Iterator trie_state_end,
Label ilabel,
159 Label olabel, std::vector<Label> *next,
161 DCHECK_EQ(trie_state_end - trie_state_begin, groups_.size());
165 for (Iterator it = trie_state_begin; it != trie_state_end; ++it, ++group_id) {
166 size_t delay = groups_[group_id]->Delay();
169 Label real_ilabel = delay == 0 ? ilabel : *(buffer_end - delay);
180 Label group_ilabel = FindFeature(group_id, ilabel);
181 return groups_[group_id]->Walk(trie_state, group_ilabel, olabel, weight);
185 template <
class Iterator>
187 Iterator trie_state_begin, Iterator trie_state_end)
const {
188 DCHECK_EQ(trie_state_end - trie_state_begin, groups_.size());
190 Weight accum = Weight::One();
191 for (Iterator it = trie_state_begin; it != trie_state_end; ++it, ++group_id)
197 inline std::pair<typename std::vector<typename A::Label>::const_iterator,
198 typename std::vector<typename A::Label>::const_iterator>
200 const InputAttribute &attrib = input_attribs_[word];
201 if (attrib.output_length == 0)
202 return std::make_pair(output_set_.begin(), output_set_.end());
204 return std::make_pair(
205 output_pool_.begin() + attrib.output_begin,
206 output_pool_.begin() + attrib.output_begin + attrib.output_length);
212 ReadType(strm, &(data->max_future_size_));
213 ReadType(strm, &(data->max_input_label_));
215 size_t num_groups = 0;
217 data->groups_.resize(num_groups);
218 for (
size_t i = 0; i < num_groups; ++i)
221 ReadType(strm, &(data->input_attribs_));
222 ReadType(strm, &(data->output_pool_));
223 ReadType(strm, &(data->output_set_));
224 ReadType(strm, &(data->group_feat_map_));
226 return data.release();
238 for (
size_t i = 0; i < groups_.size(); ++i) {
239 groups_[i]->Write(strm);
256 return group_feat_map_.Find(group, word);
261 std::istream &strm) {
269 std::ostream &strm)
const {
299 int Start()
const {
return start_; }
303 int Walk(
int cur, Label ilabel, Label olabel, Weight *weight)
const;
309 return trie_[trie_state].final_weight;
319 std::unique_ptr<FeatureGroup<A>> ret(
new FeatureGroup<A>(delay, start));
320 ret->trie_.swap(trie);
323 return ret.release();
329 std::ostream &
Write(std::ostream &strm)
const {
337 size_t Delay()
const {
return delay_; }
339 std::string Stats()
const;
350 struct WeightBackLink {
352 Weight weight, final_weight;
356 weight(Weight::One()),
357 final_weight(Weight::One()) {}
359 std::istream &
Read(std::istream &strm) {
366 std::ostream &
Write(std::ostream &strm)
const {
378 : delay_(delay), start_(start) {}
392 std::vector<int> next_state_;
403 : input(i), output(o) {}
406 return input == that.
input && output == that.
output;
409 std::istream &
Read(std::istream &strm) {
415 std::ostream &
Write(std::ostream &strm)
const {
425 return static_cast<size_t>(label.
input * 7853 + label.
output);
454 *weight =
Times(*weight, trie_[next].weight);
455 next = next_state_[next];
465 for (; parent !=
kNoTrieNodeId; parent = trie_[parent].back_link) {
466 int next = trie_.Find(parent, label);
474 std::ostringstream strm;
476 for (
int i = 2; i < next_state_.size(); ++i)
477 num_states += i == next_state_[i];
478 strm << trie_.NumNodes() <<
" node(s); " << num_states <<
" state(s)";
487 void Init(
size_t num_groups,
size_t num_words) {
488 num_groups_ = num_groups;
490 pool_.resize(num_groups * num_words,
kNoLabel);
494 return pool_[IndexOf(group_id, ilabel)];
498 size_t i = IndexOf(group_id, ilabel);
499 if (pool_[i] !=
kNoLabel && pool_[i] != feat) {
500 FSTERROR() <<
"Feature group " << group_id
501 <<
" already has feature for word " << ilabel;
508 std::istream &
Read(std::istream &strm) {
514 std::ostream &
Write(std::ostream &strm)
const {
521 size_t IndexOf(
size_t group_id,
Label ilabel)
const {
522 return ilabel * num_groups_ + group_id;
528 std::vector<Label> pool_;
533 #endif // FST_EXTENSIONS_LINEAR_LINEAR_FST_DATA_H_ std::string Stats() const
bool Set(size_t group_id, Label ilabel, Label feat)
std::ostream & Write(std::ostream &strm) const
Label GroupStartState(int group_id) const
std::pair< typename std::vector< Label >::const_iterator, typename std::vector< Label >::const_iterator > PossibleOutputLabels(Label word) const
void TakeTransition(Iterator buffer_end, Iterator trie_state_begin, Iterator trie_state_end, Label ilabel, Label olabel, std::vector< Label > *next, Weight *weight) const
int Walk(int cur, Label ilabel, Label olabel, Weight *weight) const
ErrorWeight Times(const ErrorWeight &, const ErrorWeight &)
std::istream & Read(std::istream &strm)
Weight GroupFinalWeight(int group_id, int trie_state) const
void Init(size_t num_groups, size_t num_words)
void EncodeStartState(std::vector< Label > *output) const
static constexpr Label kEndOfSentence
Label GroupTransition(int group_id, int trie_state, Label ilabel, Label olabel, Weight *weight) const
Label Find(size_t group_id, Label ilabel) const
std::ostream & WriteType(std::ostream &strm, const T t)
Weight FinalWeight(Iterator trie_state_begin, Iterator trie_state_end) const
constexpr int kNoTrieNodeId
std::ostream & Write(std::ostream &strm) const
static constexpr Label kStartOfSentence
static LinearFstData< A > * Read(std::istream &strm)
Weight FinalWeight(int trie_state) const
Label MinInputLabel() const
size_t MaxFutureSize() const
std::istream & ReadType(std::istream &strm, T *t)
Label MaxInputLabel() const
std::ostream & Write(std::ostream &strm) const
static FeatureGroup< A > * Read(std::istream &strm)