/
DefaultKeywordWithDependency.java
392 lines (329 loc) · 8.85 KB
/
DefaultKeywordWithDependency.java
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package nlp4j.impl;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.text.StringEscapeUtils;
import nlp4j.KeywordWithDependency;
import nlp4j.util.XmlUtils;
/**
* 係り受けの関係を持つキーワードのクラスです。<br>
* Keyword with Dependency.
*
* @author Hiroki Oya
* @version 1.0
*
*/
public class DefaultKeywordWithDependency extends DefaultKeyword implements KeywordWithDependency {
private static final long serialVersionUID = 1L;
protected ArrayList<KeywordWithDependency> children = new ArrayList<KeywordWithDependency>();
protected String dependencyKey;
protected KeywordWithDependency parent;
protected String relation;
// int sequence = -1;
/**
* @since 1.3.1
*/
public DefaultKeywordWithDependency() {
super();
}
/**
* @param begin 開始位置
* @param end 終了位置
* @param facet ファセット
* @param lex 正規形
* @param str 表出文字
*/
public DefaultKeywordWithDependency(int begin, int end, String facet, String lex, String str) {
super(begin, end, facet, lex, str);
}
/**
* @param namespace 名前空間
* @param begin 開始位置
* @param end 終了位置
* @param facet ファセット
* @param lex 正規形
* @param str 表出文字
* @since 1.3.1
*/
public DefaultKeywordWithDependency(String namespace, int begin, int end, String facet, String lex, String str) {
super(namespace, begin, end, facet, lex, str);
}
/**
* @param namespace 名前空間
* @param begin 開始位置
* @param end 終了位置
* @param facet ファセット
* @param lex 正規形
* @param str 表出文字
* @param relation 係り受けの関係
* @since 1.3.1
*/
public DefaultKeywordWithDependency(String namespace, int begin, int end, String facet, String lex, String str,
String relation) {
super(namespace, begin, end, facet, lex, str);
this.relation = relation;
}
@Override
public void addBeginEnd(int n) {
// super.addBeginEnd(n);
this.begin += n;
this.end += n;
if (this.children != null) {
for (int x = 0; x < children.size(); x++) {
children.get(x).addBeginEnd(n);
}
}
}
@Override
public void addChild(KeywordWithDependency keyword) {
this.children.add(keyword);
keyword.setParentOnly(this);
}
@Override
public void addChildOnly(KeywordWithDependency keyword) {
this.children.add(keyword);
}
private void appendAttributes(StringBuffer sb, int depth) {
// sequence
if (this.sequence != -1) {
sb.append("sequence=\"" + this.sequence + "\" "); //
sb.append("id=\"" + this.sequence + "\" "); //
}
// str
if (this.str != null) {
sb.append("str=\"" + StringEscapeUtils.escapeXml10(this.str) + "\" "); //
}
// lex
if (this.lex != null) {
sb.append("lex=\"" + StringEscapeUtils.escapeXml10(this.lex) + "\" "); //
}
// depth
sb.append("depth=\"" + depth + "\" "); //
// facet
if (this.facet != null) {
sb.append("facet=\"" + StringEscapeUtils.escapeXml10(this.facet) + "\" "); //
}
// upos
if (this.upos != null) {
sb.append("upos=\"" + StringEscapeUtils.escapeXml10(this.upos) + "\" "); //
}
if (this.sentenceIndex != -1) {
sb.append("sentenceIndex=\"" + this.sentenceIndex + "\" "); //
}
// begin
if (this.begin != -1) {
sb.append("begin=\"" + this.begin + "\" "); //
}
// end
if (this.end != -1) {
sb.append("end=\"" + end + "\" "); //
}
// reading
if (this.reading != null) {
sb.append("reading=\"" + StringEscapeUtils.escapeXml10(this.reading) + "\" "); //
}
// relation
if (this.relation != null) {
sb.append("relation=\"" + StringEscapeUtils.escapeXml10(this.relation) + "\" "); //
}
}
@Override
public List<KeywordWithDependency> asList() {
List<KeywordWithDependency> ret = new ArrayList<KeywordWithDependency>();
ret.add(this);
for (KeywordWithDependency c : this.children) {
ret.addAll(c.asList());
}
return ret;
}
@Override
protected DefaultKeywordWithDependency clone() {
DefaultKeywordWithDependency c = (DefaultKeywordWithDependency) super.clone();
// Initialize c.children becaues c.children copied already
c.children = new ArrayList<>();
c.parent = null;
{
ArrayList<DefaultKeywordWithDependency> list = (ArrayList<DefaultKeywordWithDependency>) this.children
.clone();
for (DefaultKeywordWithDependency obj : list) {
// add cloned object
c.children.add(obj.clone());
}
}
c.dependencyKey = this.dependencyKey;
c.relation = this.relation;
return c;
}
@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
@Override
public ArrayList<KeywordWithDependency> getChildren() {
return this.children;
}
/**
* @return この依存関係をあらわす文字列キー
*/
public String getDependencyKey() {
return dependencyKey;
}
@Override
public int getDepth() {
if (this.parent == null) {
return 0;
} else {
return parent.getDepth() + 1;
}
}
@Override
public KeywordWithDependency getParent() {
return this.parent;
}
@Override
public KeywordWithDependency getParent(int depth) {
if (depth < 0) {
return null;
} else if (depth == 0) {
return this;
} else {
if (this.parent != null) {
return this.parent.getParent(depth - 1);
} else {
return null;
}
}
}
@Override
public String getRelation() {
return relation;
}
@Override
public KeywordWithDependency getRoot() {
if (this.parent == null) {
return this;
} else {
return this.parent.getRoot();
}
}
@Override
public int getSequence() {
return sequence;
}
@Override
public boolean hasChild() {
return (children != null && children.size() > 0);
}
@Override
public boolean hasParent() {
return (parent != null);
}
@Override
public boolean isRoot() {
return !hasParent();
}
/**
* @param dependencyKey この依存関係をあらわす文字列キー
*/
public void setDependencyKey(String dependencyKey) {
this.dependencyKey = dependencyKey;
}
@Override
public void setParent(KeywordWithDependency parent) {
this.parent = parent;
parent.addChildOnly(this);
}
@Override
public void setParentOnly(KeywordWithDependency parent) {
this.parent = parent;
}
@Override
public void setRelation(String relation) {
this.relation = relation;
}
@Override
public void setSequence(int sequence) {
this.sequence = sequence;
}
@Override
public int size() {
int size = 1; // this
for (KeywordWithDependency c : children) {
size += c.size();
}
return size;
}
@Override
public String toString() {
return this.lex + " [" //
+ "relation=" + relation + ", " // @since 1.2.1.0
+ "sequence=" + sequence + ", " //
+ "dependencyKey=" + dependencyKey + ", " //
+ "hasChildren=" + (children != null && children.size() > 0) + ", " //
+ "hasParent=" + !(parent == null) + ", " // fix @1.3.1.0
+ "facet=" + facet + ", " //
+ "lex=" + lex + ", " //
+ "str=" + str + ", " //
+ "reading=" + reading + ", " //
+ "begin=" + begin + ", " //
+ "end=" + end + "" + "]";
}
@Override
public String toStringAsDependencyList() {
StringBuffer sb = new StringBuffer();
if (parent != null) {
if (sb.length() > 0) {
sb.append("\n");
}
sb.append(this.getStr() + "..." + parent.getStr());
} else {
// do nothing
}
for (KeywordWithDependency c : children) {
if (sb.length() > 0) {
sb.append("\n");
}
sb.append(c.toStringAsDependencyList());
}
return sb.toString();
}
@Override
public String toStringAsDependencyTree() {
String indent = "\t";
String bar = "-";
StringBuffer sb = new StringBuffer();
for (int n = 0; n < this.getDepth(); n++) {
sb.append(indent);
}
sb.append(bar);
sb.append("sequence=" + this.sequence + ",lex=" + this.lex + ",str=" + this.str //
+ ",relation=" + this.relation // @since 1.2.1.0
);
for (KeywordWithDependency c : children) {
sb.append("\n");
sb.append(c.toStringAsDependencyTree());
}
return sb.toString();
}
@Override
public String toStringAsXml() {
return XmlUtils.prettyFormatXml(toStringAsXml(0));
}
public String toStringAsXml(int depth) {
StringBuffer sb = new StringBuffer();
if (children == null || children.size() == 0) {
sb.append("<w "); //
appendAttributes(sb, depth);
sb.append("/>"); // <-
} //
else {
sb.append("<w ");
appendAttributes(sb, depth);
sb.append(">"); // <-
for (KeywordWithDependency c : children) {
sb.append(c.toStringAsXml(depth + 1));
}
sb.append("</w>"); // <-
}
return sb.toString();
}
}