1

我正在开发一个使用小句子的项目,所以如果用户传递长句子或复杂/复合句子,我想将其解析为简单句子,然后将其传递给软件。

我尝试了 spacy 方法,但它只适用于连词:即:我要去市场,我要买一本书。解析后:我要去市场。我会买一本书。(它分为两个简单的句子)

但是当我尝试使用更复杂的句子时,例如:

  • 我必须保存这张优惠券,以防我明天再来商店。(应该分开,因为我必须保存这张优惠券。我明天回到商店。
  • 在某种程度上,它遏制了每天发生的犯罪案件的数量。(应该拆分为以某种方式减少犯罪案件的数量。每天都在发生)

我的代码:

import spacy

en = spacy.load('en_core_web_sm')

text = "In a way it curbs the number of crime cases happening every day."

doc = en(text)

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:
        print(head.subtree)
        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)

是否有任何库/框架可以轻松做到这一点。或任何人建议如何在 spacy 上生成句子树并对其进行解析,这样我就可以在所需的位置将其分解。

4

0 回答 0