简体   繁体   English

从NLTK中的句子中提取关系

[英]Extract relationships from a sentence in NLTK

I am using NLTK to extract the relationship between a PERSON and an ORGANIZATION. 我正在使用NLTK来提取PERSON和ORGANIZATION之间的关系。

Also, I want to extract the relationship between ORGANIZATION and LOCATION. 另外,我想提取ORGANIZATION和LOCATION之间的关系。 The NLTK version is 3.2.1. NLTK版本是3.2.1。

I've made use of Part-Of-Speech tagging and Named Entity Recognition (NER). 我已经使用了词性标注和命名实体识别(NER)。 Also the Parse Tree is drawn for the NER results. 还为NER结果绘制了解析树。
But I am not able to extract the mentioned relationships from that sentence. 但我无法从该句中提取所提到的关系。

Here is the code: 这是代码:

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))


How to extract 'Person - Organization' relationship and 'Organization - Location' relationship? 如何提取“人 - 组织”关系和“组织 - 位置”关系?

I think docs is not tagged pos, it should be NE. 我认为文档没有标记pos,它应该是NE。

Working code 工作代码

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))

Output 产量

[PER: 'Mark/NNP'] 'works/VBZ in/IN' [ORG: 'JPMC/NNP'] [PER:'Mark / NNP']'作品/ VBZ in / IN'[ORG:'JPMC / NNP']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM