Shany Shany - 2 months ago 12
Python Question

Stanford CoreNLP OpenIE annotator

I have a question regarding Stanford CoreNLP OpenIE annotator.

I am using Stanford CoreNLP version stanford-corenlp-full-2015-12-09 in order to extract relations using OpenIE. I don't know much Java that's why I am using the

wrapper for Python 3.4.

I want to extract relation between all words of a sentence, below is the code I used. I am also interested in showing the confidence of each triplet:

import nltk
from pycorenlp import *
import collections
s="Twenty percent electric motors are pulled from an assembly line"
output = nlp.annotate(s, properties={"annotators":"tokenize,ssplit,pos,depparse,natlog,openie",
"outputFormat": "json","triple.strict":"true"})
result = [output["sentences"][0]["openie"] for item in output]
for i in result:
for rel in i:

This is the result i got:

[[{'relationSpan': [4, 6], 'subject': 'Twenty percent electric motors', 'objectSpan': [8, 10], 'relation': 'are pulled from', 'object': 'assembly line', 'subjectSpan': [0, 4]}, {'relationSpan': [4, 6], 'subject': 'percent electric motors', 'objectSpan': [8, 10], 'relation': 'are pulled from', 'object': 'assembly line', 'subjectSpan': [1, 4]}, {'relationSpan': [4, 5], 'subject': 'Twenty percent electric motors', 'objectSpan': [5, 6], 'relation': 'are', 'object': 'pulled', 'subjectSpan': [0, 4]}, {'relationSpan': [4, 5], 'subject': 'percent electric motors', 'objectSpan': [5, 6], 'relation': 'are', 'object': 'pulled', 'subjectSpan': [1, 4]}]]

And the triplets are:

('are pulled from', 'Twenty percent electric motors', 'assembly line')
('are pulled from', 'percent electric motors', 'assembly line')
('are', 'Twenty percent electric motors', 'pulled')
('are', 'percent electric motors', 'pulled')

First problem is that the confidence is not showing in the result. Second problem is that I only want to retrieve the triplet that that includes all words of the sentence i.e this triplet:

('are pulled from', 'Twenty percent electric motors', 'assembly line')

What I’m getting is more than one combination of triplets. I tried to use the option
because it extracts "triples only if they consume the entire fragment" but it is NOT working.

Can anyone advise me on this?


You should try this setting:


Looking through the code it appears at this time the confidence is not stored with the returned json, so you cannot get that from the CoreNLP server.

Since you bring this up I will push a change that will add those to the output json and let you know when that is live on the GitHub.