我想把文本中的换行符识别为句子的结尾。我尝试将它输入到nlp对象中,如下所示:
text = 'Guest Blogging\nGuest Blogging allows the user to collect backlinks'
nlp = spacy.load("en_core_web_lg")
config = {"punct_chars": ['\n']}
nlp.add_pipe("sentencizer", config=config)
for sent in nlp(text).sents:
print('next sentence:')
print(sent)这方面的产出如下:
next sentence:
Guest Blogging
Guest Blogging allows the user to collect backlinks我不明白斯派西为什么不把换行符当成句子的结尾。我想要的输出是:
next sentence:
Guest Blogging:
next sentence:
Guest Blogging allows the user to collect backlinks有人知道如何做到这一点吗?
发布于 2022-04-13 07:39:29
sentencizer在这里没有做任何事情的原因是parser首先运行并且已经设置了所有的句子边界,然后sentencizer没有修改任何现有的句子边界。
只有当您知道输入文本中每行只有一个句子时,sentencizer with \n才是正确的选择。否则,添加句子的自定义组件可能会在换行符之后开始(但不会设置所有的句子边界)。
如果要在运行解析器之前设置一些自定义语句边界,则需要确保在管道中的解析器之前添加自定义组件:
nlp.add_pipe("my_component", before="parser")您的自定义组件将在换行符之后为令牌设置token.is_start_start = True,并将所有其他令牌保持不变。
查看这里的第二个示例:https://spacy.io/usage/processing-pipelines#custom-components-simple
发布于 2022-07-14 02:46:30
您可以通过使用
nlp = spacy.load('en_core_web_sm', exclude=["parser"])
text = 'Guest Blogging\nGuest Blogging allows the user to collect backlinks'
config = {"punct_chars": ['\n']}
nlp.add_pipe("sentencizer", config=config)
for sent in nlp(text).sents:
print("next sentence")
print(sent)输出:
next sentence
Guest Blogging
next sentence
Guest Blogging allows the user to collect backlinkshttps://stackoverflow.com/questions/71847391
复制相似问题