-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordFinder.java
200 lines (164 loc) · 5.94 KB
/
WordFinder.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
import java.io.*;
import java.util.*;
public class WordFinder implements Constants
{
private InvertedIndex index = null;
public static void main(String[] argv) throws IOException
{
WordFinder finder = new WordFinder();
List<PointerPair[]> pairs = finder.querySearch(argv);
for (PointerPair[] pair : pairs)
{
int howMany = 0;
howMany = finder.countPhraseOccurences(pair);
if (howMany > 0)
System.out.println(pair[0].a + ": " + howMany + (howMany == 1? " time" : " times"));
}
System.out.println(finder.countPhraseOccurences(pairs) + " in total.");
}
public WordFinder() throws IOException
{
index = new InvertedIndex();
}
/*
* Gibt eine Liste aller PMIDs von Dokumenten zurück, welche
* alle Tokens enthalten. Diese sind als PointerPair[]
* kodiert, wobei
*/
private List<PointerPair[]> querySearch(String[] tokens) throws IOException
{
List<PointerPair>[] fromIndex = (List<PointerPair>[])
java.lang.reflect.Array.newInstance(List.class, new int[] {tokens.length});
List<PointerPair[]> rv = new LinkedList<PointerPair[]>();
/*
* positions[i] zeigt in fromIndex[i] und gibt an, das wievielte Element
* von fromIndex[i] wir betrachten
*/
int[] positions = new int[tokens.length];
int maxPMID = -1; // PMIDs sind immer positiv
for (int i = 0; i < tokens.length; i++)
{
positions[i] = 0;
fromIndex[i] = index.infoForToken(tokens[i]);
}
do
{
for (int i = 0; i < tokens.length; i++)
{
int otherPMID = fromIndex[i].get(positions[i]).a;
if (otherPMID > maxPMID)
{
maxPMID = otherPMID;
i = 0;
}
else if (otherPMID < maxPMID)
{
while (otherPMID < maxPMID && positions[i] != fromIndex[i].size()-1)
{
positions[i]++;
otherPMID = fromIndex[i].get(positions[i]).a;
}
if (otherPMID < maxPMID)
{
for (int j = 0; j < tokens.length; j++)
if (positions[j] != fromIndex[j].size()-1)
{
positions[j]++;
break;
}
}
}
}
boolean shouldReturn = true;
for (int i = 0; i < tokens.length && shouldReturn; i++)
shouldReturn = maxPMID == fromIndex[i].get(positions[i]).a;
if (shouldReturn)
{
PointerPair[] pp = new PointerPair[tokens.length];
for (int i = 0; i < tokens.length; i++)
pp[i] = fromIndex[i].get(positions[i]);
rv.add(pp);
maxPMID++;
}
boolean finished = false;
for (int i = 0; i < tokens.length && !finished; i++)
finished = positions[i] == fromIndex[i].size()-1;
if (finished)
return rv;
}
while (true);
}
/*
* pairs sollte nur PointerPairs enthalten, welche zu demselben Dokument
* (selbe PMID, selbes pairs[i].a) gehören; diese Funktion zählt, wie oft
* diese Vorkommen direkt aufeinander folgen, d. h., ob sie eine Phrase
* bilden (gemäß der Ordnung, in welcher die Wörter in pairs vorkommen)
*/
private int countPhraseOccurences(PointerPair[] pairs) throws IOException
{
List<Integer>[] tokenids = (List<Integer>[])
java.lang.reflect.Array.newInstance(List.class, new int[] {pairs.length});
int[] positions = new int[pairs.length];
int rv = 0;
for (int i = 0; i < pairs.length; i++)
{
positions[i] = 0;
tokenids[i] = index.tokenidsFromPosition(pairs[i].b);
}
int maxPos = -1;
do
{
for (int i = 0; i < pairs.length; i++)
{
int otherPos = tokenids[i].get(positions[i])-i;
if (otherPos > maxPos)
{
maxPos = otherPos;
i = 0;
}
else if (otherPos < maxPos)
{
while (otherPos < maxPos && positions[i] != tokenids[i].size()-1)
{
positions[i]++;
otherPos = tokenids[i].get(positions[i])-i;
}
if (otherPos < maxPos)
{
for (int j = 0; j < pairs.length; j++)
if (positions[j] != tokenids[j].size()-1)
{
positions[j]++;
break;
}
}
}
}
boolean shouldReturn = true;
for (int i = 0; i < pairs.length && shouldReturn; i++)
shouldReturn = maxPos+i == tokenids[i].get(positions[i]);
if (shouldReturn)
{
rv++;
maxPos++;
}
boolean finished = false;
for (int i = 0; i < pairs.length && !finished; i++)
finished = positions[i] == tokenids[i].size()-1;
if (finished)
return rv;
}
while (true);
}
private int countPhraseOccurences(List<PointerPair[]> list) throws IOException
{
int rv = 0;
for (PointerPair[] pairs : list)
rv += countPhraseOccurences(pairs);
return rv;
}
}
/*
* Dokument 142570 ist problematisch; allogeneic und lymphocytes in phrase
* query
*/