python - 从 NLTK 中的句子中提取关系

标签 python nlp nltk

我正在使用 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/

相关文章:

python - 如何使用 matplotlib 渲染 latex 矩阵

nlp - 提取主语、谓语、宾语等词功能的软件

algorithm - Twitter 的热门话题算法如何决定从推文中提取哪些词?

python - 在 NLTK 停用词列表中添加和删除单词

带有自定义缩写的 Python nltk 不正确的句子标记化

python - 使用在 python 中查找的字典修复带有空格的单词?

python - 在带有 PySpark 的单个多核机器中使用大型查找表

python - 使用 pandas 将 JSON 转换为数据框

python - 将函数应用于 Pandas 数据框 : is there a more efficient way of doing this?

python BeautifulSoup 表抓取