Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SentenceStats.h « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e879fb3ba7d56c804611e85984c5520b60aabb0f (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// $Id$

/***********************************************************************
Moses - factored phrase-based language decoder
Copyright (C) 2006 University of Edinburgh

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
***********************************************************************/

#ifndef moses_SentenceStats_h
#define moses_SentenceStats_h

#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include "Timer.h"
#include "Phrase.h"
#include "Hypothesis.h"
#include "TypeDef.h" //FactorArray
#include "InputType.h"
#include "Util.h" //Join()

namespace Moses
{

//! Hold info about recombination. Used by SentenceStats class
struct RecombinationInfo {
  RecombinationInfo() {} //for std::vector
  RecombinationInfo(size_t srcWords, float gProb, float bProb)
    : numSourceWords(srcWords), betterProb(gProb), worseProb(bProb) {}

  size_t numSourceWords;
  float betterProb, worseProb;
};

/**
 * stats relating to decoder operation on a given sentence
 */
class SentenceStats
{
public:

  /***
   * to be called before decoding a sentence
   */
  SentenceStats(const InputType& source) {
    Initialize(source);
  }
  void Initialize(const InputType& source) {
    m_numHyposCreated = 0;
    m_numHyposPopped = 0;
    m_numHyposPruned = 0;
    m_numHyposDiscarded = 0;
    m_numHyposEarlyDiscarded = 0;
    m_numHyposNotBuilt = 0;
    m_totalSourceWords = source.GetSize();
    m_recombinationInfos.clear();
    m_deletedWords.clear();
    m_insertedWords.clear();
  }

  /***
   * to be called after decoding a sentence
   */
  void CalcFinalStats(const Hypothesis& bestHypo);

  unsigned int GetTotalHypos() const {
    return m_numHyposCreated + m_numHyposNotBuilt;
  }
  unsigned int GetNumHyposPopped() const {
    return m_numHyposPopped;
  }
  size_t GetNumHyposRecombined() const {
    return m_recombinationInfos.size();
  }
  unsigned int GetNumHyposPruned() const {
    return m_numHyposPruned;
  }
  unsigned int GetNumHyposDiscarded() const {
    return m_numHyposDiscarded;
  }
  unsigned int GetNumHyposEarlyDiscarded() const {
    return m_numHyposEarlyDiscarded;
  }
  unsigned int GetNumHyposNotBuilt() const {
    return m_numHyposNotBuilt;
  }
  double GetTimeCollectOpts() const {
    return m_timeCollectOpts.get_elapsed_time();
  }
  double GetTimeBuildHyp() const {
    return m_timeBuildHyp.get_elapsed_time();
  }
  double GetTimeCalcLM() const {
    return m_timeCalcLM.get_elapsed_time();
  }
  double GetTimeOtherScore() const {
    return m_timeOtherScore.get_elapsed_time();
  }
  double GetTimeEstimateScore() const {
    return m_timeEstimateScore.get_elapsed_time();
  }
  double GetTimeStack() const {
    return m_timeStack.get_elapsed_time();
  }
  double GetTimeSetupCubes() const {
    return m_timeSetupCubes.get_elapsed_time();
  }
  double GetTimeManageCubes() const {
    return m_timeManageCubes.get_elapsed_time();
  }
  double GetTimeTotal() const {
    return m_timeTotal.get_elapsed_time();
  }
  size_t GetTotalSourceWords() const {
    return m_totalSourceWords;
  }
  size_t GetNumWordsDeleted() const {
    return m_deletedWords.size();
  }
  size_t GetNumWordsInserted() const {
    return m_insertedWords.size();
  }
  const std::vector<const Phrase*>& GetDeletedWords() const {
    return m_deletedWords;
  }
  const std::vector<std::string>& GetInsertedWords() const {
    return m_insertedWords;
  }

  void AddRecombination(const Hypothesis& worseHypo, const Hypothesis& betterHypo) {
    m_recombinationInfos.push_back(RecombinationInfo(worseHypo.GetWordsBitmap().GetNumWordsCovered(),
                                   betterHypo.GetFutureScore(), worseHypo.GetFutureScore()));
  }
  void AddCreated() {
    m_numHyposCreated++;
  }
  void AddPopped() {
    m_numHyposPopped++;
  }
  void AddPruning() {
    m_numHyposPruned++;
  }
  void AddEarlyDiscarded() {
    m_numHyposEarlyDiscarded++;
  }
  void AddNotBuilt() {
    m_numHyposNotBuilt++;
  }
  void AddDiscarded() {
    m_numHyposDiscarded++;
  }

  void StartTimeCollectOpts() {
    m_timeCollectOpts.start();
  }
  void StopTimeCollectOpts() {
    m_timeCollectOpts.stop();
  }
  void StartTimeBuildHyp() {
    m_timeBuildHyp.start();
  }
  void StopTimeBuildHyp() {
    m_timeBuildHyp.stop();
  }
  void StartTimeCalcLM() {
    m_timeCalcLM.start();
  }
  void StopTimeCalcLM() {
    m_timeCalcLM.stop();
  }
  void StartTimeOtherScore() {
    m_timeOtherScore.start();
  }
  void StopTimeOtherScore() {
    m_timeOtherScore.stop();
  }
  void StartTimeEstimateScore() {
    m_timeEstimateScore.start();
  }
  void StopTimeEstimateScore() {
    m_timeEstimateScore.stop();
  }
  void StartTimeSetupCubes() {
    m_timeSetupCubes.start();
  }
  void StopTimeSetupCubes() {
    m_timeSetupCubes.stop();
  }
  void StartTimeManageCubes() {
    m_timeManageCubes.start();
  }
  void StopTimeManageCubes() {
    m_timeManageCubes.stop();
  }
  void StartTimeStack() {
    m_timeStack.start();
  }
  void StopTimeStack() {
    m_timeStack.stop();
  }
  void StartTimeTotal() {
    m_timeTotal.start();
  }
  void StopTimeTotal() {
    m_timeTotal.stop();
  }

protected:

  /***
   * auxiliary to CalcFinalStats()
   */
  void AddDeletedWords(const Hypothesis& hypo);

  //hypotheses
  // TODO: Move away from clock_t in favor of just storing doubles of the number of seconds
  // since clock seconds aren't reliable in a multi-threaded environment -Jon
  // (see Manager.cpp for some initial work moving in this direction)
  std::vector<RecombinationInfo> m_recombinationInfos;
  unsigned int m_numHyposCreated;
  unsigned int m_numHyposPopped;
  unsigned int m_numHyposPruned;
  unsigned int m_numHyposDiscarded;
  unsigned int m_numHyposEarlyDiscarded;
  unsigned int m_numHyposNotBuilt;
  Timer m_timeCollectOpts;
  Timer m_timeBuildHyp;
  Timer m_timeEstimateScore;
  Timer m_timeOtherScore;
  Timer m_timeCalcLM;
  Timer m_timeStack;
  Timer m_timeSetupCubes;
  Timer m_timeManageCubes;
  Timer m_timeTotal;

  //words
  size_t m_totalSourceWords;
  std::vector<const Phrase*> m_deletedWords; //count deleted words/phrases in the final hypothesis
  std::vector<std::string> m_insertedWords; //count inserted words in the final hypothesis
};

inline std::ostream& operator<<(std::ostream& os, const SentenceStats& ss)
{
  double totalTime = ss.GetTimeTotal();
  double otherTime = totalTime - (ss.GetTimeCollectOpts() + ss.GetTimeBuildHyp() + ss.GetTimeEstimateScore() + ss.GetTimeCalcLM() + ss.GetTimeOtherScore() + ss.GetTimeStack() + ss.GetTimeSetupCubes() + ss.GetTimeManageCubes());

  return os << "total hypotheses considered = " << ss.GetTotalHypos() << std::endl
         << "    number popped from cube = " << ss.GetNumHyposPopped() << std::endl
         << "           number not built = " << ss.GetNumHyposNotBuilt() << std::endl
         << "     number discarded early = " << ss.GetNumHyposEarlyDiscarded() << std::endl
         << "           number discarded = " << ss.GetNumHyposDiscarded() << std::endl
         << "          number recombined = " << ss.GetNumHyposRecombined() << std::endl
         << "              number pruned = " << ss.GetNumHyposPruned() << std::endl

         << "time to collect opts    " << ss.GetTimeCollectOpts()   << " (" << (int)(100 * ss.GetTimeCollectOpts()/totalTime) << "%)" << std::endl
         << "        create hyps     " << ss.GetTimeBuildHyp()      << " (" << (int)(100 * ss.GetTimeBuildHyp()/totalTime) << "%)" << std::endl
         << "        estimate score  " << ss.GetTimeEstimateScore() << " (" << (int)(100 * ss.GetTimeEstimateScore()/totalTime) << "%)" << std::endl
         << "        calc lm         " << ss.GetTimeCalcLM()        << " (" << (int)(100 * ss.GetTimeCalcLM()/totalTime) << "%)" << std::endl
         << "        other hyp score " << ss.GetTimeOtherScore()    << " (" << (int)(100 * ss.GetTimeOtherScore()/totalTime) << "%)" << std::endl
         << "        set up cubes    " << ss.GetTimeSetupCubes()    << " (" << (int)(100 * ss.GetTimeSetupCubes()/totalTime) << "%)" << std::endl
         << "        manage cubes    " << ss.GetTimeManageCubes()   << " (" << (int)(100 * ss.GetTimeManageCubes()/totalTime) << "%)" << std::endl
         << "        manage stacks   " << ss.GetTimeStack()         << " (" << (int)(100 * ss.GetTimeStack()/totalTime) << "%)" << std::endl
         << "        other           " << otherTime                 << " (" << (int)(100 * otherTime/totalTime) << "%)" << std::endl

         << "total source words = " << ss.GetTotalSourceWords() << std::endl
         << "     words deleted = " << ss.GetNumWordsDeleted() << " (" << Join(" ", ss.GetDeletedWords()) << ")" << std::endl
         << "    words inserted = " << ss.GetNumWordsInserted() << " (" << Join(" ", ss.GetInsertedWords()) << ")" << std::endl;
}

}
#endif