【问题标题】:Extracting sentences with Spacy POS/DEP : actor and action用 Spacy POS/DEP 提取句子:演员和动作
【发布时间】:2022-08-19 12:16:12
【问题描述】:

谢谢您的帮助。我正在使用 spacy 来解析文档以查找某些单词的实例并在新的 df[column] 中提取句子。 以下是一些文字:

text = 'Many people like Germany. It is a great country. Germany exports lots of technology. France is also a great country. France exports wine. Europeans like to travel. They spend lot of time of beaches. Spain is one of their travel locations. Spain appreciates tourists. Spain's economy is strengthened by tourism. Spain has asked and Germany is working to assist with the travel of tourists to Spanish beaches. Spain also like to import French wine. France would like to sell more wine to Spain.'

我的代码是这样工作的:

def sent_matcher(text: str) -> list:
    doc = nlp(text)
    sent_list = []
    phrase_matcher = PhraseMatcher(nlp.vocab)
    phrases = ['Germany', 'France']
    patterns = nlp(data) for data in phrases]
    phrase_matcher.add('EU entity', None, * patterns)

    for sent in doc.sents:
        for match_id, start, end in phrase_matcher(nlp(sent.text)):
            if nlp.vocab.strings[match_id] in ['EU entity']:
                sent_list.append(sent)
    text = (sent_list)
    return text

此代码工作正常,并提取了所有包含欧盟实体的句子。

然而,我想把它提升到一个新的水平,并提取出欧盟实体是参与者的句子,并确定他们正在采取什么类型的行动。我尝试使用 POS/Dependency 来提取与动词结合的专有名词,但 nsubj 并不总是正确的,或者 nsubj 与复合名词结构中的另一个单词相关联。我尝试提取国家是第一个参与者的实例(如果 token == 'x'),但即使我对这个词进行了标记,我也总是抛出一个字符串错误。我也尝试过使用 noun_chunks 但我无法隔离国家的实例或将该块与动词联系起来。

我对 NLP 还很陌生,因此对于如何编写代码并获得所需输出的任何想法都将不胜感激。

谢谢您的帮助!

【问题讨论】:

    标签: python-3.x spacy-3


    【解决方案1】:

    听起来如果您使用merge_entities 并遵循rule-based matching guide for the DependencyMatcher,您应该可以很容易地做到这一点。它不会是完美的,但你应该能够匹配许多实例。

    【讨论】:

      猜你喜欢
      • 2020-02-01
      • 1970-01-01
      • 2019-08-26
      • 1970-01-01
      • 2021-01-27
      • 2021-01-11
      • 2021-09-21
      • 1970-01-01
      • 2017-05-03
      相关资源
      最近更新 更多