-
Notifications
You must be signed in to change notification settings - Fork 77
/
JavaOllieWrapper.java
61 lines (49 loc) · 2.4 KB
/
JavaOllieWrapper.java
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
package example;
import java.io.File;
import java.net.MalformedURLException;
import edu.knowitall.ollie.Ollie;
import edu.knowitall.ollie.OllieExtraction;
import edu.knowitall.ollie.OllieExtractionInstance;
import edu.knowitall.tool.parse.MaltParser;
import edu.knowitall.tool.parse.graph.DependencyGraph;
/** This is an example class that shows one way of using Ollie from Java. */
public class JavaOllieWrapper {
// the extractor itself
private Ollie ollie;
// the parser--a step required before the extractor
private MaltParser maltParser;
// the path of the malt parser model file
private static final String MALT_PARSER_FILENAME = "engmalt.linear-1.7.mco";
public JavaOllieWrapper() throws MalformedURLException {
// initialize MaltParser
scala.Option<File> nullOption = scala.Option.apply(null);
maltParser = new MaltParser(new File(MALT_PARSER_FILENAME).toURI().toURL(), nullOption);
// initialize Ollie
ollie = new Ollie();
}
/**
* Gets Ollie extractions from a single sentence.
* @param sentence
* @return the set of ollie extractions
*/
public Iterable<OllieExtractionInstance> extract(String sentence) {
// parse the sentence
DependencyGraph graph = maltParser.dependencyGraph(sentence);
// run Ollie over the sentence and convert to a Java collection
Iterable<OllieExtractionInstance> extrs = scala.collection.JavaConversions.asJavaIterable(ollie.extract(graph));
return extrs;
}
public static void main(String args[]) throws MalformedURLException {
System.out.println(JavaOllieWrapper.class.getResource("/logback.xml"));
// initialize
JavaOllieWrapper ollieWrapper = new JavaOllieWrapper();
// extract from a single sentence.
String sentence = "President Obama will meet with Congressional leaders on Friday, and House Republicans summoned lawmakers back for a Sunday session, in a last-ditch effort to avert a fiscal crisis brought on by automatic tax increases and spending cuts scheduled to hit next week.";
Iterable<OllieExtractionInstance> extrs = ollieWrapper.extract(sentence);
// print the extractions.
for (OllieExtractionInstance inst : extrs) {
OllieExtraction extr = inst.extr();
System.out.println(extr.arg1().text()+"\t"+extr.rel().text()+"\t"+extr.arg2().text());
}
}
}