-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmorph.cpp
More file actions
218 lines (200 loc) · 5.8 KB
/
morph.cpp
File metadata and controls
218 lines (200 loc) · 5.8 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
215
216
217
218
#include <cstdlib>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <set>
using namespace std;
struct MorphDic
{
map<string, string> verbex;
map<string, string> nounex;
map<string, string> advex;
map<string, string> adjex;
set<string> noundic;
set<string> verbdic;
set<string> adjdic;
MorphDic() {}
void Init() {
cerr << "loading morphdic...";
LoadEx("./morphdic/noun.exc", nounex);
LoadEx("./morphdic/verb.exc", verbex);
LoadEx("./morphdic/adj.exc", adjex);
LoadEx("./morphdic/adv.exc", advex);
LoadIdx("./morphdic/noun.dic", noundic);
LoadIdx("./morphdic/verb.dic", verbdic);
LoadIdx("./morphdic/adj.dic", adjdic);
cerr << "done." << endl;
}
void LoadEx(const string & filename, map<string, string> & exmap) {
ifstream ifile(filename.c_str());
if (!ifile) {
cerr << "error: cannot open " << filename << endl;
exit(1);
}
string line;
while (getline(ifile, line)) {
istringstream is(line);
string org, base;
is >> org >> base;
exmap[org] = base;
string org2(org), base2(base);
org2[0] = toupper(org[0]);
base2[0] = toupper(base[0]);
exmap[org2] = base2;
}
}
void LoadIdx(const string & filename, set<string> & dic) {
ifstream ifile(filename.c_str());
if (!ifile) {
cerr << "error: cannot open " << filename << endl;
exit(1);
}
string line;
while (getline(ifile, line)) {
if (line[0] == ' ') continue;
istringstream is(line);
string base;
is >> base;
dic.insert(base);
string base2(base);
base2[0] = toupper(base[0]);
dic.insert(base2);
}
}
bool LookUpDicNoun(const string & s) {
return noundic.find(s) != noundic.end();
}
bool LookUpDicVerb(const string & s) {
return verbdic.find(s) != verbdic.end();
}
bool LookUpDicAdj(const string & s) {
return adjdic.find(s) != adjdic.end();
}
string BaseFormNoun(const string & s) {
map<string, string>::const_iterator i = nounex.find(s);
if (i == nounex.end()) return "";
return i->second;
}
string BaseFormVerb(const string & s) {
map<string, string>::const_iterator i = verbex.find(s);
if (i == verbex.end()) return "";
return i->second;
}
string BaseFormAdj(const string & s) {
map<string, string>::const_iterator i = adjex.find(s);
if (i == adjex.end()) return "";
return i->second;
}
string BaseFormAdv(const string & s) {
map<string, string>::const_iterator i = advex.find(s);
if (i == advex.end()) return "";
return i->second;
}
};
MorphDic morphdic;
static string base_form_noun(const string & s)
{
string ex = morphdic.BaseFormNoun(s);
if (ex != "") return ex;
int len = s.size();
if (len > 1) {
string suf1 = s.substr(len - 1);
if (suf1 == "s") {
if (morphdic.LookUpDicNoun(s.substr(0, len - 1))) return s.substr(0, len - 1);
// if (morphdic.LookUpDicVerb(s.substr(0, len - 1))) return s.substr(0, len - 1);
}
}
if (len > 4) {
string suf4 = s.substr(len - 4);
if (suf4 == "ches") return s.substr(0, len - 4) + "ch";
if (suf4 == "shes") return s.substr(0, len - 4) + "sh";
}
if (len > 3) {
string suf3 = s.substr(len - 3);
if (suf3 == "ses") return s.substr(0, len - 3) + "s";
if (suf3 == "xes") return s.substr(0, len - 3) + "x";
if (suf3 == "zes") return s.substr(0, len - 3) + "z";
if (suf3 == "men") return s.substr(0, len - 3) + "man";
if (suf3 == "ies") return s.substr(0, len - 3) + "y";
}
if (len > 1) {
string suf1 = s.substr(len - 1);
if (suf1 == "s") return s.substr(0, len - 1);
}
return s;
}
static string base_form_verb(const string & s)
{
string ex = morphdic.BaseFormVerb(s);
if (ex != "") return ex;
if (morphdic.LookUpDicVerb(s)) return s;
int len = s.size();
if (len > 3) {
string suf3 = s.substr(len - 3);
if (suf3 == "ies") return s.substr(0, len - 3) + "y";
if (suf3 == "ing") {
if (morphdic.LookUpDicVerb(s.substr(0, len - 3))) return s.substr(0, len - 3);
else return s.substr(0, len - 3) + "e";
}
}
if (len > 2) {
string suf2 = s.substr(len - 2);
if (suf2 == "es" || suf2 == "ed") {
if (morphdic.LookUpDicVerb(s.substr(0, len - 2))) return s.substr(0, len - 2);
else return s.substr(0, len - 2) + "e";
}
}
if (len > 1) {
string suf1 = s.substr(len - 1);
if (suf1 == "s") return s.substr(0, len - 1);
}
return s;
}
static string base_form_adjective(const string & s)
{
string ex = morphdic.BaseFormAdj(s);
if (ex != "") return ex;
int len = s.size();
if (len > 3) {
string suf3 = s.substr(len - 3);
if (suf3 == "est") {
if (morphdic.LookUpDicAdj(s.substr(0, len - 3) + "e")) return s.substr(0, len - 3) + "e";
else return s.substr(0, len - 3);
}
}
if (len > 2) {
string suf2 = s.substr(len - 2);
if (suf2 == "er") {
if (morphdic.LookUpDicAdj(s.substr(0, len - 2) + "e")) return s.substr(0, len - 2) + "e";
else return s.substr(0, len - 2);
}
}
return s;
}
static string base_form_adverb(const string & s)
{
string ex = morphdic.BaseFormAdv(s);
if (ex != "") return ex;
return s;
}
void init_morphdic()
{
morphdic.Init();
}
string base_form(const string & s, const string & pos)
{
if (pos == "NNS") return base_form_noun(s);
if (pos == "NNPS") return base_form_noun(s);
if (pos == "JJR") return base_form_adjective(s);
if (pos == "JJS") return base_form_adjective(s);
if (pos == "RBR") return base_form_adverb(s);
if (pos == "RBS") return base_form_adverb(s);
if (pos == "VBD") return base_form_verb(s);
if (pos == "VBG") return base_form_verb(s);
if (pos == "VBN") return base_form_verb(s);
if (pos == "VBP") return base_form_verb(s);
if (pos == "VBZ") return base_form_verb(s);
return s;
}