summaryrefslogtreecommitdiff
path: root/gi/scfg/abc/scfg.cpp
blob: 1e59fb4a980ca0cfe3893234273984ae20f53181 (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
#include <iostream>
#include <fstream>

#include <boost/shared_ptr.hpp>
#include <boost/pointer_cast.hpp>
#include "lattice.h"
#include "tdict.h"
#include "agrammar.h"
#include "bottom_up_parser.h"
#include "hg.h"
#include "hg_intersect.h"
#include "../utils/ParamsArray.h"


using namespace std;

vector<string> src_corpus;
vector<string> tgt_corpus;

bool openParallelCorpora(string & input_filename){
  ifstream input_file;

  input_file.open(input_filename.c_str());
  if (!input_file) {
    cerr << "Cannot open input file " << input_filename << ". Exiting..." << endl;
    return false;
  } 

  int line =0;
  while (!input_file.eof()) {
    // get a line of source language data                                                                                                                                          
    //    cerr<<"new line "<<ctr<<endl;                                                                                                                                           
    string str;

    getline(input_file, str);
    line++;
    if (str.length()==0){
      cerr<<" sentence number "<<line<<" is empty, skip the sentence\n";
      continue;
    }
    string delimiters("|||");

    vector<string> v = tokenize(str, delimiters);

    if ( (v.size() != 2)  and (v.size() != 3) )  {
      cerr<<str<<endl;
      cerr<<" source or target sentence is not found in sentence number "<<line<<" , skip the sentence\n";
      continue;
    }

    src_corpus.push_back(v[0]);
    tgt_corpus.push_back(v[1]);
  }
  return true;
}


typedef aTextGrammar aGrammar;
aGrammar * load_grammar(string & grammar_filename){
  cerr<<"start_load_grammar "<<grammar_filename<<endl;

  aGrammar * test = new aGrammar(grammar_filename);

  return test;
}

Lattice convertSentenceToLattice(const string & str){

  std::vector<WordID> vID;
  TD::ConvertSentence(str , &vID);
  Lattice lsentence;
  lsentence.resize(vID.size());

  for (int i=0; i<vID.size(); i++){

    lsentence[i].push_back( LatticeArc(vID[i], 0.0, 1) );  
  }

  //  if(!lsentence.IsSentence())
  //  cout<<"not a sentence"<<endl;

  return lsentence;

}

bool parseSentencePair(const string & goal_sym, const string & src, const string & tgt,  GrammarPtr & g, Hypergraph &hg){


  //  cout<<"  Start parse the sentence pairs\n"<<endl;
  Lattice lsource = convertSentenceToLattice(src);
  
  //parse the source sentence by the grammar

  vector<GrammarPtr> grammars(1, g);

  ExhaustiveBottomUpParser parser = ExhaustiveBottomUpParser(goal_sym, grammars);
  
  if (!parser.Parse(lsource, &hg)){

     cerr<<"source sentence is not parsed by the grammar!"<<endl;
     return false;
   }

  //intersect the hg with the target sentence
  Lattice ltarget = convertSentenceToLattice(tgt);

  //forest.PrintGraphviz();
  if (!HG::Intersect(ltarget, & hg)) return false;

  SparseVector<double> reweight;
  
  reweight.set_value(FD::Convert("MinusLogP"), -1 );
  hg.Reweight(reweight);

  return true;
  
}




int main(int argc, char** argv){

  ParamsArray params(argc, argv);
  params.setDescription("scfg models");

  params.addConstraint("grammar_file", "grammar file (default ./grammar.pr )", true); //  optional                               

  params.addConstraint("input_file", "parallel input file (default ./parallel_corpora)", true); //optional                                         

  params.addConstraint("output_file", "grammar output file (default ./grammar_output)", true); //optional                                         

  params.addConstraint("goal_symbol", "top nonterminal symbol (default: X)", true); //optional                                         

  params.addConstraint("split", "split one nonterminal into 'split' nonterminals (default: 2)", true); //optional                                         

  params.addConstraint("prob_iters", "number of iterations (default: 10)", true); //optional                                         

  params.addConstraint("split_iters", "number of splitting iterations (default: 3)", true); //optional                                         

  params.addConstraint("alpha", "alpha (default: 0.1)", true); //optional                                         

  if (!params.runConstraints("scfg")) {
    return 0;
  }
  cerr<<"get parametters\n\n\n";


  string grammar_file = params.asString("grammar_file", "./grammar.pr");

  string input_file = params.asString("input_file", "parallel_corpora");

  string output_file = params.asString("output_file", "grammar_output");

  string goal_sym = params.asString("goal_symbol", "X");

  int max_split = atoi(params.asString("split", "2").c_str());
  
  int prob_iters = atoi(params.asString("prob_iters", "2").c_str());
  int split_iters = atoi(params.asString("split_iters", "1").c_str());
  double alpha = atof(params.asString("alpha", ".001").c_str());

  /////
  cerr<<"grammar_file ="<<grammar_file<<endl;
  cerr<<"input_file ="<< input_file<<endl;
  cerr<<"output_file ="<< output_file<<endl;
  cerr<<"goal_sym ="<< goal_sym<<endl;
  cerr<<"max_split ="<< max_split<<endl;
  cerr<<"prob_iters ="<< prob_iters<<endl;
  cerr<<"split_iters ="<< split_iters<<endl;
  cerr<<"alpha ="<< alpha<<endl;
  //////////////////////////

  cerr<<"\n\nLoad parallel corpus...\n";
  if (! openParallelCorpora(input_file))
    exit(1);

  cerr<<"Load grammar file ...\n";
  aGrammar * agrammar = load_grammar(grammar_file);
  agrammar->SetGoalNT(goal_sym);
  agrammar->setMaxSplit(max_split);
  agrammar->set_alpha(alpha);

  srand(123);

  GrammarPtr g( agrammar);
  Hypergraph hg;

  int data_size = src_corpus.size();
  int cnt_unparsed =0;
  for (int i =0; i <split_iters; i++){
    
    cerr<<"Split Nonterminals, iteration "<<(i+1)<<endl;
    agrammar->PrintAllRules(output_file+".s" + itos(i+1));
    agrammar->splitAllNonterminals();

    //vector<string> src_corpus;
    //vector<string> tgt_corpus;
    
    for (int j=0; j<prob_iters; j++){
      cerr<<"reset grammar score\n";
      agrammar->ResetScore();
      //      cerr<<"done reset grammar score\n";
      for (int k=0; k <data_size; k++){
	string src = src_corpus[k];
  
	string tgt = tgt_corpus[k];
	cerr <<"parse sentence pair: "<<src<<"  |||  "<<tgt<<endl;

	if (! parseSentencePair(goal_sym, src, tgt, g, hg) ){
	  cerr<<"target sentence is not parsed by the grammar!\n";
	  //return 1;
	  cnt_unparsed++;
	  continue;

	} 

	cerr<<"update edge posterior prob"<<endl;
	boost::static_pointer_cast<aGrammar>(g)->UpdateHgProsteriorProb(hg);
	hg.clear();
	if (k%1000 ==0 ) cerr<<"sentences "<<k<<endl;
      }
      cerr<<"cnt_unparased="<<cnt_unparsed<<endl;
      boost::static_pointer_cast<aGrammar>(g)->UpdateScore();
    }
    boost::static_pointer_cast<aGrammar>(g)->PrintAllRules(output_file+".e" + itos(i+1));
  }




  

 


  // // agrammar->ResetScore();
  // // agrammar->UpdateScore();
  // if (! parseSentencePair(goal_sym, src, tgt, g, hg) ){
  //   cerr<<"target sentence is not parsed by the grammar!\n";
  //   return 1;

  //  }
  // //   hg.PrintGraphviz();
  //  //hg.clear();

  // agrammar->PrintAllRules();
  // /*split grammar*/
  // cout<<"split NTs\n"; 
  // cerr<<"first of all write all nonterminals"<<endl;
  // // agrammar->printAllNonterminals();
  // cout<<"after split nonterminal"<<endl;
  // agrammar->PrintAllRules();
  // Hypergraph hg1;
  // if (! parseSentencePair(goal_sym, src, tgt,  g, hg1) ){
  //   cerr<<"target sentence is not parsed by the grammar!\n";
  //   return 1;

  // }

  // hg1.PrintGraphviz();
  

  // agrammar->splitNonterminal(15);
  // cout<<"after split nonterminal"<<TD::Convert(15)<<endl;
  // agrammar->PrintAllRules();

  
  /*load training corpus*/


  /*for each sentence pair in training corpus*/
 
  //  forest.PrintGraphviz();
  /*calculate expected count*/
  
}