Cambridge SMT System
MertPrune.cpp
Go to the documentation of this file.
1 //Copyright (c) 2012, University of Cambridge
2 //All rights reserved.
3 //
4 //Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met://
5 //
6 // * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7 // * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8 // * Neither the name of the University of Cambridge nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9 //
10 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
11 
12 #include "MertPrune.h"
13 
15  before (0), after (0) {
16 }
17 ;
18 
19 void Prune (fst::MutableFst<FunctionArc> *fst, PruneStats& stats) {
20  stats.before += ArcCount (*fst);
21  Prune (fst);
22  stats.after += ArcCount (*fst);
23 }
24 
25 void FullPrune (fst::MutableFst<FunctionArc> *fst,
26  const std::vector<FunctionWeight>& distance, PruneStats& stats) {
27  stats.before += ArcCount (*fst);
28  FullPrune (fst, distance);
29  stats.after += ArcCount (*fst);
30 }
31 
32 void Prune (fst::MutableFst<FunctionArc> *fst) {
33  std::map<StatePair, int> dupeArcs;
34  std::map<StatePair, FunctionWeight> summedWeights;
35  int totalArcs = 0;
36  for (fst::StateIterator < fst::MutableFst<FunctionArc> > siter (*fst);
37  !siter.Done(); siter.Next() ) {
38  FunctionArc::StateId startState = siter.Value();
39  int arcCount = 0;
40  for (fst::ArcIterator < fst::MutableFst<FunctionArc> > aiter (*fst,
41  siter.Value() ); !aiter.Done(); aiter.Next() ) {
42  ++totalArcs;
43  ++arcCount;
44  const FunctionArc& arc = aiter.Value();
45  FunctionArc::StateId endState = arc.nextstate;
46  StatePair pair (startState, endState);
47  if (summedWeights.count (pair) == 0) {
48  summedWeights[pair] = arc.weight;
49  dupeArcs[pair] = 1;
50  } else {
51  summedWeights[pair] = Plus (summedWeights[pair], arc.weight);
52  dupeArcs[pair] += 1;
53  }
54  }
55  //cout << startState << "\t" << arcCount << endl;
56  }
57  FunctionArc::StateId deadState = fst->AddState();
58  for (std::map<StatePair, int>::iterator it = dupeArcs.begin(); it
59  != dupeArcs.end(); ++it) {
60  if (it->second > 1) {
61  for (fst::MutableArcIterator < fst::MutableFst<FunctionArc> > aiter (fst,
62  it->first.first); !aiter.Done(); aiter.Next() ) {
63  FunctionArc arc = aiter.Value();
64  bool keepArc = false;
65  if (arc.nextstate == it->first.second) {
66  for (MertList::const_iterator miter =
67  summedWeights[it->first].Value().begin(); miter
68  != summedWeights[it->first].Value().end(); ++miter) {
69  keepArc |= arc.weight.Value().front() == *miter;
70  }
71  if (!keepArc) {
72  arc.nextstate = deadState;
73  aiter.SetValue (arc);
74  }
75  }
76  }
77  }
78  //cout << it->first.first << "->" << it->first.second << "\t"
79  // << it->second << endl;
80  }
81  Connect (fst);
82 }
83 
84 void FullPrune (fst::MutableFst<FunctionArc> *fst,
85  const std::vector<FunctionWeight>& distance) {
86  FunctionArc::StateId deadState = fst->AddState();
87  for (fst::StateIterator < fst::MutableFst<FunctionArc> > siter (*fst);
88  !siter.Done(); siter.Next() ) {
89  std::map<FunctionArc::StateId, FunctionWeight> otherArcs;
90  for (fst::ArcIterator < fst::Fst<FunctionArc> > aiter (*fst, siter.Value() );
91  !aiter.Done(); aiter.Next() ) {
92  FunctionArc arc = aiter.Value();
93  if (otherArcs.count (arc.nextstate) == 0) {
94  otherArcs[arc.nextstate] = Times (arc.weight, distance[arc.nextstate]);
95  } else {
96  otherArcs[arc.nextstate] = Plus (otherArcs[arc.nextstate],
97  Times (arc.weight, distance[arc.nextstate]) );
98  }
99  }
100  /*for (map<FunctionArc::StateId, FunctionWeight>::iterator miter =
101  otherArcs.begin(); miter != otherArcs.end(); ++miter) {
102  miter->second = Times(miter->second, distance[miter->first]);
103  }*/
104  std::set<FunctionArc::StateId> toDelete;
105  for (std::map<FunctionArc::StateId, FunctionWeight>::iterator outer =
106  otherArcs.begin(); outer != otherArcs.end(); ++outer) {
108  for (std::map<FunctionArc::StateId, FunctionWeight>::iterator inner =
109  otherArcs.begin(); inner != otherArcs.end(); ++inner) {
110  if (outer->first != inner->first) {
111  otherSum = Plus (otherSum, inner->second);
112  }
113  }
114  if (otherSum == distance[siter.Value()]) {
115  toDelete.insert (outer->first);
116  }
117  }
118  for (fst::MutableArcIterator < fst::MutableFst<FunctionArc> > aiter (fst,
119  siter.Value() ); !aiter.Done(); aiter.Next() ) {
120  FunctionArc arc = aiter.Value();
121  if (toDelete.count (arc.nextstate) > 0) {
122  arc.nextstate = deadState;
123  aiter.SetValue (arc);
124  }
125  }
126  }
127  Connect (fst);
128 }
129 
130 int ArcCount (const fst::Fst<FunctionArc>& fst) {
131  int arcs = 0;
132  for (fst::StateIterator < fst::Fst<FunctionArc> > siter (fst); !siter.Done();
133  siter.Next() ) {
134  FunctionArc::StateId startState = siter.Value();
135  for (fst::ArcIterator < fst::Fst<FunctionArc> > aiter (fst, siter.Value() );
136  !aiter.Done(); aiter.Next() ) {
137  ++arcs;
138  }
139  //cout << startState << "\t" << arcCount << endl;
140  }
141  return arcs;
142 }
Definition: fstio.hpp:27
StateId nextstate
void Prune(fst::MutableFst< FunctionArc > *fst, PruneStats &stats)
Definition: MertPrune.cpp:19
static const FunctionWeight & Zero()
const MertList & Value() const
TropicalSparseTupleWeight< T > Plus(const TropicalSparseTupleWeight< T > &vw1, const TropicalSparseTupleWeight< T > &vw2)
unsigned int after
Definition: MertPrune.h:27
void FullPrune(fst::MutableFst< FunctionArc > *fst, const std::vector< FunctionWeight > &distance, PruneStats &stats)
Definition: MertPrune.cpp:25
unsigned int before
Definition: MertPrune.h:26
TropicalSparseTupleWeight< T > Times(const TropicalSparseTupleWeight< T > &w1, const TropicalSparseTupleWeight< T > &w2)
pair< FunctionArc::StateId, FunctionArc::StateId > StatePair
Definition: MertPrune.h:21
int ArcCount(const fst::Fst< FunctionArc > &fst)
Definition: MertPrune.cpp:130