This repository has been archived by the owner on Jul 3, 2023. It is now read-only.
ANY23-304 Add extractor for OpenIE 1st pass #33
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
core/src/main/java/org/apache/any23/extractor/openie/OpenIEExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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.apache.any23.extractor.openie; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import javax.xml.transform.TransformerConfigurationException; | ||
import javax.xml.transform.TransformerFactoryConfigurationError; | ||
|
||
import org.apache.any23.extractor.Extractor; | ||
import org.apache.any23.extractor.ExtractionContext; | ||
import org.apache.any23.extractor.ExtractorDescription; | ||
import org.apache.any23.util.StreamUtils; | ||
import org.eclipse.rdf4j.model.IRI; | ||
import org.apache.any23.extractor.ExtractionException; | ||
import org.apache.any23.extractor.ExtractionParameters; | ||
import org.apache.any23.extractor.ExtractionResult; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.w3c.dom.Document; | ||
|
||
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 scala.collection.JavaConversions; | ||
import scala.collection.Seq; | ||
|
||
|
||
|
||
/** | ||
* An <a href="https://github.com/allenai/openie-standalone">OpenIE</a> | ||
* extractor able to generate <i>RDF</i> statements from | ||
* sentences representing relations in the text. | ||
*/ | ||
public class OpenIEExtractor implements Extractor.TagSoupDOMExtractor { | ||
|
||
private final Logger LOG = LoggerFactory.getLogger(getClass()); | ||
|
||
private IRI documentRoot; | ||
|
||
/** | ||
* default constructor | ||
*/ | ||
OpenIEExtractor() { | ||
// default constructor | ||
} | ||
|
||
/** | ||
* @see org.apache.any23.extractor.Extractor#getDescription() | ||
*/ | ||
@Override | ||
public ExtractorDescription getDescription() { | ||
return OpenIEExtractorFactory.getDescriptionInstance(); | ||
} | ||
|
||
@Override | ||
public void run(ExtractionParameters extractionParameters, | ||
ExtractionContext context, Document in, ExtractionResult out) | ||
throws IOException, ExtractionException { | ||
OpenIE openIE = new OpenIE(new ClearParser(new ClearPostagger(new ClearTokenizer())), new ClearSrl(), false, false); | ||
|
||
|
||
Seq<Instance> extractions = null; | ||
try { | ||
extractions = openIE.extract(StreamUtils.asString(StreamUtils.documentToInputStream(in))); | ||
} catch (TransformerConfigurationException | TransformerFactoryConfigurationError e) { | ||
LOG.error("Error during extraction: {}", e); | ||
} | ||
|
||
List<Instance> listExtractions = JavaConversions.seqAsJavaList(extractions); | ||
for(Instance instance : listExtractions) { | ||
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> listArg2s = JavaConversions.seqAsJavaList(instance.extr().arg2s()); | ||
for(Argument argument : listArg2s) { | ||
sb.append(argument.text()).append("; "); | ||
} | ||
System.out.println(sb.toString()); | ||
} | ||
} | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
core/src/main/java/org/apache/any23/extractor/openie/OpenIEExtractorFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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.apache.any23.extractor.openie; | ||
|
||
import java.util.Arrays; | ||
|
||
import org.apache.any23.extractor.ExtractorDescription; | ||
import org.apache.any23.extractor.ExtractorFactory; | ||
import org.apache.any23.extractor.SimpleExtractorFactory; | ||
import org.apache.any23.rdf.Prefixes; | ||
|
||
/** | ||
* @author lewismc | ||
* | ||
*/ | ||
public class OpenIEExtractorFactory extends SimpleExtractorFactory<OpenIEExtractor> | ||
implements ExtractorFactory<OpenIEExtractor> { | ||
|
||
public static final String NAME = "openie"; | ||
|
||
public static final Prefixes prefixes = null; | ||
|
||
private static final ExtractorDescription descriptionInstance = new OpenIEExtractorFactory(); | ||
|
||
public OpenIEExtractorFactory() { | ||
super(NAME, prefixes, Arrays.asList("text/html;q=0.1", "application/xhtml+xml;q=0.1"), "example-openie.html"); | ||
} | ||
|
||
@Override | ||
public OpenIEExtractor createExtractor() { | ||
return new OpenIEExtractor(); | ||
} | ||
|
||
public static ExtractorDescription getDescriptionInstance() { | ||
return descriptionInstance; | ||
} | ||
|
||
|
||
} |
24 changes: 24 additions & 0 deletions
24
core/src/main/java/org/apache/any23/extractor/openie/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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. | ||
*/ | ||
|
||
/** | ||
* This package provides an | ||
* <a href="https://github.com/allenai/openie-standalone">OpenIE</a> | ||
* extractor able to generate <i>RDF</i> statements from | ||
* sentences representing relations in the text. | ||
*/ | ||
package org.apache.any23.extractor.openie; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Presuming this is stub code right now that will eventually send results to the ExtractorResult.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, correct.