|
1 |
// Copyright © 2010-2013 ENS de Lyon.
|
|
2 |
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
|
|
3 |
// Lyon 2, University of Franche-Comté, University of Nice
|
|
4 |
// Sophia Antipolis, University of Paris 3.
|
|
5 |
//
|
|
6 |
// The TXM platform is free software: you can redistribute it
|
|
7 |
// and/or modify it under the terms of the GNU General Public
|
|
8 |
// License as published by the Free Software Foundation,
|
|
9 |
// either version 2 of the License, or (at your option) any
|
|
10 |
// later version.
|
|
11 |
//
|
|
12 |
// The TXM platform is distributed in the hope that it will be
|
|
13 |
// useful, but WITHOUT ANY WARRANTY; without even the implied
|
|
14 |
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
15 |
// PURPOSE. See the GNU General Public License for more
|
|
16 |
// details.
|
|
17 |
//
|
|
18 |
// You should have received a copy of the GNU General
|
|
19 |
// Public License along with the TXM platform. If not, see
|
|
20 |
// http://www.gnu.org/licenses.
|
|
21 |
//
|
|
22 |
//
|
|
23 |
//
|
|
24 |
// $LastChangedDate: 2016-09-19 10:31:00 +0200 (Mon, 19 Sep 2016) $
|
|
25 |
// $LastChangedRevision: 3298 $
|
|
26 |
// $LastChangedBy: mdecorde $
|
|
27 |
//
|
|
28 |
package org.txm.index.core.functions;
|
|
29 |
|
|
30 |
import java.io.File;
|
|
31 |
import java.io.FileNotFoundException;
|
|
32 |
import java.io.FileOutputStream;
|
|
33 |
import java.io.IOException;
|
|
34 |
import java.io.OutputStreamWriter;
|
|
35 |
import java.io.UnsupportedEncodingException;
|
|
36 |
import java.util.ArrayList;
|
|
37 |
import java.util.Arrays;
|
|
38 |
import java.util.Map;
|
|
39 |
|
|
40 |
import org.eclipse.core.runtime.IProgressMonitor;
|
|
41 |
import org.txm.core.messages.TXMCoreMessages;
|
|
42 |
import org.txm.core.preferences.TXMPreferences;
|
|
43 |
import org.txm.core.results.Parameter;
|
|
44 |
import org.txm.core.results.TXMParameters;
|
|
45 |
import org.txm.core.results.TXMResult;
|
|
46 |
import org.txm.index.core.messages.IndexCoreMessages;
|
|
47 |
import org.txm.searchengine.cqp.ICqiClient;
|
|
48 |
import org.txm.searchengine.cqp.clientExceptions.CqiClientException;
|
|
49 |
import org.txm.searchengine.cqp.corpus.Corpus;
|
|
50 |
import org.txm.searchengine.cqp.corpus.CorpusManager;
|
|
51 |
import org.txm.searchengine.cqp.corpus.MainCorpus;
|
|
52 |
import org.txm.searchengine.cqp.corpus.Part;
|
|
53 |
import org.txm.searchengine.cqp.corpus.Property;
|
|
54 |
import org.txm.searchengine.cqp.corpus.Subcorpus;
|
|
55 |
import org.txm.searchengine.cqp.corpus.query.CQLQuery;
|
|
56 |
import org.txm.statsengine.core.StatException;
|
|
57 |
import org.txm.statsengine.core.data.Vector;
|
|
58 |
import org.txm.statsengine.r.core.data.VectorImpl;
|
|
59 |
import org.txm.utils.logger.Log;
|
|
60 |
|
|
61 |
// TODO should be put into stat.data package ?
|
|
62 |
/**
|
|
63 |
* Represent a frequency list according to a {@link Corpus} (or a.
|
|
64 |
*
|
|
65 |
* {@link Subcorpus}) and a {@link Property}.
|
|
66 |
*
|
|
67 |
* @author sjacquot
|
|
68 |
* @author sloiseau
|
|
69 |
*/
|
|
70 |
public class ___Lexicon2 extends Index {
|
|
71 |
|
|
72 |
/** The nolex. */
|
|
73 |
protected static int nolex = 1;
|
|
74 |
|
|
75 |
/** The prefix r. */
|
|
76 |
protected static String prefixR = "Lexicon_"; //$NON-NLS-1$
|
|
77 |
|
|
78 |
/** The forms. */
|
|
79 |
private String[] forms;
|
|
80 |
|
|
81 |
/** The freqs. */
|
|
82 |
private int[] freqs;
|
|
83 |
|
|
84 |
/** The ids. */
|
|
85 |
private int[] ids;
|
|
86 |
|
|
87 |
/** The number of tokens. */
|
|
88 |
int numberOfTokens = -1;
|
|
89 |
|
|
90 |
/** The symbol. */
|
|
91 |
private String symbol;
|
|
92 |
|
|
93 |
/** The writer. */
|
|
94 |
private OutputStreamWriter writer;
|
|
95 |
|
|
96 |
/**
|
|
97 |
* The property.
|
|
98 |
*/
|
|
99 |
@Parameter(key=TXMPreferences.UNIT_PROPERTY)
|
|
100 |
protected Property pProperty;
|
|
101 |
|
|
102 |
|
|
103 |
/**
|
|
104 |
* Creates a not computed lexicon from the specified corpus.
|
|
105 |
* @param parent
|
|
106 |
*/
|
|
107 |
public ___Lexicon2(TXMResult parent) {
|
|
108 |
super(parent);
|
|
109 |
}
|
|
110 |
|
|
111 |
/**
|
|
112 |
* Creates a not computed lexicon from the specified uuid node name.
|
|
113 |
* @param uuid
|
|
114 |
*/
|
|
115 |
public ___Lexicon2(String uuid) {
|
|
116 |
super(uuid);
|
|
117 |
}
|
|
118 |
|
|
119 |
/**
|
|
120 |
*
|
|
121 |
* @param uuid
|
|
122 |
* @param parent
|
|
123 |
*/
|
|
124 |
public ___Lexicon2(String uuid, TXMResult parent) {
|
|
125 |
super(uuid, parent);
|
|
126 |
}
|
|
127 |
|
|
128 |
|
|
129 |
@Override
|
|
130 |
public Corpus getParent() {
|
|
131 |
return (Corpus)super.getParent();
|
|
132 |
}
|
|
133 |
|
|
134 |
@Override
|
|
135 |
public boolean loadParameters() {
|
|
136 |
super.loadParameters();
|
|
137 |
try {
|
|
138 |
//this.persistable = false; // FIXME: remove that later
|
|
139 |
String p = this.getStringParameterValue(TXMPreferences.UNIT_PROPERTY);
|
|
140 |
if (p != null && p.length() > 0) {
|
|
141 |
Corpus parent = getParent();
|
|
142 |
if (parent != null) {
|
|
143 |
this.pProperty = parent.getProperty(p);
|
|
144 |
}
|
|
145 |
else {
|
|
146 |
return false;
|
|
147 |
}
|
|
148 |
}
|
|
149 |
}
|
|
150 |
catch (CqiClientException e) {
|
|
151 |
// TODO Auto-generated catch block
|
|
152 |
e.printStackTrace();
|
|
153 |
return false;
|
|
154 |
}
|
|
155 |
return true;
|
|
156 |
}
|
|
157 |
|
|
158 |
@Override
|
|
159 |
public boolean saveParameters() {
|
|
160 |
super.saveParameters();
|
|
161 |
// FIXME: became useless? check that it's well automated by autoSaveParametersFromAnnotations()?
|
|
162 |
//this.saveParameter(TXMPreferences.UNIT_PROPERTY, this.pProperty);
|
|
163 |
return true;
|
|
164 |
}
|
|
165 |
|
|
166 |
|
|
167 |
@Override
|
|
168 |
public void clean() {
|
|
169 |
super.clean();
|
|
170 |
// TODO Auto-generated method stub
|
|
171 |
}
|
|
172 |
|
|
173 |
@Override
|
|
174 |
public boolean canCompute() {
|
|
175 |
return super.canCompute() && (pProperty != null);
|
|
176 |
}
|
|
177 |
|
|
178 |
@Override
|
|
179 |
protected boolean _compute() throws Exception {
|
|
180 |
if (this.getParent() instanceof MainCorpus) {
|
|
181 |
return computeWithMainCorpus((MainCorpus)this.getParent(), pProperty, monitor);
|
|
182 |
}
|
|
183 |
else if (this.getParent() instanceof Subcorpus) {
|
|
184 |
return computewithSubCorpus((Subcorpus)this.getParent(), pProperty, monitor);
|
|
185 |
}
|
|
186 |
else {
|
|
187 |
Log.severe("Error: Lexicon parent is neither a Maincorpus nor a Subcorpus."); //$NON-NLS-1$
|
|
188 |
return false;
|
|
189 |
}
|
|
190 |
}
|
|
191 |
|
|
192 |
/**
|
|
193 |
* Gets the lexicon relative to a given property.
|
|
194 |
*
|
|
195 |
* @param property
|
|
196 |
* the property
|
|
197 |
*
|
|
198 |
* @return the lexicon
|
|
199 |
*
|
|
200 |
* @throws CqiClientException
|
|
201 |
* the cqi client exception
|
|
202 |
*/
|
|
203 |
protected boolean computeWithMainCorpus(MainCorpus corpus, Property property, IProgressMonitor monitor) throws CqiClientException {
|
|
204 |
// System.out.println("in "+this.getCqpId()+" look for cached lexicon "+property);
|
|
205 |
// System.out.println("not found");
|
|
206 |
this.subTask("Computing lexicon size...");
|
|
207 |
Log.finest(IndexCoreMessages.LEXICON_RESULT_TYPE + corpus.getName());
|
|
208 |
int lexiconSize;
|
|
209 |
try {
|
|
210 |
lexiconSize = CorpusManager.getCorpusManager().getCqiClient().lexiconSize(property.getQualifiedName());
|
|
211 |
} catch (Exception e) {
|
|
212 |
throw new CqiClientException(e);
|
|
213 |
}
|
|
214 |
|
|
215 |
int[] ids = new int[lexiconSize];
|
|
216 |
for (int i = 0; i < ids.length; i++) {
|
|
217 |
ids[i] = i;
|
|
218 |
}
|
|
219 |
|
|
220 |
int[] freqs;
|
|
221 |
try {
|
|
222 |
this.subTask("Computing lexicon frequencies...");
|
|
223 |
freqs = CorpusManager.getCorpusManager().getCqiClient().id2Freq(property.getQualifiedName(), ids);
|
|
224 |
} catch (Exception e) {
|
|
225 |
throw new CqiClientException(e);
|
|
226 |
}
|
|
227 |
|
|
228 |
this.init(corpus, property, freqs, ids);
|
|
229 |
return true;
|
|
230 |
}
|
|
231 |
|
|
232 |
/**
|
|
233 |
*
|
|
234 |
* @param corpus
|
|
235 |
* @param property
|
|
236 |
* @param monitor
|
|
237 |
* @return
|
|
238 |
* @throws CqiClientException
|
|
239 |
*/
|
|
240 |
// FIXME: why this method needs to create and delete some new subcorpus???? the computing can't be done directly on the corpus argument???
|
|
241 |
// eg. dist = CorpusManager.getCorpusManager().getCqiClient().fdist1(corpus.getQualifiedCqpId(), 0, ICqiClient.CQI_CONST_FIELD_MATCH, property.getName());
|
|
242 |
protected boolean computewithSubCorpus(Subcorpus corpus, Property property, IProgressMonitor monitor) throws CqiClientException {
|
|
243 |
|
|
244 |
if (corpus instanceof Part) {
|
|
245 |
this.persistable = false;
|
|
246 |
}
|
|
247 |
|
|
248 |
//System.out.println("not found");
|
|
249 |
Log.finest(TXMCoreMessages.SUBCORPUS_LEXICON + corpus.getName());
|
|
250 |
//long start = System.currentTimeMillis();
|
|
251 |
int[][] fdist = null;
|
|
252 |
Subcorpus tmp = null;
|
|
253 |
try {
|
|
254 |
this.subTask("Computing lexicon frequencies...");
|
|
255 |
tmp = corpus.createSubcorpus(new CQLQuery("[]"), "S" + corpus.getNextSubcorpusCounter(), true); //$NON-NLS-1$
|
|
256 |
if (tmp != null) {
|
|
257 |
fdist = CorpusManager.getCorpusManager().getCqiClient().fdist1(tmp.getQualifiedCqpId(), 0, ICqiClient.CQI_CONST_FIELD_MATCH, property.getName());
|
|
258 |
|
|
259 |
corpus.dropSubcorpus(tmp);
|
|
260 |
tmp.delete();
|
|
261 |
}
|
|
262 |
//System.out.println("nb lines: "+fdist.length);
|
|
263 |
} catch (Exception e) {
|
|
264 |
throw new CqiClientException(e);
|
|
265 |
} finally {
|
|
266 |
if (tmp != null) {
|
|
267 |
try {
|
|
268 |
corpus.dropSubcorpus(tmp);
|
|
269 |
tmp.delete();
|
|
270 |
}
|
|
271 |
catch (Exception e2) {
|
|
272 |
}
|
|
273 |
}
|
|
274 |
}
|
|
275 |
int lexiconSize = fdist.length;
|
|
276 |
|
|
277 |
int[] freqs = new int[lexiconSize];
|
|
278 |
int[] ids = new int[lexiconSize];
|
|
279 |
for (int i = 0; i < fdist.length; i++) {
|
|
280 |
ids[i] = fdist[i][0];
|
|
281 |
freqs[i] = fdist[i][1];
|
|
282 |
}
|
|
283 |
|
|
284 |
init(corpus, property, freqs, ids);
|
|
285 |
return true;
|
|
286 |
}
|
|
287 |
|
|
288 |
/**
|
|
289 |
* Convert the Lexicon into a Vector object.
|
|
290 |
*
|
|
291 |
* @return the vector
|
|
292 |
* @throws StatException the stat exception
|
|
293 |
*/
|
|
294 |
public Vector asVector() throws StatException {
|
|
295 |
String symbol = prefixR + (nolex++);
|
|
296 |
VectorImpl v = new VectorImpl(freqs, symbol);
|
|
297 |
v.setRNames(getForms());
|
|
298 |
this.symbol = v.getSymbol();
|
|
299 |
return v;
|
|
300 |
}
|
|
301 |
|
|
302 |
/**
|
|
303 |
* Compute number of tokens. / this.nbr
|
|
304 |
*/
|
|
305 |
private void computeNumberOfTokens() {
|
|
306 |
numberOfTokens = 0;
|
|
307 |
for (int i = 0; i < freqs.length; i++) {
|
|
308 |
numberOfTokens += freqs[i];
|
|
309 |
}
|
|
310 |
}
|
|
311 |
|
|
312 |
|
|
313 |
|
|
314 |
@Override
|
|
315 |
public boolean delete() {
|
|
316 |
super.delete();
|
|
317 |
|
|
318 |
// FIXME: deprecated + anyway if it's still useful after the TXMObject nerf it should be in the clean() method
|
|
319 |
if (this.parent != null) {
|
|
320 |
this.getParent().removeData(this);
|
|
321 |
}
|
|
322 |
// end of fixme
|
|
323 |
|
|
324 |
return true;
|
|
325 |
}
|
|
326 |
|
|
327 |
/**
|
|
328 |
* Dump lexicon forms and frequencies in a String.
|
|
329 |
*
|
|
330 |
* @param col the col
|
|
331 |
* @param txt the txt
|
|
332 |
* @return the string
|
|
333 |
*/
|
|
334 |
public String dump(String col, String txt) {
|
|
335 |
StringBuffer buffer = new StringBuffer();
|
|
336 |
getForms();
|
|
337 |
for (int i = 0; i < forms.length; i++) {
|
|
338 |
buffer.append(txt + forms[i].replace(txt, txt + txt) + txt + col + freqs[i] + "\n"); //$NON-NLS-1$
|
|
339 |
}
|
|
340 |
return buffer.toString();
|
|
341 |
}
|
|
342 |
|
|
343 |
/* (non-Javadoc)
|
|
344 |
* @see java.lang.Object#equals(java.lang.Object)
|
|
345 |
*/
|
|
346 |
@Override
|
|
347 |
public boolean equals(Object obj) {
|
|
348 |
if (!(obj instanceof ___Lexicon2)) {
|
|
349 |
return false;
|
|
350 |
}
|
|
351 |
___Lexicon2 other = (___Lexicon2) obj;
|
|
352 |
|
|
353 |
if (other.nbrOfType() != this.nbrOfType()) {
|
|
354 |
return false;
|
|
355 |
}
|
|
356 |
return (Arrays.equals(freqs, other.getFreq()) && Arrays.equals(getForms(), other.getForms()));
|
|
357 |
}
|
|
358 |
|
|
359 |
|
|
360 |
|
|
361 |
public String getDetails() {
|
|
362 |
try {
|
|
363 |
return this.getParent().getName() + " " + this.pProperty.getName(); //$NON-NLS-1$
|
|
364 |
}
|
|
365 |
catch (Exception e) {
|
|
366 |
return this.getCurrentName();
|
|
367 |
}
|
|
368 |
}
|
|
369 |
|
|
370 |
//TODO: move this into a Lexicon chart renderer
|
|
371 |
// /**
|
|
372 |
// * Draw a pareto graphic with this frequency list and record it into the
|
|
373 |
// * provided filename into svg format.
|
|
374 |
// *
|
|
375 |
// * @param file where to save the pareto graphic.
|
|
376 |
// * @return the pareto graphic
|
|
377 |
// * @throws StatException if anything goes wrong.
|
|
378 |
// */
|
|
379 |
// public void getParetoGraphic(File file) throws StatException {
|
|
380 |
// String rName = asVector().getSymbol();
|
|
381 |
// String expr = "pareto(" + rName + ")"; //$NON-NLS-1$ //$NON-NLS-2$
|
|
382 |
// try {
|
|
383 |
// RWorkspace.getRWorkspaceInstance().plot(file, expr, RDevice.SVG);
|
|
384 |
// } catch (Exception e) {
|
|
385 |
// throw new StatException(e);
|
|
386 |
// }
|
|
387 |
// }
|
|
388 |
|
|
389 |
/**
|
|
390 |
* The dif ferent types in the lexicon, the type at the index <code>j</code>
|
|
391 |
* of this array have the frequency at index <code>j</code> in the array
|
|
392 |
* returned by {@link #getFreq()}.
|
|
393 |
*
|
|
394 |
* @return types as an array of <code>String</code>
|
|
395 |
*/
|
|
396 |
public String[] getForms() {
|
|
397 |
if (forms == null) {
|
|
398 |
if(ids == null) {
|
|
399 |
return new String[0];
|
|
400 |
}
|
|
401 |
try {
|
|
402 |
forms = CorpusManager.getCorpusManager().getCqiClient().id2Str(pProperty.getQualifiedName(), ids);
|
|
403 |
} catch (Exception e) {
|
|
404 |
// TODO Auto-generated catch block
|
|
405 |
org.txm.utils.logger.Log.printStackTrace(e);
|
|
406 |
}
|
|
407 |
}
|
|
408 |
return forms;
|
|
409 |
}
|
|
410 |
|
|
411 |
/**
|
|
412 |
* The different types in the lexicon, the type at the index <code>j</code>
|
|
413 |
* of this array have the frequency at index <code>j</code> in the array
|
|
414 |
* returned by {@link #getFreq()}.
|
|
415 |
*
|
|
416 |
* @param number the number
|
|
417 |
* @return types as an array of <code>String</code>
|
|
418 |
*/
|
|
419 |
public String[] getForms(int number) {
|
|
420 |
//System.out.println("Lexicon("+this.property+" get forms. number="+number+", ids len="+ids.length);
|
|
421 |
if (forms == null) {
|
|
422 |
try {
|
|
423 |
number = Math.min(number, ids.length);
|
|
424 |
if (number <= 0) {
|
|
425 |
return new String[0];
|
|
426 |
}
|
|
427 |
int[] subpositions = new int[number];
|
|
428 |
System.arraycopy(ids, 0, subpositions, 0, number);
|
|
429 |
return CorpusManager.getCorpusManager().getCqiClient().id2Str(pProperty.getQualifiedName(), subpositions);
|
|
430 |
} catch (Exception e) {
|
|
431 |
// TODO Auto-generated catch block
|
|
432 |
org.txm.utils.logger.Log.printStackTrace(e);
|
|
433 |
return null;
|
|
434 |
}
|
|
435 |
} else {
|
|
436 |
number = Math.min(number, ids.length);
|
|
437 |
if (number <= 0) {
|
|
438 |
return new String[0];
|
|
439 |
}
|
|
440 |
String[] subforms = new String[number];
|
|
441 |
System.arraycopy(ids, 0, subforms, 0, number);
|
|
442 |
return subforms;
|
|
443 |
}
|
|
444 |
}
|
|
445 |
|
|
446 |
/**
|
|
447 |
* The dif ferent frequencies in the lexicon. See {@link #getForms()}.
|
|
448 |
*
|
|
449 |
* @return frequencies as an array of <code>int</code>
|
|
450 |
*/
|
|
451 |
public int[] getFreq() {
|
|
452 |
return freqs;
|
|
453 |
}
|
|
454 |
|
|
455 |
/**
|
|
456 |
* return the ids of the entries.
|
|
457 |
*
|
|
458 |
* @return types as an array of <code>String</code>
|
|
459 |
*/
|
|
460 |
public int[] getIds() {
|
|
461 |
return ids;
|
|
462 |
}
|
|
463 |
|
|
464 |
@Override
|
|
465 |
public String getName() {
|
|
466 |
try {
|
|
467 |
return IndexCoreMessages.LEXICON_RESULT_TYPE + ": " + this.parent.getSimpleName() + ": " + this.getSimpleName();
|
|
468 |
}
|
|
469 |
catch(Exception e) {
|
|
470 |
}
|
|
471 |
return ""; //$NON-NLS-1$
|
|
472 |
}
|
|
473 |
|
|
474 |
/**
|
|
475 |
* The property this lexicon is build on.
|
|
476 |
*
|
|
477 |
* @return the property
|
|
478 |
*/
|
|
479 |
public Property getProperty() {
|
|
480 |
return pProperty;
|
|
481 |
}
|
|
482 |
|
|
483 |
@Override
|
|
484 |
public String getSimpleName() {
|
|
485 |
try {
|
|
486 |
return this.getProperty().getName();
|
|
487 |
}
|
|
488 |
catch(Exception e) {
|
|
489 |
return this.getEmptyName();
|
|
490 |
}
|
|
491 |
}
|
|
492 |
|
|
493 |
/**
|
|
494 |
* Gets the symbol.
|
|
495 |
*
|
|
496 |
* @return the symbol
|
|
497 |
*/
|
|
498 |
public String getSymbol() {
|
|
499 |
return this.symbol;
|
|
500 |
}
|
|
501 |
|
|
502 |
/**
|
|
503 |
* Hack frequencies using a map to set forms and frequencies
|
|
504 |
*
|
|
505 |
* @param corpus the corpus
|
|
506 |
* @param pProperty the property
|
|
507 |
* @param map the map
|
|
508 |
* {@link Corpus#getLexicon(Property)} or
|
|
509 |
* {@link Subcorpus#getLexicon(Property)}.
|
|
510 |
*/
|
|
511 |
public boolean hack(Map<String, Integer> map) {
|
|
512 |
if (map.size() != forms.length) {
|
|
513 |
return false;
|
|
514 |
}
|
|
515 |
|
|
516 |
//super(corpus);
|
|
517 |
int size = map.size();
|
|
518 |
int[] freqs = new int[size];
|
|
519 |
String[] forms = map.keySet().toArray(new String[] {});
|
|
520 |
for (int i = 0; i < forms.length; i++) {
|
|
521 |
freqs[i] = map.get(forms[i]);
|
|
522 |
}
|
|
523 |
|
|
524 |
this.freqs = freqs;
|
|
525 |
return true;
|
|
526 |
}
|
|
527 |
|
|
528 |
/**
|
|
529 |
* Protected on purpose: should be accessed through others initializer.
|
|
530 |
*
|
|
531 |
* @param corpus the corpus
|
|
532 |
* @param property the property
|
|
533 |
* @param freq the freq
|
|
534 |
* @param ids the ids
|
|
535 |
* {@link Corpus#getLexicon(Property)} or
|
|
536 |
* {@link Subcorpus#getLexicon(Property)}.
|
|
537 |
*/
|
|
538 |
protected void init(TXMResult corpus, Property property, int[] freq, int[] ids) {
|
|
539 |
if (freq.length != ids.length) {
|
|
540 |
throw new IllegalArgumentException(IndexCoreMessages.Lexicon_0);
|
|
541 |
}
|
|
542 |
this.freqs = freq;
|
|
543 |
this.ids = ids;
|
|
544 |
this.forms = null;
|
|
545 |
this.pProperty = property;
|
|
546 |
}
|
|
547 |
|
|
548 |
|
|
549 |
/**
|
|
550 |
* Number of tokens (sum of all the frequencies) in the corpus.
|
|
551 |
*
|
|
552 |
* @return the size of the corpus or subcorpus.
|
|
553 |
*/
|
|
554 |
public int nbrOfToken() {
|
|
555 |
if (numberOfTokens <= 0) {
|
|
556 |
computeNumberOfTokens();
|
|
557 |
}
|
|
558 |
return numberOfTokens;
|
|
559 |
}
|
|
560 |
|
|
561 |
|
|
562 |
/**
|
|
563 |
* Number of different types in the frequency list.
|
|
564 |
*
|
|
565 |
* @return number of types in the corpus or subcorpus.
|
|
566 |
*/
|
|
567 |
public int nbrOfType() {
|
|
568 |
try {
|
|
569 |
return freqs.length;
|
|
570 |
}
|
|
571 |
catch (Exception e) {
|
|
572 |
return 0;
|
|
573 |
}
|
|
574 |
}
|
|
575 |
|
|
576 |
public void setParameters(Property property) {
|
|
577 |
this.pProperty = property;
|
|
578 |
}
|
|
579 |
|
|
580 |
@Override
|
|
581 |
public boolean setParameters(TXMParameters parameters) {
|
|
582 |
try {
|
|
583 |
Property p = (Property) parameters.get("properties");
|
|
584 |
this.setParameters(p);
|
|
585 |
} catch (Exception e) {
|
|
586 |
Log.printStackTrace(e);
|
|
587 |
return false;
|
|
588 |
}
|
|
589 |
return true;
|
|
590 |
}
|
|
591 |
|
|
592 |
/**
|
|
593 |
* Sets the symbol.
|
|
594 |
*
|
|
595 |
* @param symbol the new symbol
|
|
596 |
*/
|
|
597 |
public void setSymbol(String symbol) {
|
|
598 |
this.symbol = symbol;
|
|
599 |
}
|
|
600 |
|
|
601 |
|
|
602 |
@Override
|
|
603 |
public String toString() {
|
|
604 |
return IndexCoreMessages.Lexicon_3 + this.getCurrentName();
|
|
605 |
}
|
|
606 |
|
|
607 |
/**
|
|
608 |
* To txt.
|
|
609 |
*
|
|
610 |
* @param outfile the outfile
|
|
611 |
* @param encoding the encoding
|
|
612 |
* @param colseparator the colseparator
|
|
613 |
* @param txtseparator the txtseparator
|
|
614 |
* @return true, if successful
|
|
615 |
*/
|
|
616 |
@Deprecated
|
|
617 |
public boolean toTxt(File outfile, String encoding, String colseparator, String txtseparator) {
|
|
618 |
// NK: writer declared as class attribute to perform a clean if the operation is interrupted
|
|
619 |
// OutputStreamWriter writer;
|
|
620 |
try {
|
|
621 |
this.writer = new OutputStreamWriter(new FileOutputStream(outfile),
|
|
622 |
encoding);
|
|
623 |
} catch (UnsupportedEncodingException e1) {
|
|
624 |
org.txm.utils.logger.Log.printStackTrace(e1);
|
|
625 |
return false;
|
|
626 |
} catch (FileNotFoundException e1) {
|
|
627 |
org.txm.utils.logger.Log.printStackTrace(e1);
|
|
628 |
return false;
|
|
629 |
}
|
|
630 |
|
|
631 |
try {
|
|
632 |
writer.write(this.dump(colseparator, txtseparator));
|
|
633 |
writer.close();
|
|
634 |
} catch (IOException e) {
|
|
635 |
org.txm.utils.logger.Log.printStackTrace(e);
|
|
636 |
return false;
|
|
637 |
}
|
|
638 |
|
|
639 |
return true;
|
|
640 |
}
|
|
641 |
|
|
642 |
/**
|
|
643 |
* Sets the unit property.
|
|
644 |
* @param property the unit property
|
|
645 |
*/
|
|
646 |
public void setProperty(Property property) {
|
|
647 |
this.pProperty = property;
|
|
648 |
}
|
|
649 |
|
|
650 |
|
|
651 |
/**
|
|
652 |
* Gets a Lexicon from the specified corpus.
|
|
653 |
* If a Lexicon child exists in the Corpus fro the specified property, returns it otherwise creates and computes a new Lexicon.
|
|
654 |
* @param corpus
|
|
655 |
* @param property
|
|
656 |
* @return
|
|
657 |
* @throws Exception
|
|
658 |
*/
|
|
659 |
public static ___Lexicon2 getLexicon(Corpus corpus, Property property, IProgressMonitor monitor) throws Exception {
|
|
660 |
___Lexicon2 lexicon = null;
|
|
661 |
|
|
662 |
// recycling parent Lexicon if exists
|
|
663 |
ArrayList<___Lexicon2> partLexicons = (ArrayList<___Lexicon2>)corpus.getChildren(___Lexicon2.class);
|
|
664 |
for (int i = 0; i < partLexicons.size(); i++) {
|
|
665 |
if(partLexicons.get(i).getProperty() == property) {
|
|
666 |
lexicon = partLexicons.get(i);
|
|
667 |
break;
|
|
668 |
}
|
|
669 |
}
|
|
670 |
|
|
671 |
// creating new Lexicon
|
|
672 |
if(lexicon == null || lexicon.getProperty() != property) {
|
|
673 |
lexicon = new ___Lexicon2(corpus);
|
|
674 |
lexicon.setProperty(property);
|
|
675 |
}
|
|
676 |
|
|
677 |
if(!lexicon.hasBeenComputedOnce()) {
|
|
678 |
lexicon.compute(monitor);
|
|
679 |
}
|
|
680 |
|
|
681 |
return lexicon;
|
|
682 |
}
|
|
683 |
|
|
684 |
|
|
685 |
// /**
|
|
686 |
// * Find or build a lexicon given a Corpus (MainCorpus or SubCorpus).
|
|
687 |
// *
|
|
688 |
// * @param corpus
|
|
689 |
// * @param property
|
|
690 |
// * @return a Lexicon. May return null if the lexicon forms or freqs are null.
|
|
691 |
// * @throws Exception
|
|
692 |
// */
|
|
693 |
// public static Lexicon getLexicon(Corpus corpus, Property property) throws Exception {
|
|
694 |
// HashSet<Object> results = corpus.getStoredData(Lexicon.class);
|
|
695 |
// for (Object result : results) {
|
|
696 |
// Lexicon lex = (Lexicon)result;
|
|
697 |
// if (lex.getProperty().equals(property)) {
|
|
698 |
// return lex;
|
|
699 |
// }
|
|
700 |
// }
|
|
701 |
//
|
|
702 |
// Lexicon lex = new Lexicon(corpus);
|
|
703 |
// lex.setParameters(property);
|
|
704 |
// if (lex.compute(null) && lex.getForms() != null && lex.getFreq() != null) {
|
|
705 |
// corpus.storeData(lex);
|
|
706 |
// return lex;
|
|
707 |
// } else {
|
|
708 |
// return null;
|
|
709 |
// }
|
|
710 |
// }
|
|
711 |
|
|
712 |
|
|
713 |
|
|
714 |
@Override
|
|
715 |
public String getResultype() {
|
|
716 |
return IndexCoreMessages.LEXICON_RESULT_TYPE;
|
|
717 |
}
|
|
718 |
|
|
719 |
|
|
720 |
}
|
0 |
721 |
|