首页 > 解决方案 > NLP:spacy 获取依赖项

问题描述

我试图从这句话中提取一些数字,但我想验证正确的数字是否与正确的文本匹配。

nlp = spacy.load('en_core_web_sm')  
s2 = 'Revenue from the advertising and subscription business for the first quarter of 2019 was RMB897.0 million (US$133.7 million), representing a 13.9% increase from RMB787.5 million (US$117.3 million) in the corresponding period in 2018.'

doc = nlp(s2)
for w in doc.ents:
    print(w.text, w.label_, w.root)
    for i in w.subtree:
        print("   ", i, i.head)
        for a in i.ancestors:
            print("       ", a, a.head)

我想联系RMB897.0 millionadvertising and subscription不知道该怎么做。还尝试了名词分块。

for chunk in doc.noun_chunks:
    print(chunk.text, chunk.root.text, chunk.root.dep_,
          chunk.root.head.text)

    for c in chunk.subtree:
        print("   ", c, c.head)

标签: pythonnlpspacy

解决方案


首先,您需要考虑您的数据。如果您发现数字和相关名词的某些模式,您也许可以使用它们来找出关系,无论如何,这并不是很可靠。更好的方法是使用依赖解析或完全解析算法,使用词性标记 (POS) 信息和可能的关于头部动词的语义信息。这是一项真正的语言任务,这意味着您需要语言信息。连接动词表示数字与动词参数有关,因此提取和关系很简单。其他情况可能不太明显。

也许开始对包含数字的表达式及其出现的矩阵动词(be,amount to,reduce to,increase等)进行一些搭配分析,然后您可以使用动词参数的信息进行注释关系。也许看看相关和照应分辨率,这可能会帮助你。


推荐阅读