我正在使用 NLTK 来提取个人和组织之间的关系。
此外,我想提取 ORGANIZATION 和 LOCATION 之间的关系。 NLTK 版本为 3.2.1。
我使用了词性标注和命名实体识别 (NER)。还为 NER 结果绘制了解析树。
但我无法从该句子中提取提到的关系。
代码如下:
import nltk, re
from nltk import word_tokenize
sentence = "Mark works at JPMC in London every day"
pos_tags = nltk.pos_tag(word_tokenize(sentence)) # POS tagging of the sentence
ne = nltk.ne_chunk(pos_tags) # Named Entity Recognition
ne.draw() # Draw the Parse Tree
IN = re.compile(r'.*\bin\b(?!\b.+ing)')
for rel1 in nltk.sem.extract_rels('PER', 'ORG', pos_tags, pattern = IN):
print(nltk.sem.rtuple(rel1))
for rel2 in nltk.sem.extract_rels('ORG', 'LOC', pos_tags, pattern = IN):
print(nltk.sem.rtuple(rel2))
如何提取'Person - Organization' 关系和'Organization - Location' 关系?
最佳答案
我觉得docs没有标pos,应该是NE。
工作代码
senten = "Mark works in JPMC in London every day"
pos_tags = nltk.pos_tag(word_tokenize(senten)) # POS tagging of the sentence
ne = nltk.ne_chunk(pos_tags) # Named Entity Recognition
chunked = nltk.ne_chunk_sents(pos_tags, binary=True)
# ne.draw() # Draw the Parse Tree
print(pos_tags)
IN = re.compile(r'.*\bin\b(?!\b.+ing)')
for rel in nltk.sem.extract_rels('PERSON', 'ORGANIZATION', ne, corpus='ace', pattern=IN):
print(nltk.sem.rtuple(rel))
输出
[PER: 'Mark/NNP'] 'works/VBZ in/IN' [ORG: 'JPMC/NNP']
关于python - 从 NLTK 中的句子中提取关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42762072/