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

custom-pt.cc « mm « UG « TranslationModel « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1c1e0893c45f73e0b81706b18b9b643f4560b90c (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
// build a phrase table for the given input
// #include "ug_lexical_phrase_scorer2.h"

#include <stdint.h>
#include <string>
#include <vector>
#include <cassert>
#include <iomanip>
#include <algorithm>

#include "moses/TranslationModel/UG/generic/sorting/VectorIndexSorter.h"
#include "moses/TranslationModel/UG/generic/sampling/Sampling.h"
#include "moses/TranslationModel/UG/generic/file_io/ug_stream.h"

#include <boost/math/distributions/binomial.hpp>
#include <boost/unordered_map.hpp>
#include <boost/foreach.hpp>

#include "ug_mm_ttrack.h"
#include "ug_mm_tsa.h"
#include "tpt_tokenindex.h"
#include "ug_corpus_token.h"
#include "ug_typedefs.h"
#include "tpt_pickler.h"
#include "ug_bitext.h"
#include "../mmsapt_phrase_scorers.h"
#include "ug_lexical_phrase_scorer2.h"

using namespace std;
using namespace ugdiss;
using namespace Moses;
using namespace Moses::bitext;

#define CACHING_THRESHOLD 1000
#define lbop boost::math::binomial_distribution<>::find_lower_bound_on_p 
size_t mctr=0,xctr=0;

typedef L2R_Token<SimpleWordId> Token;
typedef mmBitext<Token> mmbitext;
mmbitext bt;


float lbsmooth = .005;


PScorePfwd<Token> calc_pfwd;
PScorePbwd<Token> calc_pbwd;
PScoreLex<Token>  calc_lex(1.0);
PScoreWP<Token>   apply_wp;
vector<float> fweights;

void
nbest_phrasepairs(uint64_t const  pid1, 
		  pstats   const& ps, 
		  vector<PhrasePair> & nbest)
{
  pstats::trg_map_t::const_iterator m;
  vector<size_t> idx(nbest.size());
  size_t i=0;
  for (m  = ps.trg.begin(); 
       m != ps.trg.end() && i < nbest.size(); 
       ++m)
    {
      // cout << m->second.rcnt() << " " << ps.good << endl;
      if ((m->second.rcnt() < 3) && (m->second.rcnt() * 100 < ps.good))
	continue;
      nbest[i].init(pid1,ps,5);
      nbest[i].update(m->first,m->second);
      calc_pfwd(bt, nbest[i]);
      calc_pbwd(bt, nbest[i]);
      calc_lex(bt, nbest[i]);
      apply_wp(bt, nbest[i]);
      nbest[i].eval(fweights);
      idx[i] = i;
      ++i;
    }
  // cout << i << " " << nbest.size() << endl;
  if (i < nbest.size()) 
    {
      // cout << "Resizing from " << nbest.size() << " to " << i << endl;
      nbest.resize(i);
      idx.resize(i);
    }
  VectorIndexSorter<PhrasePair> sorter(nbest,greater<PhrasePair>());
  if (m != ps.trg.end()) 
    {
      make_heap(idx.begin(),idx.end(),sorter);
      PhrasePair cand; 
      cand.init(pid1,ps,5);
      for (; m != ps.trg.end(); ++m)
	{
	  if ((m->second.rcnt() < 3) && (m->second.rcnt() * 100 < ps.good))
	    continue;
	  cand.update(m->first,m->second);
	  calc_pfwd(bt, cand);
	  calc_pbwd(bt, cand);
	  calc_lex(bt, cand);
	  apply_wp(bt, cand);
	  cand.eval(fweights);
	  if (cand < nbest[idx[0]]) continue;
	  pop_heap(idx.begin(),idx.end(),sorter);
	  nbest[idx.back()] = cand;
	  push_heap(idx.begin(),idx.end(),sorter);
	}
    }
  sort(nbest.begin(),nbest.end(),greater<PhrasePair>());
}
  
int main(int argc, char* argv[])
{
  // assert(argc == 4);
#if 0
  string base = argv[1];
  string L1   = argv[2];
  string L2   = argv[3];
  size_t max_samples = argc > 4 ? atoi(argv[4]) : 0;
#else
  string base = "/fs/syn5/germann/exp/sapt/crp/trn/mm/";
  string L1 = "de";
  string L2 = "en";
  size_t max_samples = argc > 1 ? atoi(argv[1]) : 1000;
#endif
  char c = *base.rbegin(); 
  if (c != '/' && c != '.') 
    base += ".";

  fweights.resize(5,.25);
  fweights[0] = 1;
  bt.open(base,L1,L2);
  bt.setDefaultSampleSize(max_samples);

  size_t i;
  i = calc_pfwd.init(0,.05,'g');
  i = calc_pbwd.init(i,.05,'g');
  i = calc_lex.init(i,base+L1+"-"+L2+".lex");
  i = apply_wp.init(i);

  string line;
  while (getline(cin,line))
    {
      vector<id_type> snt; 
      bt.V1->fillIdSeq(line,snt);
      for (size_t i = 0; i < snt.size(); ++i)
  	{
  	  TSA<Token>::tree_iterator m(bt.I1.get());
	  for (size_t k = i; k < snt.size() && m.extend(snt[k]); ++k)
	    bt.prep(m);
	}
      // continue;
      for (size_t i = 0; i < snt.size(); ++i)
      	{
      	  TSA<Token>::tree_iterator m(bt.I1.get());
      	  for (size_t k = i; k < snt.size() && m.extend(snt[k]); ++k)
      	    {
	      uint64_t spid = m.getPid();
      	      sptr<pstats> s = bt.lookup(m);
      	      for (size_t j = i; j <= k; ++j)
      		cout << (*bt.V1)[snt[j]] << " ";
      	      cout << s->good << "/" 
		   << s->sample_cnt << "/" 
		   << s->raw_cnt << endl;
	      // vector<PhrasePair> nbest(min(s->trg.size(),size_t(20)));
	      vector<PhrasePair> nbest(s->trg.size());
	      nbest_phrasepairs(spid, *s, nbest);
	      BOOST_FOREACH(PhrasePair const& pp, nbest)
		{
		  uint32_t sid,off,len;
		  parse_pid(pp.p2,sid,off,len);
		  uint32_t stop = off + len;
		  // cout << sid << " " << off << " " << len << endl;
		  Token const* o = bt.T2->sntStart(sid);
		  cout << "   " << setw(6) << pp.score << " ";
		  for (uint32_t i = off; i < stop; ++i)
		    cout << (*bt.V2)[o[i].id()] << " ";
		  cout << pp.joint << "/" 
		       << pp.raw1  << "/"
		       << pp.raw2  << " |";
		  BOOST_FOREACH(float f, pp.fvals) 
		    cout << " " << f;
		  cout << endl;
		}
      	    }
      	}
    }
  
    exit(0);
}