Révision 3321
TXM/trunk/org.txm.analec.rcp/src/org/txm/macro/urs/exploit/CQPUtils.groovy (revision 3321) | ||
---|---|---|
2 | 2 |
// Licensed under the terms of the GNU General Public License (http://www.gnu.org/licenses) |
3 | 3 |
// @author sheiden |
4 | 4 |
|
5 |
package org.txm.macro.urs.exploit
|
|
5 |
package org.txm.macro.cqp
|
|
6 | 6 |
|
7 | 7 |
import org.txm.searchengine.cqp.corpus.* |
8 |
import org.txm.rcp.views.corpora.* |
|
9 | 8 |
|
10 | 9 |
def getCorpusByName(name) { |
11 |
def cl = CorpusManager.getCorpusManager().getCorpora().values()
|
|
10 |
def cl = CorpusManager.getCorpusManager().getCorpora() |
|
12 | 11 |
def rc = cl.find { |
13 |
it.getID() == name
|
|
12 |
it.getName() == name
|
|
14 | 13 |
} |
15 | 14 |
|
16 | 15 |
if (rc) return rc |
... | ... | |
19 | 18 |
rc = cl.find { c -> |
20 | 19 |
def scl = c.getSubcorpora() |
21 | 20 |
def rrsc = scl.find { sc -> |
22 |
sc.getID() == name
|
|
21 |
sc.getName() == name
|
|
23 | 22 |
} |
24 | 23 |
rsc = rrsc |
25 | 24 |
} |
... | ... | |
27 | 26 |
return rsc |
28 | 27 |
} |
29 | 28 |
|
30 |
def refreshCorpusViewExpand(monitor, corpus) { |
|
31 |
monitor.syncExec(new Runnable() { |
|
32 |
public void run() { |
|
33 |
CorporaView.refresh() |
|
34 |
CorporaView.expand(corpus) |
|
35 |
} |
|
36 |
}) |
|
37 |
} |
|
38 |
|
|
39 | 29 |
def getCorpora(def script) { |
40 | 30 |
|
41 | 31 |
def scriptName = this.class.getSimpleName() |
... | ... | |
160 | 150 |
|
161 | 151 |
} |
162 | 152 |
|
153 |
def positionsList2positions(positionsList, maxMatches, pretty) { |
|
154 |
|
|
155 |
def res = "" |
|
156 |
|
|
157 |
if (positionsList.size() == 0) { |
|
158 |
return "<empty>" |
|
159 |
} else { |
|
160 |
def first = 1 |
|
161 |
positionsList.take(maxMatches).each { |
|
162 |
if (first) { first = 0 } else { |
|
163 |
if (pretty) { |
|
164 |
res += ", " |
|
165 |
} else { |
|
166 |
res += "," |
|
167 |
} |
|
168 |
} |
|
169 |
// res += "[" |
|
170 |
start = it[0] |
|
171 |
end = it[1] |
|
172 |
if (start < end) { |
|
173 |
res += start+"-"+end |
|
174 |
} else { |
|
175 |
res += start |
|
176 |
} |
|
177 |
// res += "]" |
|
178 |
} |
|
179 |
} |
|
180 |
|
|
181 |
if (positionsList.size() > maxMatches) { |
|
182 |
res += "..." |
|
183 |
} |
|
184 |
|
|
185 |
return res |
|
186 |
} |
|
187 |
|
|
163 | 188 |
def corpus2positions(def corpus, def maxMatches, def pretty = false) { |
164 | 189 |
|
165 | 190 |
def res = "" |
... | ... | |
199 | 224 |
println "** CQPUtils is not meant to be called directly.\nPlease import its methods ${methods}\nwith the following declaration:\nimport org.txm.macro.cqp.CQPUtilsMacro" |
200 | 225 |
|
201 | 226 |
/* |
202 |
___ ___ ___ ___ ___ ___
|
|
203 |
/\ \ /\__\ /\__\ /\ \ |\__\ /\ \
|
|
204 |
/::\ \ /::| | /::| | /::\ \ |:| | /::\ \
|
|
205 |
/:/\:\ \ /:|:| | /:|:| | /:/\:\ \ |:| | /:/\:\ \
|
|
206 |
/::\~\:\ \ /:/|:| |__ /:/|:| |__ /::\~\:\ \ |:|__|__ /::\~\:\ \
|
|
227 |
___ ___ ___ ___ ___ ___
|
|
228 |
/\ \ /\__\ /\__\ /\ \ |\__\ /\ \
|
|
229 |
/::\ \ /::| | /::| | /::\ \ |:| | /::\ \
|
|
230 |
/:/\:\ \ /:|:| | /:|:| | /:/\:\ \ |:| | /:/\:\ \ |
|
231 |
/::\~\:\ \ /:/|:| |__ /:/|:| |__ /::\~\:\ \ |:|__|__ /::\~\:\ \ |
|
207 | 232 |
/:/\:\ \:\__\ /:/ |:| /\__\ /:/ |:| /\__\ /:/\:\ \:\__\ ____/::::\__\ /:/\:\ \:\__\ |
208 | 233 |
\/__\:\/:/ / \/__|:|/:/ / \/__|:|/:/ / \:\~\:\ \/__/ \::::/~~/~ \:\~\:\ \/__/ |
209 |
\::/ / |:/:/ / |:/:/ / \:\ \:\__\ ~~|:|~~| \:\ \:\__\
|
|
210 |
/:/ / |::/ / |::/ / \:\ \/__/ |:| | \:\ \/__/
|
|
211 |
/:/ / /:/ / /:/ / \:\__\ |:| | \:\__\
|
|
212 |
\/__/ \/__/ \/__/ \/__/ \|__| \/__/
|
|
234 |
\::/ / |:/:/ / |:/:/ / \:\ \:\__\ ~~|:|~~| \:\ \:\__\
|
|
235 |
/:/ / |::/ / |::/ / \:\ \/__/ |:| | \:\ \/__/
|
|
236 |
/:/ / /:/ / /:/ / \:\__\ |:| | \:\__\
|
|
237 |
\/__/ \/__/ \/__/ \/__/ \|__| \/__/
|
|
213 | 238 |
|
214 | 239 |
|
215 | 240 |
|
... | ... | |
245 | 270 |
import java.util.*; |
246 | 271 |
|
247 | 272 |
public class Ideone |
248 |
{
|
|
249 |
public static void main (String[] args) throws java.lang.Exception
|
|
250 |
{
|
|
251 |
ArrayList<Interval> x = new ArrayList<>();
|
|
273 |
{ |
|
274 |
public static void main (String[] args) throws java.lang.Exception
|
|
275 |
{
|
|
276 |
ArrayList<Interval> x = new ArrayList<>();
|
|
252 | 277 |
|
253 |
x.add(new Interval(1, 3));
|
|
254 |
x.add(new Interval(2, 6));
|
|
255 |
x.add(new Interval(8, 10));
|
|
256 |
x.add(new Interval(15, 18));
|
|
257 |
x.add(new Interval(17, 20));
|
|
278 |
x.add(new Interval(1, 3));
|
|
279 |
x.add(new Interval(2, 6));
|
|
280 |
x.add(new Interval(8, 10));
|
|
281 |
x.add(new Interval(15, 18));
|
|
282 |
x.add(new Interval(17, 20));
|
|
258 | 283 |
|
259 |
x = merge(x);
|
|
284 |
x = merge(x);
|
|
260 | 285 |
|
261 |
for(Interval i : x)
|
|
262 |
{
|
|
263 |
System.out.println(i.getStart() + " " + i.getEnd());
|
|
264 |
}
|
|
265 |
}
|
|
286 |
for(Interval i : x)
|
|
287 |
{
|
|
288 |
System.out.println(i.getStart() + " " + i.getEnd());
|
|
289 |
}
|
|
290 |
}
|
|
266 | 291 |
|
267 |
public static ArrayList<Interval> merge(ArrayList<Interval> intervals) {
|
|
292 |
public static ArrayList<Interval> merge(ArrayList<Interval> intervals) {
|
|
268 | 293 |
|
269 |
if(intervals.size() == 0 || intervals.size() == 1)
|
|
270 |
return intervals;
|
|
294 |
if(intervals.size() == 0 || intervals.size() == 1)
|
|
295 |
return intervals;
|
|
271 | 296 |
|
272 |
Collections.sort(intervals, new IntervalComparator());
|
|
297 |
Collections.sort(intervals, new IntervalComparator());
|
|
273 | 298 |
|
274 |
Interval first = intervals.get(0);
|
|
275 |
int start = first.getStart();
|
|
276 |
int end = first.getEnd();
|
|
299 |
Interval first = intervals.get(0);
|
|
300 |
int start = first.getStart();
|
|
301 |
int end = first.getEnd();
|
|
277 | 302 |
|
278 |
ArrayList<Interval> result = new ArrayList<Interval>();
|
|
303 |
ArrayList<Interval> result = new ArrayList<Interval>();
|
|
279 | 304 |
|
280 |
for (int i = 1; i < intervals.size(); i++) {
|
|
281 |
Interval current = intervals.get(i);
|
|
282 |
if (current.getStart() <= end) {
|
|
283 |
end = Math.max(current.getEnd(), end);
|
|
284 |
} else {
|
|
285 |
result.add(new Interval(start, end));
|
|
286 |
start = current.getStart();
|
|
287 |
end = current.getEnd();
|
|
288 |
}
|
|
289 |
}
|
|
305 |
for (int i = 1; i < intervals.size(); i++) {
|
|
306 |
Interval current = intervals.get(i);
|
|
307 |
if (current.getStart() <= end) {
|
|
308 |
end = Math.max(current.getEnd(), end);
|
|
309 |
} else {
|
|
310 |
result.add(new Interval(start, end));
|
|
311 |
start = current.getStart();
|
|
312 |
end = current.getEnd();
|
|
313 |
}
|
|
314 |
}
|
|
290 | 315 |
|
291 |
result.add(new Interval(start, end));
|
|
292 |
return result;
|
|
293 |
}
|
|
316 |
result.add(new Interval(start, end));
|
|
317 |
return result;
|
|
318 |
}
|
|
294 | 319 |
} |
295 | 320 |
|
296 |
class Interval
|
|
321 |
class Interval |
|
297 | 322 |
{ |
298 |
private int start;
|
|
299 |
private int end;
|
|
323 |
private int start;
|
|
324 |
private int end;
|
|
300 | 325 |
|
301 |
Interval() {
|
|
302 |
start = 0;
|
|
303 |
end = 0;
|
|
304 |
}
|
|
326 |
Interval() {
|
|
327 |
start = 0;
|
|
328 |
end = 0;
|
|
329 |
}
|
|
305 | 330 |
|
306 |
Interval(int s, int e)
|
|
307 |
{
|
|
308 |
start = s;
|
|
309 |
end = e;
|
|
310 |
}
|
|
331 |
Interval(int s, int e)
|
|
332 |
{
|
|
333 |
start = s;
|
|
334 |
end = e;
|
|
335 |
}
|
|
311 | 336 |
|
312 |
public int getStart() {
|
|
313 |
return start;
|
|
314 |
}
|
|
337 |
public int getStart() {
|
|
338 |
return start;
|
|
339 |
}
|
|
315 | 340 |
|
316 |
public int getEnd() {
|
|
317 |
return end;
|
|
318 |
}
|
|
341 |
public int getEnd() {
|
|
342 |
return end;
|
|
343 |
}
|
|
319 | 344 |
} |
320 | 345 |
|
321 | 346 |
class IntervalComparator implements Comparator<Interval> |
322 | 347 |
{ |
323 |
public int compare(Interval i1, Interval i2)
|
|
324 |
{
|
|
325 |
return i1.getStart() - i2.getStart();
|
|
326 |
}
|
|
348 |
public int compare(Interval i1, Interval i2)
|
|
349 |
{
|
|
350 |
return i1.getStart() - i2.getStart();
|
|
351 |
}
|
|
327 | 352 |
} |
328 | 353 |
|
329 | 354 |
B) from http://www.programcreek.com/2012/12/leetcode-merge-intervals/ |
330 | 355 |
|
331 | 356 |
public List<Interval> merge(List<Interval> intervals) { |
332 |
List<Interval> result = new ArrayList<Interval>();
|
|
357 |
List<Interval> result = new ArrayList<Interval>();
|
|
333 | 358 |
|
334 |
if(intervals==null||intervals.size()==0)
|
|
335 |
return result;
|
|
359 |
if(intervals==null||intervals.size()==0)
|
|
360 |
return result;
|
|
336 | 361 |
|
337 |
Collections.sort(intervals, new Comparator<Interval>(){
|
|
338 |
public int compare(Interval i1, Interval i2){
|
|
339 |
if(i1.start!=i2.start)
|
|
340 |
return i1.start-i2.start;
|
|
341 |
else
|
|
342 |
return i1.end-i2.end;
|
|
343 |
}
|
|
344 |
});
|
|
362 |
Collections.sort(intervals, new Comparator<Interval>(){
|
|
363 |
public int compare(Interval i1, Interval i2){
|
|
364 |
if(i1.start!=i2.start)
|
|
365 |
return i1.start-i2.start;
|
|
366 |
else
|
|
367 |
return i1.end-i2.end;
|
|
368 |
}
|
|
369 |
});
|
|
345 | 370 |
|
346 |
Interval pre = intervals.get(0);
|
|
347 |
for(int i=0; i<intervals.size(); i++){
|
|
348 |
Interval curr = intervals.get(i);
|
|
349 |
if(curr.start>pre.end){
|
|
350 |
result.add(pre);
|
|
351 |
pre = curr;
|
|
352 |
}else{
|
|
353 |
Interval merged = new Interval(pre.start, Math.max(pre.end, curr.end));
|
|
354 |
pre = merged;
|
|
355 |
}
|
|
356 |
}
|
|
357 |
result.add(pre);
|
|
371 |
Interval pre = intervals.get(0);
|
|
372 |
for(int i=0; i<intervals.size(); i++){
|
|
373 |
Interval curr = intervals.get(i);
|
|
374 |
if(curr.start>pre.end){
|
|
375 |
result.add(pre);
|
|
376 |
pre = curr;
|
|
377 |
}else{
|
|
378 |
Interval merged = new Interval(pre.start, Math.max(pre.end, curr.end));
|
|
379 |
pre = merged;
|
|
380 |
}
|
|
381 |
}
|
|
382 |
result.add(pre);
|
|
358 | 383 |
|
359 |
return result;
|
|
384 |
return result;
|
|
360 | 385 |
} |
361 |
*/ |
|
386 |
*/ |
Formats disponibles : Unified diff