Revision 2599

tmp/org.txm.cql2lsa.rcp/src/org/txm/functions/cql2lsa/ValuesOfQuery.java (revision 2599)
31 31
 *
32 32
 */
33 33
public class ValuesOfQuery extends TXMResult {
34

  
34
	
35 35
	CQPCorpus corpus;
36
	
36 37
	CQLQuery query;
38
	
37 39
	Property prop;
40
	
38 41
	int fmin;
39

  
40
	public ValuesOfQuery(CQPCorpus corpus, CQLQuery query, Property prop, int fmin)  {
42
	
43
	public ValuesOfQuery(CQPCorpus corpus, CQLQuery query, Property prop, int fmin) {
41 44
		super(corpus);
42 45
		this.corpus = corpus;
43 46
		this.query = query;
44 47
		this.prop = prop;
45 48
		this.fmin = fmin;
46 49
	}
47

  
50
	
48 51
	public boolean compute(File outputFile) throws CqiClientException, IOException, CqiServerError {
49 52
		
50 53
		if (!(CQPSearchEngine.getCqiClient() instanceof MemCqiClient)) return false;
......
52 55
		monitor.beginTask("Start querying...", 100);
53 56
		
54 57
		File file = File.createTempFile("query", ".txt");
55
		//ExecTimer t = new ExecTimer();
56
		//System.out.println("run query "+query);t.start();
58
		// ExecTimer t = new ExecTimer();
59
		// System.out.println("run query "+query);t.start();
57 60
		QueryResult result = corpus.query(query, "ValuesOf", false);
58
		//System.out.println("query done"+t.stop());t.start();
61
		// System.out.println("query done"+t.stop());t.start();
59 62
		
60
		//System.out.println("group query "+query+" and save in "+file.getAbsolutePath());t.start();
61
		MemCqiClient cli = (MemCqiClient)CQPSearchEngine.getCqiClient();
62
		cli.query("group "+result.getQualifiedCqpId()+" match "+prop.getName()+" > \""+file+"\";");
63
		//System.out.println("query done"+t.stop());t.start();
64

  
63
		// System.out.println("group query "+query+" and save in "+file.getAbsolutePath());t.start();
64
		MemCqiClient cli = (MemCqiClient) CQPSearchEngine.getCqiClient();
65
		cli.query("group " + result.getQualifiedCqpId() + " match " + prop.getName() + " > \"" + file + "\";");
66
		// System.out.println("query done"+t.stop());t.start();
67
		
65 68
		monitor.worked(50);
66 69
		
67 70
		if (!file.exists()) return false;
68

  
71
		
69 72
		BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
70 73
		String line = reader.readLine(); // "#---------------------------------"
71
		line = reader.readLine();        // "(none)          word     \t   freq"
74
		line = reader.readLine();        // "(none) word \t freq"
72 75
		
73 76
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8"));
74

  
77
		
75 78
		while (line != null) {
76 79
			line = line.substring(30);
77
//			int len = line.length();
78
//			
80
			// int len = line.length();
81
			//
79 82
			int i = line.indexOf('\t');
80
//			while (line.charAt(i) == ' ') i++;
81
//			int i_start = i;
82
//			while (line.charAt(i) != ' ') i++;
83
//			String w = line.substring(i_start, i);
84
//
85
//			while (line.charAt(i) == ' ') i++;
86
//			i_start = i;
87
//			while (line.charAt(i) != ' ' && i < len) i++;
88
//			String f = line.substring(i_start, i);
89
//			
90
//			System.out.println("'"+w+"'\t'"+f+"'");
83
			// while (line.charAt(i) == ' ') i++;
84
			// int i_start = i;
85
			// while (line.charAt(i) != ' ') i++;
86
			// String w = line.substring(i_start, i);
87
			//
88
			// while (line.charAt(i) == ' ') i++;
89
			// i_start = i;
90
			// while (line.charAt(i) != ' ' && i < len) i++;
91
			// String f = line.substring(i_start, i);
92
			//
93
			// System.out.println("'"+w+"'\t'"+f+"'");
91 94
			int f = Integer.parseInt(line.substring(i).trim());
92 95
			String s;
93 96
			if (f > fmin) {
94
				//System.out.println(line.substring(0, i).trim() + "\t"+f);
97
				// System.out.println(line.substring(0, i).trim() + "\t"+f);
95 98
				s = line.substring(0, i).trim();
96
				writer.write(s+"=["+prop.getName()+"=\""+s+"\"]\n");
97
			} else {
99
				writer.write(s + "=[" + prop.getName() + "=\"" + s + "\"]\n");
100
			}
101
			else {
98 102
				break;
99 103
			}
100
			line = reader.readLine(); 
104
			line = reader.readLine();
101 105
			monitor.worked(1);
102 106
		}
103 107
		reader.close();
104 108
		writer.close();
105
		System.out.println("Done printing queries in "+outputFile.getAbsolutePath());
109
		System.out.println("Done printing queries in " + outputFile.getAbsolutePath());
106 110
		return true;
107

  
111
		
108 112
	}
109

  
113
	
110 114
	/**
111 115
	 * Test purpose function.
112
	 * Does not manage big corpus 
116
	 * Does not manage big corpus
113 117
	 * 
114 118
	 * @throws CqiClientException
115 119
	 * @throws IOException
......
122 126
		for (Match m : result.getMatches()) {
123 127
			positions[i++] = m.getStart();
124 128
		}
125

  
129
		
126 130
		String[] values = null;
127 131
		if (prop instanceof StructuralUnitProperty) {
128 132
			int[] structs = CQPSearchEngine.getCqiClient().cpos2Struc(prop.getQualifiedName(), positions);
......
130 134
			structs = uniquify(structs);
131 135
			values = CQPSearchEngine.getCqiClient().struc2Str(prop.getQualifiedName(), structs);
132 136
			structs = null;
133
		} else {
137
		}
138
		else {
134 139
			int[] indexes = CQPSearchEngine.getCqiClient().cpos2Id(prop.getQualifiedName(), positions);
135 140
			positions = null;
136
			indexes =  uniquify(indexes);
141
			indexes = uniquify(indexes);
137 142
			values = CQPSearchEngine.getCqiClient().id2Str(prop.getQualifiedName(), indexes);
138 143
			indexes = null;
139 144
		}
140

  
145
		
141 146
		System.out.println("Values: ");
142 147
		for (String v : values) {
143 148
			System.out.println(v);
144 149
		}
145 150
	}
146

  
151
	
147 152
	/**
148 153
	 * 
149 154
	 * @param idx
......
153 158
		int[] result = new int[idx.length];
154 159
		int n = 0;
155 160
		Arrays.sort(idx);
156

  
161
		
157 162
		int previous = -1;
158 163
		for (int i : idx) {
159 164
			if (previous != i) {
......
161 166
				previous = i;
162 167
			}
163 168
		}
164

  
169
		
165 170
		int[] final_result = new int[n];
166 171
		System.arraycopy(result, 0, final_result, 0, n);
167

  
172
		
168 173
		return final_result;
169 174
	}
170

  
175
	
171 176
	@Override
172 177
	public boolean toTxt(File outfile, String encoding, String colseparator,
173 178
			String txtseparator) throws Exception {
174 179
		// TODO Auto-generated method stub
175 180
		return false;
176 181
	}
177

  
182
	
178 183
	@Override
179 184
	public void clean() {
180 185
		// TODO Auto-generated method stub
181 186
		
182 187
	}
183

  
188
	
184 189
	@Override
185 190
	public TXMResult getParent() {
186 191
		return corpus;
187 192
	}
188

  
193
	
189 194
	@Override
190 195
	public String getName() {
191 196
		return "ValuesOfQuery";
192 197
	}
193

  
198
	
194 199
	@Override
195 200
	public String getSimpleName() {
196 201
		return "ValuesOfQuery";
197 202
	}
198

  
203
	
199 204
	@Override
200 205
	public String getDetails() {
201
		return 	this.corpus.getName()+" "+this.query+" "+this.prop+" "+this.fmin;
206
		return this.corpus.getName() + " " + this.query + " " + this.prop + " " + this.fmin;
202 207
	}
203

  
208
	
204 209
	@Override
205 210
	public boolean setParameters(TXMParameters parameters) {
206 211
		return false;
207 212
	}
208

  
213
	
209 214
	@Override
210 215
	public boolean saveParameters() throws Exception {
211 216
		// TODO Auto-generated method stub
212 217
		return false;
213 218
	}
214

  
219
	
215 220
	@Override
216 221
	public boolean loadParameters() throws Exception {
217 222
		// TODO Auto-generated method stub
218 223
		return false;
219 224
	}
220

  
225
	
221 226
	@Override
222 227
	public boolean canCompute() throws Exception {
223 228
		// TODO Auto-generated method stub
224 229
		return false;
225 230
	}
226

  
231
	
227 232
	@Override
228 233
	protected boolean _compute() throws Exception {
229 234
		// TODO Auto-generated method stub
......
235 240
		// TODO
236 241
		return this.getClass().getSimpleName();
237 242
	}
238

  
243
	
239 244
}
tmp/org.txm.cql2lsa.rcp/src/org/txm/functions/cql2lsa/ExpII.java (revision 2599)
42 42
import org.txm.utils.logger.Log;
43 43

  
44 44
public class ExpII extends TXMResult implements IAdaptable {
45
	
45 46
	CQPCorpus corpus;
47
	
46 48
	List<String> texts;
49
	
47 50
	int[] textBoundaries;
48

  
49
	LinkedHashMap<String, QueryIndexLine> lines = new LinkedHashMap<String, QueryIndexLine>();
50

  
51
	
52
	LinkedHashMap<String, QueryIndexLine> lines = new LinkedHashMap<>();
53
	
51 54
	/** The writer. */
52 55
	private OutputStreamWriter writer;
53

  
56
	
54 57
	public ExpII(MainCorpus corpus) throws CqiClientException, IOException, CqiServerError, InvalidCqpIdException {
55 58
		super(corpus);
56 59
		this.corpus = corpus;
57 60
		texts = Arrays.asList(corpus.getCorpusTextIdsList());
58
		textBoundaries = corpus.getTextEndLimits(); 
59

  
61
		textBoundaries = corpus.getTextEndLimits();
62
		
60 63
	}
61

  
64
	
62 65
	public int getT() {
63 66
		int t = 0;
64 67
		for (QueryIndexLine line : lines.values()) {
......
66 69
		}
67 70
		return t;
68 71
	}
69

  
72
	
70 73
	public int getFmin() {
71 74
		int t = 999999999;
72 75
		for (QueryIndexLine line : lines.values()) {
......
75 78
		}
76 79
		return t;
77 80
	}
78

  
81
	
79 82
	public int getFmax() {
80 83
		int t = 0;
81 84
		for (QueryIndexLine line : lines.values()) {
......
84 87
		}
85 88
		return t;
86 89
	}
87

  
90
	
88 91
	public int getV() {
89 92
		return lines.values().size();
90 93
	}
91

  
94
	
95
	@Override
92 96
	public String getName() {
93 97
		return corpus.getName();
94 98
	}
95

  
99
	
96 100
	public List<String> getTextNames() {
97 101
		return texts;
98 102
	}
99

  
103
	
104
	@Override
100 105
	public TXMResult getParent() {
101 106
		return corpus;
102 107
	}
103

  
108
	
104 109
	public CQPCorpus getCorpus() {
105 110
		return corpus;
106 111
	}
107

  
112
	
108 113
	public Collection<QueryIndexLine> getLines() {
109 114
		return lines.values();
110 115
	}
111

  
116
	
112 117
	public LinkedHashMap<String, QueryIndexLine> getLinesHash() {
113 118
		return lines;
114 119
	}
115

  
120
	
116 121
	int multi = 1;
122
	
117 123
	public void sortLines(SortMode mode, boolean revert) {
118

  
124
		
119 125
		multi = 1;
120 126
		if (revert) multi = -1;
121
		List<Map.Entry<String, QueryIndexLine>> entries =
122
				new ArrayList<Map.Entry<String, QueryIndexLine>>(lines.entrySet());
123

  
127
		List<Map.Entry<String, QueryIndexLine>> entries = new ArrayList<>(lines.entrySet());
128
		
124 129
		if (mode == SortMode.FREQUNIT) {
125 130
			Collections.sort(entries, new Comparator<Map.Entry<String, QueryIndexLine>>() {
126
				public int compare(Map.Entry<String, QueryIndexLine> a, Map.Entry<String, QueryIndexLine> b){
131
				
132
				@Override
133
				public int compare(Map.Entry<String, QueryIndexLine> a, Map.Entry<String, QueryIndexLine> b) {
127 134
					int ret = multi * (a.getValue().getFrequency() - b.getValue().getFrequency());
128 135
					if (ret == 0) {
129 136
						return multi * a.getValue().getName().compareTo(b.getValue().getName());
......
131 138
					return ret;
132 139
				}
133 140
			});
134
		} else if (mode == SortMode.FREQ) {
141
		}
142
		else if (mode == SortMode.FREQ) {
135 143
			Collections.sort(entries, new Comparator<Map.Entry<String, QueryIndexLine>>() {
136
				public int compare(Map.Entry<String, QueryIndexLine> a, Map.Entry<String, QueryIndexLine> b){
144
				
145
				@Override
146
				public int compare(Map.Entry<String, QueryIndexLine> a, Map.Entry<String, QueryIndexLine> b) {
137 147
					return multi * (a.getValue().getFrequency() - b.getValue().getFrequency());
138 148
				}
139 149
			});
140
		} else if (mode == SortMode.UNIT) {
150
		}
151
		else if (mode == SortMode.UNIT) {
141 152
			Collections.sort(entries, new Comparator<Map.Entry<String, QueryIndexLine>>() {
142
				public int compare(Map.Entry<String, QueryIndexLine> a, Map.Entry<String, QueryIndexLine> b){
153
				
154
				@Override
155
				public int compare(Map.Entry<String, QueryIndexLine> a, Map.Entry<String, QueryIndexLine> b) {
143 156
					return multi * a.getValue().getName().compareTo(b.getValue().getName());
144 157
				}
145 158
			});
146
		} else if (mode == SortMode.UNITFREQ) {
159
		}
160
		else if (mode == SortMode.UNITFREQ) {
147 161
			Collections.sort(entries, new Comparator<Map.Entry<String, QueryIndexLine>>() {
148
				public int compare(Map.Entry<String, QueryIndexLine> a, Map.Entry<String, QueryIndexLine> b){
162
				
163
				@Override
164
				public int compare(Map.Entry<String, QueryIndexLine> a, Map.Entry<String, QueryIndexLine> b) {
149 165
					int ret = multi * a.getValue().getName().compareTo(b.getValue().getName());
150 166
					if (ret == 0) {
151 167
						return multi * (a.getValue().getFrequency() - b.getValue().getFrequency());
......
154 170
				}
155 171
			});
156 172
		}
157

  
158
		LinkedHashMap<String, QueryIndexLine> sortedMap = new LinkedHashMap<String, QueryIndexLine>();
173
		
174
		LinkedHashMap<String, QueryIndexLine> sortedMap = new LinkedHashMap<>();
159 175
		for (Map.Entry<String, QueryIndexLine> entry : entries) {
160 176
			sortedMap.put(entry.getKey(), entry.getValue());
161 177
		}
162

  
178
		
163 179
		lines = sortedMap;
164 180
	}
165

  
181
	
166 182
	HashMap<String, int[]> keywordStartPositions;
183
	
167 184
	HashMap<String, int[]> keywordEndPositions;
168

  
185
	
169 186
	public void compute(File queriesFile, File keywordFile, File outputDir) throws CqiClientException, IOException, CqiServerError {
170 187
		System.out.println("Starting ExpII");
171 188
		
......
174 191
		
175 192
		PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8")));
176 193
		
177
		keywordStartPositions = new HashMap<String, int[]>();
178
		keywordEndPositions = new HashMap<String, int[]>();
179
		HashMap<String, String> keywordQueriestoName = new HashMap<String, String>();
194
		keywordStartPositions = new HashMap<>();
195
		keywordEndPositions = new HashMap<>();
196
		HashMap<String, String> keywordQueriestoName = new HashMap<>();
180 197
		int idx_lexicon_counter = 0;
181 198
		
182 199
		// assos query et idx
183 200
		// si queries est dans keywords alors, pas la peine de calculer keyword, parce que la query est un keyword
184
		LinkedHashMap<String, Integer> idxLexicon = new LinkedHashMap<String, Integer>();
185
		LinkedHashMap<String, int[]> keywordsMaxCountsLexicon = new LinkedHashMap<String, int[]>(); 
201
		LinkedHashMap<String, Integer> idxLexicon = new LinkedHashMap<>();
202
		LinkedHashMap<String, int[]> keywordsMaxCountsLexicon = new LinkedHashMap<>();
186 203
		
187 204
		BufferedReader keywordFileReader = new BufferedReader(new InputStreamReader(
188 205
				new FileInputStream(keywordFile), "UTF-8")); //$NON-NLS-1$
189
		ArrayList<String> keywordLines = new ArrayList<String>();
206
		ArrayList<String> keywordLines = new ArrayList<>();
190 207
		String l = keywordFileReader.readLine();
191 208
		while (l != null) {
192 209
			keywordLines.add(l);
193 210
			l = keywordFileReader.readLine();
194 211
		}
195 212
		keywordFileReader.close();
196
		System.out.println("Number of keywords lines: "+keywordLines.size());
197
		monitor.beginTask("Querying keywords...", keywordLines.size());
198

  
213
		System.out.println("Number of keywords lines: " + keywordLines.size());
214
		this.subTask("Querying keywords...");
215
		
199 216
		int nkeyword = 0;
200 217
		for (String line : keywordLines) {
201 218
			String[] split = line.split("=", 2); //$NON-NLS-1$
202 219
			if (split.length == 2) {
203 220
				CQLQuery q = new CQLQuery(split[1]);
204 221
				keywordQueriestoName.put(split[1], split[0]);
205
				QueryResult result = corpus.query(q, "K"+nkeyword++, false); //$NON-NLS-1$
222
				QueryResult result = corpus.query(q, "K" + nkeyword++, false); //$NON-NLS-1$
206 223
				keywordStartPositions.put(split[1], result.getStarts());
207 224
				keywordEndPositions.put(split[1], result.getEnds());
208 225
				keywordsMaxCountsLexicon.put(split[1], new int[textBoundaries.length]);
209 226
				idxLexicon.put(split[1], idx_lexicon_counter++);
210
				monitor.worked(1);
227
				this.worked(1);
211 228
			}
212 229
		}
213 230
		keywordFileReader.close();
214 231
		
215 232
		BufferedReader queriesFileReader = new BufferedReader(new InputStreamReader(new FileInputStream(queriesFile), "UTF-8")); //$NON-NLS-1$
216
		ArrayList<String> lines = new ArrayList<String>();
233
		ArrayList<String> lines = new ArrayList<>();
217 234
		l = queriesFileReader.readLine();
218 235
		while (l != null) {
219 236
			lines.add(l);
220 237
			l = queriesFileReader.readLine();
221 238
		}
222 239
		queriesFileReader.close();
223

  
224
		System.out.println("Number of lemma lines: "+lines.size());
225
		monitor.beginTask("Querying...", lines.size());
226

  
240
		
241
		System.out.println("Number of lemma lines: " + lines.size());
242
		this.subTask("Querying...");
243
		
227 244
		int nquery = 0;
228 245
		for (String line : lines) {
229 246
			String[] split = line.split("=", 2); //$NON-NLS-1$
230 247
			if (split.length == 2) {
231 248
				if (hasLine(split[0])) {
232 249
					System.out.println(TXMCoreMessages.bind(TXMCoreMessages.warningColonDuplicateQueryEntryColonP0, line));
233
				} else {
234
					QueryResult result = corpus.query(new CQLQuery(split[1]), "Q"+nquery++, false); //$NON-NLS-1$
250
				}
251
				else {
252
					QueryResult result = corpus.query(new CQLQuery(split[1]), "Q" + nquery++, false); //$NON-NLS-1$
235 253
					int[] starts = result.getStarts();
236
					int[] ends =  result.getEnds();
237

  
254
					int[] ends = result.getEnds();
255
					
238 256
					int[] counts = new int[textBoundaries.length];
239 257
					int count = 0;
240 258
					int noText = 0;
......
252 270
						count++;
253 271
					}
254 272
					counts[noText] = count;
255

  
273
					
256 274
					if (idxLexicon.containsKey(split[1])) { // the query is already computed !!
257
						for (noText = 0 ; noText < textBoundaries.length ; noText++) {
275
						for (noText = 0; noText < textBoundaries.length; noText++) {
258 276
							if (counts[noText] > 0) {
259
								//System.out.println("MCL-"+split[1]+"\t"+noText+"\t"+idxLexicon.get(split[1])+"\t"+counts[noText]+"\t"+idxLexicon.get(split[1]));
260
								writer.println(noText+"\t"+idxLexicon.get(split[1])+"\t"+counts[noText]+"\t"+idxLexicon.get(split[1]));
277
								// System.out.println("MCL-"+split[1]+"\t"+noText+"\t"+idxLexicon.get(split[1])+"\t"+counts[noText]+"\t"+idxLexicon.get(split[1]));
278
								writer.println(noText + "\t" + idxLexicon.get(split[1]) + "\t" + counts[noText] + "\t" + idxLexicon.get(split[1]));
261 279
							}
262 280
						}
263
					} else { // test if match is covered by keywords
281
					}
282
					else { // test if match is covered by keywords
264 283
						idxLexicon.put(split[1], idx_lexicon_counter++); // put Lemma query
265 284
						
266
						//int max_sum_f = 0;
285
						// int max_sum_f = 0;
267 286
						int[] max_f = new int[textBoundaries.length];
268 287
						String[] max_key = new String[textBoundaries.length];
269 288
						
270 289
						for (String key : keywordEndPositions.keySet()) {
271 290
							int[] keyMaxValues = keywordsMaxCountsLexicon.get(key);
272 291
							int[] f = covered(starts, ends, keywordStartPositions.get(key), keywordEndPositions.get(key));
273
							for (noText = 0 ; noText < textBoundaries.length ; noText++) {
292
							for (noText = 0; noText < textBoundaries.length; noText++) {
274 293
								if (f[noText] > max_f[noText]) {
275 294
									max_f[noText] = f[noText];
276 295
									max_key[noText] = key;
......
280 299
								}
281 300
							}
282 301
						}
283
//						System.out.println("max_f="+Arrays.toString(counts));
284
//						System.out.println("max_f="+Arrays.toString(max_f));
285
						for (noText = 0 ; noText < textBoundaries.length ; noText++) {
302
						// System.out.println("max_f="+Arrays.toString(counts));
303
						// System.out.println("max_f="+Arrays.toString(max_f));
304
						for (noText = 0; noText < textBoundaries.length; noText++) {
286 305
							int F = counts[noText] - max_f[noText];
287 306
							if (F > 0) {
288
								//System.out.println("L-"+split[1]+"\t"+noText+"\t"+idxLexicon.get(split[1])+"\t"+F+"\t0");
289
								writer.println(noText+"\t"+idxLexicon.get(split[1])+"\t"+F+"\t0");
307
								// System.out.println("L-"+split[1]+"\t"+noText+"\t"+idxLexicon.get(split[1])+"\t"+F+"\t0");
308
								writer.println(noText + "\t" + idxLexicon.get(split[1]) + "\t" + F + "\t0");
290 309
							}
291 310
						}
292
					}				
311
					}
293 312
				}
294 313
			}
295
			monitor.worked(1);
314
			this.worked(1);
296 315
		}
297 316
		
298
		monitor.subTask("Finalizing doc_word_freq_2.txt file...");
317
		this.subTask("Finalizing doc_word_freq_2.txt file...");
299 318
		for (String key : keywordsMaxCountsLexicon.keySet()) {
300 319
			int[] keyMaxValues = keywordsMaxCountsLexicon.get(key);
301
			for (int noText = 0 ; noText < textBoundaries.length ; noText++) {
320
			for (int noText = 0; noText < textBoundaries.length; noText++) {
302 321
				int F = keyMaxValues[noText];
303 322
				if (F > 0) {
304
					//System.out.println("MC-"+key+"\t"+noText+"\t"+idxLexicon.get(key)+"\t"+F+"\t"+idxLexicon.get(key));
305
					writer.println(noText+"\t"+idxLexicon.get(key)+"\t"+F+"\t"+idxLexicon.get(key));
323
					// System.out.println("MC-"+key+"\t"+noText+"\t"+idxLexicon.get(key)+"\t"+F+"\t"+idxLexicon.get(key));
324
					writer.println(noText + "\t" + idxLexicon.get(key) + "\t" + F + "\t" + idxLexicon.get(key));
306 325
				}
307 326
			}
308 327
		}
309 328
		queriesFileReader.close();
310 329
		writer.close();
311 330
		
312
		monitor.subTask("Writing lexicon file...");
331
		this.subTask("Writing lexicon file...");
313 332
		writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(lexiconFile), "UTF-8")));
314 333
		for (String query : idxLexicon.keySet()) {
315 334
			String name = keywordQueriestoName.get(query);
316 335
			if (name != null) {
317 336
				writer.println(keywordQueriestoName.get(query));
318
			} else {
337
			}
338
			else {
319 339
				writer.println(query);
320 340
			}
321 341
			
322 342
		}
323 343
		writer.close();
324 344
		
325
		System.out.println("Done, result saved in: \n - "+outputFile.getAbsolutePath()+"\n - "+lexiconFile.getAbsolutePath());
345
		System.out.println("Done, result saved in: \n - " + outputFile.getAbsolutePath() + "\n - " + lexiconFile.getAbsolutePath());
326 346
	}
327

  
347
	
328 348
	/**
329 349
	 * Write all the lines on a writer.
330 350
	 *
......
334 354
	 * @param txtseparator the txtseparator
335 355
	 * @return true, if successful
336 356
	 */
357
	@Override
337 358
	public boolean toTxt(File outfile, String encoding, String colseparator, String txtseparator) {
338 359
		try {
339 360
			toTxt(outfile, 0, lines.size(), encoding, colseparator, txtseparator);
340
		} catch (Exception e) {
361
		}
362
		catch (Exception e) {
341 363
			Log.severe(TXMCoreMessages.bind(IndexCoreMessages.error_failedToExportLexiconColonP0, Log.toString(e)));
342 364
			return false;
343 365
		}
344 366
		return true;
345 367
	}
346

  
368
	
347 369
	/**
348 370
	 * Write the lines between from and to on a writer.
349 371
	 *
......
360 382
			throws CqiClientException, IOException {
361 383
		// NK: writer declared as class attribute to perform a clean if the operation is interrupted
362 384
		this.writer = new OutputStreamWriter(new FileOutputStream(outfile),
363
				encoding); 
385
				encoding);
364 386
		String header = "Queries"; //$NON-NLS-1$
365
		header = txtseparator+ header.substring(0, header.length() - 1) +txtseparator;
366
		header += colseparator+ txtseparator+ "F" + txtseparator; //$NON-NLS-1$
367

  
387
		header = txtseparator + header.substring(0, header.length() - 1) + txtseparator;
388
		header += colseparator + txtseparator + "F" + txtseparator; //$NON-NLS-1$
389
		
368 390
		for (String t : texts)
369
			header += colseparator + txtseparator+ t.replace(txtseparator, txtseparator+txtseparator)+txtseparator; 
391
			header += colseparator + txtseparator + t.replace(txtseparator, txtseparator + txtseparator) + txtseparator;
370 392
		header += "\n"; //$NON-NLS-1$
371 393
		writer.write(header);
372

  
394
		
373 395
		// for(Line ligne: lines)
374 396
		for (String name : lines.keySet()) {
375 397
			QueryIndexLine ligne = lines.get(name);
376
			writer.write(txtseparator+ ligne.getName().replace(txtseparator, txtseparator+txtseparator)+ txtseparator + colseparator + ligne.getFrequency()); 
377

  
398
			writer.write(txtseparator + ligne.getName().replace(txtseparator, txtseparator + txtseparator) + txtseparator + colseparator + ligne.getFrequency());
399
			
378 400
			for (int j = 0; j < texts.size(); j++)
379
				writer.write(colseparator + ligne.getFrequency(j)); 
401
				writer.write(colseparator + ligne.getFrequency(j));
380 402
			writer.write("\n"); //$NON-NLS-1$
381 403
		}
382 404
		writer.flush();
383 405
		writer.close();
384 406
	}
385

  
407
	
386 408
	/**
387 409
	 * Only one query result and uses texts boundaries to count frequencies for each text
410
	 * 
388 411
	 * @param name
389 412
	 * @param query
390 413
	 * @return
......
393 416
	public QueryIndexLine addLine(String name, CQLQuery query) throws CqiClientException {
394 417
		if (lines.containsKey(name)) return null;
395 418
		QueryResult qresult = corpus.query(query, "tmp", true); //$NON-NLS-1$
396
		//System.out.println(query.toString()+" "+qresult.getNMatch());
419
		// System.out.println(query.toString()+" "+qresult.getNMatch());
397 420
		int[] counts = new int[textBoundaries.length];
398 421
		int count = 0;
399 422
		int noText = 0;
400 423
		int endOfCurrentText = textBoundaries[noText];
401 424
		for (Match m : qresult.getMatches()) {
402 425
			while (m.getStart() >= endOfCurrentText) {
403
				//System.out.println(m.getStart() +">="+endOfCurrentText);
426
				// System.out.println(m.getStart() +">="+endOfCurrentText);
404 427
				if (noText >= textBoundaries.length) break;
405
				//System.out.println("Text: "+texts.get(noText)+" count="+count+" notext="+noText);
428
				// System.out.println("Text: "+texts.get(noText)+" count="+count+" notext="+noText);
406 429
				counts[noText] = count;
407 430
				noText++;
408 431
				if (noText >= textBoundaries.length) break;
......
412 435
			}
413 436
			count++;
414 437
		}
415
		//System.out.println(noText +"<?"+textBoundaries.length+" count="+count);
438
		// System.out.println(noText +"<?"+textBoundaries.length+" count="+count);
416 439
		if (noText < textBoundaries.length) // last text
417 440
			counts[noText] = count;
418

  
441
		
419 442
		qresult.drop();
420

  
443
		
421 444
		QueryIndexLine line = new QueryIndexLine(name, query, null);
422 445
		line.setFrequencies(counts);
423 446
		lines.put(name, line);
424 447
		return line;
425 448
	}
426

  
449
	
427 450
	public LexicalTableImpl toLexicalTable() {
428

  
451
		
429 452
		int npart = texts.size();
430 453
		int[][] freqs = new int[lines.size()][npart];
431 454
		String[] rownames = new String[lines.size()];
432 455
		String[] colnames = new String[npart];
433

  
434
		for (int i = 0 ; i < lines.size() ; i++) {
456
		
457
		for (int i = 0; i < lines.size(); i++) {
435 458
			QueryIndexLine line = lines.get(i);
436 459
			int[] linefreqs = line.getFreqs();
437 460
			rownames[i] = line.getName();
438
			for (int j = 0 ; j < npart ; j++) {
461
			for (int j = 0; j < npart; j++) {
439 462
				freqs[i][j] = linefreqs[j];
440 463
			}
441 464
		}
......
444 467
			colnames[j] = t;
445 468
			j++;
446 469
		}
447

  
470
		
448 471
		try {
449 472
			LexicalTableImpl lt = new LexicalTableImpl(freqs, rownames, colnames);
450 473
			return lt;
451
		} catch (Exception e) {
474
		}
475
		catch (Exception e) {
452 476
			org.txm.utils.logger.Log.printStackTrace(e);
453 477
		}
454 478
		return null;
455 479
	}
456

  
480
	
457 481
	public boolean removeLine(String name) {
458 482
		if (lines.containsKey(name)) {
459 483
			this.acquireSemaphore();
460 484
			lines.remove(name);
461 485
			this.releaseSemaphore();
462 486
			return true;
463
		} else {
487
		}
488
		else {
464 489
			return false;
465 490
		}
466 491
	}
467

  
492
	
468 493
	public boolean hasLine(String name) {
469 494
		return lines.containsKey(name);
470 495
	}
471

  
496
	
472 497
	public ArrayList<QueryIndexLine> getLines(int from, int to) {
473
		if (lines.size() == 0) return new ArrayList<QueryIndexLine>();
474

  
498
		if (lines.size() == 0) return new ArrayList<>();
499
		
475 500
		if (from < 0) from = 0;
476 501
		if (to < 0) to = 0;
477 502
		if (to > lines.size()) to = lines.size();
478 503
		if (from > to) from = to - 1;
479
		ArrayList<QueryIndexLine> tmp = new ArrayList<QueryIndexLine>();
504
		ArrayList<QueryIndexLine> tmp = new ArrayList<>();
480 505
		int i = 0;
481 506
		for (QueryIndexLine line : lines.values()) {
482 507
			if (i >= from && i < to) {
......
484 509
			}
485 510
			i++;
486 511
		}
487

  
512
		
488 513
		return tmp;
489 514
	}
490

  
515
	
491 516
	@Override
492 517
	public String[] getExportTXTExtensions() {
493
		return new String[]{"*.csv"};
518
		return new String[] { "*.csv" };
494 519
	}
495

  
520
	
496 521
	@Override
497 522
	public void clean() {
498 523
		try {
499 524
			this.writer.flush();
500
			this.writer.close();	
501
		} catch (IOException e) {
525
			this.writer.close();
526
		}
527
		catch (IOException e) {
502 528
			// TODO Auto-generated catch block
503 529
			org.txm.utils.logger.Log.printStackTrace(e);
504 530
		}
505 531
	}
506

  
532
	
507 533
	@Override
508 534
	public Object getAdapter(Class adapterType) {
509 535
		if (adapterType == IWorkbenchAdapter.class)
510 536
			return ExpIIAAdapter;
511 537
		return null;
512 538
	}
513

  
539
	
514 540
	/** The WordCloud adapter. */
515 541
	private static IWorkbenchAdapter ExpIIAAdapter = new IWorkbenchAdapter() {
516

  
542
		
517 543
		@Override
518 544
		public Object[] getChildren(Object o) {
519 545
			return new Object[0];
520 546
		}
521

  
547
		
522 548
		@Override
523 549
		public ImageDescriptor getImageDescriptor(Object object) {
524 550
			return null;
525 551
		}
526

  
552
		
527 553
		@Override
528 554
		public String getLabel(Object o) {
529 555
			return ((ExpII) o).getName();
530 556
		}
531

  
557
		
532 558
		@Override
533 559
		public Object getParent(Object o) {
534 560
			return ((ExpII) o).getCorpus();
535 561
		}
536 562
	};
537

  
538

  
539

  
563
	
564
	
565
	
540 566
	private int[] covered(int[] starts, int[] ends, int[] keywordStarts, int[] keywordEnds) {
541 567
		int[] counts = new int[textBoundaries.length];
542 568
		int noText = 0;
543 569
		int endOfCurrentText = textBoundaries[noText];
544 570
		int f = 0;
545
		//		printMatchs(starts, ends, keywordStarts, keywordEnds);
546

  
571
		// printMatchs(starts, ends, keywordStarts, keywordEnds);
572
		
547 573
		int i_keyword = 0;
548
		//System.out.println("len i="+starts.length+ " len i_keyword="+keywordStarts.length);
549
		for (int i = 0 ; i < starts.length ; ) {
550

  
551
			//System.out.println("i="+i+ " i_keyword="+i_keyword);
574
		// System.out.println("len i="+starts.length+ " len i_keyword="+keywordStarts.length);
575
		for (int i = 0; i < starts.length;) {
576
			
577
			// System.out.println("i="+i+ " i_keyword="+i_keyword);
552 578
			if (i_keyword >= keywordStarts.length) break; // no more keyword positions
553
			//System.out.println(""+starts[i]+"->"+ends[i]+" : "+keywordStarts[i_keyword]+"->"+keywordEnds[i_keyword]);
554

  
579
			// System.out.println(""+starts[i]+"->"+ends[i]+" : "+keywordStarts[i_keyword]+"->"+keywordEnds[i_keyword]);
580
			
555 581
			while (starts[i] >= endOfCurrentText) {
556 582
				if (noText >= textBoundaries.length) break;
557 583
				counts[noText] = f;
......
562 588
				f = 0;
563 589
			}
564 590
			if (starts[i] < keywordStarts[i_keyword]) {
565
				//System.out.println(" match start is not covered");
591
				// System.out.println(" match start is not covered");
566 592
				i++;
567
			} else if (starts[i] > keywordEnds[i_keyword]) {
568
				//System.out.println(" next keyword");
593
			}
594
			else if (starts[i] > keywordEnds[i_keyword]) {
595
				// System.out.println(" next keyword");
569 596
				i_keyword++; // see next keyword match
570
			} else if (ends[i] <= keywordEnds[i_keyword]) {
571
				//System.out.println(" next match");
572
				//System.out.println(">>>> "+starts[i]+"->"+ends[i]+" : "+keywordStarts[i_keyword]+"->"+keywordEnds[i_keyword]);
597
			}
598
			else if (ends[i] <= keywordEnds[i_keyword]) {
599
				// System.out.println(" next match");
600
				// System.out.println(">>>> "+starts[i]+"->"+ends[i]+" : "+keywordStarts[i_keyword]+"->"+keywordEnds[i_keyword]);
573 601
				i++; // OK, test next match
574 602
				f++;
575
			} else {
576
				//System.out.println(" match end is not covered");
603
			}
604
			else {
605
				// System.out.println(" match end is not covered");
577 606
				i++; // OK, test next match
578 607
			}
579 608
		}
580 609
		counts[noText] = f;
581 610
		return counts;
582 611
	}
583

  
612
	
584 613
	private static void printMatchs(int[] starts, int[] ends, int[] keywordStarts, int[] keywordEnds) {
585 614
		int min = 999999999;
586 615
		int max = 0;
587
		for (int i : starts) if (i < min) min = i;
588
		for (int i : keywordStarts) if (i < min) min = i;
589
		for (int i : ends) if (i > max) max = i;
590
		for (int i : keywordEnds) if (i > max) max = i;
591
		for (int i = min ; i <= max ; i++) System.out.print(""+i+"\t");
616
		for (int i : starts)
617
			if (i < min) min = i;
618
		for (int i : keywordStarts)
619
			if (i < min) min = i;
620
		for (int i : ends)
621
			if (i > max) max = i;
622
		for (int i : keywordEnds)
623
			if (i > max) max = i;
624
		for (int i = min; i <= max; i++)
625
			System.out.print("" + i + "\t");
592 626
		System.out.println();
593 627
		int j = 0;
594 628
		int k = 0;
595 629
		boolean inout = false;
596
		for (int i = min ; i <= max ; i++) {
630
		for (int i = min; i <= max; i++) {
597 631
			if (j < starts.length && starts[j] == i) {
598 632
				if (k < ends.length && ends[k] == i) {
599
					System.out.print(""+starts[j++]+"><\t");
633
					System.out.print("" + starts[j++] + "><\t");
600 634
					k++;
601
				} else {
602
					System.out.print(""+starts[j++]+">\t");
635
				}
636
				else {
637
					System.out.print("" + starts[j++] + ">\t");
603 638
					inout = true;
604 639
				}
605
			} else if (k < ends.length && ends[k] == i) {
606
				System.out.print("<"+ends[k++]+"\t");
640
			}
641
			else if (k < ends.length && ends[k] == i) {
642
				System.out.print("<" + ends[k++] + "\t");
607 643
				inout = false;
608
			} else {
644
			}
645
			else {
609 646
				if (inout) {
610 647
					System.out.print("-\t");
611
				} else {
648
				}
649
				else {
612 650
					System.out.print("\t");
613 651
				}
614 652
			}
615 653
		}
616 654
		System.out.println();
617

  
655
		
618 656
		j = 0;
619 657
		k = 0;
620 658
		inout = false;
621
		for (int i = min ; i <= max ; i++) {
659
		for (int i = min; i <= max; i++) {
622 660
			if (j < keywordStarts.length && keywordStarts[j] == i) {
623 661
				if (k < keywordEnds.length && keywordEnds[k] == i) {
624
					System.out.print(""+keywordStarts[j++]+"><\t");
662
					System.out.print("" + keywordStarts[j++] + "><\t");
625 663
					k++;
626
				} else {
627
					System.out.print(""+keywordStarts[j++]+">\t");
664
				}
665
				else {
666
					System.out.print("" + keywordStarts[j++] + ">\t");
628 667
					inout = true;
629 668
				}
630
			} else if (k < keywordEnds.length && keywordEnds[k] == i) {
631
				System.out.print("<"+keywordEnds[k++]+"\t");
669
			}
670
			else if (k < keywordEnds.length && keywordEnds[k] == i) {
671
				System.out.print("<" + keywordEnds[k++] + "\t");
632 672
				inout = false;
633
			} else {
673
			}
674
			else {
634 675
				if (inout) {
635 676
					System.out.print("-\t");
636
				} else {
677
				}
678
				else {
637 679
					System.out.print("\t");
638 680
				}
639

  
681
				
640 682
			}
641 683
		}
642 684
		System.out.println();
643 685
	}
644

  
686
	
645 687
	public static void main(String[] args) {
646
		//		int starts[] = {1, 10, 16, 24};
647
		//		int ends[] = {2, 12, 16, 30};
648
		//		int kstarts[] = {5, 9, 16, 22};
649
		//		int kends[] = {8, 12, 16, 35};
650
		//		System.out.println(covered(starts, ends, kstarts, kends));
651

  
652
		//		int starts[] = {10, 15 ,24, 50};
653
		//		int ends[] = {12, 16 ,30, 60};
654
		//		int kstarts[] = {5, 9, 14, 20};
655
		//		int kends[] = {8, 12, 18, 40};
656
		//		System.out.println(covered(starts, ends, kstarts, kends));
657

  
658
		//		int starts[] = {1, 10, 15 ,24};
659
		//		int ends[] = {2, 12, 16 ,30};
660
		//		int kstarts[] = {5, 9, 14, 20};
661
		//		int kends[] = {8, 12, 18, 40};
662
		//		System.out.println(covered(starts, ends, kstarts, kends));
663

  
664
		//		int starts[] = {4, 10, 16};
665
		//		int ends[] = {5, 11, 17};
666
		//		int kstarts[] = {3, 11, 15};
667
		//		int kends[] = {8, 13, 20};
668
		//		System.out.println(covered(starts, ends, kstarts, kends));
669

  
670
		int starts[] = {4, 10, 16, 20};
671
		int ends[] = {5, 11, 17, 22};
672
		int kstarts[] = {3, 11, 15};
673
		int kends[] = {8, 13, 20};
674
		//System.out.println(covered(starts, ends, kstarts, kends));
688
		// int starts[] = {1, 10, 16, 24};
689
		// int ends[] = {2, 12, 16, 30};
690
		// int kstarts[] = {5, 9, 16, 22};
691
		// int kends[] = {8, 12, 16, 35};
692
		// System.out.println(covered(starts, ends, kstarts, kends));
693
		
694
		// int starts[] = {10, 15 ,24, 50};
695
		// int ends[] = {12, 16 ,30, 60};
696
		// int kstarts[] = {5, 9, 14, 20};
697
		// int kends[] = {8, 12, 18, 40};
698
		// System.out.println(covered(starts, ends, kstarts, kends));
699
		
700
		// int starts[] = {1, 10, 15 ,24};
701
		// int ends[] = {2, 12, 16 ,30};
702
		// int kstarts[] = {5, 9, 14, 20};
703
		// int kends[] = {8, 12, 18, 40};
704
		// System.out.println(covered(starts, ends, kstarts, kends));
705
		
706
		// int starts[] = {4, 10, 16};
707
		// int ends[] = {5, 11, 17};
708
		// int kstarts[] = {3, 11, 15};
709
		// int kends[] = {8, 13, 20};
710
		// System.out.println(covered(starts, ends, kstarts, kends));
711
		
712
		int starts[] = { 4, 10, 16, 20 };
713
		int ends[] = { 5, 11, 17, 22 };
714
		int kstarts[] = { 3, 11, 15 };
715
		int kends[] = { 8, 13, 20 };
716
		// System.out.println(covered(starts, ends, kstarts, kends));
675 717
	}
676

  
718
	
677 719
	@Override
678 720
	public String getSimpleName() {
679 721
		return "ExpII";
680 722
	}
681

  
723
	
682 724
	@Override
683 725
	public String getDetails() {
684 726
		return texts.toString();
685 727
	}
686

  
728
	
687 729
	@Override
688 730
	public boolean setParameters(TXMParameters parameters) {
689 731
		return false;
690 732
	}
691

  
733
	
692 734
	@Override
693 735
	public boolean saveParameters() throws Exception {
694 736
		// TODO Auto-generated method stub
695 737
		return false;
696 738
	}
697

  
739
	
698 740
	@Override
699 741
	public boolean loadParameters() throws Exception {
700 742
		// TODO Auto-generated method stub
701 743
		return false;
702 744
	}
703

  
745
	
704 746
	@Override
705 747
	public boolean canCompute() throws Exception {
706 748
		// TODO Auto-generated method stub
707 749
		return false;
708 750
	}
709

  
751
	
710 752
	@Override
711 753
	protected boolean _compute() throws Exception {
712 754
		// TODO Auto-generated method stub
......
718 760
		// TODO
719 761
		return this.getClass().getSimpleName();
720 762
	}
721

  
722
}
763
	
764
}
tmp/org.txm.cql2lsa.rcp/src/org/txm/functions/cql2lsa/ExpI.java (revision 2599)
39 39
import org.txm.utils.logger.Log;
40 40

  
41 41
public class ExpI extends TXMResult implements IAdaptable {
42
	
42 43
	CQPCorpus corpus;
44
	
43 45
	List<String> texts;
46
	
44 47
	int[] textBoundaries;
45

  
46
	LinkedHashMap<String, QueryIndexLine> lines = new LinkedHashMap<String, QueryIndexLine>();
47

  
48
	
49
	LinkedHashMap<String, QueryIndexLine> lines = new LinkedHashMap<>();
50
	
48 51
	/** The writer. */
49 52
	private OutputStreamWriter writer;
50

  
53
	
51 54
	public ExpI(MainCorpus corpus) throws CqiClientException, IOException, CqiServerError, InvalidCqpIdException {
52 55
		super(corpus);
53 56
		this.corpus = corpus;
54
		//System.out.println("get text ids");
57
		// System.out.println("get text ids");
55 58
		texts = Arrays.asList(corpus.getCorpusTextIdsList());
56
		//System.out.println( "init texts: "+texts);
57
		//System.out.println("get text limits: "+texts.size());
58
		textBoundaries = corpus.getTextEndLimits(); 
59
		//System.out.println("rdy: "+textBoundaries.length);
60
//		System.out.println(Arrays.toString(textBoundaries));
61
//		System.out.println("length: "+textBoundaries.length);
62
//		System.out.println(texts);
63
//		System.out.println("length: "+texts.size());
59
		// System.out.println( "init texts: "+texts);
60
		// System.out.println("get text limits: "+texts.size());
61
		textBoundaries = corpus.getTextEndLimits();
62
		// System.out.println("rdy: "+textBoundaries.length);
63
		// System.out.println(Arrays.toString(textBoundaries));
64
		// System.out.println("length: "+textBoundaries.length);
65
		// System.out.println(texts);
66
		// System.out.println("length: "+texts.size());
64 67
	}
65

  
68
	
69
	@Override
66 70
	public String getName() {
67 71
		return corpus.getName();
68 72
	}
......
70 74
	public List<String> getTextNames() {
71 75
		return texts;
72 76
	}
73

  
77
	
78
	@Override
74 79
	public org.txm.core.results.TXMResult getParent() {
75 80
		return corpus;
76 81
	}
77

  
82
	
78 83
	public CQPCorpus getCorpus() {
79 84
		return corpus;
80 85
	}
81

  
86
	
82 87
	public Collection<QueryIndexLine> getLines() {
83 88
		return lines.values();
84 89
	}
85

  
90
	
86 91
	public LinkedHashMap<String, QueryIndexLine> getLinesHash() {
87 92
		return lines;
88 93
	}
89

  
94
	
90 95
	int multi = 1;
96
	
91 97
	public void sortLines(SortMode mode, boolean revert) {
92

  
98
		
93 99
		multi = 1;
94 100
		if (revert) multi = -1;
95
		List<Map.Entry<String, QueryIndexLine>> entries =
96
				new ArrayList<Map.Entry<String, QueryIndexLine>>(lines.entrySet());
97

  
101
		List<Map.Entry<String, QueryIndexLine>> entries = new ArrayList<>(lines.entrySet());
102
		
98 103
		if (mode == SortMode.FREQUNIT) {
99 104
			Collections.sort(entries, new Comparator<Map.Entry<String, QueryIndexLine>>() {
100
				public int compare(Map.Entry<String, QueryIndexLine> a, Map.Entry<String, QueryIndexLine> b){
105
				
106
				@Override
107
				public int compare(Map.Entry<String, QueryIndexLine> a, Map.Entry<String, QueryIndexLine> b) {
101 108
					int ret = multi * (a.getValue().getFrequency() - b.getValue().getFrequency());
102 109
					if (ret == 0) {
103 110
						return multi * a.getValue().getName().compareTo(b.getValue().getName());
......
105 112
					return ret;
106 113
				}
107 114
			});
108
		} else if (mode == SortMode.FREQ) {
115
		}
116
		else if (mode == SortMode.FREQ) {
109 117
			Collections.sort(entries, new Comparator<Map.Entry<String, QueryIndexLine>>() {
110
				public int compare(Map.Entry<String, QueryIndexLine> a, Map.Entry<String, QueryIndexLine> b){
118
				
119
				@Override
120
				public int compare(Map.Entry<String, QueryIndexLine> a, Map.Entry<String, QueryIndexLine> b) {
111 121
					return multi * (a.getValue().getFrequency() - b.getValue().getFrequency());
112 122
				}
113 123
			});
114
		} else if (mode == SortMode.UNIT) {
124
		}
125
		else if (mode == SortMode.UNIT) {
115 126
			Collections.sort(entries, new Comparator<Map.Entry<String, QueryIndexLine>>() {
116
				public int compare(Map.Entry<String, QueryIndexLine> a, Map.Entry<String, QueryIndexLine> b){
127
				
128
				@Override
129
				public int compare(Map.Entry<String, QueryIndexLine> a, Map.Entry<String, QueryIndexLine> b) {
117 130
					return multi * a.getValue().getName().compareTo(b.getValue().getName());
118 131
				}
119 132
			});
120
		} else if (mode == SortMode.UNITFREQ) {
133
		}
134
		else if (mode == SortMode.UNITFREQ) {
121 135
			Collections.sort(entries, new Comparator<Map.Entry<String, QueryIndexLine>>() {
122
				public int compare(Map.Entry<String, QueryIndexLine> a, Map.Entry<String, QueryIndexLine> b){
136
				
137
				@Override
138
				public int compare(Map.Entry<String, QueryIndexLine> a, Map.Entry<String, QueryIndexLine> b) {
123 139
					int ret = multi * a.getValue().getName().compareTo(b.getValue().getName());
124 140
					if (ret == 0) {
125 141
						return multi * (a.getValue().getFrequency() - b.getValue().getFrequency());
......
128 144
				}
129 145
			});
130 146
		}
131

  
132
		LinkedHashMap<String, QueryIndexLine> sortedMap = new LinkedHashMap<String, QueryIndexLine>();
147
		
148
		LinkedHashMap<String, QueryIndexLine> sortedMap = new LinkedHashMap<>();
133 149
		for (Map.Entry<String, QueryIndexLine> entry : entries) {
134 150
			sortedMap.put(entry.getKey(), entry.getValue());
135 151
		}
136

  
152
		
137 153
		lines = sortedMap;
138 154
	}
139

  
155
	
140 156
	public void addLinesFromFile(File propFile) throws CqiClientException, IOException {
141 157
		BufferedReader reader = new BufferedReader(new InputStreamReader(
142 158
				new FileInputStream(propFile), "UTF-8")); //$NON-NLS-1$
143
		ArrayList<String> lines = new ArrayList<String>();
159
		ArrayList<String> lines = new ArrayList<>();
144 160
		String l = reader.readLine();
145 161
		while (l != null) {
146 162
			lines.add(l);
......
148 164
		}
149 165
		reader.close();
150 166
		
151
		System.out.println("Number of query lines: "+lines.size());
152
		monitor.beginTask("Querying...", lines.size());
167
		System.out.println("Number of query lines: " + lines.size());
168
		this.subTask("Querying...");
153 169
		
154 170
		for (String line : lines) {
155 171
			String[] split = line.split("=", 2); //$NON-NLS-1$
156 172
			if (split.length == 2) {
157 173
				if (hasLine(split[0])) {
158 174
					System.out.println(TXMCoreMessages.bind(TXMCoreMessages.warningColonDuplicateQueryEntryColonP0, line));
159
				} else {
175
				}
176
				else {
160 177
					if (addLine(split[0], new CQLQuery(split[1])) == null) {
161 178
						System.out.println(TXMCoreMessages.bind(TXMCoreMessages.warningColonQueryFailedColonP0, line));
162 179
					}
163
					monitor.worked(1);
180
					this.worked(1);
164 181
				}
165 182
			}
166 183
		}
167 184
	}
168

  
185
	
169 186
	/**
170 187
	 * Write all the lines on a writer.
171 188
	 *
......
175 192
	 * @param txtseparator the txtseparator
176 193
	 * @return true, if successful
177 194
	 */
195
	@Override
178 196
	public boolean toTxt(File outfile, String encoding, String colseparator, String txtseparator) {
179 197
		try {
180 198
			toTxt(outfile, 0, lines.size(), encoding, colseparator, txtseparator);
181
		} catch (Exception e) {
199
		}
200
		catch (Exception e) {
182 201
			Log.severe(TXMCoreMessages.bind(IndexCoreMessages.error_failedToExportLexiconColonP0, Log.toString(e)));
183 202
			return false;
184 203
		}
185 204
		return true;
186 205
	}
187

  
206
	
188 207
	/**
189 208
	 * Write the lines between from and to on a writer.
190 209
	 *
......
201 220
			throws CqiClientException, IOException {
202 221
		// NK: writer declared as class attribute to perform a clean if the operation is interrupted
203 222
		this.writer = new OutputStreamWriter(new FileOutputStream(outfile),
204
				encoding); 
223
				encoding);
205 224
		String header = "Queries"; //$NON-NLS-1$
206
		header = txtseparator+ header.substring(0, header.length() - 1) +txtseparator;
207
		header += colseparator+ txtseparator+ "F" + txtseparator; //$NON-NLS-1$
208

  
225
		header = txtseparator + header.substring(0, header.length() - 1) + txtseparator;
226
		header += colseparator + txtseparator + "F" + txtseparator; //$NON-NLS-1$
227
		
209 228
		for (String t : texts)
210
			header += colseparator + txtseparator+ t.replace(txtseparator, txtseparator+txtseparator)+txtseparator; 
229
			header += colseparator + txtseparator + t.replace(txtseparator, txtseparator + txtseparator) + txtseparator;
211 230
		header += "\n"; //$NON-NLS-1$
212 231
		writer.write(header);
213

  
232
		
214 233
		// for(Line ligne: lines)
215 234
		for (String name : lines.keySet()) {
216 235
			QueryIndexLine ligne = lines.get(name);
217
			writer.write(txtseparator+ ligne.getName().replace(txtseparator, txtseparator+txtseparator)+ txtseparator + colseparator + ligne.getFrequency()); 
218

  
236
			writer.write(txtseparator + ligne.getName().replace(txtseparator, txtseparator + txtseparator) + txtseparator + colseparator + ligne.getFrequency());
237
			
219 238
			for (int j = 0; j < texts.size(); j++)
220
				writer.write(colseparator + ligne.getFrequency(j)); 
239
				writer.write(colseparator + ligne.getFrequency(j));
221 240
			writer.write("\n"); //$NON-NLS-1$
222 241
		}
223 242
		writer.flush();
224 243
		writer.close();
225 244
	}
226

  
245
	
227 246
	/**
228 247
	 * Only one query result and uses texts boundaries to count frequencies for each text
248
	 * 
229 249
	 * @param name
230 250
	 * @param query
231 251
	 * @return
......
234 254
	public QueryIndexLine addLine(String name, CQLQuery query) throws CqiClientException {
235 255
		if (lines.containsKey(name)) return null;
236 256
		QueryResult qresult = corpus.query(query, "tmp", true); //$NON-NLS-1$
237
		//System.out.println(query.toString()+" "+qresult.getNMatch());
257
		// System.out.println(query.toString()+" "+qresult.getNMatch());
238 258
		int[] counts = new int[textBoundaries.length];
239 259
		int count = 0;
240 260
		int noText = 0;
241 261
		int endOfCurrentText = textBoundaries[noText];
242 262
		for (Match m : qresult.getMatches()) {
243 263
			while (m.getStart() >= endOfCurrentText) {
244
				//System.out.println(m.getStart() +">="+endOfCurrentText);
264
				// System.out.println(m.getStart() +">="+endOfCurrentText);
245 265
				if (noText >= textBoundaries.length) break;
246
				//System.out.println("Text: "+texts.get(noText)+" count="+count+" notext="+noText);
266
				// System.out.println("Text: "+texts.get(noText)+" count="+count+" notext="+noText);
247 267
				counts[noText] = count;
248 268
				noText++;
249 269
				if (noText >= textBoundaries.length) break;
......
253 273
			}
254 274
			count++;
255 275
		}
256
		//System.out.println(noText +"<?"+textBoundaries.length+" count="+count);
276
		// System.out.println(noText +"<?"+textBoundaries.length+" count="+count);
257 277
		if (noText < textBoundaries.length) // last text
258 278
			counts[noText] = count;
259 279
		
......
264 284
		lines.put(name, line);
265 285
		return line;
266 286
	}
267

  
287
	
268 288
	public LexicalTableImpl toLexicalTable() {
269

  
289
		
270 290
		int npart = texts.size();
271 291
		int[][] freqs = new int[lines.size()][npart];
272 292
		String[] rownames = new String[lines.size()];
273 293
		String[] colnames = new String[npart];
274

  
275
		for (int i = 0 ; i < lines.size() ; i++) {
294
		
295
		for (int i = 0; i < lines.size(); i++) {
276 296
			QueryIndexLine line = lines.get(i);
277 297
			int[] linefreqs = line.getFreqs();
278 298
			rownames[i] = line.getName();
279
			for (int j = 0 ; j < npart ; j++) {
299
			for (int j = 0; j < npart; j++) {
280 300
				freqs[i][j] = linefreqs[j];
281 301
			}
282 302
		}
......
285 305
			colnames[j] = t;
286 306
			j++;
287 307
		}
288

  
308
		
289 309
		try {
290 310
			LexicalTableImpl lt = new LexicalTableImpl(freqs, rownames, colnames);
291 311
			return lt;
292
		} catch (RWorkspaceException e) {
312
		}
313
		catch (RWorkspaceException e) {
293 314
			org.txm.utils.logger.Log.printStackTrace(e);
294 315
		}
295 316
		return null;
296 317
	}
297

  
318
	
298 319
	public boolean removeLine(String name) {
299 320
		if (lines.containsKey(name)) {
300 321
			this.acquireSemaphore();
301 322
			lines.remove(name);
302 323
			this.releaseSemaphore();
303 324
			return true;
304
		} else {
325
		}
326
		else {
305 327
			return false;
306 328
		}
307 329
	}
308

  
330
	
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff