Python中的子句提取/长句切分 [英] Clause extraction / long sentence segmentation in python

查看:31
本文介绍了Python中的子句提取/长句切分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在进行一个涉及句子向量的项目(来自Roberta预先训练的模型)。当句子较长时,这些向量的质量较低,并且我的语料库包含许多带子句的长句。

我一直在寻找用于子句提取/长句分割的方法,但令我惊讶的是,没有任何主要的NLP包(例如Spacy或stanza)提供这一功能。

我想这可以通过使用空格或节的依赖关系解析来完成,但是要正确处理各种复杂的句子和边缘情况可能会相当复杂。

我遇到过带有Spacy的ClausIE信息提取系统的this implementation,它可以执行类似的操作,但它尚未更新,无法在我的计算机上运行。

我也遇到过句子简化的this repo,但在本地运行时,从Stanford coreNLP得到一个注释错误。

有没有什么我明显忽略的包/方法?如果没有,有没有用节或空格实现这一点的简单方法?

推荐答案

以下是适用于您的特定示例的代码。将其扩展到涵盖所有案例并非易事,但可以根据需要随着时间的推移进行处理。

import spacy
import deplacy
en = spacy.load('en_core_web_sm')

text = "This all encompassing experience wore off for a moment and in that moment, my awareness came gasping to the surface of the hallucination and I was able to consider momentarily that I had killed myself by taking an outrageous dose of an online drug and this was the most pathetic death experience of all time."

doc = en(text)
#deplacy.render(doc)

seen = set() # keep track of covered words

chunks = []
for sent in doc.sents:
    heads = [cc for cc in sent.root.children if cc.dep_ == 'conj']

    for head in heads:
        words = [ww for ww in head.subtree]
        for word in words:
            seen.add(word)
        chunk = (' '.join([ww.text for ww in words]))
        chunks.append( (head.i, chunk) )

    unseen = [ww for ww in sent if ww not in seen]
    chunk = ' '.join([ww.text for ww in unseen])
    chunks.append( (sent.root.i, chunk) )

chunks = sorted(chunks, key=lambda x: x[0])

for ii, chunk in chunks:
    print(chunk)
        

deplacy是可选的,但我发现它对于可视化依赖项很有用。

此外,我还看到您对这不是常见NLP库的固有特性表示惊讶。原因很简单-大多数应用程序不需要这个,虽然这看起来是一项简单的任务,但您尝试涵盖的案例越多,它实际上就会变得非常复杂和特定于应用程序。另一方面,对于任何特定的应用程序,如我给出的示例,将足够好的解决方案组合在一起相对容易。

这篇关于Python中的子句提取/长句切分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆