Révision 478
tmp/org.txm.querycooccurrences.rcp/.classpath (revision 478) | ||
---|---|---|
1 |
<?xml version="1.0" encoding="UTF-8"?> |
|
2 |
<classpath> |
|
3 |
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/> |
|
4 |
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"> |
|
5 |
<accessrules> |
|
6 |
<accessrule kind="accessible" pattern="**"/> |
|
7 |
</accessrules> |
|
8 |
</classpathentry> |
|
9 |
<classpathentry kind="src" path="src"/> |
|
10 |
<classpathentry kind="output" path="bin"/> |
|
11 |
</classpath> |
|
0 | 12 |
tmp/org.txm.querycooccurrences.rcp/META-INF/MANIFEST.MF (revision 478) | ||
---|---|---|
1 |
Manifest-Version: 1.0 |
|
2 |
Bundle-ManifestVersion: 2 |
|
3 |
Bundle-Name: org.txm.querycooccurrence.rcp |
|
4 |
Bundle-SymbolicName: org.txm.querycooccurrence.rcp;singleton:=true |
|
5 |
Bundle-Version: 1.0.0.qualifier |
|
6 |
Bundle-Activator: listcooccurrences.Activator |
|
7 |
Require-Bundle: org.eclipse.ui, |
|
8 |
org.eclipse.core.runtime, |
|
9 |
org.eclipse.core.expressions;bundle-version="3.4.500", |
|
10 |
org.txm.rcp;bundle-version="0.7.8", |
|
11 |
org.txm.core;bundle-version="0.7.0", |
|
12 |
org.txm.queryindex.rcp;bundle-version="1.0.0", |
|
13 |
org.txm.ca.core, |
|
14 |
org.txm.statsengine.r.core, |
|
15 |
org.txm.lexicaltable.core, |
|
16 |
org.txm.statsengine.core, |
|
17 |
org.txm.statsengine.r.rcp |
|
18 |
Bundle-RequiredExecutionEnvironment: JavaSE-1.6 |
|
19 |
Bundle-ActivationPolicy: lazy |
|
20 |
Export-Package: org.txm.functions.coocmatrix |
|
21 |
Bundle-Vendor: Textometrie.org |
|
0 | 22 |
tmp/org.txm.querycooccurrences.rcp/.project (revision 478) | ||
---|---|---|
1 |
<?xml version="1.0" encoding="UTF-8"?> |
|
2 |
<projectDescription> |
|
3 |
<name>QueryCooccurrencesRCP</name> |
|
4 |
<comment></comment> |
|
5 |
<projects> |
|
6 |
</projects> |
|
7 |
<buildSpec> |
|
8 |
<buildCommand> |
|
9 |
<name>org.eclipse.jdt.core.javabuilder</name> |
|
10 |
<arguments> |
|
11 |
</arguments> |
|
12 |
</buildCommand> |
|
13 |
<buildCommand> |
|
14 |
<name>org.eclipse.pde.ManifestBuilder</name> |
|
15 |
<arguments> |
|
16 |
</arguments> |
|
17 |
</buildCommand> |
|
18 |
<buildCommand> |
|
19 |
<name>org.eclipse.pde.SchemaBuilder</name> |
|
20 |
<arguments> |
|
21 |
</arguments> |
|
22 |
</buildCommand> |
|
23 |
</buildSpec> |
|
24 |
<natures> |
|
25 |
<nature>org.eclipse.pde.PluginNature</nature> |
|
26 |
<nature>org.eclipse.jdt.core.javanature</nature> |
|
27 |
</natures> |
|
28 |
</projectDescription> |
|
0 | 29 |
tmp/org.txm.querycooccurrences.rcp/src/org/txm/functions/coocmatrix/QueryAutoCooccurrence.java (revision 478) | ||
---|---|---|
1 |
package org.txm.functions.coocmatrix; |
|
2 |
|
|
3 |
import java.io.File; |
|
4 |
import java.util.ArrayList; |
|
5 |
|
|
6 |
import org.eclipse.core.runtime.IAdaptable; |
|
7 |
import org.eclipse.jface.resource.ImageDescriptor; |
|
8 |
import org.eclipse.ui.model.IWorkbenchAdapter; |
|
9 |
import org.txm.ca.core.functions.CA; |
|
10 |
import org.txm.ca.core.functions.ICAComputable; |
|
11 |
import org.txm.core.results.TXMResult; |
|
12 |
import org.txm.functions.ProgressWatcher; |
|
13 |
import org.txm.lexicaltable.core.functions.LexicalTable; |
|
14 |
import org.txm.lexicaltable.core.statsengine.r.data.LexicalTableImpl; |
|
15 |
import org.txm.rcp.IImageKeys; |
|
16 |
import org.txm.searchengine.cqp.clientExceptions.CqiClientException; |
|
17 |
import org.txm.searchengine.cqp.corpus.Corpus; |
|
18 |
import org.txm.searchengine.cqp.corpus.Partition; |
|
19 |
import org.txm.searchengine.cqp.corpus.Property; |
|
20 |
import org.txm.searchengine.cqp.corpus.QueryResult; |
|
21 |
import org.txm.searchengine.cqp.corpus.query.Query; |
|
22 |
import org.txm.statsengine.core.StatException; |
|
23 |
import org.txm.statsengine.r.core.RWorkspace; |
|
24 |
import org.txm.statsengine.r.core.exceptions.RWorkspaceException; |
|
25 |
import org.txm.utils.logger.Log; |
|
26 |
|
|
27 |
public class QueryAutoCooccurrence extends TXMResult implements ICAComputable, IAdaptable { |
|
28 |
|
|
29 |
protected Corpus corpus; |
|
30 |
protected int[][] coocs; |
|
31 |
protected ArrayList<Query> queries; |
|
32 |
protected ArrayList<String> names; |
|
33 |
|
|
34 |
protected int dist; |
|
35 |
protected String struct = ""; |
|
36 |
protected String symbol; |
|
37 |
protected static int noCoocMatrix = 1; |
|
38 |
|
|
39 |
protected static String prefixR = "CoocMatrix"; |
|
40 |
|
|
41 |
protected int nNodes = 0; |
|
42 |
protected int nEdges = 0; |
|
43 |
protected int minCooc; |
|
44 |
protected boolean oriented = false; |
|
45 |
|
|
46 |
public QueryAutoCooccurrence(Corpus corpus, ArrayList<Query> queries, |
|
47 |
ArrayList<String> names, int dist, int minCooc, String struct, boolean oriented, ProgressWatcher monitor) throws CqiClientException, RWorkspaceException { |
|
48 |
this.queries = queries; |
|
49 |
this.names = names; |
|
50 |
this.corpus = corpus; |
|
51 |
this.dist = dist; |
|
52 |
this.minCooc = minCooc; |
|
53 |
this.oriented = oriented; |
|
54 |
if (struct != null) |
|
55 |
this.struct = "within "+struct; |
|
56 |
else |
|
57 |
this.struct = "within text"; |
|
58 |
|
|
59 |
nNodes = queries.size(); |
|
60 |
|
|
61 |
coocs = new int[queries.size()][queries.size()]; |
|
62 |
if (monitor != null) monitor.beginTask("Querying cooccurrences...", nNodes); |
|
63 |
for (int i = 0 ; i < queries.size() ; i++) { |
|
64 |
int j = i; |
|
65 |
if (oriented) j = 0; // if the graph is not oriented, we don't need to evaluate all queries |
|
66 |
for (; j < queries.size() ; j++) { |
|
67 |
if (monitor != null) monitor.worked(1); |
|
68 |
String query = null; |
|
69 |
if (oriented) |
|
70 |
query = "("+queries.get(i)+"[]{0,"+dist+"}"+queries.get(j)+") "+this.struct; |
|
71 |
else |
|
72 |
query = "("+queries.get(i)+"[]{0,"+dist+"}"+queries.get(j)+")|("+queries.get(j)+"[]{0,"+dist+"}"+queries.get(i)+") "+this.struct; |
|
73 |
|
|
74 |
|
|
75 |
Log.info("ListCooc: query "+query); |
|
76 |
QueryResult result = corpus.query(new Query(query), "tmp", false); |
|
77 |
coocs[i][j] = result.getNMatch(); |
|
78 |
if (coocs[i][j] >= minCooc) { |
|
79 |
nEdges++; |
|
80 |
} else { |
|
81 |
coocs[i][j] = 0; |
|
82 |
} |
|
83 |
result.drop(); |
|
84 |
} |
|
85 |
} |
|
86 |
|
|
87 |
if (!oriented) //// if the graph is not oriented, copy upper triangle values to lower triangle values |
|
88 |
for (int i = 0 ; i < queries.size() ; i++) { |
|
89 |
for (int j = i ; j < queries.size() ; j++) { |
|
90 |
coocs[j][i] = coocs[i][j]; |
|
91 |
} |
|
92 |
} |
|
93 |
if (monitor != null) monitor.done(); |
|
94 |
} |
|
95 |
|
|
96 |
public String getName() { |
|
97 |
return corpus.getName()+ " d="+dist+ " min="+minCooc; |
|
98 |
} |
|
99 |
|
|
100 |
public Corpus getParent() { |
|
101 |
return corpus; |
|
102 |
} |
|
103 |
|
|
104 |
public String getSymbol() { |
|
105 |
return symbol; |
|
106 |
} |
|
107 |
|
|
108 |
public int getNNodes() { |
|
109 |
return nNodes; |
|
110 |
} |
|
111 |
|
|
112 |
public int getNEdges() { |
|
113 |
return nEdges; |
|
114 |
} |
|
115 |
|
|
116 |
private void sendToR() throws RWorkspaceException { |
|
117 |
if (symbol != null) return; |
|
118 |
|
|
119 |
this.symbol = prefixR+(QueryCooccurrence.noCoocMatrix ++); |
|
120 |
RWorkspace rw = RWorkspace.getRWorkspaceInstance(); |
|
121 |
rw.eval("rm("+symbol+")"); |
|
122 |
rw.addMatrixToWorkspace(symbol, coocs); |
|
123 |
rw.addVectorToWorkspace("cooclabels", names.toArray(new String[names.size()])); |
|
124 |
rw.eval("rownames("+symbol+") <- cooclabels"); |
|
125 |
rw.eval("colnames("+symbol+") <- cooclabels"); |
|
126 |
} |
|
127 |
|
|
128 |
|
|
129 |
public LexicalTable toLexicalTable() throws RWorkspaceException, CqiClientException { |
|
130 |
sendToR(); |
|
131 |
RWorkspace rw = RWorkspace.getRWorkspaceInstance(); |
|
132 |
rw.eval("LT<-"+this.getSymbol()); |
|
133 |
rw.eval("LT<-LT[rowSums(LT) != 0, ]"); |
|
134 |
rw.eval("LT<-LT[, colSums(LT) != 0]"); |
|
135 |
rw.eval(this.getSymbol()+"LT<-LT"); |
|
136 |
rw.eval("rm(LT)"); |
|
137 |
LexicalTable table = new LexicalTable(corpus, corpus.getProperty("word"), new LexicalTableImpl(this.getSymbol()+"LT")); |
|
138 |
return table; |
|
139 |
} |
|
140 |
|
|
141 |
public boolean toGraphml(File outfile) throws RWorkspaceException { |
|
142 |
|
|
143 |
RWorkspace rw = null; |
|
144 |
try { |
|
145 |
rw = RWorkspace.getRWorkspaceInstance(); |
|
146 |
rw.eval("library(\"igraph\")"); |
|
147 |
Log.info("Package 'igraph' installed. Now running CoocMatrix"); |
|
148 |
} catch (Exception e) { |
|
149 |
Log.printStackTrace(e); |
|
150 |
Log.warning("The 'igraph' package is not installed: "+e); |
|
151 |
boolean install = true; |
|
152 |
if (install) { |
|
153 |
Log.warning("Installing 'igraph'"); |
|
154 |
try { |
|
155 |
rw.eval("install.packages(\"igraph\", dependencies=TRUE, repos=\"http://cran.rstudio.com\");"); |
|
156 |
} catch(Exception e2) { |
|
157 |
Log.severe("Could not install the 'igraph' package"); |
|
158 |
Log.printStackTrace(e2); |
|
159 |
return false; |
|
160 |
} |
|
161 |
} else { |
|
162 |
System.out.println("Operation canceled by user"); |
|
163 |
return false; |
|
164 |
} |
|
165 |
} |
|
166 |
|
|
167 |
sendToR(); |
|
168 |
|
|
169 |
outfile.delete(); |
|
170 |
|
|
171 |
String orientationMode = "undirected"; |
|
172 |
//if (oriented) orientationMode = "directed"; |
|
173 |
|
|
174 |
rw.eval("library(igraph)"); |
|
175 |
rw.eval("g <- graph.adjacency("+symbol+", weighted=T, mode = \""+orientationMode+"\", diag=FALSE)"); // create graph from matrix |
|
176 |
rw.eval("V(g)$label <- V(g)$name"); // copy names in labels |
|
177 |
//rw.eval("g <- set.vertex.attribute(g, \"label\", colnames("+symbol+"))"); |
|
178 |
//rw.eval("g <- set.vertex.attribute(g, \"color\", value=colors)"); |
|
179 |
|
|
180 |
String path = outfile.getAbsolutePath();//.replaceAll("\\\\", "\\\\") |
|
181 |
path = path.replace('\\', '/'); |
|
182 |
rw.eval("write.graph(g, \""+path+"\", format = 'graphml')"); |
|
183 |
|
|
184 |
return outfile.exists(); |
|
185 |
} |
|
186 |
|
|
187 |
public Corpus getCorpus() { |
|
188 |
return corpus; |
|
189 |
} |
|
190 |
|
|
191 |
public int[][] getCoocs() { |
|
192 |
return coocs; |
|
193 |
} |
|
194 |
|
|
195 |
public ArrayList<Query> getQueries() { |
|
196 |
return queries; |
|
197 |
} |
|
198 |
|
|
199 |
public java.util.ArrayList<String> getNames() { |
|
200 |
return names; |
|
201 |
} |
|
202 |
|
|
203 |
public int getDist() { |
|
204 |
return dist; |
|
205 |
} |
|
206 |
|
|
207 |
public void printCoocs() { |
|
208 |
for (int i = 0 ; i < queries.size() ; i++) { |
|
209 |
System.out.print(names.get(i)); |
|
210 |
for (int j = 0 ; j < queries.size() ; j++) { |
|
211 |
System.out.print("\t"+coocs[i][j]); |
|
212 |
} |
|
213 |
System.out.println(); |
|
214 |
} |
|
215 |
} |
|
216 |
|
|
217 |
@Override |
|
218 |
public void clean() { |
|
219 |
// TODO Auto-generated method stub |
|
220 |
} |
|
221 |
|
|
222 |
public boolean toTxt(File outfile, String encoding, String colseparator, |
|
223 |
String txtseparator) throws Exception { |
|
224 |
return toGraphml(outfile); |
|
225 |
} |
|
226 |
|
|
227 |
public String toString() { |
|
228 |
return getName(); |
|
229 |
} |
|
230 |
|
|
231 |
@Override |
|
232 |
public CA toCA() throws StatException, CqiClientException { |
|
233 |
Corpus corpus = this.getCorpus(); |
|
234 |
Property analysisProperty = null; |
|
235 |
try { |
|
236 |
analysisProperty = this.getCorpus().getProperty("word"); //$NON-NLS-1$ |
|
237 |
} catch (CqiClientException e) { |
|
238 |
// TODO Auto-generated catch block |
|
239 |
org.txm.utils.logger.Log.printStackTrace(e); |
|
240 |
} |
|
241 |
LexicalTable table = this.toLexicalTable(); |
|
242 |
int fmin = table.getFmin(); |
|
243 |
|
|
244 |
CA ca = new CA(table); |
|
245 |
ca.setPartition(null); |
|
246 |
ca.setCorpus(corpus); |
|
247 |
ca.setFMin(fmin); |
|
248 |
ca.setAnalysisProperty(analysisProperty); |
|
249 |
|
|
250 |
return ca; |
|
251 |
} |
|
252 |
|
|
253 |
@Override |
|
254 |
public Partition getPartition() { |
|
255 |
return null; |
|
256 |
} |
|
257 |
|
|
258 |
@Override |
|
259 |
public boolean delete() { |
|
260 |
return corpus.removeResult(this); |
|
261 |
} |
|
262 |
|
|
263 |
/** The WordCloud adapter. */ |
|
264 |
private static IWorkbenchAdapter coocMatrixAdapter = new IWorkbenchAdapter() { |
|
265 |
|
|
266 |
@Override |
|
267 |
public Object[] getChildren(Object o) { |
|
268 |
return new Object[0]; |
|
269 |
} |
|
270 |
|
|
271 |
@Override |
|
272 |
public ImageDescriptor getImageDescriptor(Object object) { |
|
273 |
return IImageKeys.getImageDescriptor(IImageKeys.COOCMATRIX); |
|
274 |
} |
|
275 |
|
|
276 |
@Override |
|
277 |
public String getLabel(Object o) { |
|
278 |
return ((QueryAutoCooccurrence) o).getName(); |
|
279 |
} |
|
280 |
|
|
281 |
@Override |
|
282 |
public Object getParent(Object o) { |
|
283 |
if (((QueryAutoCooccurrence) o).getPartition() != null) { |
|
284 |
return ((QueryAutoCooccurrence) o).getPartition(); |
|
285 |
} else { |
|
286 |
return ((QueryAutoCooccurrence) o).getCorpus(); |
|
287 |
} |
|
288 |
} |
|
289 |
}; |
|
290 |
|
|
291 |
@Override |
|
292 |
public Object getAdapter(Class adapter) { |
|
293 |
return coocMatrixAdapter; |
|
294 |
} |
|
295 |
|
|
296 |
@Override |
|
297 |
public String getSimpleName() { |
|
298 |
return "QueryCooccurrence"; |
|
299 |
} |
|
300 |
|
|
301 |
@Override |
|
302 |
public String getDetails() { |
|
303 |
return queries.toString(); |
|
304 |
} |
|
305 |
} |
|
0 | 306 |
tmp/org.txm.querycooccurrences.rcp/src/org/txm/functions/coocmatrix/package.html (revision 478) | ||
---|---|---|
1 |
<html> |
|
2 |
<body> |
|
3 |
<p>Cooccurrence Matrix</p> |
|
4 |
</body> |
|
5 |
</html> |
|
0 | 6 |
tmp/org.txm.querycooccurrences.rcp/src/org/txm/functions/coocmatrix/QueryCooccurrence.java (revision 478) | ||
---|---|---|
1 |
package org.txm.functions.coocmatrix; |
|
2 |
|
|
3 |
import java.io.File; |
|
4 |
import java.util.ArrayList; |
|
5 |
|
|
6 |
import org.eclipse.core.runtime.IAdaptable; |
|
7 |
import org.eclipse.jface.resource.ImageDescriptor; |
|
8 |
import org.eclipse.ui.model.IWorkbenchAdapter; |
|
9 |
import org.txm.ca.core.functions.CA; |
|
10 |
import org.txm.ca.core.functions.ICAComputable; |
|
11 |
import org.txm.core.results.TXMResult; |
|
12 |
import org.txm.lexicaltable.core.functions.LexicalTable; |
|
13 |
import org.txm.lexicaltable.core.statsengine.r.data.LexicalTableImpl; |
|
14 |
import org.txm.rcp.IImageKeys; |
|
15 |
import org.txm.searchengine.cqp.clientExceptions.CqiClientException; |
|
16 |
import org.txm.searchengine.cqp.corpus.Corpus; |
|
17 |
import org.txm.searchengine.cqp.corpus.Partition; |
|
18 |
import org.txm.searchengine.cqp.corpus.Property; |
|
19 |
import org.txm.searchengine.cqp.corpus.QueryResult; |
|
20 |
import org.txm.searchengine.cqp.corpus.query.Query; |
|
21 |
import org.txm.statsengine.core.StatException; |
|
22 |
import org.txm.statsengine.r.core.RWorkspace; |
|
23 |
import org.txm.statsengine.r.core.exceptions.RWorkspaceException; |
|
24 |
import org.txm.utils.logger.Log; |
|
25 |
|
|
26 |
public class QueryCooccurrence extends TXMResult implements ICAComputable, IAdaptable { |
|
27 |
protected Corpus corpus; |
|
28 |
protected int[][] coocs; |
|
29 |
protected ArrayList<Query> queries1; |
|
30 |
protected ArrayList<String> names1; |
|
31 |
protected ArrayList<Query> queries2; |
|
32 |
protected ArrayList<String> names2; |
|
33 |
|
|
34 |
protected int dist; |
|
35 |
protected String struct = ""; |
|
36 |
protected String symbol; |
|
37 |
protected static int noCoocMatrix = 1; |
|
38 |
|
|
39 |
protected static String prefixR = "CoocMatrix"; |
|
40 |
|
|
41 |
protected int nNodes = 0; |
|
42 |
protected int nEdges = 0; |
|
43 |
protected int minCooc; |
|
44 |
|
|
45 |
public QueryCooccurrence(Corpus corpus, |
|
46 |
ArrayList<Query> queries1, ArrayList<String> names1, |
|
47 |
ArrayList<Query> queries2, ArrayList<String> names2, |
|
48 |
int dist, int minCooc, String struct) throws CqiClientException, RWorkspaceException { |
|
49 |
this.queries1 = queries1; |
|
50 |
this.names1 = names1; |
|
51 |
this.queries2 = queries2; |
|
52 |
this.names2 = names2; |
|
53 |
this.corpus = corpus; |
|
54 |
this.dist = dist; |
|
55 |
this.minCooc = minCooc; |
|
56 |
if (struct != null) |
|
57 |
this.struct = "within "+struct; |
|
58 |
else |
|
59 |
this.struct = "within text"; |
|
60 |
|
|
61 |
nNodes = queries1.size(); |
|
62 |
|
|
63 |
coocs = new int[queries1.size()][queries2.size()]; |
|
64 |
for (int i = 0 ; i < queries1.size() ; i++) { |
|
65 |
|
|
66 |
//if (oriented) j = 0; // if the graph is not oriented, we don't need to evaluate all queries |
|
67 |
for (int j = 0 ; j < queries2.size() ; j++) { |
|
68 |
String query = "("+queries1.get(i)+"[]{0,"+dist+"}"+queries2.get(j)+")|("+queries2.get(j)+"[]{0,"+dist+"}"+queries1.get(i)+") "+this.struct; |
|
69 |
|
|
70 |
Log.info("ListCooc: query "+query); |
|
71 |
QueryResult result = corpus.query(new Query(query), "tmp", false); |
|
72 |
coocs[i][j] = result.getNMatch(); |
|
73 |
if (coocs[i][j] >= minCooc) { |
|
74 |
nEdges++; |
|
75 |
} else { |
|
76 |
coocs[i][j] = 0; |
|
77 |
} |
|
78 |
result.drop(); |
|
79 |
} |
|
80 |
} |
|
81 |
} |
|
82 |
|
|
83 |
public String getName() { |
|
84 |
return corpus.getName()+ " d="+dist+ " min="+minCooc; |
|
85 |
} |
|
86 |
|
|
87 |
public Corpus getParent() { |
|
88 |
return corpus; |
|
89 |
} |
|
90 |
|
|
91 |
public String getSymbol() { |
|
92 |
return symbol; |
|
93 |
} |
|
94 |
|
|
95 |
public int getNNodes() { |
|
96 |
return nNodes; |
|
97 |
} |
|
98 |
|
|
99 |
public int getNEdges() { |
|
100 |
return nEdges; |
|
101 |
} |
|
102 |
|
|
103 |
public LexicalTable toLexicalTable() throws RWorkspaceException, CqiClientException { |
|
104 |
sendToR(); |
|
105 |
RWorkspace rw = RWorkspace.getRWorkspaceInstance(); |
|
106 |
rw.eval("LT<-"+this.getSymbol()); |
|
107 |
rw.eval("LT<-LT[rowSums(LT) != 0, ]"); |
|
108 |
rw.eval("LT<-LT[, colSums(LT) != 0]"); |
|
109 |
rw.eval(this.getSymbol()+"LT<-LT"); |
|
110 |
rw.eval("rm(LT)"); |
|
111 |
LexicalTable table = new LexicalTable(corpus, corpus.getProperty("word"), new LexicalTableImpl(this.getSymbol()+"LT")); |
|
112 |
return table; |
|
113 |
} |
|
114 |
|
|
115 |
private void sendToR() throws RWorkspaceException { |
|
116 |
if (symbol != null) return; |
|
117 |
|
|
118 |
this.symbol = prefixR+(QueryCooccurrence.noCoocMatrix ++); |
|
119 |
RWorkspace rw = RWorkspace.getRWorkspaceInstance(); |
|
120 |
rw.eval("rm("+symbol+")"); |
|
121 |
rw.addMatrixToWorkspace(symbol, coocs); |
|
122 |
rw.addVectorToWorkspace("cooclabels", names1.toArray(new String[names1.size()])); |
|
123 |
rw.eval("rownames("+symbol+") <- cooclabels"); |
|
124 |
rw.eval("colnames("+symbol+") <- cooclabels"); |
|
125 |
} |
|
126 |
|
|
127 |
public Corpus getCorpus() { |
|
128 |
return corpus; |
|
129 |
} |
|
130 |
|
|
131 |
public int[][] getCoocs() { |
|
132 |
return coocs; |
|
133 |
} |
|
134 |
|
|
135 |
public ArrayList<Query> getQueries() { |
|
136 |
return queries1; |
|
137 |
} |
|
138 |
|
|
139 |
public java.util.ArrayList<String> getNames() { |
|
140 |
return names1; |
|
141 |
} |
|
142 |
|
|
143 |
public int getDist() { |
|
144 |
return dist; |
|
145 |
} |
|
146 |
|
|
147 |
public void printCoocs() { |
|
148 |
for (int i = 0 ; i < queries1.size() ; i++) { |
|
149 |
System.out.print(names1.get(i)); |
|
150 |
for (int j = 0 ; j < queries1.size() ; j++) { |
|
151 |
System.out.print("\t"+coocs[i][j]); |
|
152 |
} |
|
153 |
System.out.println(); |
|
154 |
} |
|
155 |
} |
|
156 |
|
|
157 |
@Override |
|
158 |
public void clean() { |
|
159 |
// TODO Auto-generated method stub |
|
160 |
} |
|
161 |
|
|
162 |
public boolean toTxt(File outfile, String encoding, String colseparator, |
|
163 |
String txtseparator) throws Exception { |
|
164 |
return false; |
|
165 |
} |
|
166 |
|
|
167 |
public String toString() { |
|
168 |
return getName(); |
|
169 |
} |
|
170 |
|
|
171 |
@Override |
|
172 |
public CA toCA() throws StatException, CqiClientException { |
|
173 |
Corpus corpus = this.getCorpus(); |
|
174 |
Property analysisProperty = null; |
|
175 |
try { |
|
176 |
analysisProperty = this.getCorpus().getProperty("word"); //$NON-NLS-1$ |
|
177 |
} catch (CqiClientException e) { |
|
178 |
// TODO Auto-generated catch block |
|
179 |
org.txm.utils.logger.Log.printStackTrace(e); |
|
180 |
} |
|
181 |
LexicalTable table = this.toLexicalTable(); |
|
182 |
int fmin = table.getFmin(); |
|
183 |
|
|
184 |
CA ca = new CA(table); |
|
185 |
ca.setPartition(null); |
|
186 |
ca.setCorpus(corpus); |
|
187 |
ca.setFMin(fmin); |
|
188 |
ca.setAnalysisProperty(analysisProperty); |
|
189 |
|
|
190 |
return ca; |
|
191 |
} |
|
192 |
|
|
193 |
@Override |
|
194 |
public Partition getPartition() { |
|
195 |
return null; |
|
196 |
} |
|
197 |
|
|
198 |
@Override |
|
199 |
public boolean delete() { |
|
200 |
return corpus.removeResult(this); |
|
201 |
} |
|
202 |
|
|
203 |
public String[] getExportTXTExtensions() { |
|
204 |
return new String[]{"*.txt"}; |
|
205 |
} |
|
206 |
|
|
207 |
|
|
208 |
/** The WordCloud adapter. */ |
|
209 |
private static IWorkbenchAdapter coocMatrixAdapter = new IWorkbenchAdapter() { |
|
210 |
|
|
211 |
@Override |
|
212 |
public Object[] getChildren(Object o) { |
|
213 |
return new Object[0]; |
|
214 |
} |
|
215 |
|
|
216 |
@Override |
|
217 |
public ImageDescriptor getImageDescriptor(Object object) { |
|
218 |
return IImageKeys.getImageDescriptor(IImageKeys.COOCMATRIX); |
|
219 |
} |
|
220 |
|
|
221 |
@Override |
|
222 |
public String getLabel(Object o) { |
|
223 |
return ((QueryCooccurrence) o).getName(); |
|
224 |
} |
|
225 |
|
|
226 |
@Override |
|
227 |
public Object getParent(Object o) { |
|
228 |
if (((QueryCooccurrence) o).getPartition() != null) { |
|
229 |
return ((QueryCooccurrence) o).getPartition(); |
|
230 |
} else { |
|
231 |
return ((QueryCooccurrence) o).getCorpus(); |
|
232 |
} |
|
233 |
} |
|
234 |
}; |
|
235 |
|
|
236 |
@Override |
|
237 |
public Object getAdapter(Class adapter) { |
|
238 |
return coocMatrixAdapter; |
|
239 |
} |
|
240 |
|
|
241 |
@Override |
|
242 |
public String getSimpleName() { |
|
243 |
return "QueryCooccurrence"; |
|
244 |
} |
|
245 |
|
|
246 |
@Override |
|
247 |
public String getDetails() { |
|
248 |
return queries1.toString()+ " "+queries2.toString(); |
|
249 |
} |
|
250 |
} |
|
0 | 251 |
tmp/org.txm.querycooccurrences.rcp/src/org/txm/rcpapplication/commands/function/ComputeQueryAutoCooccurrence.java (revision 478) | ||
---|---|---|
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:$ |
|
25 |
// $LastChangedRevision:$ |
|
26 |
// $LastChangedBy:$ |
|
27 |
// |
|
28 |
package org.txm.rcpapplication.commands.function; |
|
29 |
|
|
30 |
import java.io.File; |
|
31 |
import java.util.ArrayList; |
|
32 |
|
|
33 |
import org.eclipse.core.commands.AbstractHandler; |
|
34 |
import org.eclipse.core.commands.ExecutionEvent; |
|
35 |
import org.eclipse.core.commands.ExecutionException; |
|
36 |
import org.eclipse.core.runtime.IProgressMonitor; |
|
37 |
import org.eclipse.core.runtime.IStatus; |
|
38 |
import org.eclipse.core.runtime.Status; |
|
39 |
import org.eclipse.jface.dialogs.Dialog; |
|
40 |
import org.eclipse.jface.viewers.IStructuredSelection; |
|
41 |
import org.eclipse.jface.window.Window; |
|
42 |
import org.eclipse.swt.SWT; |
|
43 |
import org.eclipse.swt.events.SelectionEvent; |
|
44 |
import org.eclipse.swt.events.SelectionListener; |
|
45 |
import org.eclipse.swt.graphics.GC; |
|
46 |
import org.eclipse.swt.layout.GridData; |
|
47 |
import org.eclipse.swt.layout.GridLayout; |
|
48 |
import org.eclipse.swt.widgets.Button; |
|
49 |
import org.eclipse.swt.widgets.Composite; |
|
50 |
import org.eclipse.swt.widgets.Control; |
|
51 |
import org.eclipse.swt.widgets.FileDialog; |
|
52 |
import org.eclipse.swt.widgets.Label; |
|
53 |
import org.eclipse.swt.widgets.Shell; |
|
54 |
import org.eclipse.swt.widgets.Spinner; |
|
55 |
import org.eclipse.ui.handlers.HandlerUtil; |
|
56 |
import org.txm.functions.coocmatrix.QueryAutoCooccurrence; |
|
57 |
import org.txm.functions.queryindex.QueryIndex; |
|
58 |
import org.txm.functions.queryindex.QueryIndexLine; |
|
59 |
import org.txm.rcp.JobsTimer; |
|
60 |
import org.txm.rcp.Messages; |
|
61 |
import org.txm.rcp.utils.JobHandler; |
|
62 |
import org.txm.rcp.views.QueriesView; |
|
63 |
import org.txm.rcp.views.corpora.CorporaView; |
|
64 |
import org.txm.searchengine.cqp.CQPEngine; |
|
65 |
import org.txm.searchengine.cqp.clientExceptions.CqiClientException; |
|
66 |
import org.txm.searchengine.cqp.corpus.Corpus; |
|
67 |
import org.txm.searchengine.cqp.corpus.StructuralUnit; |
|
68 |
import org.txm.searchengine.cqp.corpus.query.Query; |
|
69 |
import org.txm.stat.utils.ConsoleProgressBar; |
|
70 |
import org.txm.statsengine.r.rcp.views.RVariablesView; |
|
71 |
//import org.txm.functions.queryindex.*; |
|
72 |
import org.txm.utils.logger.Log; |
|
73 |
|
|
74 |
/** |
|
75 |
* if the selection is a corpus, open an empty Cooc editor if the selection is a |
|
76 |
* concordance, open a Cooc editor already computed @ author mdecorde. |
|
77 |
*/ |
|
78 |
public class ComputeQueryAutoCooccurrence extends AbstractHandler { |
|
79 |
|
|
80 |
float reqtime = 0.5f; |
|
81 |
|
|
82 |
/* (non-Javadoc) |
|
83 |
* @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent) |
|
84 |
*/ |
|
85 |
@Override |
|
86 |
public Object execute(ExecutionEvent event) throws ExecutionException { |
|
87 |
IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getCurrentSelection(event); |
|
88 |
final Object element = selection.getFirstElement(); |
|
89 |
|
|
90 |
final Shell shell = HandlerUtil.getActiveWorkbenchWindowChecked(event) |
|
91 |
.getShell(); |
|
92 |
|
|
93 |
JobHandler jobhandler = new JobHandler("Computing cooccurrences matrix") { |
|
94 |
@Override |
|
95 |
protected IStatus run(IProgressMonitor monitor) { |
|
96 |
this.runInit(monitor); |
|
97 |
try { |
|
98 |
JobsTimer.start(); |
|
99 |
|
|
100 |
if (!(element instanceof QueryIndex)) { |
|
101 |
System.out.println(Messages.ComputeCooccurrences_1+ element); |
|
102 |
return Status.CANCEL_STATUS; |
|
103 |
} |
|
104 |
QueryIndex qi = ((QueryIndex)element); |
|
105 |
Corpus corpus = qi.getCorpus(); |
|
106 |
|
|
107 |
final CoocMatrixDialog dialog = new ComputeQueryAutoCooccurrence.CoocMatrixDialog(shell, corpus); |
|
108 |
syncExec(new Runnable() { |
|
109 |
@Override |
|
110 |
public void run() { |
|
111 |
dialog.open(); |
|
112 |
} |
|
113 |
}); |
|
114 |
|
|
115 |
if (dialog.getReturnCode() != Window.OK) System.out.println("Canceled"); |
|
116 |
|
|
117 |
this.acquireSemaphore(); |
|
118 |
final QueryAutoCooccurrence cooc = computeCooc(shell, corpus, (QueryIndex)element, dialog); |
|
119 |
this.releaseSemaphore(); |
|
120 |
|
|
121 |
if (cooc == null) return null; |
|
122 |
|
|
123 |
corpus.addResult(cooc); |
|
124 |
System.out.println(Messages.ComputeCoocMatrix_0+cooc.getSymbol()); |
|
125 |
|
|
126 |
syncExec(new Runnable() { |
|
127 |
@Override |
|
128 |
public void run() { |
|
129 |
CorporaView.refresh(); |
|
130 |
CorporaView.expand(cooc.getCorpus()); |
|
131 |
QueriesView.refresh(); |
|
132 |
RVariablesView.refresh(); |
|
133 |
} |
|
134 |
}); |
|
135 |
} catch (ThreadDeath td) { |
|
136 |
return Status.CANCEL_STATUS; |
|
137 |
} catch (Exception e) { |
|
138 |
Log.printStackTrace(e); |
|
139 |
try { System.out.println(Messages.LastCQPError+CQPEngine.getCqiClient().getLastCQPError()); |
|
140 |
} catch (Exception e1) { e1.printStackTrace(); } |
|
141 |
} finally { |
|
142 |
monitor.done(); |
|
143 |
JobsTimer.stopAndPrint(); |
|
144 |
} |
|
145 |
return Status.OK_STATUS; |
|
146 |
} |
|
147 |
}; |
|
148 |
jobhandler.startJob(); |
|
149 |
return null; |
|
150 |
} |
|
151 |
|
|
152 |
|
|
153 |
private QueryAutoCooccurrence computeCooc(Shell shell, Corpus corpus, QueryIndex element, CoocMatrixDialog dialog) { |
|
154 |
ArrayList<Query> queries = new ArrayList<Query>(); |
|
155 |
ArrayList<String> names = new ArrayList<String>(); |
|
156 |
|
|
157 |
String struct = dialog.getStruct(); |
|
158 |
int dist = dialog.getDist(); |
|
159 |
int min = dialog.getMin(); |
|
160 |
File outfile = dialog.getOutFile(); |
|
161 |
boolean oriented = dialog.getOriented(); |
|
162 |
|
|
163 |
for (QueryIndexLine line : element.getLines()) { |
|
164 |
queries.add(line.getQuery()); |
|
165 |
names.add(line.getName()); |
|
166 |
} |
|
167 |
|
|
168 |
System.out.println(Messages.ProgressionDialog_8+queries); |
|
169 |
System.out.println(Messages.MergeCols_5+names); |
|
170 |
|
|
171 |
QueryAutoCooccurrence cooc = null; |
|
172 |
try { |
|
173 |
|
|
174 |
int nqueries = (queries.size()*queries.size()/2); |
|
175 |
System.out.println("Number of queries: "+nqueries); |
|
176 |
System.out.println("Estimated time: "+(nqueries*reqtime/1000)+" secs"); |
|
177 |
cooc = new QueryAutoCooccurrence(corpus, queries, names, dist, min, struct, oriented, new ConsoleProgressBar(nqueries)); |
|
178 |
System.out.println(Messages.ComputeCoocMatrix_1+cooc.getNNodes()); |
|
179 |
System.out.println(Messages.ComputeCoocMatrix_2+cooc.getNEdges()); |
|
180 |
System.out.println(Messages.ComputeCoocMatrix_3+dist); |
|
181 |
|
|
182 |
if (outfile != null) { |
|
183 |
cooc.toGraphml(outfile); |
|
184 |
System.out.println(Messages.ComputeCoocMatrix_4+outfile); |
|
185 |
} |
|
186 |
|
|
187 |
} catch (Exception e) { |
|
188 |
Log.printStackTrace(e); |
|
189 |
try { System.out.println(Messages.LastCQPError+CQPEngine.getCqiClient().getLastCQPError()); |
|
190 |
} catch (Exception e1) { e1.printStackTrace(); } |
|
191 |
} |
|
192 |
return cooc; |
|
193 |
} |
|
194 |
|
|
195 |
public class CoocMatrixDialog extends Dialog { |
|
196 |
Corpus corpus; |
|
197 |
|
|
198 |
org.eclipse.swt.widgets.List propsCombo; |
|
199 |
Spinner distSpinner; |
|
200 |
Spinner minSpinner; |
|
201 |
Label outfileLabel; |
|
202 |
|
|
203 |
File outfile; |
|
204 |
int dist; |
|
205 |
int min; |
|
206 |
String struct; |
|
207 |
private boolean oriented; |
|
208 |
|
|
209 |
private Button orientedButton; |
|
210 |
|
|
211 |
public CoocMatrixDialog(Shell parentShell, Corpus corpus) { |
|
212 |
super(parentShell); |
|
213 |
this.setShellStyle(this.getShellStyle() | SWT.RESIZE); |
|
214 |
this.corpus = corpus; |
|
215 |
} |
|
216 |
|
|
217 |
|
|
218 |
public boolean getOriented() { |
|
219 |
return oriented; |
|
220 |
} |
|
221 |
|
|
222 |
|
|
223 |
/* (non-Javadoc) |
|
224 |
* @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell) |
|
225 |
*/ |
|
226 |
@Override |
|
227 |
protected void configureShell(Shell newShell) { |
|
228 |
super.configureShell(newShell); |
|
229 |
newShell.setText(Messages.ComputeCoocMatrix_7); |
|
230 |
} |
|
231 |
|
|
232 |
/* (non-Javadoc) |
|
233 |
* @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite) |
|
234 |
*/ |
|
235 |
@Override |
|
236 |
protected Control createDialogArea(Composite parent) { |
|
237 |
Composite composite = parent; //new Composite(parent, SWT.NONE); |
|
238 |
// TODO : we just put a composite into another composite. Everything |
|
239 |
// should be directly put into parent |
|
240 |
GridLayout layout = new GridLayout(2, false); |
|
241 |
composite.setLayout(layout); |
|
242 |
|
|
243 |
Label anaPropLabel = new Label(composite, SWT.NONE); |
|
244 |
anaPropLabel.setText(Messages.ParaBrowserEditor_5); |
|
245 |
anaPropLabel.setLayoutData(new GridData(GridData.END, GridData.CENTER, false, false)); |
|
246 |
|
|
247 |
propsCombo = new org.eclipse.swt.widgets.List(composite, SWT.READ_ONLY); |
|
248 |
GridData gdata = new GridData(GridData.FILL, GridData.FILL, true, false); |
|
249 |
propsCombo.setLayoutData(gdata); |
|
250 |
GC gc = new GC(propsCombo); |
|
251 |
gc.setFont(this.propsCombo.getFont()); |
|
252 |
gdata.heightHint = gc.getFontMetrics().getHeight() * 5; |
|
253 |
gc.dispose(); |
|
254 |
propsCombo.add(""); |
|
255 |
try { |
|
256 |
for (StructuralUnit p : corpus.getStructuralUnits()) { |
|
257 |
if (!("text".equals(p.getName()) || //$NON-NLS-1$ |
|
258 |
"txmcorpus".equals(p.getName()))) //$NON-NLS-1$ |
|
259 |
propsCombo.add(p.getName()); |
|
260 |
} |
|
261 |
} catch (CqiClientException e) { |
|
262 |
Log.printStackTrace(e); |
|
263 |
return parent; |
|
264 |
} |
|
265 |
|
|
266 |
Label distLabel = new Label(composite, SWT.NONE); |
|
267 |
distLabel.setText(Messages.ComputeCoocMatrix_10); |
|
268 |
distLabel.setLayoutData(new GridData(GridData.END, GridData.CENTER, |
|
269 |
false, false)); |
|
270 |
distSpinner = new Spinner(composite, SWT.BORDER); |
|
271 |
distSpinner.setMinimum(0); |
|
272 |
distSpinner.setMaximum(1000); |
|
273 |
distSpinner.setSelection(10); |
|
274 |
|
|
275 |
Label minLabel = new Label(composite, SWT.NONE); |
|
276 |
minLabel.setText(Messages.ComputeCoocMatrix_11); |
|
277 |
minLabel.setLayoutData(new GridData(GridData.END, GridData.CENTER, |
|
278 |
false, false)); |
|
279 |
minSpinner = new Spinner(composite, SWT.BORDER); |
|
280 |
minSpinner.setMinimum(1); |
|
281 |
minSpinner.setMaximum(1000); |
|
282 |
minSpinner.setSelection(20); |
|
283 |
|
|
284 |
Label orientedLabel = new Label(composite, SWT.NONE); |
|
285 |
orientedLabel.setText("Oriented"); |
|
286 |
orientedLabel.setLayoutData(new GridData(GridData.END, GridData.CENTER, |
|
287 |
false, false)); |
|
288 |
orientedButton = new Button(composite, SWT.CHECK); |
|
289 |
orientedButton.setSelection(false); |
|
290 |
|
|
291 |
outfileLabel = new Label(composite, SWT.NONE); |
|
292 |
outfileLabel.setText(Messages.ComputeCoocMatrix_12); |
|
293 |
outfileLabel.setLayoutData(new GridData(GridData.END, GridData.CENTER, |
|
294 |
false, false)); |
|
295 |
|
|
296 |
Button outfileButton = new Button(composite, SWT.PUSH); |
|
297 |
outfileButton.setText(Messages.ComputeCoocMatrix_13); |
|
298 |
outfileButton.addSelectionListener(new SelectionListener() { |
|
299 |
@Override |
|
300 |
public void widgetSelected(SelectionEvent e) { |
|
301 |
FileDialog dialog = new FileDialog(e.display.getActiveShell(), SWT.SAVE); |
|
302 |
String extensions[] = {"*.graphml"}; //$NON-NLS-1$ |
|
303 |
dialog.setFilterExtensions(extensions); |
|
304 |
String path = dialog.open(); |
|
305 |
if (path != null) { |
|
306 |
outfile = new File(path); |
|
307 |
if (!(outfile.getName().toLowerCase().endsWith(".graphml"))) { //$NON-NLS-1$ |
|
308 |
outfile = new File(path+".graphml"); //$NON-NLS-1$ |
|
309 |
} |
|
310 |
outfileLabel.setText(outfile.getAbsolutePath()); |
|
311 |
} |
|
312 |
} |
|
313 |
|
|
314 |
@Override |
|
315 |
public void widgetDefaultSelected(SelectionEvent e) { } |
|
316 |
}); |
|
317 |
|
|
318 |
return parent; |
|
319 |
} |
|
320 |
|
|
321 |
public File getOutFile() { |
|
322 |
return outfile; |
|
323 |
} |
|
324 |
|
|
325 |
public int getDist() { |
|
326 |
return dist; |
|
327 |
} |
|
328 |
|
|
329 |
public int getMin() { |
|
330 |
return min; |
|
331 |
} |
|
332 |
|
|
333 |
public String getStruct() { |
|
334 |
return struct; |
|
335 |
} |
|
336 |
|
|
337 |
/* (non-Javadoc) |
|
338 |
* @see org.eclipse.jface.dialogs.Dialog#okPressed() |
|
339 |
*/ |
|
340 |
@Override |
|
341 |
protected void okPressed() { |
|
342 |
dist = distSpinner.getSelection(); |
|
343 |
min = minSpinner.getSelection(); |
|
344 |
struct = null; |
|
345 |
oriented = orientedButton.getSelection(); |
|
346 |
for (int i : propsCombo.getSelectionIndices()) { |
|
347 |
String sname = propsCombo.getItem(i); |
|
348 |
if (sname != null && sname.length() > 0) { |
|
349 |
struct = sname; |
|
350 |
break; |
|
351 |
} |
|
352 |
} |
|
353 |
super.okPressed(); |
|
354 |
} |
|
355 |
} |
|
356 |
} |
|
0 | 357 |
tmp/org.txm.querycooccurrences.rcp/src/org/txm/rcpapplication/commands/function/ComputeQueryCooccurrence.java (revision 478) | ||
---|---|---|
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:$ |
|
25 |
// $LastChangedRevision:$ |
|
26 |
// $LastChangedBy:$ |
|
27 |
// |
|
28 |
package org.txm.rcpapplication.commands.function; |
|
29 |
|
|
30 |
import java.io.BufferedReader; |
|
31 |
import java.io.File; |
|
32 |
import java.io.FileInputStream; |
|
33 |
import java.io.IOException; |
|
34 |
import java.io.InputStreamReader; |
|
35 |
import java.util.ArrayList; |
|
36 |
|
|
37 |
import org.eclipse.core.commands.AbstractHandler; |
|
38 |
import org.eclipse.core.commands.ExecutionEvent; |
|
39 |
import org.eclipse.core.commands.ExecutionException; |
|
40 |
import org.eclipse.core.runtime.IProgressMonitor; |
|
41 |
import org.eclipse.core.runtime.IStatus; |
|
42 |
import org.eclipse.core.runtime.Status; |
|
43 |
import org.eclipse.jface.dialogs.Dialog; |
|
44 |
import org.eclipse.jface.viewers.IStructuredSelection; |
|
45 |
import org.eclipse.jface.window.Window; |
|
46 |
import org.eclipse.swt.SWT; |
|
47 |
import org.eclipse.swt.events.SelectionEvent; |
|
48 |
import org.eclipse.swt.events.SelectionListener; |
|
49 |
import org.eclipse.swt.graphics.GC; |
|
50 |
import org.eclipse.swt.layout.GridData; |
|
51 |
import org.eclipse.swt.layout.GridLayout; |
|
52 |
import org.eclipse.swt.widgets.Button; |
|
53 |
import org.eclipse.swt.widgets.Composite; |
|
54 |
import org.eclipse.swt.widgets.Control; |
|
55 |
import org.eclipse.swt.widgets.FileDialog; |
|
56 |
import org.eclipse.swt.widgets.Label; |
|
57 |
import org.eclipse.swt.widgets.Shell; |
|
58 |
import org.eclipse.swt.widgets.Spinner; |
|
59 |
import org.eclipse.ui.handlers.HandlerUtil; |
|
60 |
import org.txm.functions.coocmatrix.QueryCooccurrence; |
|
61 |
//import org.txm.functions.queryindex.*; |
|
62 |
import org.txm.rcp.JobsTimer; |
|
63 |
import org.txm.rcp.Messages; |
|
64 |
import org.txm.rcp.utils.JobHandler; |
|
65 |
import org.txm.rcp.views.QueriesView; |
|
66 |
import org.txm.rcp.views.corpora.CorporaView; |
|
67 |
import org.txm.searchengine.cqp.CQPEngine; |
|
68 |
import org.txm.searchengine.cqp.clientExceptions.CqiClientException; |
|
69 |
import org.txm.searchengine.cqp.corpus.Corpus; |
|
70 |
import org.txm.searchengine.cqp.corpus.StructuralUnit; |
|
71 |
import org.txm.searchengine.cqp.corpus.query.Query; |
|
72 |
import org.txm.statsengine.r.rcp.views.RVariablesView; |
|
73 |
import org.txm.utils.logger.Log; |
|
74 |
|
|
75 |
// TODO: Auto-generated Javadoc |
|
76 |
/** |
|
77 |
* if the selection is a corpus, open an empty Cooc editor if the selection is a |
|
78 |
* concordance, open a Cooc editor already computed @ author mdecorde. |
|
79 |
*/ |
|
80 |
public class ComputeQueryCooccurrence extends AbstractHandler { |
|
81 |
|
|
82 |
float reqtime = 0.5f; |
|
83 |
|
|
84 |
/* (non-Javadoc) |
|
85 |
* @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent) |
|
86 |
*/ |
|
87 |
@Override |
|
88 |
public Object execute(ExecutionEvent event) throws ExecutionException { |
|
89 |
IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getCurrentSelection(event); |
|
90 |
final Object element = selection.getFirstElement(); |
|
91 |
|
|
92 |
final Shell shell = HandlerUtil.getActiveWorkbenchWindowChecked(event) |
|
93 |
.getShell(); |
|
94 |
|
|
95 |
JobHandler jobhandler = new JobHandler("Computing cooccurrences matrix") { |
|
96 |
@Override |
|
97 |
protected IStatus run(IProgressMonitor monitor) { |
|
98 |
this.runInit(monitor); |
|
99 |
try { |
|
100 |
JobsTimer.start(); |
|
101 |
|
|
102 |
if (!(element instanceof Corpus)) { |
|
103 |
System.out.println(Messages.ComputeCooccurrences_1+ element); |
|
104 |
return Status.CANCEL_STATUS; |
|
105 |
} |
|
106 |
Corpus corpus = ((Corpus)element); |
|
107 |
|
|
108 |
final CoocMatrixDialog dialog = new ComputeQueryCooccurrence.CoocMatrixDialog(shell, corpus); |
|
109 |
syncExec(new Runnable() { |
|
110 |
@Override |
|
111 |
public void run() { |
|
112 |
dialog.open(); |
|
113 |
} |
|
114 |
}); |
|
115 |
|
|
116 |
if (dialog.getReturnCode() != Window.OK) System.out.println("Canceled"); |
|
117 |
|
|
118 |
this.acquireSemaphore(); |
|
119 |
final QueryCooccurrence cooc = computeCooc(shell, corpus, dialog); |
|
120 |
this.releaseSemaphore(); |
|
121 |
|
|
122 |
if (cooc == null) return null; |
|
123 |
|
|
124 |
corpus.addResult(cooc); |
|
125 |
System.out.println(Messages.ComputeCoocMatrix_0+cooc.getSymbol()); |
|
126 |
|
|
127 |
syncExec(new Runnable() { |
|
128 |
@Override |
|
129 |
public void run() { |
|
130 |
CorporaView.refresh(); |
|
131 |
CorporaView.expand(cooc.getCorpus()); |
|
132 |
QueriesView.refresh(); |
|
133 |
RVariablesView.refresh(); |
|
134 |
} |
|
135 |
}); |
|
136 |
} catch (ThreadDeath td) { |
|
137 |
return Status.CANCEL_STATUS; |
|
138 |
} catch (Exception e) { |
|
139 |
Log.printStackTrace(e); |
|
140 |
try { System.out.println(Messages.LastCQPError+CQPEngine.getCqiClient().getLastCQPError()); |
|
141 |
} catch (Exception e1) { e1.printStackTrace(); } |
|
142 |
} finally { |
|
143 |
monitor.done(); |
|
144 |
JobsTimer.stopAndPrint(); |
|
145 |
} |
|
146 |
return Status.OK_STATUS; |
|
147 |
} |
|
148 |
}; |
|
149 |
jobhandler.startJob(); |
|
150 |
return null; |
|
151 |
} |
|
152 |
|
|
153 |
|
|
154 |
private QueryCooccurrence computeCooc(Shell shell, Corpus corpus, CoocMatrixDialog dialog) throws IOException { |
|
155 |
ArrayList<Query> queries1 = new ArrayList<Query>(); |
|
156 |
ArrayList<String> names1 = new ArrayList<String>(); |
|
157 |
ArrayList<Query> queries2 = new ArrayList<Query>(); |
|
158 |
ArrayList<String> names2 = new ArrayList<String>(); |
|
159 |
|
|
160 |
String struct = dialog.getStruct(); |
|
161 |
int dist = dialog.getDist(); |
|
162 |
int min = dialog.getMin(); |
|
163 |
getQueriesAndNames(dialog.getQueryFile1(), queries1, names1); |
|
164 |
getQueriesAndNames(dialog.getQueryFile2(), queries2, names2); |
|
165 |
|
|
166 |
System.out.println(Messages.ProgressionDialog_8+queries1); |
|
167 |
System.out.println(Messages.MergeCols_5+names1); |
|
168 |
|
|
169 |
QueryCooccurrence cooc = null; |
|
170 |
try { |
|
171 |
int nqueries = (queries1.size()*queries2.size()); |
|
172 |
System.out.println("Number of queries: "+nqueries); |
|
173 |
System.out.println("Estimated time: "+(nqueries*reqtime/1000)+" secs"); |
|
174 |
cooc = new QueryCooccurrence(corpus, queries1, names1, queries2, names2, dist, min, struct); |
|
175 |
System.out.println(Messages.ComputeCoocMatrix_1+cooc.getNNodes()); |
|
176 |
System.out.println(Messages.ComputeCoocMatrix_2+cooc.getNEdges()); |
|
177 |
System.out.println(Messages.ComputeCoocMatrix_3+dist); |
|
178 |
} catch (Exception e) { |
|
179 |
// TODO Auto-generated catch block |
|
180 |
Log.printStackTrace(e); |
|
181 |
try { System.out.println(Messages.LastCQPError+CQPEngine.getCqiClient().getLastCQPError()); |
|
182 |
} catch (Exception e1) { e1.printStackTrace(); } |
|
183 |
} |
|
184 |
return cooc; |
|
185 |
} |
|
186 |
|
|
187 |
private void getQueriesAndNames(File propFile, ArrayList<Query> queries, |
|
188 |
ArrayList<String> names) throws IOException { |
|
189 |
BufferedReader reader = new BufferedReader(new InputStreamReader( |
|
190 |
new FileInputStream(propFile), "UTF-8")); //$NON-NLS-1$ |
|
191 |
|
|
192 |
String line = reader.readLine(); |
|
193 |
while (line != null) { |
|
194 |
String[] split = line.split("=", 2); //$NON-NLS-1$ |
|
195 |
if (split.length == 2) { |
|
196 |
queries.add(new Query(split[1])); |
|
197 |
names.add(split[0]); |
|
198 |
} |
|
199 |
|
|
200 |
line = reader.readLine(); |
|
201 |
} |
|
202 |
reader.close(); |
|
203 |
} |
|
204 |
|
|
205 |
public class CoocMatrixDialog extends Dialog { |
|
206 |
Corpus corpus; |
|
207 |
|
|
208 |
org.eclipse.swt.widgets.List propsCombo; |
|
209 |
Spinner distSpinner; |
|
210 |
Spinner minSpinner; |
|
211 |
Label queriesFile1Label; |
|
212 |
Label queriesFile2Label; |
|
213 |
|
|
214 |
File queriesFile1; |
|
215 |
File queriesFile2; |
|
216 |
int dist; |
|
217 |
int min; |
|
218 |
String struct; |
|
219 |
private boolean oriented; |
|
220 |
|
|
221 |
private Button orientedButton; |
|
222 |
|
|
223 |
|
|
224 |
|
|
225 |
public CoocMatrixDialog(Shell parentShell, Corpus corpus) { |
|
226 |
super(parentShell); |
|
227 |
this.setShellStyle(this.getShellStyle() | SWT.RESIZE); |
|
228 |
this.corpus = corpus; |
|
229 |
} |
|
230 |
|
|
231 |
|
|
232 |
public boolean getOriented() { |
|
233 |
return oriented; |
|
234 |
} |
|
235 |
|
|
236 |
|
|
237 |
/* (non-Javadoc) |
|
238 |
* @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell) |
|
239 |
*/ |
|
240 |
@Override |
|
241 |
protected void configureShell(Shell newShell) { |
|
242 |
super.configureShell(newShell); |
|
243 |
newShell.setText(Messages.ComputeCoocMatrix_7); |
|
244 |
} |
|
245 |
|
|
246 |
/* (non-Javadoc) |
|
247 |
* @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite) |
|
248 |
*/ |
|
249 |
@Override |
|
250 |
protected Control createDialogArea(Composite parent) { |
|
251 |
Composite composite = parent; //new Composite(parent, SWT.NONE); |
|
252 |
// TODO : we just put a composite into another composite. Everything |
|
253 |
// should be directly put into parent |
|
254 |
GridLayout layout = new GridLayout(2, false); |
|
255 |
composite.setLayout(layout); |
|
256 |
|
|
257 |
Label anaPropLabel = new Label(composite, SWT.NONE); |
|
258 |
anaPropLabel.setText(Messages.ParaBrowserEditor_5); |
|
259 |
anaPropLabel.setLayoutData(new GridData(GridData.END, GridData.CENTER, false, false)); |
|
260 |
|
|
261 |
propsCombo = new org.eclipse.swt.widgets.List(composite, SWT.READ_ONLY); |
|
262 |
GridData gdata = new GridData(GridData.FILL, GridData.FILL, true, false); |
|
263 |
propsCombo.setLayoutData(gdata); |
|
264 |
GC gc = new GC(propsCombo); |
|
265 |
gc.setFont(this.propsCombo.getFont()); |
|
266 |
gdata.heightHint = gc.getFontMetrics().getHeight() * 5; |
|
267 |
gc.dispose(); |
|
268 |
propsCombo.add(""); |
|
269 |
try { |
|
270 |
for (StructuralUnit p : corpus.getStructuralUnits()) { |
|
271 |
if (!("text".equals(p.getName()) || //$NON-NLS-1$ |
|
272 |
"txmcorpus".equals(p.getName()))) //$NON-NLS-1$ |
|
273 |
propsCombo.add(p.getName()); |
|
274 |
} |
|
275 |
} catch (CqiClientException e) { |
|
276 |
Log.printStackTrace(e); |
|
277 |
return parent; |
|
278 |
} |
|
279 |
|
|
280 |
Label distLabel = new Label(composite, SWT.NONE); |
|
281 |
distLabel.setText(Messages.ComputeCoocMatrix_10); |
|
282 |
distLabel.setLayoutData(new GridData(GridData.END, GridData.CENTER, |
|
283 |
false, false)); |
|
284 |
distSpinner = new Spinner(composite, SWT.BORDER); |
|
285 |
distSpinner.setMinimum(0); |
|
286 |
distSpinner.setMaximum(1000); |
|
287 |
distSpinner.setSelection(10); |
|
288 |
|
|
289 |
Label minLabel = new Label(composite, SWT.NONE); |
|
290 |
minLabel.setText(Messages.ComputeCoocMatrix_11); |
|
291 |
minLabel.setLayoutData(new GridData(GridData.END, GridData.CENTER, |
|
292 |
false, false)); |
|
293 |
minSpinner = new Spinner(composite, SWT.BORDER); |
|
294 |
minSpinner.setMinimum(1); |
|
295 |
minSpinner.setMaximum(1000); |
|
296 |
minSpinner.setSelection(20); |
|
297 |
|
|
298 |
Label orientedLabel = new Label(composite, SWT.NONE); |
|
299 |
orientedLabel.setText("Oriented"); |
|
300 |
orientedLabel.setLayoutData(new GridData(GridData.END, GridData.CENTER, |
|
301 |
false, false)); |
|
302 |
orientedButton = new Button(composite, SWT.CHECK); |
|
303 |
orientedButton.setSelection(false); |
|
304 |
|
|
305 |
queriesFile1Label = new Label(composite, SWT.NONE); |
|
306 |
queriesFile1Label.setText("Query file 1"); |
|
307 |
queriesFile1Label.setLayoutData(new GridData(GridData.END, GridData.CENTER, |
|
308 |
false, false)); |
|
309 |
|
|
310 |
Button queriesFile1Button = new Button(composite, SWT.PUSH); |
|
311 |
queriesFile1Button.setText("Select..."); |
|
312 |
queriesFile1Button.addSelectionListener(new SelectionListener() { |
|
313 |
@Override |
|
314 |
public void widgetSelected(SelectionEvent e) { |
|
315 |
FileDialog dialog = new FileDialog(e.display.getActiveShell(), SWT.OPEN); |
|
316 |
String extensions[] = {"*.properties"}; //$NON-NLS-1$ |
|
317 |
dialog.setFilterExtensions(extensions); |
|
318 |
String path = dialog.open(); |
|
319 |
if (path != null) { |
|
320 |
queriesFile1 = new File(path); |
|
321 |
queriesFile1Label.setText(queriesFile1.getAbsolutePath()); |
|
322 |
} |
|
323 |
} |
|
324 |
|
|
325 |
@Override |
|
326 |
public void widgetDefaultSelected(SelectionEvent e) { } |
|
327 |
}); |
|
328 |
|
|
329 |
queriesFile2Label = new Label(composite, SWT.NONE); |
|
330 |
queriesFile2Label.setText("Query file 2"); |
|
331 |
queriesFile2Label.setLayoutData(new GridData(GridData.END, GridData.CENTER, |
|
332 |
false, false)); |
|
333 |
|
|
334 |
Button queriesFile2Button = new Button(composite, SWT.PUSH); |
|
335 |
queriesFile2Button.setText("Select..."); |
|
336 |
queriesFile2Button.addSelectionListener(new SelectionListener() { |
|
337 |
@Override |
|
338 |
public void widgetSelected(SelectionEvent e) { |
|
339 |
FileDialog dialog = new FileDialog(e.display.getActiveShell(), SWT.OPEN); |
|
340 |
String extensions[] = {"*.properties"}; //$NON-NLS-1$ |
|
341 |
dialog.setFilterExtensions(extensions); |
|
342 |
String path = dialog.open(); |
|
343 |
if (path != null) { |
|
344 |
queriesFile2 = new File(path); |
|
345 |
queriesFile2Label.setText(queriesFile2.getAbsolutePath()); |
|
346 |
} |
|
347 |
} |
|
348 |
|
|
349 |
@Override |
|
350 |
public void widgetDefaultSelected(SelectionEvent e) { } |
|
351 |
}); |
|
352 |
|
|
353 |
return parent; |
|
354 |
} |
|
355 |
|
|
356 |
public File getQueryFile1() { |
|
357 |
return queriesFile1; |
|
358 |
} |
|
359 |
|
|
360 |
public File getQueryFile2() { |
|
361 |
return queriesFile2; |
|
362 |
} |
|
363 |
|
|
364 |
public int getDist() { |
|
365 |
return dist; |
|
366 |
} |
|
367 |
|
|
368 |
public int getMin() { |
|
369 |
return min; |
|
370 |
} |
|
371 |
|
|
372 |
public String getStruct() { |
|
373 |
return struct; |
|
374 |
} |
|
375 |
|
|
376 |
/* (non-Javadoc) |
|
377 |
* @see org.eclipse.jface.dialogs.Dialog#okPressed() |
|
378 |
*/ |
|
379 |
@Override |
|
380 |
protected void okPressed() { |
|
381 |
dist = distSpinner.getSelection(); |
|
382 |
min = minSpinner.getSelection(); |
|
383 |
struct = null; |
|
384 |
oriented = orientedButton.getSelection(); |
|
385 |
for (int i : propsCombo.getSelectionIndices()) { |
|
386 |
String sname = propsCombo.getItem(i); |
|
387 |
if (sname != null && sname.length() > 0) { |
|
388 |
struct = sname; |
|
389 |
break; |
|
390 |
} |
|
391 |
} |
|
392 |
super.okPressed(); |
|
393 |
} |
|
394 |
} |
|
395 |
} |
|
0 | 396 |
tmp/org.txm.querycooccurrences.rcp/src/org/txm/links/coocmatrix/toCA.java (revision 478) | ||
---|---|---|
1 |
package org.txm.links.coocmatrix; |
|
2 |
|
|
3 |
import org.txm.ca.core.functions.CA; |
|
4 |
import org.txm.functions.coocmatrix.QueryCooccurrence; |
|
5 |
import org.txm.lexicaltable.core.functions.LexicalTable; |
|
6 |
import org.txm.searchengine.cqp.clientExceptions.CqiClientException; |
|
7 |
import org.txm.searchengine.cqp.corpus.Corpus; |
|
8 |
import org.txm.searchengine.cqp.corpus.Property; |
|
9 |
import org.txm.statsengine.core.StatException; |
|
10 |
|
|
11 |
public class toCA { |
|
12 |
/** |
|
13 |
* Instantiates a new cA. |
|
14 |
* |
|
15 |
* @param lexicaltable the lexicaltable |
|
16 |
* @throws StatException the stat exception |
|
17 |
* @throws CqiClientException |
|
18 |
*/ |
|
19 |
public static CA link(QueryCooccurrence coocs) throws StatException, CqiClientException { |
|
20 |
Corpus corpus = coocs.getCorpus(); |
|
21 |
Property analysisProperty = null; |
|
22 |
try { |
|
23 |
analysisProperty = coocs.getCorpus().getProperty("word"); //$NON-NLS-1$ |
|
24 |
} catch (CqiClientException e) { |
|
25 |
// TODO Auto-generated catch block |
|
26 |
org.txm.utils.logger.Log.printStackTrace(e); |
|
27 |
} |
|
28 |
LexicalTable table = coocs.toLexicalTable(); |
|
29 |
int fmin = table.getFmin(); |
|
30 |
|
|
31 |
CA ca = new CA(table); |
|
32 |
ca.setPartition(null); |
|
33 |
ca.setCorpus(corpus); |
|
34 |
ca.setFMin(fmin); |
|
35 |
ca.setAnalysisProperty(analysisProperty); |
|
36 |
|
Formats disponibles : Unified diff