Cambridge SMT System
RefsData.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 "RefsData.h"
13 
14 RefsData::RefsData() {};
15 
17 
18 RefsData::RefsData (const std::vector<Sentence>& refs) {
19  for (unsigned int n = 0; n < BleuStats::MAX_BLEU_ORDER; ++n) {
20  std::vector<NGramToCountMap> tmpCounts (refs.size() );
21  for (unsigned int k = 0; k < refs.size(); ++k) {
22  int refssize_minus_n = refs[k].size() -
23  n; // needs to be an int, not a unsigned int in case it is negative
24  for (int i = 0; i < refssize_minus_n; ++i) {
25  NGram u = SubStr (refs[k], i, n);
26  refCounts[u] = 1;
27  tmpCounts[k][u]++;
28  }
29  }
30  for (unsigned int k = 0; k < refs.size(); ++k) {
31  for (NGramToCountMap::iterator it = refCounts.begin();
32  it != refCounts.end(); ++it) {
33  it->second = max (it->second, tmpCounts[k][it->first]);
34  }
35  }
36  }
37  for (unsigned int k = 0; k < refs.size(); ++k) {
38  refLengths.push_back (refs[k].size() );
39  }
40  sort (refLengths.begin(), refLengths.end() );
41 }
42 
44  BleuStats bs;
45  bs.refLength = ClosestReferenceLength (hyp.size() );
46  for (unsigned int n = 0; n < BleuStats::MAX_BLEU_ORDER && n < hyp.size();
47  ++n) {
48  NGramToCountMap hypCounts;
49  if (hyp.size() > n) {
50  bs.tots[n] = hyp.size() - n;
51  }
52  for (unsigned int i = 0; i < hyp.size() - n; ++i) {
53  hypCounts[SubStr (hyp, i, n)]++;
54  }
55  for (NGramToCountMap::const_iterator hit = hypCounts.begin();
56  hit != hypCounts.end(); ++hit) {
57  NGramToCountMap::const_iterator rit = refCounts.find (hit->first);
58  bs.hits[n] += min (rit == refCounts.end() ? 0 : rit->second,
59  hit->second); // Sum clipped counts
60  }
61  }
62  return bs;
63 }
64 
65 NGram RefsData::SubStr (const Sentence& s, const unsigned int n,
66  const unsigned int l) const {
67  return NGram (s.begin() + n, s.begin() + n + l + 1);
68 }
69 
70 unsigned int RefsData::ClosestReferenceLength (
71  const unsigned int hypLength) const {
72  unsigned int refLength = 0;
73  unsigned int refDistance = numeric_limits<unsigned int>::max();
74  for (unsigned int k = 0; k < refLengths.size(); ++k) {
75  unsigned int distance = abs ( (int) refLengths[k] - (int) hypLength);
76  if (distance < refDistance) {
77  refDistance = distance;
78  refLength = refLengths[k];
79  }
80  }
81  return refLength;
82 }
83 
84 std::string RefsData::ToString (const char separator = ' ') const {
85  ostringstream oss;
86  for (NGramToCountMap::const_iterator it = refCounts.begin();
87  it != refCounts.end(); ++it) {
88  NGram u = it->first;
89  unsigned int count = it->second;
90  for (int w = 0; w < u.size(); ++w) {
91  if (w > 0) {
92  oss << " ";
93  }
94  oss << u[w];
95  }
96  oss << '\t' << count << separator;
97  }
98  return oss.str();
99 }
100 
101 std::vector<std::vector<std::string> > LoadRefFiles (std::vector<std::string>
102  files) {
103  tracer << "loading and initializing reference ngrams...\n";
104  std::vector<std::vector<std::string> > refsStr (files.size() );
105  for (unsigned int r = 0; r < files.size(); ++r) {
106  std::ifstream ifs (files[r].c_str() );
107  if (!ifs.good() ) {
108  cerr << "ERROR: unable to open file " << files[r] << '\n';
109  exit (1);
110  }
111  std::string line;
112  refsStr[r].push_back ("");
113  while (getline (ifs, line) ) {
114  refsStr[r].push_back (line);
115  }
116  if (opts.verbose) {
117  tracer << "loaded " << refsStr[r].size() - 1 << " translations from "
118  << files[r] << '\n';
119  }
120  ifs.close();
121  }
122  return refsStr;
123 }
124 
125 void IntegerEncRefs::LoadRefData (std::vector<std::string> files) {
126  std::vector<std::vector<std::string> > refsStr = LoadRefFiles (files);
127  refsData.push_back (RefsData::dummy);
128  for (unsigned int sid = 0; sid < refsStr[0].size() - 1 ; ++sid) {
129  std::vector<Sentence> refsIdx;
130  for (unsigned int r = 0; r < files.size(); ++r) {
131  std::istringstream iss (refsStr[r][sid + 1]);
132  Sentence s;
133  Wid w;
134  while (iss >> w) {
135  s.push_back (w);
136  }
137  refsIdx.push_back (s);
138  }
139  refsData.push_back (RefsData (refsIdx) );
140  }
141 }
142 
144  Sentence h) const {
145  if (sid + 1 > refsData.size() ) {
146  cerr << "ERROR: no references loaded for sentence s=" << sid << '\n';
147  exit (1);
148  }
149  return refsData[sid].ComputeBleuStats (h);
150 }
long long Wid
MertOpt opts
Definition: MertCommon.cpp:14
unordered_map< NGram, unsigned int, hashfvecint64, hasheqvecint64 > NGramToCountMap
Definition: RefsData.h:36
#define tracer
Definition: data.lmbr.hpp:18
unsigned int Sid
Definition: MertCommon.h:45
vector< int > hits
Definition: BleuStats.h:46
bool verbose
Definition: MertCommon.h:33
static RefsData dummy
Definition: RefsData.h:50
void LoadRefData(vector< string >)
Definition: RefsData.cpp:125
vector< int > tots
Definition: BleuStats.h:45
std::vector< Wid > Sentence
Definition: MertCommon.h:48
iszfstream & getline(iszfstream &izs, std::string &line)
Definition: szfstream.hpp:178
long refLength
Definition: BleuStats.h:47
static const unsigned int MAX_BLEU_ORDER
Definition: BleuStats.h:50
BleuStats ComputeBleuStats(const Sentence &hyp) const
Definition: RefsData.cpp:43
ErrorStats ComputeErrorStats(Sid, Sentence) const
Definition: RefsData.cpp:143
std::vector< Wid > NGram
Definition: MertCommon.h:47
std::vector< std::vector< std::string > > LoadRefFiles(std::vector< std::string > files)
Definition: RefsData.cpp:101
string ToString(const char separator) const
Definition: RefsData.cpp:84