-
Notifications
You must be signed in to change notification settings - Fork 14
/
README
309 lines (203 loc) · 9.97 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
=====================================================================
======
README
======
KEA as XML-RPC service
4 August 2010
KeaXML-RPC Service applicable to KEA 5.0 (http://www.nzdl.org/Kea/)
Commissioned through vworker, coded by Ali Afshar ([email protected])
Released under a BSD license.
=====================================================================
Contents:
---------
1. Installation
2. Getting started
- Running the server
- Java Client Example
- Python Client Example
3. History
4. Compilation
5. How it is done
----------------------------------------------------------------------
NOTE:
-----
The service is packed and delivered as a patch
It contains two modules:
1) patch\main\KEAServer.java
This is the server module.
2) patch\filters\KEAFilter.java
This is a copy of the module kea\filters\KEAFilter.java from KEA 5.0 with only one difference
in method, see lines 1339-1341:
private FastVector convertInstance(Instance instance, boolean training)
....
//double prob = probs[1]; //Commented out
//If numeric class is used change to:
double prob = probs[0]; //Uncommented
....
The Service is developed in Java 5.0, on Eclipse under Win XP.
It has been also tested on Windows XP and Windows 2000.
----------------------------------------------------------------------
1. Installation:
----------------
Assumes kea-5.0_full is installed.
a) Download the KeaXML-RPC Service.
b) Create a folder called patch in kea-5.0_full home directory (KEAHOME)
e.g. E:\programs\kea-5.0_full\patch
c) Unzip the KeaXML-RPC and copy the kea_server.jar into %KEAHOME%\patch\kea_server.jar
d) set KEAXMLRPC=%KEAHOME%\patch\kea_server.jar
e) Add %KEAXMLRPC% to your CLASSPATH environment variable.
i.e. set CLASSPATH=%KEAXMLRPC%;%CLASSPATH%; Note KEAXMLRPC must come first
f) Download:
http://ftp.us.xemacs.org/pub/mirrors/maven2/xmlrpc/xmlrpc/1.2-b1/xmlrpc-1.2-b1.jar
and add it also to your classpath.
----------------------------------------------------------------------
2. Getting started:
-------------------
Assume xmlrpc-1.2-b1.jar is included in your classpath
* Running the server
Type at command prompt:
java patch.main.KEAServer <model> [port] # defult port 8000 is used if not provided
e.g. java patch.main.KEAServer path\to\my_model 8090
If xmlrpc-1.2-b1.jar is not included then the command would be:
java -cp %classpath%;path\to\xmlrpc-1.2-b1.jar; patch.main.KEAServer <model> [port]
e.g. java -cp %classpath%;E:\tools\xmlrpc-1.2-b1\xmlrpc-1.2-b1.jar; patch.main.KEAServer C:\Temp\my_model 8090
* You can also use the Python server wrapper (port 8001) on top of the
Java server. If the Java KEA service throws an exception, the wrapper
returns an empty list of keywords. This appears to happen on weird
Unicode characters, in 0.3% of page text. YMMV.
./python-server-wrapper.py
Note that Python server wrapper doesn't seem to work under Pytohn 2.6. It does work under 2.4.
* Java Client Example
//////////////////////////////////////////////////////
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;
import org.apache.xmlrpc.XmlRpcClient;
public class KEAClient {
public static void main(String[] args) {
try {
XmlRpcClient server = new XmlRpcClient("127.0.0.1", 8090);
System.out.println("Input document: "+args[0]);
Vector<String> v = new Vector<String>();
v.add(readFile(args[0]));
Vector<String> keywords = (Vector<String>) server.execute("kea.extractKeyphrases", v);
System.out.println("\nmessage recieved: " + Arrays.toString(keywords.toArray()));
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
public static String readFile(String filename ) {
File f = new File(filename);
StringBuilder contents = new StringBuilder();
try {
BufferedReader input = new BufferedReader(new FileReader(f));
try {
String line = null;
while ((line = input.readLine()) != null) {
contents.append(line);
contents.append(System.getProperty("line.separator"));
}
} finally {
input.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
return contents.toString();
}
}
How to execute the client:
--------------------------
Assume xmlrpc-1.2-b1.jar is included in the classpath.
Compile and then run the client by typing at command prompt:
java KEAClient path\to\document\document.txt
e.g.
java KEAClient E:\Temp\kea-5.0_full\my_testing\winrock_wi10ce.txt
If xmlrpc-1.2-b1.jar is not included in your classpath:
java -cp ./;E:\programs\tools\xmlrpc-1.2-b1\xmlrpc-1.2-b1.jar; KEAClient E:\Temp\kea-5.0_full\my_testing\winrock_wi10ce.txt
/////////////////////////////////////////////////////
* Python Client Example,
/////////////////////////////////////////////////////
import xmlrpclib
s = xmlrpclib.ServerProxy('http://127.0.0.1:8090')
txt = open("E:\\Temp\\iirr_ii02re.txt").read()
keywords = s.kea.extractKeyphrases(txt)
print keywords
/////////////////////////////////////////////////////
Start python and import client.
----------------------------------------------------------------------
3. Further enhancement:
-----------------------
- eliminate hard coded init parameters,
- give the clients the option of choosing different number of keywords and
- test it with the latest XML-RPC library.
-----------------------------------------------------------------------
4. Compilation:
---------------
javac patch/main/KEAServer.java
jar cvf0 kea_server.jar patch/main/KEAServer.class
-----------------------------------------------------------------------
5. How it is done:
------------------
The service to be exposed is specified as follows: Given a (.txt) document and model, key phrases needed to be generated.
KEA 5.0 provides a function which with some modifications realizes this requirement. KEA extracts key phrases from the (.txt) documents in a given directory and puts these key phrases back into the same directory (corresponding files with extension “.key” are generated).
Where and how KEA does this extraction was to be identified, after some investigation it turned out that the module kea.main.KEAKeyphraseExtractor.java is the starting point for this service.
Assumptions:
Training has taken place and a model is generated.
Steps:
- Create a class in which the XML-RPC service is to be started, in this case it is called patch.main.KEAServer.java.
- Identify the relevant methods from KEAKeyphraseExtractor and copy them into the patch.main.KEAServer.
Two methods are identified:
public void loadModel() throws Exception
public void extractKeyphrases(Hashtable stems) throws Exception
- Start with initializations and modifications
First we need to do some initializations including document language, encoding, vocabulary etc.
The next task is loading the model, the model is the only input parameter (the format is drive:\path\to\modelName) to kea\filters\KEAFilter.java.
There is an instance of kea.main.KEAKeyphraseExtractor to handle the initialization parameters and an instance of to patch.filters.KEAFilter to read/hold the model.
The constructor of the KEAServer.java is where we handle these two issues.
public KEAServer(String model) {
setOptions();
try {
loadModel(model);
m_KEAFilter.setDebug(true);
myLogger.info("Loaded model " +model);
} catch (Exception e) {
myLogger.log(Level.SEVERE, "Failed loading model {0} , exception: {1}", new Object[]{model, e.toString()});
}
}
In the modified method loadModel(String model) we read the model into an instance of patch.filters.KEAFilter.
This filter converts the incoming data into data appropriate for keyword classification, that is it creates a
set of weka.core.Instance instances for every document.
The only change we need to make here is at lines 1339 and 1341, (see kea\filters\KEAFilter.java)
1339: //double prob = probs[1]; //Commented
1340: // If numeric class is used change to:
1341: double prob = probs[0]; //Uncommented
- Now we are ready to instantiate the XML-RPC service and prepare for incoming requests.
The main method is where we do this, see patch\main\KEAServer.java
…
try {
System.out.println("Starting server on port: " + port);
patch.main.KEAServer kea = new patch.main.KEAServer ( model );
org.apache.xmlrpc.WebServer server = new org.apache.xmlrpc.WebServer ( port );
//server.setParanoid(true);
server.acceptClient(localhost); // add hosts here
server.addHandler("kea", kea);
server.start();
System.out.println("Server started ");
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
System.exit(0);
}
- The other important method in kea.main.KEAKeyphraseExtractor was:
public void extractKeyphrases(Hashtable stems) (see line 69 in kea\main\KEAKeyphraseExtractor.java)
We need to make modification so that the method returns the key phrases given a string (representing a .txt document).
The corresponding method in class patch.main.KEAServer would look like:
private List<weka.core.Instance> myExtractKeyphrases ( String document )
That is, given a string "document" a list of weka.core.Instance is returned.
Next, in the method
public String[] extractKeyphrases ( String document )
the list of Instances are parsed and a list of keywords is delivered to the client.