-
Notifications
You must be signed in to change notification settings - Fork 0
/
ToMakeHier.cpp
202 lines (175 loc) · 6.38 KB
/
ToMakeHier.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
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<iterator>
#include<fstream>
#include<sstream>
#include<boost/regex.hpp>
#include<boost/algorithm/string.hpp>
#include<boost/lexical_cast.hpp>
using namespace std;
struct TreeNode
{
string value;
TreeNode *parent;
int id;
int Begin;
int Back;
vector<TreeNode*> children;
TreeNode(const string&s, TreeNode* m_parent):value(s),parent(m_parent),id(0){};
};
class SynTree
{
public:
TreeNode root;
SynTree(const string&ori_s):root("",NULL){
stringstream ss(ori_s);
copy(istream_iterator<string,char>(ss),istream_iterator<string,char>(),back_insert_iterator<vector<string> >(sentenceVec));
ss.str("");
}
int ParseTree(const string& s, int mode=0);
int ClearTree();
void PrintTree(TreeNode *node,int hier_n);
TreeNode* GetNodeWithId(int id);
int GetSpanStartWithNode(TreeNode *node);
int GetSpanEndWithNode(TreeNode *node);
void GetStrVec(int begin, int back,vector<string>&result);
int GetSize(){return nodeIdMap.size();}
vector<string> sentenceVec;
private:
map<int, TreeNode*> nodeIdMap;
};
/*******************************************************************************/
int LeftBranket(const string& s, string & str_flw_lb)
{
if (s.length() < 2 )
return -1;
else{ str_flw_lb = s.substr(1);return 1;}
}
int RightBranket(const string& s, string & str_bf_rb, int count , vector<string>sentenceVec)
{
str_bf_rb=sentenceVec[count];
int startPos=str_bf_rb.length();
return s.length()-startPos;
}
int SynTree::ParseTree(const string&s, int mode)
{
stringstream ss(s);
string word,str_flw_lb,str_bf_rb;
TreeNode *currentNode(&root);
TreeNode *tmpNode = new TreeNode("",NULL);
int emptyLB=0,check,brkNum;
while(ss>>word)
{
check = LeftBranket(word,str_flw_lb);
if(check==-1){emptyLB++;continue;}
else break;
}
if(check == 0 || check == -1){cerr<<"wrong start\n";cerr<<s<<endl;exit(1);}
tmpNode = new TreeNode(str_flw_lb,currentNode);
currentNode->children.push_back(tmpNode);
currentNode = tmpNode;
while(ss>>word)
{
if (word[0]=='('&&word[word.size()-1]!=')'){ //check word starting with (
check = LeftBranket(word,str_flw_lb);
if(check==1)
{
tmpNode = new TreeNode(str_flw_lb,currentNode);tmpNode->Begin = mode;
currentNode->children.push_back(tmpNode);
currentNode = tmpNode; continue;
}
else if(check == 0)
brkNum = RightBranket(word,str_bf_rb,mode,sentenceVec);
}
else if (word==")")continue; //the last )
else if(word[word.length()-1]==')')brkNum = RightBranket(word,str_bf_rb,mode,sentenceVec);
else {cerr<<"wrong right brankets\n\n";exit(1);}
tmpNode = new TreeNode(str_bf_rb,currentNode);
if(mode!=-1) {tmpNode->id=mode;tmpNode->Begin=mode;tmpNode->Back=mode;nodeIdMap[mode++]= tmpNode;} //for node id map
currentNode->children.push_back(tmpNode);currentNode->Back = mode-1;
if (brkNum==0)continue;
for (;brkNum>0;brkNum--)
{
currentNode = currentNode->parent;
currentNode->Back = mode-1;
}
}
PrintTree(&root,0);
cout<<endl;
return 0;
}
int SynTree::ClearTree()
{
TreeNode * currentNode = &root;
while(currentNode->parent != NULL || !currentNode->children.empty())
{
while(!currentNode->children.empty())
currentNode = currentNode->children.back();
currentNode = currentNode->parent;
for(size_t i(0);i<currentNode->children.size();++i)
delete currentNode->children[i];
vector<TreeNode *>().swap(currentNode->children);
currentNode = currentNode->parent;
delete currentNode->children.back();
currentNode->children.pop_back();
}
return 0;
}
void SynTree::PrintTree(TreeNode *node,int hier_n)
{
TreeNode *currentNode = node;
if (currentNode->children[0]->children.empty())
{cout<<' '<<currentNode->children[0]->value;return;}
for(size_t i(0);i< node->children.size();++i)
{
currentNode = node->children[i];
cout<<'\n';
for(size_t j(0);j<hier_n;++j)cout<<" ";
cout<<" ("<<currentNode->value/*<<"//"<<currentNode->Begin<<"//"<<currentNode->Back*/;
PrintTree( currentNode,hier_n+2); cout<<")";
}
}
int SynTree::GetSpanStartWithNode(TreeNode *node)
{
TreeNode *currentNode = node;
while(!currentNode->children.empty())currentNode=currentNode->children[0];
return currentNode->id;
}
int SynTree::GetSpanEndWithNode(TreeNode *node)
{
TreeNode *currentNode = node;
while(!currentNode->children.empty())currentNode=currentNode->children.back();
return currentNode->id;
}
TreeNode *SynTree::GetNodeWithId(int id)
{
return nodeIdMap[id];
}
void SynTree::GetStrVec(int begin, int back,vector<string> &result)
{
if(back>nodeIdMap.size()-1 || back == -1)back = nodeIdMap.size()-1;
for(;begin<=back;++begin)
result.push_back(nodeIdMap[begin]->value);
}
int main(int argc,char *argv[])
{
/* string s="( (FRAG (NR 新华社) (NR 上海) (NT 12月) (NT 13日) (NN 电) (NR () (NN 记者) (NR 刘丹)) (NT 13日) (NP (CP (IP (VP (PP (P 在) (NP (NR 上海))) (VP (VV 闭幕)))) (DEC 的)) (NP (NR \")) (NP (NP (NR 中国) (NN 青少年) (NN 社会) (NN 教育) (NN 论坛)) (PRN (PU ──) (NP (QP (CD 2004・)) (NP (NN 媒体) (CC 与) (NN 未成年人)))) (NP (NN 发展))) (NP (NN \"))) (NN 倡议) (PU :) (NP (NR 中国)) (VP (VP (VV 应) (VP (VV 建立) (NP (DNP (PP (P 有关) (NP (NN 未成年人) (NN 状况))) (DEG 的)) (NP (NN 新闻) (NN 发言人)) (NP (NN 制度))))) (PU ,) (VP (ADVP (AD 定期)) (PP (P 就) (NP (NP (NN 未成年人)) (CP (IP (VP (VV 发展) (NP (NN 现状) (CC 和) (NN 存在)))) (DEC 的)) (NP (NN 问题)))) (VP (VV 发布) (NP (NN 消息)))) (PU 。))) )";
SynTree *mytree=new SynTree;
mytree->ParseTree(s,0);*/
if(argc!=3){cerr<<argv[0]<<"<orifile> <synfile> \n";exit(1);}
ifstream in(argv[1]);
ifstream in1(argv[2]);
int count(0);
if(!in||!in1){cerr<<"wrong to open\n";exit(1);}
string s,s1;
while(getline(in,s)&&getline(in1,s1))
{
SynTree *mytree=new SynTree(s);
cout<<count++<<":";
mytree->ParseTree(s1,0);
}
return 0;
}