【问题标题】:Any elegant solution for finding compound noun-adjective pairs from sentence by using Spacy?使用 Spacy 从句子中查找复合名词形容词对的任何优雅解决方案?
【发布时间】:2019-03-26 04:19:03
【问题描述】:

我最近认识了Spacy,并且对这个 Python 库非常感兴趣。但是,在我的规范中,我打算从输入句子中提取复合名词-形容词对作为关键短语。我认为Spacy 提供了许多实用程序来处理 NLP 任务,但没有为我想要的任务找到满意的线索。我在SOrelated post 中查看了一个非常相似的帖子,解决方案效率不高,不适用于自定义输入语句。

这里是一些输入语句:

sentence_1="My problem was with DELL Customer Service"
sentence_2="Obviously one of the most important features of any computer is the human interface."
sentence_3="The battery life seems to be very good and have had no issues with it."

这是我尝试过的代码:

import spacy, en_core_web_sm
nlp=en_core_web_sm.load()

def get_compound_nn_adj(doc):
    compounds_nn_pairs = []
    parsed=nlp(doc)
    compounds = [token for token in sent if token.dep_ == 'compound']
    compounds = [nc for nc in compounds if nc.i == 0 or sent[nc.i - 1].dep_ != 'compound']
    if compounds:
        for token in compounds:
            pair_1, pair_2 = (False, False)
            noun = sent[token.i:token.head.i + 1]
            pair_1 = noun
            if noun.root.dep_ == 'nsubj':
                adj_list = [rt for rt in noun.root.head.rights if rt.pos_ == 'ADJ']
                if adj_list:
                    pair_2 = adj_list[0]
            if noun.root.dep_ == 'dobj':
                verb_root = [vb for vb in noun.root.ancestors if vb.pos_ == 'VERB']
                if verb_root:
                    pair_2 = verb_root[0]
            if pair_1 and pair_2:
                compounds_nn_pairs.append(pair_1, pair_2)
    return compounds_nn_pairs

我推测应该在辅助函数上方应用什么样的修饰,因为它没有返回我预期的复合名词-形容词对。有谁对Spacy有好的经验吗?如何改进上述草图解决方案?有更好的主意吗?

期望的输出

我希望从每个输入句子中得到复合名词-形容词对,如下所示:

desired_output_1="DELL Customer Service"
desired_output_2="human interface"
desired_output_3="battery life"

有什么办法可以得到预期的输出?上述实施需要什么样的更新?还有什么想法吗?提前致谢!

【问题讨论】:

    标签: python sentiment-analysis spacy


    【解决方案1】:

    看起来 spaCy 只检测句子 1 和 3 中的复合关系,并将 2 视为 amod 关系。 (这里有一些检查其解析的快速代码:[(i, i.pos_, i.dep_) for i in nlp(sentence_1)])。

    要从 1 和 3 中取出化合物,试试这个:

    for i in nlp(sentence_1):
        if i.pos_ in ["NOUN", "PROPN"]:
            comps = [j for j in i.children if j.dep_ == "compound"]
            if comps:
                print(comps, i)
    

    对于句子中的每个名词或专有名词,它会检查其子树中的compound 关系。

    要撒下一张更广泛的网,也可以收集形容词,您可以在单词的子树中查找形容词和名词,而不仅仅是复合词:

    for i in nlp(sentence_2):
        if i.pos_ in ["NOUN", "PROPN"]:
            comps = [j for j in i.children if j.pos_ in ["ADJ", "NOUN", "PROPN"]]
            if comps:
                print(comps, i)
    

    【讨论】:

      【解决方案2】:

      我怀疑这必须使用复合名词数据库来处理。 “复合名词”的地位来自于用法的共性。因此,也许各种 n-gram 数据库(如 Google 的)可以作为来源。

      【讨论】:

      • 也许这更适合作为对 OP 的评论而不是答案。或者您可以尝试详细说明或证明您的观点。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多