Révision 3265
tmp/org.txm.groovy.core/src/groovy/org/txm/macro/cqp/ShrinkCQLValuesMacro.groovy (revision 3265) | ||
---|---|---|
1 |
package org.txm.macro.cqp |
|
2 |
|
|
3 |
import org.txm.macro.projects.antract.OkapiSaphirAPI |
|
4 |
import org.txm.searchengine.cqp.CQPSearchEngine |
|
5 |
|
|
6 |
import org.kohsuke.args4j.* |
|
7 |
import groovy.transform.Field |
|
8 |
import org.txm.rcp.swt.widget.parameters.* |
|
9 |
import org.txm.searchengine.cqp.corpus.CQPCorpus |
|
10 |
import org.txm.searchengine.cqp.corpus.Subcorpus |
|
11 |
import org.txm.searchengine.cqp.corpus.query.CQLQuery |
|
12 |
import org.txm.rcp.views.corpora.CorporaView |
|
13 |
|
|
14 |
@Field @Option(name="values", usage="Liste des valeurs séparés par des '|'", widget="Text", required=true, def="") |
|
15 |
def values |
|
16 |
|
|
17 |
if (!ParametersDialog.open(this)) return |
|
18 |
|
|
19 |
|
|
20 |
values = values.replace("\n", "|").replace("\t", "|").replace(" ", "|").replaceAll("(\\|)++", "|").trim() |
|
21 |
|
|
22 |
println "RESULT:" |
|
23 |
println getShortCQL(values.split("\\|") as List) |
|
24 |
|
|
25 |
def getShortCQL(def list) { |
|
26 |
def origtree = ["":list] |
|
27 |
breakNode(origtree, "", 0) |
|
28 |
def CQL1 = list.join("|") |
|
29 |
def CQL2 = writeNode(origtree) |
|
30 |
if (CQL1.length() < CQL2.length()) { |
|
31 |
return CQL1; |
|
32 |
} else { |
|
33 |
return CQL2; |
|
34 |
} |
|
35 |
} |
|
36 |
|
|
37 |
def breakNode(def tree, def key, def p) { |
|
38 |
def space = (" "*p) |
|
39 |
//println space + "breaking: $key of $tree at $p" |
|
40 |
|
|
41 |
def children = new LinkedHashMap() |
|
42 |
def ids = tree[key] |
|
43 |
tree[key] = children |
|
44 |
|
|
45 |
for (def child : ids) { |
|
46 |
//println space+"child="+child |
|
47 |
if (child.length() <= p) continue; |
|
48 |
|
|
49 |
def letter = child[p] |
|
50 |
if (!children.containsKey(letter)) { |
|
51 |
children[letter] = [] |
|
52 |
} |
|
53 |
|
|
54 |
children[letter].add(child) |
|
55 |
} |
|
56 |
|
|
57 |
for (def k : children.keySet()) { |
|
58 |
breakNode(children, k , p+1) |
|
59 |
} |
|
60 |
} |
|
61 |
|
|
62 |
|
|
63 |
def writeNode(def tree) { |
|
64 |
def s = "" |
|
65 |
//println "tree: "+tree |
|
66 |
|
|
67 |
int n = 0; |
|
68 |
if (tree.size() > 1) s += "(" |
|
69 |
String ss = "" |
|
70 |
for (def child : tree.keySet()) { |
|
71 |
if (n > 0) ss += "|" |
|
72 |
ss += child |
|
73 |
ss += writeNode(tree[child]) |
|
74 |
n++ |
|
75 |
} |
|
76 |
|
|
77 |
if (ss.matches("([0-9]\\|){2,10}[0-9]")) ss = "["+ss.replace("|", "") +"]" |
|
78 |
if (ss == "[0123456789]") ss = "[0-9]" |
|
79 |
if (ss == "[123456789]") ss = "[1-9]" |
|
80 |
if (ss == "[23456789]") ss = "[2-9]" |
|
81 |
if (ss == "[3456789]") ss = "[3-9]" |
|
82 |
if (ss == "[0123456789]") ss = "[0-9]" |
|
83 |
if (ss == "[012345678]") ss = "[0-8]" |
|
84 |
if (ss == "[01234567]") ss = "[0-7]" |
|
85 |
if (ss == "[0123456]") ss = "[0-6]" |
|
86 |
if (ss == "[12345678]") ss = "[1-8]" |
|
87 |
if (ss == "[1234567]") ss = "[1-7]" |
|
88 |
s += ss |
|
89 |
|
|
90 |
if (tree.size() > 1) s += ")" |
|
91 |
return s |
|
92 |
} |
Formats disponibles : Unified diff