-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoadTreeAndAlign.cpp
More file actions
214 lines (193 loc) · 7.17 KB
/
LoadTreeAndAlign.cpp
File metadata and controls
214 lines (193 loc) · 7.17 KB
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
#include "LoadTreeAndAlign.h"
int TCTTreeAndAlign::LoadResource(const string &tct, const string&align)
{
//align: 带句子长度
this->mChsTree = new SynTreeSQ;
if(this->mChsTree->ParseTree(tct,0)==-1)return -1; // tct分析出错
//读取对齐数据
using namespace boost;
stringstream ss(align);
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));
}
//记录wordIndex 2 edu
allChnEdus = GetAllEdus(&mChsTree->root);
if (allChnEdus.size()<2)return -1;
GetWord2EduMap(allChnEdus, word2EduChn);
//记录所有关系点
allChsNodes = GetAllNode(&mChsTree->root);
return 0;
}
void TCTTreeAndAlign::PushBackBoundaryPair(int i,int enBegin,int enBack)
{
this->alignCh2EnMap[i]= new pair<int,int>(enBegin,enBack);
}
pair<int,int> * TCTTreeAndAlign::GetEnSpanIndex(int i)
{
map<int,pair<int,int>* >::iterator it = this->alignCh2EnMap.find(i);
if (it != alignCh2EnMap.end() && it->second != NULL)return it->second;
else return NULL;
}
pair<int,int> * TCTTreeAndAlign::GetPrjSpanIndex(int i,bool chn)
{
map<int,pair<int,int>* >::iterator it = chn?this->alignCh2EnMap.find(i):this->alignEn2ChMap.find(i);
if ((it != alignCh2EnMap.end()|| it!= alignEn2ChMap.end()) && it->second != NULL)return it->second;
else return NULL;
}
int TCTTreeAndAlign::GetOneRelationAndEdus(int index,RelEdu &res)
{
RTreeNode *node = allChsNodes[index];
vector<int>().swap(res.eduVec);
if (node->value == "BL")res.relIndex=1;
else if(node->value == "LG")res.relIndex=2;else if(node->value == "DJ")res.relIndex=3;
else if(node->value == "XZ")res.relIndex=4;else if(node->value == "MD")res.relIndex=5;
else if(node->value == "JS")res.relIndex=6;else if(node->value == "TJ")res.relIndex=7;
else if(node->value == "ZE")res.relIndex=8;else if(node->value == "JZ")res.relIndex=9;
else if(node->value == "LS")res.relIndex=10;else if(node->value == "UKN")res.relIndex=11;
else if(node->value == "XX")res.relIndex=0;else if(node->value == "YG")res.relIndex=12;
else res.relIndex=13;
if (node->children.size()<2) return -1;//只有一个孩子,不是关系节点
for(size_t i(0);i<node->children.size();++i){
map<int,int>::iterator it = word2EduChn.find(node->children[i]->Begin);
if (it!=word2EduChn.end())res.eduVec.push_back(it->second);
else {cerr<<"wrong in word2EduChn\n";
exit(0);
}
}
return 0;
}
void TCTTreeAndAlign::GetMinAndMaxBoundary(int Begin, int Back, pair<int,int> &boundary)
{
int a(sizeE),b(-1); map<int,int>::iterator it;
for(int i(Begin);i<=Back;++i){
it = C2EMap.find(i);
if (it!=C2EMap.end()){
if (it->second < a)a=it->second;
if (it->second > b)b=it->second;
}
}
boundary.first=a;boundary.second=b;
}
void TCTTreeAndAlign::Grow()
{
pair<int,int> bd;
for(size_t i(0);i<allChnEdus.size();++i)
{
if(allChnEdus[i]->Back == allChnEdus[i]->Begin){alignCh2EnMap[i] = NULL;continue;} //逗号跳过
GetMinAndMaxBoundary(allChnEdus[i]->Begin,allChnEdus[i]->Back,bd);
if (bd.first == sizeE || bd.second == -1){//未对齐跳过
alignCh2EnMap[i] = NULL;continue;
}
int curPos,left,right;
//向左延伸
for(curPos=(bd.first-1);curPos>=0;curPos--){
if (E2CMap.find(curPos)!=E2CMap.end())break;
}
left = curPos+1;
//向右延伸
right = bd.second > bd.first+2 ? bd.first+2:bd.second;
alignCh2EnMap[i]= new pair<int,int>(left,right);
}
}
int TCTTreeAndAlign::PrintWord2RelPosfile(const string &tct)
{
this->mChsTree = new SynTreeSQ;
if(this->mChsTree->ParseTree(tct,0)==-1)return -1;
//记录所有关系点
allChsNodes = GetAllNode(&mChsTree->root);
RTreeNode *node;
for(size_t i(0);i<allChsNodes.size();++i){
node = allChsNodes[i];
if(node->children.size()<2)continue;
for(size_t j(1);j<node->children.size();++j){
if (node->children[j]->Begin == node->children[j]->Back)continue;
//cerr<<"<"<<node->children[j]->Begin<<" "<<node->children[j]->Back<<"> ";
cout << node->children[j]->Begin+1 <<" "<<node->value;
if (j==node->children.size()-1)
cout<<"2"<<" ";
else
cout<<"1"<<" ";
}
}
cout<<endl;
}
int TCTTreeAndAlign::PrintRelStartEnd(const string & tct)
{
this->mChsTree = new SynTreeSQ;
if(this->mChsTree->ParseTree(tct,0)==-1)return -1;
//记录所有关系点
allChsNodes = GetAllNode(&mChsTree->root);
RTreeNode *node;
for(size_t i(0);i<allChsNodes.size();++i){
node = allChsNodes[i];
if(node->value == "0" && node->children.size() > 1)cout<<"NULL ";
else if(node->children.size()<2)cout<<"EDU ";
else cout<<node->value<<" ";
cout<<node->Begin<<" "<<node->Back<<" ";
}
cout<<endl;
}
int GetLowerBound(int wordId, bool chn)
{
multimap<int,int>* mp = chn? &C2EMap : &E2CMap;
multimap<int,int>::iterator it = mp->lower_bound(wordId);
if(it==mp->end())return -1; //align to null
return it->second;
}
int GetUpperBound(int wordId, bool Chn=true)
{
multimap<int,int>* mp = chn? &C2EMap : &E2CMap;
multimap<int,int>::iterator it = mp->upper_bound(wordId);
if(it==mp->end())return -1; //align to null
return it->second;
}
int TCTTreeAndAlign::GetEDUIndex(int wordId, bool chn)
{
map<int,int>::iterator it;
if(chn){
it = word2EduChn.find(wordId);
}
else it = word2EduEng.find(wordId);
if (it==word2EduChn.end() || it == word2EduEng.end())return -1;// wrong to find wordid
else return it->second;
}
int TCTTreeAndAlign::ComputeEDUDice(int eduId,bool chn, vector<eSpanScorePair>&diceRes)
{
//return -1: wrong
//return 0: NULL return
//return 1: normal
RTreeNode *node;
if(chn && eduId<allChnEdus.size())node = allChnEdus[eduId];
else if(!chn && eduId < allEngEdus.size())node = allEngEdus[eduId];
else{cerr<<"eduId "<<eduId<<" is greater than the edu\n Is chinese ? "<<chn<<endl; return -1;} //wrong
int prjBeg, prjBack;pair<int,int> *p = GetPrjSpanIndex(eduId,chn);
if(p == NULL)return 0;
prjBeg = GetEDUIndex(p->first,chn);
prjBack = GetEDUIndex(p->second,chn);
if(prjBeg == -1 || prjBack == -1) {cerr<<"chnedu: "<<p->first<<" "<<p->second<<endl;return -1;}
int inter = 0; ;
int lower, upper, countAlign(0), countInter(0);
map<int,int> *mp = chn? &this->word2EduChn : &this->word2EduEng;
vector<RTreeNode *> *vp =chn? &this->allEngEdus : &this->allChnEdus;
map<int,int>::iterator it=mp->begin();
for(int i(prjBeg);i<=prjBack;++i){
node = vp->at(i);
for(int j(node->Begin);j<=node->Back;++j){
lower = GetLowerBound(j,!chn);upper = GetUpperBound(j,!chn);
if (lower == -1 || upper == -1) continue;
}
}
}