-
Notifications
You must be signed in to change notification settings - Fork 0
/
headFinder.C
92 lines (84 loc) · 2.42 KB
/
headFinder.C
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
#include <set>
#include "headFinder.h"
#include "Term.h"
#include "InputTree.h"
// wul: 两个全局的头列表(less<ECString>定义了它们的比较函数)
set<ECString,less<ECString> > head1s;
set<ECString,less<ECString> > head2s;
void
readHeadInfo(ECString& path)
{
ECString headStrg(path);
headStrg += "headInfo.txt";
ifstream headStrm(headStrg.c_str());
assert(headStrm);
ECString next, next2;
headStrm >> next;
assert(next == "1");
int whichHeads = 1;
while(headStrm)
{
headStrm >> next;
if(!headStrm) break;
// wul: 这段代码似乎有bug,应该放到insert那段后面
// wul: 否则在格式切换时,喽掉一个数据
if(next == "2")
{
whichHeads = 2;
continue;
}
headStrm >> next2;
if(!headStrm) error("Bad format for headInfo.txt");
next += next2;
if(whichHeads == 1) head1s.insert(next);
else head2s.insert(next);
}
}
// wul: lhs表示left hand side, rhs表示right hand side
int
headPriority(ECString lhsString, ECString rhsString, int ansPriority)
{
const Term* rhsTerm = Term::get(rhsString);
if(!rhsTerm) return 11;
ECString both(lhsString);
both += rhsString;
if(lhsString == "PP" && ansPriority == 1) return 10;//make fst IN head of PP
if(head1s.find(both) != head1s.end()) return 1;
else if(ansPriority <= 2) return 10;
else if(rhsString == lhsString)
return 2; //lhs constit. e.g. np -> NP , np;
else if(head2s.find(both) != head2s.end()) return 3;
else if(ansPriority == 3) return 10;
else if(rhsTerm->terminal_p() && !rhsTerm->isPunc()) return 4;
else if(ansPriority == 4) return 10;
else if(!rhsTerm->terminal_p() && rhsTerm->name() != "PP")
return 5;
else if(ansPriority == 5) return 10;
else if(!rhsTerm->terminal_p()) return 6;
else if(ansPriority == 6) return 10;
else return 7;
}
int
headPosFromTree(InputTree* tree)
{
int ansPriority = 10;
ECString lhsString(tree->term());
if(lhsString == "") lhsString = "S1";
int pos = -1;
int ans = -1;
ConstInputTreesIter subTreeIter = tree->subTrees().begin();
InputTree *subTree;
for( ; subTreeIter != tree->subTrees().end() ; subTreeIter++ )
{
subTree = *subTreeIter;
pos++;
ECString rhsString(subTree->term());
int nextPriority = headPriority(lhsString, rhsString, ansPriority);
if(nextPriority <= ansPriority)
{
ans = pos;
ansPriority = nextPriority;
}
}
return ans;
}