【问题标题】:How to write code to merge punctuations and phrases using spaCy如何使用 spaCy 编写代码来合并标点符号和短语
【发布时间】:2021-03-12 23:09:19
【问题描述】:

我想做的事

我想使用 spaCy(用于自然语言处理的开源库之一)进行持久性和依赖性分析。

特别是,我希望知道如何为 Python 中的标点符号和短语合并选项编写代码。

问题

在 displaCy Dependency Vizualizer Web 应用程序上有用于区分标点和短语的按钮。

但是,在本地环境中编写代码时,我找不到编写这些选项的方法。

当前代码返回以下未合并版本。

例句来自your dictionary

当前代码

来自the spaCy official website上的示例代码。

请告诉我如何修复它以设置标点符号和短语合并选项。

import spacy
from spacy import displacy
nlp = spacy.load("en_core_web_sm")

sentence = "On Christmas Eve, we sit in front of the fire and take turns reading Christmas stories."

doc = nlp(sentence)
displacy.render(doc, style="dep")

我想做什么

one example 用于合并实现。 但是,当我应用该句子时它不起作用。

import spacy

nlp = spacy.load("en_core_web_sm")
doc = nlp("On Christmas Eve, we sit in front of the fire and take turns reading Christmas stories.")
span = doc[doc[4].left_edge.i : doc[4].right_edge.i+1]
with doc.retokenize() as retokenizer:
    retokenizer.merge(span)
for token in doc:
    print(token.text, token.dep_, token.head.text, token.head.pos_,
            [child for child in token.children])

示例代码

import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("Credit and mortgage account holders must submit their requests")
span = doc[doc[4].left_edge.i : doc[4].right_edge.i+1]
with doc.retokenize() as retokenizer:
    retokenizer.merge(span)
for token in doc:
    print(token.text, token.pos_, token.dep_, token.head.text)

【问题讨论】:

    标签: python python-3.x nlp spacy


    【解决方案1】:

    如果您需要合并名词块,请查看the built-in merge_noun_chunks pipeline component。当使用 nlp.add_pipe 添加到您的管道时,它会自动合并跨度。

    您可以只使用 displaCy Dependency Vizualizer 中的代码:

    import spacy
    nlp = spacy.load("en_core_web_sm")
    
    def merge_phrases(doc):
        with doc.retokenize() as retokenizer:
            for np in list(doc.noun_chunks):
                attrs = {
                    "tag": np.root.tag_,
                    "lemma": np.root.lemma_,
                    "ent_type": np.root.ent_type_,
                }
                retokenizer.merge(np, attrs=attrs)
        return doc
    
    def merge_punct(doc):
        spans = []
        for word in doc[:-1]:
            if word.is_punct or not word.nbor(1).is_punct:
                continue
            start = word.i
            end = word.i + 1
            while end < len(doc) and doc[end].is_punct:
                end += 1
            span = doc[start:end]
            spans.append((span, word.tag_, word.lemma_, word.ent_type_))
        with doc.retokenize() as retokenizer:
            for span, tag, lemma, ent_type in spans:
                attrs = {"tag": tag, "lemma": lemma, "ent_type": ent_type}
                retokenizer.merge(span, attrs=attrs)
        return doc
    
    text = "On Christmas Eve, we sit in front of the fire and take turns reading Christmas stories."
    
    doc = nlp(text)
    # Merge noun phrases into one token.
    doc = merge_phrases(doc)
    # Attach punctuation to tokens
    doc = merge_punct(doc)
    
    for token in doc:
        print(token.text, token.pos_, token.dep_, token.head.text)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-16
      • 1970-01-01
      • 2020-04-07
      • 1970-01-01
      • 1970-01-01
      • 2019-05-21
      相关资源
      最近更新 更多