summaryrefslogtreecommitdiff
path: root/tools/thirdparty/OpenFst/fst/lib/arcsum.h
blob: 442e2c11a08d7512120495d7403a3130f9c7ffcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// arcsum.h
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//
// \file
// Functions to sum arcs (sum weights) in an fst.

#ifndef FST_LIB_ARCSUM_H__
#define FST_LIB_ARCSUM_H__

#include "fst/lib/mutable-fst.h"
#include "fst/lib/weight.h"

namespace fst {

template <class A>
class ArcSumCompare {
 public:
  bool operator()(const A& x, const A& y) {
    if (x.ilabel < y.ilabel) return true;
    if (x.ilabel > y.ilabel) return false;
    if (x.olabel < y.olabel) return true;
    if (x.olabel < y.olabel) return false;
    if (x.nextstate < y.nextstate) return true;
    if (x.nextstate > y.nextstate) return false;
    return false;
  }
};


template <class A>
class ArcSumEqual {
 public:
  bool operator()(const A& x, const A& y) {
    return (x.ilabel == y.ilabel &&
            x.olabel == y.olabel &&
            x.nextstate == y.nextstate);
  }
};


// Combines identically labeled arcs, summing weights. For each state
// we combine arcs with the same input and output label, summing their
// weights using Weight:::Plus().
template <class A>
void ArcSum(MutableFst<A>* fst) {
  typedef typename A::StateId StateId;

  vector<A> arcs;
  for (StateIterator<Fst<A> > siter(*fst); !siter.Done(); siter.Next()) {
    StateId s = siter.Value();
    if (fst->NumArcs(s) == 0) continue;

    // Sums arcs into arcs array.
    arcs.clear();
    arcs.reserve(fst->NumArcs(s));
    for (ArcIterator<Fst<A> > aiter(*fst, s); !aiter.Done();
         aiter.Next())
      arcs.push_back(aiter.Value());

    // At each state, first sorts the exiting arcs by input label, output label
    // and destination state and then combines arcs identical in these
    // attributes.
    ArcSumCompare<A> comp;
    sort(arcs.begin(), arcs.end(), comp);

    // Deletes current arcs and copy in sumed arcs.
    fst->DeleteArcs(s);
    A current_arc = arcs[0];
    ArcSumEqual<A> equal;
    for (size_t i = 1; i < arcs.size(); ++i) {
      if (equal(current_arc, arcs[i])) {
        current_arc.weight = Plus(current_arc.weight, arcs[i].weight);
      } else {
        fst->AddArc(s, current_arc);
        current_arc = arcs[i];
      }
    }
    fst->AddArc(s, current_arc);
  }
}

}

#endif  // FST_LIB_ARCSUM_H__