I am trying to accept a sentence and extract the connection between Person (PER) and Place (GPE).
Sentence : "John from Ohio, Michael from Florida, and Rebecca from Nashville, which is in Tennessee."
For the final person, she has both a city and a state, which can be obtained as her place. So far I have been trying to use nltk for this, but I was only able to extract its city, not its state.
What I tried:
import re
from nltk import ne_chunk, pos_tag, word_tokenize
from nltk.sem.relextract import extract_rels, rtuple
sentence = "John is from Ohio, Michael is from Florida and Rebecca is from Nashville which is in Tennessee."
chunked = ne_chunk(pos_tag(word_tokenize(sentence)))
ISFROM = re.compile(r'.*\bfrom\b.*')
rels = extract_rels('PER', 'GPE', chunked, corpus = 'ace', pattern = ISFROM)
for rel in rels:
print(rtuple(rel))
My conclusion:
[PER: 'John/NNP'] 'is/VBZ from/IN' [GPE: 'Ohio/NNP']
[PER: 'Michael/NNP'] 'is/VBZ from/IN' [GPE: 'Florida/NNP']
[PER: 'Rebecca/NNP'] 'is/VBZ from/IN' [GPE: 'Nashville/NNP']
Rebecca's problem. How can I extract that both Nashville and Tennessee are part of its location? Or even just Tennessee alone?
source
share