Parse.java
3.02 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
package is2.data;
import java.util.BitSet;
public class Parse implements Comparable<Parse> {
public short[] heads;
public short[] labels;
public double f1;
public Parse() {}
public Parse(int i) {
heads = new short[i];
labels = new short[i];
}
/**
* @param heads2
* @param types2
* @param p_new
*/
public Parse(short[] heads2, short[] types2, float p_new) {
this.heads = new short[heads2.length];
this.labels = new short[types2.length];
// this.heads=heads2;
// this.labels=types2;
System.arraycopy(heads2, 0, heads, 0, heads.length);
System.arraycopy(types2, 0, labels, 0, labels.length);
f1=p_new;
}
/**
* @param heads2
* @param types2
* @param p_new
*/
public Parse(String parse, float p_new) {
// this(parse.length()/2);
signature2parse(parse);
f1=p_new;
}
public void signature2parse(String parse) {
int p=0;
heads = new short[parse.length()/2];
labels = new short[heads.length];
// DB.println("pl "+parse.length());
for(int k=0;k<heads.length;k++) {
heads[k]= (short)parse.charAt(p++);
labels[k] = (short)parse.charAt(p++);
}
}
@Override
public Parse clone() {
Parse p = new Parse();
p.heads = new short[heads.length];
p.labels = new short[labels.length];
System.arraycopy(heads, 0, p.heads, 0, heads.length);
System.arraycopy(labels, 0, p.labels, 0, labels.length);
p.f1=f1;
return p;
}
/**
* Check if it is a tree
* @return
*/
public boolean checkTree() {
BitSet set = new BitSet(heads.length);
set.set(0);
return checkTree(set, 0);
}
/**
* @param set
* @return
*/
private boolean checkTree(BitSet set, int h) {
//System.out.print(" h "+h);
for(int i=0;i<heads.length;i++) {
if (heads[i]==h) {
// System.out.print(" "+i);
if (!set.get(i)) checkTree(set, i);
set.set(i);
}
}
for(int i=0;i<heads.length;i++) {
if (!set.get(i)) return false;
}
return true;
}
public String toString() {
StringBuilder b = new StringBuilder();
for(int k=0;k<this.heads.length;k++) {
b.append(k).append(" ").append(heads[k]+" ").append(this.labels[k]).append("\n");
}
return b.toString();
}
/* (non-Javadoc)
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
@Override
public int compareTo(Parse o) {
if (f1==o.f1) return this.signature().compareTo(o.signature());
return f1<o.f1?1:f1==o.f1?0:-1;
}
/**
* @return the signature of a parse
*/
public String signature() {
StringBuilder b = new StringBuilder(heads.length*2);
for(int k=0;k<heads.length;k++) {
b.append((char)heads[k]).append((char)labels[k]);
}
return b.toString();
}
/**
* @return the signature of a parse
*/
public StringBuilder signatureSB() {
StringBuilder b = new StringBuilder(heads.length*2);
for(int k=0;k<heads.length;k++) {
b.append((char)heads[k]).append((char)labels[k]);
}
return b;
}
}