-
Notifications
You must be signed in to change notification settings - Fork 201
Coreference Resolution
Lynten edited this page Apr 25, 2018
·
3 revisions
Sentence "My sister has a friend called John. Really, tell me more about him? She think he is so funny!" taken from Medium.
nlp = StanfordCoreNLP('G:\JavaLibraries\stanford-corenlp-full-2018-02-27')
text = "My sister has a friend called John. Really, tell me more about him? She think he is so funny!"
print(nlp.coref(text))
nlp.close()
# Out:
# Each tuple represents (sentence_index, start_index, end_index, text), starts with 1-index
[[(1, 1, 2, 'My'), (2, 4, 5, 'me')], [(1, 7, 8, 'John'), (2, 7, 8, 'him'), (3, 3, 4, 'he')], [(1, 1, 3, 'My sister'), (3, 1, 2, 'She')]]
Get more information with general API:
# coding=utf-8
import json
from stanfordcorenlp import StanfordCoreNLP
nlp = StanfordCoreNLP('http://localhost', port=9000)
text = "My sister has a friend called John. Really, tell me more about him? She think he is so funny!"
pros = {'annotators': 'coref', 'pinelineLanguage': 'en'}
result_dict = json.loads(nlp.annotate(text, properties=pros))
for idx, mentions in result_dict['corefs'].items():
print('Entity:', idx)
for m in mentions:
print(m)
# Out:
Entity: 4
{u'endIndex': 2, u'animacy': u'ANIMATE', u'text': u'My', u'isRepresentativeMention': True, u'number': u'SINGULAR', u'startIndex': 1, u'sentNum': 1, u'gender': u'UNKNOWN', u'position': [1, 2], u'headIndex': 1, u'type': u'PRONOMINAL', u'id': 1}
{u'endIndex': 5, u'animacy': u'ANIMATE', u'text': u'me', u'isRepresentativeMention': False, u'number': u'SINGULAR', u'startIndex': 4, u'sentNum': 2, u'gender': u'UNKNOWN', u'position': [2, 1], u'headIndex': 4, u'type': u'PRONOMINAL', u'id': 4}
Entity: 7
{u'endIndex': 8, u'animacy': u'ANIMATE', u'text': u'John', u'isRepresentativeMention': True, u'number': u'SINGULAR', u'startIndex': 7, u'sentNum': 1, u'gender': u'MALE', u'position': [1, 1], u'headIndex': 7, u'type': u'PROPER', u'id': 0}
{u'endIndex': 8, u'animacy': u'ANIMATE', u'text': u'him', u'isRepresentativeMention': False, u'number': u'SINGULAR', u'startIndex': 7, u'sentNum': 2, u'gender': u'MALE', u'position': [2, 2], u'headIndex': 7, u'type': u'PRONOMINAL', u'id': 5}
{u'endIndex': 4, u'animacy': u'ANIMATE', u'text': u'he', u'isRepresentativeMention': False, u'number': u'SINGULAR', u'startIndex': 3, u'sentNum': 3, u'gender': u'MALE', u'position': [3, 2], u'headIndex': 3, u'type': u'PRONOMINAL', u'id': 7}
Entity: 6
{u'endIndex': 3, u'animacy': u'ANIMATE', u'text': u'My sister', u'isRepresentativeMention': True, u'number': u'SINGULAR', u'startIndex': 1, u'sentNum': 1, u'gender': u'FEMALE', u'position': [1, 3], u'headIndex': 2, u'type': u'NOMINAL', u'id': 2}
{u'endIndex': 2, u'animacy': u'ANIMATE', u'text': u'She', u'isRepresentativeMention': False, u'number': u'SINGULAR', u'startIndex': 1, u'sentNum': 3, u'gender': u'FEMALE', u'position': [3, 1], u'headIndex': 1, u'type': u'PRONOMINAL', u'id': 6}