MtasTokenCollection.java.html
23 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
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
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>MtasTokenCollection.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">MTAS</a> > <a href="index.source.html" class="el_package">mtas.analysis.token</a> > <span class="el_source">MtasTokenCollection.java</span></div><h1>MtasTokenCollection.java</h1><pre class="source lang-java linenums">package mtas.analysis.token;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.lucene.analysis.payloads.PayloadHelper;
import org.apache.lucene.util.BytesRef;
import mtas.analysis.util.MtasParserException;
/**
* The Class MtasTokenCollection.
*/
public class MtasTokenCollection {
/** The token collection. */
<span class="fc" id="L23"> private HashMap<Integer, MtasToken> tokenCollection = new HashMap<>();</span>
/** The token collection index. */
<span class="fc" id="L26"> private ArrayList<Integer> tokenCollectionIndex = new ArrayList<>();</span>
/**
* Instantiates a new mtas token collection.
*/
<span class="fc" id="L31"> public MtasTokenCollection() {</span>
<span class="fc" id="L32"> clear();</span>
<span class="fc" id="L33"> }</span>
/**
* Adds the.
*
* @param token the token
* @return the integer
*/
public Integer add(MtasToken token) {
<span class="fc" id="L42"> Integer id = token.getId();</span>
<span class="fc" id="L43"> tokenCollection.put(id, token);</span>
<span class="fc" id="L44"> return id;</span>
}
/**
* Gets the.
*
* @param id the id
* @return the mtas token
*/
public MtasToken get(Integer id) {
<span class="fc" id="L54"> return tokenCollection.get(id);</span>
}
/**
* Iterator.
*
* @return the iterator
* @throws MtasParserException the mtas parser exception
*/
public Iterator<MtasToken> iterator() throws MtasParserException {
<span class="fc" id="L64"> checkTokenCollectionIndex();</span>
<span class="fc" id="L65"> return new Iterator<MtasToken>() {</span>
<span class="fc" id="L67"> private Iterator<Integer> indexIterator = tokenCollectionIndex.iterator();</span>
@Override
public boolean hasNext() {
<span class="fc" id="L71"> return indexIterator.hasNext();</span>
}
@Override
public MtasToken next() {
<span class="fc" id="L76"> return tokenCollection.get(indexIterator.next());</span>
}
@Override
public void remove() {
<span class="nc" id="L81"> throw new UnsupportedOperationException();</span>
}
};
}
/**
* Prints the.
*
* @throws MtasParserException the mtas parser exception
*/
public void print() throws MtasParserException {
<span class="nc" id="L92"> Iterator<MtasToken> it = this.iterator();</span>
<span class="nc bnc" id="L93" title="All 2 branches missed."> while (it.hasNext()) {</span>
<span class="nc" id="L94"> MtasToken token = it.next();</span>
<span class="nc" id="L95"> System.out.println(token);</span>
<span class="nc" id="L96"> }</span>
<span class="nc" id="L97"> }</span>
/**
* Gets the list.
*
* @return the list
* @throws MtasParserException the mtas parser exception
*/
public String[][] getList() throws MtasParserException {
<span class="nc" id="L106"> String[][] result = new String[(tokenCollection.size() + 1)][];</span>
<span class="nc" id="L107"> result[0] = new String[] { "id", "start real offset", "end real offset",</span>
"provide real offset", "start offset", "end offset", "provide offset",
"start position", "end position", "multiple positions", "parent",
"provide parent", "payload", "prefix", "postfix" };
<span class="nc" id="L111"> int number = 1;</span>
<span class="nc" id="L112"> Iterator<MtasToken> it = this.iterator();</span>
<span class="nc bnc" id="L113" title="All 2 branches missed."> while (it.hasNext()) {</span>
<span class="nc" id="L114"> MtasToken token = it.next();</span>
<span class="nc" id="L115"> String[] row = new String[15];</span>
<span class="nc" id="L116"> row[0] = token.getId().toString();</span>
<span class="nc bnc" id="L117" title="All 2 branches missed."> if (token.getRealOffsetStart() != null) {</span>
<span class="nc" id="L118"> row[1] = token.getRealOffsetStart().toString();</span>
<span class="nc" id="L119"> row[2] = token.getRealOffsetEnd().toString();</span>
<span class="nc bnc" id="L120" title="All 2 branches missed."> row[3] = token.getProvideRealOffset() ? "1" : null;</span>
}
<span class="nc bnc" id="L122" title="All 2 branches missed."> if (token.getOffsetStart() != null) {</span>
<span class="nc" id="L123"> row[4] = token.getOffsetStart().toString();</span>
<span class="nc" id="L124"> row[5] = token.getOffsetEnd().toString();</span>
<span class="nc bnc" id="L125" title="All 2 branches missed."> row[6] = token.getProvideOffset() ? "1" : null;</span>
}
<span class="nc bnc" id="L127" title="All 2 branches missed."> if (token.getPositionLength() != null) {</span>
<span class="nc bnc" id="L128" title="All 2 branches missed."> if (token.getPositionStart().equals(token.getPositionEnd())) {</span>
<span class="nc" id="L129"> row[7] = token.getPositionStart().toString();</span>
<span class="nc" id="L130"> row[8] = token.getPositionEnd().toString();</span>
<span class="nc" id="L131"> row[9] = null;</span>
<span class="nc bnc" id="L132" title="All 2 branches missed."> } else if ((token.getPositions() == null)</span>
<span class="nc" id="L133"> || (token.getPositions().length == (1 + token.getPositionEnd()</span>
<span class="nc bnc" id="L134" title="All 2 branches missed."> - token.getPositionStart()))) {</span>
<span class="nc" id="L135"> row[7] = token.getPositionStart().toString();</span>
<span class="nc" id="L136"> row[8] = token.getPositionEnd().toString();</span>
<span class="nc" id="L137"> row[9] = null;</span>
} else {
<span class="nc" id="L139"> row[7] = null;</span>
<span class="nc" id="L140"> row[8] = null;</span>
<span class="nc" id="L141"> row[9] = Arrays.toString(token.getPositions());</span>
}
}
<span class="nc bnc" id="L144" title="All 2 branches missed."> if (token.getParentId() != null) {</span>
<span class="nc" id="L145"> row[10] = token.getParentId().toString();</span>
<span class="nc bnc" id="L146" title="All 2 branches missed."> row[11] = token.getProvideParentId() ? "1" : null;</span>
}
<span class="nc bnc" id="L148" title="All 2 branches missed."> if (token.getPayload() != null) {</span>
<span class="nc" id="L149"> BytesRef payload = token.getPayload();</span>
<span class="nc" id="L150"> row[12] = Float.toString(PayloadHelper.decodeFloat(Arrays.copyOfRange(</span>
payload.bytes, payload.offset, (payload.offset + payload.length))));
}
<span class="nc" id="L153"> row[13] = token.getPrefix();</span>
<span class="nc" id="L154"> row[14] = token.getPostfix();</span>
<span class="nc" id="L155"> result[number] = row;</span>
<span class="nc" id="L156"> number++;</span>
<span class="nc" id="L157"> }</span>
<span class="nc" id="L158"> return result;</span>
}
/**
* Check.
*
* @param autoRepair the auto repair
* @param makeUnique the make unique
* @throws MtasParserException the mtas parser exception
*/
public void check(Boolean autoRepair, Boolean makeUnique)
throws MtasParserException {
<span class="pc bpc" id="L170" title="1 of 2 branches missed."> if (autoRepair) {</span>
<span class="fc" id="L171"> autoRepair();</span>
}
<span class="pc bpc" id="L173" title="1 of 2 branches missed."> if (makeUnique) {</span>
<span class="fc" id="L174"> makeUnique();</span>
}
<span class="fc" id="L176"> checkTokenCollectionIndex();</span>
<span class="fc bfc" id="L177" title="All 2 branches covered."> for (Integer i : tokenCollectionIndex) {</span>
// minimal properties
<span class="pc bpc" id="L179" title="1 of 2 branches missed."> if (tokenCollection.get(i).getId() == null</span>
<span class="pc bpc" id="L180" title="1 of 2 branches missed."> || tokenCollection.get(i).getPositionStart() == null</span>
<span class="pc bpc" id="L181" title="1 of 2 branches missed."> || tokenCollection.get(i).getPositionEnd() == null</span>
<span class="pc bpc" id="L182" title="1 of 2 branches missed."> || tokenCollection.get(i).getValue() == null) {</span>
<span class="nc" id="L183"> clear();</span>
<span class="nc" id="L184"> break;</span>
}
<span class="fc" id="L186"> }</span>
<span class="fc" id="L187"> }</span>
/**
* Make unique.
*/
private void makeUnique() {
<span class="fc" id="L193"> HashMap<String, ArrayList<MtasToken>> currentPositionTokens = new HashMap<>();</span>
ArrayList<MtasToken> currentValueTokens;
<span class="fc" id="L195"> int currentStartPosition = -1;</span>
<span class="fc" id="L196"> MtasToken currentToken = null;</span>
<span class="fc bfc" id="L197" title="All 2 branches covered."> for (Entry<Integer, MtasToken> entry : tokenCollection.entrySet()) {</span>
<span class="fc" id="L198"> currentToken = entry.getValue();</span>
<span class="fc bfc" id="L199" title="All 2 branches covered."> if (currentToken.getPositionStart() > currentStartPosition) {</span>
<span class="fc" id="L200"> currentPositionTokens.clear();</span>
<span class="fc" id="L201"> currentStartPosition = currentToken.getPositionStart();</span>
} else {
<span class="fc bfc" id="L203" title="All 2 branches covered."> if (currentPositionTokens.containsKey(currentToken.getValue())) {</span>
<span class="fc" id="L204"> currentValueTokens = currentPositionTokens</span>
<span class="fc" id="L205"> .get(currentToken.getValue());</span>
} else {
<span class="fc" id="L208"> currentValueTokens = new ArrayList<>();</span>
<span class="fc" id="L209"> currentPositionTokens.put(currentToken.getValue(),</span>
currentValueTokens);
}
<span class="fc" id="L212"> currentValueTokens.add(currentToken);</span>
}
<span class="fc" id="L214"> }</span>
<span class="fc" id="L215"> }</span>
/**
* Auto repair.
*/
private void autoRepair() {
<span class="fc" id="L221"> ArrayList<Integer> trash = new ArrayList<>();</span>
<span class="fc" id="L222"> HashMap<Integer, Integer> translation = new HashMap<>();</span>
<span class="fc" id="L223"> HashMap<Integer, MtasToken> newTokenCollection = new HashMap<>();</span>
Integer parentId;
<span class="fc" id="L225"> Integer maxId = null;</span>
<span class="fc" id="L226"> Integer minId = null;</span>
MtasToken token;
// check id, position and value
<span class="fc bfc" id="L229" title="All 2 branches covered."> for (Entry<Integer, MtasToken> entry : tokenCollection.entrySet()) {</span>
<span class="fc" id="L230"> token = entry.getValue();</span>
boolean putInTrash;
<span class="pc bpc" id="L232" title="1 of 2 branches missed."> putInTrash = token.getId() == null;</span>
<span class="pc bpc" id="L233" title="1 of 2 branches missed."> putInTrash |= (token.getPositionStart() == null)</span>
<span class="pc bpc" id="L234" title="1 of 2 branches missed."> || (token.getPositionEnd() == null);</span>
<span class="pc bpc" id="L235" title="2 of 4 branches missed."> putInTrash |= token.getValue() == null || (token.getValue().isEmpty());</span>
<span class="pc bpc" id="L236" title="2 of 4 branches missed."> putInTrash |= token.getPrefix() == null || (token.getPrefix().isEmpty());</span>
<span class="pc bpc" id="L237" title="1 of 2 branches missed."> if (putInTrash) {</span>
<span class="nc" id="L238"> trash.add(entry.getKey());</span>
}
<span class="fc" id="L240"> }</span>
// check parentId
<span class="fc bfc" id="L242" title="All 2 branches covered."> for (Entry<Integer, MtasToken> entry : tokenCollection.entrySet()) {</span>
<span class="fc" id="L243"> token = entry.getValue();</span>
<span class="fc" id="L244"> parentId = token.getParentId();</span>
<span class="pc bpc" id="L245" title="1 of 4 branches missed."> if (parentId != null && (!tokenCollection.containsKey(parentId)</span>
<span class="pc bpc" id="L246" title="1 of 2 branches missed."> || trash.contains(parentId))) {</span>
<span class="nc" id="L247"> token.setParentId(null);</span>
}
<span class="fc" id="L249"> }</span>
// empty bin
<span class="pc bpc" id="L251" title="1 of 2 branches missed."> if (!trash.isEmpty()) {</span>
<span class="nc bnc" id="L252" title="All 2 branches missed."> for (Integer i : trash) {</span>
<span class="nc" id="L253"> tokenCollection.remove(i);</span>
<span class="nc" id="L254"> }</span>
}
// always check ids
<span class="pc bpc" id="L257" title="1 of 2 branches missed."> if (tokenCollection.size() > 0) {</span>
<span class="fc bfc" id="L258" title="All 2 branches covered."> for (Integer i : tokenCollection.keySet()) {</span>
<span class="fc bfc" id="L259" title="All 2 branches covered."> maxId = ((maxId == null) ? i : Math.max(maxId, i));</span>
<span class="fc bfc" id="L260" title="All 2 branches covered."> minId = ((minId == null) ? i : Math.min(minId, i));</span>
<span class="fc" id="L261"> }</span>
// check
<span class="pc bpc" id="L263" title="2 of 4 branches missed."> if ((minId > 0) || ((1 + maxId - minId) != tokenCollection.size())) {</span>
<span class="nc" id="L264"> int newId = 0;</span>
// create translation
<span class="nc bnc" id="L266" title="All 2 branches missed."> for (Integer i : tokenCollection.keySet()) {</span>
<span class="nc" id="L267"> translation.put(i, newId);</span>
<span class="nc" id="L268"> newId++;</span>
<span class="nc" id="L269"> }</span>
// translate objects
<span class="nc bnc" id="L271" title="All 2 branches missed."> for (Entry<Integer, MtasToken> entry : tokenCollection.entrySet()) {</span>
<span class="nc" id="L272"> token = entry.getValue();</span>
<span class="nc" id="L273"> parentId = token.getParentId();</span>
<span class="nc" id="L274"> token.setId(translation.get(entry.getKey()));</span>
<span class="nc bnc" id="L275" title="All 2 branches missed."> if (parentId != null) {</span>
<span class="nc" id="L276"> token.setParentId(translation.get(parentId));</span>
}
<span class="nc" id="L278"> }</span>
// new tokenCollection
<span class="nc" id="L280"> Iterator<Map.Entry<Integer, MtasToken>> iter = tokenCollection</span>
<span class="nc" id="L281"> .entrySet().iterator();</span>
<span class="nc bnc" id="L282" title="All 2 branches missed."> while (iter.hasNext()) {</span>
<span class="nc" id="L283"> Map.Entry<Integer, MtasToken> entry = iter.next();</span>
<span class="nc" id="L284"> newTokenCollection.put(translation.get(entry.getKey()),</span>
<span class="nc" id="L285"> entry.getValue());</span>
<span class="nc" id="L286"> iter.remove();</span>
<span class="nc" id="L287"> }</span>
<span class="nc" id="L288"> tokenCollection = newTokenCollection;</span>
}
}
<span class="fc" id="L291"> }</span>
/**
* Check token collection index.
*
* @throws MtasParserException the mtas parser exception
*/
private void checkTokenCollectionIndex() throws MtasParserException {
<span class="fc bfc" id="L299" title="All 2 branches covered."> if (tokenCollectionIndex.size() != tokenCollection.size()) {</span>
MtasToken token;
<span class="fc" id="L301"> Integer maxId = null;</span>
<span class="fc" id="L302"> Integer minId = null;</span>
<span class="fc" id="L303"> tokenCollectionIndex.clear();</span>
<span class="fc bfc" id="L304" title="All 2 branches covered."> for (Entry<Integer, MtasToken> entry : tokenCollection.entrySet()) {</span>
<span class="fc" id="L305"> token = entry.getValue();</span>
<span class="fc bfc" id="L306" title="All 2 branches covered."> maxId = ((maxId == null) ? entry.getKey()</span>
<span class="fc" id="L307"> : Math.max(maxId, entry.getKey()));</span>
<span class="fc bfc" id="L308" title="All 2 branches covered."> minId = ((minId == null) ? entry.getKey()</span>
<span class="fc" id="L309"> : Math.min(minId, entry.getKey()));</span>
<span class="pc bpc" id="L310" title="1 of 2 branches missed."> if (token.getId() == null) {</span>
<span class="nc" id="L311"> throw new MtasParserException(</span>
<span class="nc" id="L312"> "no id for token (" + token.getValue() + ")");</span>
<span class="pc bpc" id="L313" title="1 of 2 branches missed."> } else if ((token.getPositionStart() == null)</span>
<span class="pc bpc" id="L314" title="1 of 2 branches missed."> || (token.getPositionEnd() == null)) {</span>
<span class="nc" id="L315"> throw new MtasParserException("no position for token with id "</span>
<span class="nc" id="L316"> + token.getId() + " (" + token.getValue() + ")");</span>
<span class="pc bpc" id="L317" title="2 of 4 branches missed."> } else if (token.getValue() == null || (token.getValue().equals(""))) {</span>
<span class="nc" id="L318"> throw new MtasParserException(</span>
<span class="nc" id="L319"> "no value for token with id " + token.getId());</span>
<span class="pc bpc" id="L320" title="1 of 2 branches missed."> } else if (token.getPrefix() == null</span>
<span class="pc bpc" id="L321" title="1 of 2 branches missed."> || (token.getPrefix().equals(""))) {</span>
<span class="nc" id="L322"> throw new MtasParserException(</span>
<span class="nc" id="L323"> "no prefix for token with id " + token.getId());</span>
<span class="fc bfc" id="L324" title="All 2 branches covered."> } else if ((token.getParentId() != null)</span>
<span class="pc bpc" id="L325" title="1 of 2 branches missed."> && !tokenCollection.containsKey(token.getParentId())) {</span>
<span class="nc" id="L326"> throw new MtasParserException(</span>
<span class="nc" id="L327"> "missing parentId for token with id " + token.getId());</span>
<span class="pc bpc" id="L328" title="1 of 2 branches missed."> } else if ((token.getOffsetStart() == null)</span>
<span class="pc bpc" id="L329" title="1 of 2 branches missed."> || (token.getOffsetEnd() == null)) {</span>
<span class="nc" id="L330"> throw new MtasParserException("missing offset for token with id "</span>
<span class="nc" id="L331"> + token.getId() + " (" + token.getValue() + ")");</span>
}
<span class="fc" id="L333"> tokenCollectionIndex.add(entry.getKey());</span>
<span class="fc" id="L334"> }</span>
<span class="pc bpc" id="L335" title="1 of 2 branches missed."> if ((tokenCollection.size() > 0)</span>
<span class="pc bpc" id="L336" title="2 of 4 branches missed."> && ((minId > 0) || ((1 + maxId - minId) != tokenCollection.size()))) {</span>
<span class="nc" id="L337"> throw new MtasParserException("missing ids");</span>
}
<span class="fc" id="L339"> Collections.sort(tokenCollectionIndex, getCompByName());</span>
}
<span class="fc" id="L341"> }</span>
/**
* Gets the comp by name.
*
* @return the comp by name
*/
public Comparator<Integer> getCompByName() {
<span class="pc bpc" id="L349" title="1 of 2 branches missed."> return new Comparator<Integer>() {</span>
@Override
public int compare(Integer t1, Integer t2) {
<span class="fc" id="L352"> Integer p1 = tokenCollection.get(t1).getPositionStart();</span>
<span class="fc" id="L353"> Integer p2 = tokenCollection.get(t2).getPositionStart();</span>
<span class="pc bpc" id="L354" title="2 of 4 branches missed."> assert p1 != null : "no position for " + tokenCollection.get(t1);</span>
<span class="pc bpc" id="L355" title="2 of 4 branches missed."> assert p2 != null : "no position for " + tokenCollection.get(t2);</span>
<span class="fc bfc" id="L356" title="All 2 branches covered."> if (p1.equals(p2)) {</span>
<span class="fc" id="L357"> Integer o1 = tokenCollection.get(t1).getOffsetStart();</span>
<span class="fc" id="L358"> Integer o2 = tokenCollection.get(t2).getOffsetStart();</span>
<span class="pc bpc" id="L359" title="2 of 4 branches missed."> if (o1 != null && o2 != null) {</span>
<span class="pc bpc" id="L360" title="1 of 2 branches missed."> if (o1.equals(o2)) {</span>
<span class="fc" id="L361"> return tokenCollection.get(t1).getValue()</span>
<span class="fc" id="L362"> .compareTo(tokenCollection.get(t2).getValue());</span>
} else {
<span class="nc" id="L364"> return o1.compareTo(o2);</span>
}
} else {
<span class="nc" id="L367"> return tokenCollection.get(t1).getValue()</span>
<span class="nc" id="L368"> .compareTo(tokenCollection.get(t2).getValue());</span>
}
}
<span class="fc" id="L371"> return p1.compareTo(p2);</span>
}
};
}
/**
* Clear.
*/
private void clear() {
<span class="fc" id="L380"> tokenCollectionIndex.clear();</span>
<span class="fc" id="L381"> tokenCollection.clear();</span>
<span class="fc" id="L382"> }</span>
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.7.9.201702052155</span></div></body></html>