【问题标题】:Rule based Part of Speech parsing dilemma基于规则的词性解析困境
【发布时间】:2019-08-27 14:04:29
【问题描述】:

我目前正在尝试构建一个提取未知词性的句子解析器。它有点抽象,但我的方法基本上是创建一组语法规则,函数可以用来解析文本。

我现在正在使用 Spacy 的 PoS 标记器,只是为了从例句中提取 pos 标记。我知道 Spacy 也有一个依赖解析器,但从我在文档中阅读的内容来看,它用于匹配 known 短语。

所以我的问题是这样的:

通过创建一组语法规则,根据这些规则从字符串中提取未知目标词的最佳方法是什么?

例如:

import spacy

nlp = spacy.load('en_core_web_sm')

Example = "I really hate all people who are green, I wish they would go back home"
ex_string = Example.split()
doc = nlp(Example)
pos_tagged_context = [token.tag_ for token in doc]
Word_Dict = {}

这种情况下的第一条规则是pos_tagged_context的PoS标签列表,它与ex_string的句子结构相匹配

['PRP', 'RB', 'VBP', 'DT', 'NNS', 'WP', 'VBP', 'JJ', ',', 'PRP', 'VBP', 'PRP', 'MD', 'VB', 'RB', 'RB']

但由此产生了两个问题,一个更简单的问题是在打印 Word_Dict 时会丢失几个 PoS 标签:

{'I': ',', 'really': 'RB', 'hate': 'VBP', 'all': 'DT', 'people': 'NNS', 'who': 'WP', 'are': 'VBP', 'green,': 'JJ', 'wish': 'PRP', 'they': 'VBP', 'would': 'PRP', 'go': 'MD', 'back': 'VB', 'home': 'RB'}

第二个问题更抽象,因为“否定”句子的结构本质上是相对的,在创建这些规则时是否有一个好的“一般形式”?

理想的输出将使用句子的结构并识别其中的目标词,在本例中为“绿色”。

如果问题太抽象或需要更多说明,请告诉我!

【问题讨论】:

    标签: python python-3.x nlp spacy


    【解决方案1】:

    看看下面的代码:

    import spacy
    
    
    nlp = spacy.load('en_core_web_sm')
    
    examples = ["I really dislike endives because they taste bitter.", "I like Fridays but I hate Mondays."]
    
    negativeVerbs = ['hate', 'dislike'] 
    
    for e in examples:
        doc = nlp(e)   
        candidateVerbs = [t for t in doc if (t.text in negativeVerbs and t.pos_ == 'VERB')]
        if candidateVerbs:
            candidateVerb = candidateVerbs[0]
    
            #print(candidateVerbs)
            print(e)
            print([c.text for c in candidateVerb.children if c.dep_ == 'dobj'])
    

    输出:

    我真的不喜欢菊苣,因为它们尝起来很苦。

    ['endives']
    

    我喜欢星期五,但我讨厌星期一。

    ['Mondays']
    

    【讨论】:

    • 这很好,唯一需要注意的是提供了您的列表negativeVerbs,我试图在不知道实际单词是什么的情况下找到否定词(在这种情况下是仇恨言论)。所以这个词的上下文是我想要建立的。所以 tldr 我正在寻找未知的单词,但要查看整个句子
    猜你喜欢
    • 1970-01-01
    • 2013-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-01
    • 2012-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多