【问题标题】:Extract relationships from a sentence in NLTK从 NLTK 中的句子中提取关系
【发布时间】:2017-03-13 11:05:48
【问题描述】:

我正在使用 NLTK 来提取 PERSON 和 ORGANIZATION 之间的关系。

另外,我想提取 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'关系?

【问题讨论】:

    标签: python nlp nltk


    【解决方案1】:

    我认为 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']

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-02
      • 1970-01-01
      • 2017-03-14
      • 2010-12-28
      • 2018-07-15
      • 1970-01-01
      • 2015-07-28
      相关资源
      最近更新 更多