Skip to content

Commit

Permalink
An initial openIE implementation (not completed yet)
Browse files Browse the repository at this point in the history
Please see the issue I created
  • Loading branch information
Yongyao committed Feb 8, 2017
1 parent f7810ed commit 681aadb
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,13 @@
<version>1.3.1</version>
<scope>compile</scope>
</dependency>

<!-- OpenIE -->
<dependency>
<groupId>edu.washington.cs.knowitall.openie</groupId>
<artifactId>openie_2.10</artifactId>
<version>4.2.1</version>
</dependency>

<!-- Logging deps -->
<dependency>
Expand Down
110 changes: 110 additions & 0 deletions src/main/java/org/esipfed/eskg/nlp/OpenIE.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.esipfed.eskg.nlp;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;


import edu.knowitall.openie.Argument;
import edu.knowitall.openie.Instance;
import edu.knowitall.openie.OpenIE;
import edu.knowitall.tool.parse.ClearParser;
import edu.knowitall.tool.postag.ClearPostagger;
import edu.knowitall.tool.srl.ClearSrl;
import edu.knowitall.tool.tokenize.ClearTokenizer;
import opennlp.tools.sentdetect.SentenceDetector;
import opennlp.tools.sentdetect.SentenceDetectorME;
import opennlp.tools.sentdetect.SentenceModel;
import scala.collection.JavaConversions;
import scala.collection.Seq;

public class OpenIE {

public OpenIE() {
// TODO Auto-generated constructor stub
}

static String readFile(String path, Charset encoding)
throws IOException
{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub

SentenceDetector sentenceDetector = null;
InputStream modelIn = null;

try {
//need to change this to the resource folder
File fi = new File("C:/Users/Yongyao/Desktop/papers/reverb/en-sent.bin");

modelIn = new FileInputStream(fi);
final SentenceModel sentenceModel = new SentenceModel(modelIn);
modelIn.close();
sentenceDetector = new SentenceDetectorME(sentenceModel);
}
catch (final IOException ioe) {
ioe.printStackTrace();
}
System.out.println("---Started---");
OpenIE openIE = new OpenIE(new ClearParser(new ClearPostagger(new ClearTokenizer())), new ClearSrl(), false, false);

//any text file that contains English sentences would work
File file = new File("C:/Users/Yongyao/Desktop/papers/reverb/test.txt");
String text = readFile(file.getAbsolutePath(), StandardCharsets.UTF_8);

String sentences[]= sentenceDetector.sentDetect(text);
for(int i=0; i<sentences.length; i++)
{

Seq<Instance> extractions = openIE.extract(sentences[i]);

List<Instance> list_extractions = JavaConversions.seqAsJavaList(extractions);


for(Instance instance : list_extractions) {
StringBuilder sb = new StringBuilder();

sb.append(instance.confidence())
.append('\t')
.append(instance.extr().context())
.append('\t')
.append(instance.extr().arg1().text())
.append('\t')
.append(instance.extr().rel().text())
.append('\t');

List<Argument> list_arg2s = JavaConversions.seqAsJavaList(instance.extr().arg2s());
for(Argument argument : list_arg2s) {
sb.append(argument.text()).append("; ");
}

System.out.println(sb.toString());
}
}

}

}
14 changes: 14 additions & 0 deletions src/main/java/org/esipfed/eskg/nlp/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.esipfed.eskg.nlp;
Binary file added src/main/resources/en-sent.bin
Binary file not shown.

0 comments on commit 681aadb

Please sign in to comment.