-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExtractRule.cpp
333 lines (311 loc) · 10.5 KB
/
ExtractRule.cpp
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include "ExtractRule.h"
using namespace std;
void AlignedRel::LoadAlignedSentence(const string &s)
{
using namespace boost;
stringstream ss(s);
int key(0),value(0);
size_t pos;string word;
ss>>word;
sizeC = lexical_cast<int>(word);
ss>>word;
sizeE = lexical_cast<int>(word);
while(ss>>word)
{
if((pos=word.find("-"))!=string::npos)
{
key=lexical_cast<int>(word.substr(0,pos));
value=lexical_cast<int>(word.substr(pos+1));
}
C2EMap.insert(make_pair(key,value));
E2CMap.insert(make_pair(value,key));
}
}
int AlignedRel::GetChSize()
{
return C2EMap.rbegin()->first+1;
}
int AlignedRel::GetEnSize()
{
return E2CMap.rbegin()->first+1;
}
vector<int> AlignedRel::GetCSpanGivenESpan(int start,int back)
{
vector<int> res;
multimap<int,int>::iterator it,it1;
for(size_t i(start);i<=back;++i)
{
if(E2CMap.find(i)==E2CMap.end())continue;
it=E2CMap.lower_bound(i);it1=E2CMap.upper_bound(i);
while(it!=it1)
{
res.push_back(it->second);
it++;
}
}
sort(res.begin(),res.end());
// copy(res.begin(),res.end(),ostream_iterator<int>(cerr," "));cerr<<endl;
return res;
}
vector<int> AlignedRel::GetESpanGivenCSpan(int start,int back)
{
vector<int> res;
if (back<start){cerr<<"back less than start\n";return res;}
multimap<int,int>::iterator it,it1;
for(size_t i(start);i<=back;++i)
{
if(C2EMap.find(i)==C2EMap.end())continue;
it=C2EMap.lower_bound(i);it1=C2EMap.upper_bound(i);
while(it!=it1)
{
res.push_back(it->second);
it++;
}
}
sort(res.begin(),res.end());
// copy(res.begin(),res.end(),ostream_iterator<int>(cerr," "));cerr<<endl;
return res;
}
int AlignedRel::CountOfAPreceedB(vector<int> &avec,vector<int>&bvec)
{
int countAll(0),countIter;
// cerr<<avec.size()<<" "<<bvec.size()<<endl;
for(size_t i(0);i<avec.size();++i)
{
countIter=0;
for(size_t j(0);j<bvec.size();++j)
{
// cerr<<avec[i]<<' '<<bvec[j]<<endl;
if(avec[i]> bvec[j])continue;
countIter = bvec.size()-j; break;
}
countAll += countIter;
}
return countAll;
}
bool AlignedRel::IsReverse(int a,int b,int c)
{
return a*2 < b*c;
}
bool TransRule::LoadItemAndRST(const string&aligns,const string&sRST,const string&ens)
{
//cerr<<sRST<<endl;
rstTree.MyConstructRSTTree(sRST);
cerr<<"rst tree has been parsed\n";
singleTreeVec = rstTree.GetAllRSTTree();
// cerr<<singleTreeVec.size()<<endl;
GetEnWordVec(ens);
align.LoadAlignedSentence(aligns);
//if(align.GetChSize() > rstTree.wordVec.size()){cerr<<"ch in align unequal to rst vec size\n"<<align.GetChSize()<<" "<<rstTree.wordVec.size();exit(1);}
if(align.GetSizeOfC()!=rstTree.wordVec.size()){cerr<<"ch in align unequal to rst vec size\n"<<align.GetSizeOfC()<<" "<<rstTree.wordVec.size();return false;}
if(align.GetSizeOfE()!=enWordVec.size()){cerr<<"en in align unequal to data vec size\n"<<align.GetSizeOfE()<<" "<<enWordVec.size();return false;}
vector<int>().swap(orderVec);
return true;
}
std::string TransRule::GetEnStrWithChId(vector<int> &vec)
{
string res;
set<int> mset;
vector<int> mvec;
vector<int> enVec;
for(size_t i(0);i<vec.size();++i)
{
enVec = align.GetESpanGivenCSpan(vec[i],vec[i]);
mvec.insert(mvec.end(),enVec.begin(),enVec.end());
}
copy(mvec.begin(),mvec.end(),insert_iterator<set<int> >(mset,mset.begin()));
set<int>::iterator it=mset.begin();
while(it!=mset.end())
{
// cerr<<(*it)<<" ";
res += enWordVec[(*it)]+" ";it++;
}
return res.substr(0,res.length()-1);
}
std::string TransRule::GetStrWithId(unsigned start_word,unsigned end_word)
{
stringstream ss;
if((int) start_word < 0){cerr<<"wrong start-word : "<<int (start_word)<<endl;return "";}
copy(rstTree.wordVec.begin()+start_word,rstTree.wordVec.begin()+end_word+1, ostream_iterator<string,char>(ss," "));
string r=ss.str();
return r.substr(0,r.length()-1);
}
int TransRule::GetSpanGiveSrc(RSTNode *node)
{
vector<int>().swap(leftVec);vector<int>().swap(rightVec);
if(node->leftChild==NULL || node->rightChild==NULL)return -1;
leftVec=align.GetESpanGivenCSpan(node->leftChild->Begin,node->leftChild->Back);
rightVec=align.GetESpanGivenCSpan(node->rightChild->Begin,node->rightChild->Back);
if(leftVec.empty()||rightVec.empty())return -1;
return 1;
}
int TransRule::ExtractOrder(RSTNode *node)
{
int countLPreceed=align.CountOfAPreceedB(leftVec,rightVec);
if(align.IsReverse(countLPreceed,leftVec.size(),rightVec.size()))
return 1;
else return 0;
}
int CountWordNum(const string &s)
{
if(s=="")return 0;
stringstream ss(s);int count=0;string word;
while(ss>>word)count++;
return count;
}
bool GetKeyWordId(int startId,const string&key,const string&s,vector<int> &resVec)
{
using namespace boost;
boost::match_results<string::const_iterator> mr;
vector<int>().swap(resVec);
string tmpkey = boost::replace_all_copy(key,"\\s"," ");
boost::replace_all(tmpkey,"[X]"," (.*) ");
stringstream ss(s);
int count(0);
if(tmpkey.find(" ")==string::npos)
{
string word;
while(ss>>word)
{
resVec.push_back(count+startId);
if (word == tmpkey){return true;}
count++;
}
return false;
}
else
{
int pre(0),cur,preCount(0);string prefix;
// boost::replace(tmpkey,"X"," (.*) ");
tmpkey="( |^)"+tmpkey+"( |$)";
if(!regex_search(s,mr,regex(tmpkey)))return false;
prefix = s.substr(0,mr.position());count = CountWordNum(prefix);
pre=mr.position();preCount = count;
for(int i(2);i<mr.size();++i)
{
prefix = s.substr(pre,mr.position(i)-pre);
count = preCount+CountWordNum(prefix);
for(int i(preCount);i<count;++i)resVec.push_back(i+startId);
pre = mr.position(i)+mr.length(i);
preCount = count+CountWordNum(mr.str(i));
}
if(pre!=mr.position()+mr.length())
{
prefix=s.substr(pre,mr.position()+mr.length()-pre);
count = preCount+CountWordNum(prefix);
for(int i(preCount);i<count;++i)resVec.push_back(i+startId);
}
return true;
}
}
int TransRule::UnitExtractTransPairs(RSTNode *node, string &res)
{
vector<int>resVec;
string key=node->keyWord;
string s=GetStrWithId(node->Begin,node->Back);
if(s=="")return -1;
bool IsKeyId=GetKeyWordId(node->Begin,key,s,resVec);
if(!IsKeyId)return -1;
copy(resVec.begin(),resVec.end(),ostream_iterator<int>(cerr," "));
cerr<<endl;
res = GetEnStrWithChId(resVec);
vector<int>().swap(resVec);
return 1;
}
int TransRule::ExtractTransPairs(RSTNode *node,string &res)
{
string leftStr,rightStr;res="";
if(UnitExtractTransPairs(node->leftChild,leftStr)==-1)leftStr="NULL";
if(UnitExtractTransPairs(node->rightChild,rightStr)==-1)rightStr="NULL";
if(leftStr=="NULL" && rightStr=="NULL")return -1;
res=leftStr+"/"+rightStr;
return 1;
}
void TransRule::UpdateOrderVec()
{
int reverse;
for(size_t i(0);i<singleTreeVec.size();++i)
{
if(GetSpanGiveSrc(singleTreeVec[i])==-1){reverse=-1;continue;}
reverse = ExtractOrder(singleTreeVec[i]);
orderVec.push_back(reverse);
}
}
float SumRow(ValueAndCount &row)
{
int sum(0);
map<string,CountStruct>::iterator it=row.begin();
while(it!=row.end())
{
sum += (*it).second.count;
it++;
}
return float(sum);
}
float SumNewRow(vector<VCPair> &row)
{
int sum(0);
for(size_t i(0);i<row.size();++i)sum += row[i].second.count;
return float(sum);
}
void MakeStOfBiLm_1(int index,TransRule &rule,map<string,int>&biIndex,vector<vector<int> > &biLmIndexMap,\
vector<vector<int> >&orderIndexMap, Row &transSet)
{
string key,fk,sk,tranStr;
int indexMap,reverse,isExtracPair;bool IsSpanLegal;
for (size_t i(0);i<rule.singleTreeVec.size();++i)
{
if(rule.singleTreeVec[i]->rightChild!=NULL)
{
if(rule.singleTreeVec[i]->leftChild->keyWord=="")fk="NULL";
else fk=rule.singleTreeVec[i]->leftChild->keyWord;
if(rule.singleTreeVec[i]->rightChild->keyWord=="")sk="NULL";
else sk=rule.singleTreeVec[i]->rightChild->keyWord;
if(fk=="NULL" && sk=="NULL")continue;
key=fk+'/'+sk;
}
else continue;
IsSpanLegal=(rule.GetSpanGiveSrc(rule.singleTreeVec[i])==1)?true:false;
if(IsSpanLegal)
{
reverse=rule.ExtractOrder(rule.singleTreeVec[i]);
isExtracPair=rule.ExtractTransPairs(rule.singleTreeVec[i],tranStr);
if(biIndex.find(key)!=biIndex.end()){
indexMap = biIndex[key];
biLmIndexMap[indexMap].push_back(index);
// if(reverse==1)orderIndexMap[indexMap][1]+=1;
// else if(reverse==0)orderIndexMap[indexMap][0]+=1;
if(isExtracPair==1)
{
if(transSet[indexMap].find(tranStr)!=transSet[indexMap].end())
{
transSet[indexMap][tranStr].count += 1;
if(reverse==1)transSet[indexMap][tranStr].revCount+=1;
}
else
{
transSet[indexMap][tranStr].count=1;
if(reverse==1)transSet[indexMap][tranStr].revCount=1;
else transSet[indexMap][tranStr].revCount=0;
}
}
}
else{
//cerr<<biIndex.size()<<endl;
biIndex.insert(make_pair(key,biIndex.size()));
biLmIndexMap.push_back(vector<int>(1,index));
// orderIndexMap.push_back(vector<int>(2,0));
// if(reverse==1)orderIndexMap.back()[1]+=1;
// else if(reverse==0)orderIndexMap.back()[0]+=1;
if(isExtracPair==1)
{
map<string,CountStruct> tmpMap;tmpMap[tranStr].count=1;
if(reverse==1)tmpMap[tranStr].revCount=1;
else tmpMap[tranStr].revCount=0;
transSet.push_back(tmpMap);
}
else transSet.push_back(map<string,CountStruct>());
}
}//is span legal
}
}