Spacy V3自定义句子细分

发布于 2025-01-22 19:27:27 字数 737 浏览 2 评论 0原文

我想使用自定义定界符IE {s}将大型语料库(.txt)分为句子。我正在与Spacy 3.1合作。

以以下句子为例,应将其视为一个:

{S} — Quel âge as -tu? demanda Angel. — Je ne sais pas, — Sais -tu faire la soupe ?{S}

spacy返回:

{S}
—
Quel âge as
-tu?
demanda Angel.
— Je ne sais pas, —
Sais
-tu faire la soupe ?

我已经在没有运气的情况下尝试了以下句子:

@Language.component("segm")
def set_custom_segmentation(doc):
    for token in doc[:-1]:
        if token.text == '{S}':
            doc[token.i+1].is_sent_start = False
    return doc

nlp.add_pipe('segm', first=True)

以及将{s}视为单个令牌的规则:

special_case = [{ORTH: "{S}"}]
nlp.tokenizer.add_special_case("{S}", special_case)

I want to split into sentences a large corpus (.txt) only using a custom delimiter i.e. {S} . I am working with Spacy 3.1.

Taking as an example the following sentence, which should be considered as one :

{S} — Quel âge as -tu? demanda Angel. — Je ne sais pas, — Sais -tu faire la soupe ?{S}

Spacy returns :

{S}
—
Quel âge as
-tu?
demanda Angel.
— Je ne sais pas, —
Sais
-tu faire la soupe ?

I have already tried the following with no luck :

@Language.component("segm")
def set_custom_segmentation(doc):
    for token in doc[:-1]:
        if token.text == '{S}':
            doc[token.i+1].is_sent_start = False
    return doc

nlp.add_pipe('segm', first=True)

as well as a rule to consider {S} as a single token :

special_case = [{ORTH: "{S}"}]
nlp.tokenizer.add_special_case("{S}", special_case)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

謸气贵蔟 2025-01-29 19:27:27

您要使用token.is_sent_start = true添加句子边界,因此更像:

@Language.component("segm")
def set_custom_segmentation(doc):
    for token in doc[:-1]:
        if token.text == '{S}':
            doc[token.i+1].is_sent_start = True
        else:
            doc[token.i+1].is_sent_start = False
    return doc

You want to use token.is_sent_start = True to add sentence boundaries, so something more like:

@Language.component("segm")
def set_custom_segmentation(doc):
    for token in doc[:-1]:
        if token.text == '{S}':
            doc[token.i+1].is_sent_start = True
        else:
            doc[token.i+1].is_sent_start = False
    return doc
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文