Python 在使用spacy NLP进行总结时,在每一句后面添加句号“.”

Python 在使用spacy NLP进行总结时,在每一句后面添加句号“.”,python,python-3.x,nlp,spacy,Python,Python 3.x,Nlp,Spacy,为了使用heapq或gensim进行总结,在执行文本清理后,我想在从清理后的文本中获得的每一行句子后添加一个句号。如果我没有一个句号,heapq或Gensim将无法理解不同的句子,并将把所有的句子作为一个句子。我正在使用以下代码: import en_core_web_sm nlp = en_core_web_sm.load() text = nlp(str1_clean_summary) for sent in text.sents: print(sent.string.strip())

为了使用heapq或gensim进行总结,在执行文本清理后,我想在从清理后的文本中获得的每一行句子后添加一个句号。如果我没有一个句号,heapq或Gensim将无法理解不同的句子,并将把所有的句子作为一个句子。我正在使用以下代码:

import en_core_web_sm
nlp = en_core_web_sm.load()
text = nlp(str1_clean_summary)

for sent in text.sents:
  print(sent.string.strip())
str1\u clean\u摘要如下所示:

many price increase options 
still believe us need prove consistently 
aim please delay end displeasingich
responds wuickly

这给了我不同行的句子,但我需要在每个句子后面加一个句号,这样它们就可以单独处理了

如果你不想处理跨度索引,我建议你在运行spacy之前在每个句子中添加最后一个点

例:

输出:

许多价格上涨的选择

仍然相信我们需要始终如一的证明

目标请延迟结束不愉快

他怯懦地回答

否则,您将不得不使用标记定位跨是标记列表,并且由于spacy内部组织其词汇表和其他资源的方式,跨中的标记是指向标记字典的指针。要添加一个新标记,您必须将每个跨度的尾部向前移动,这比只玩一个简单的替换更糟糕。
阅读更多信息。

对于文本发送。sents:printssent.string.strip只打印没有前导/尾随空格的句子,你的意思是,如果输出丢失了,你还想在输出中添加一个点吗?如果你给我们一些输入文本str1\u clean的例子,会有帮助的_summary@WiktorStribiżew:是的,我想在这段代码之后的每一行输出之后添加一个句号。我已经分享了上面的输出集。那么为什么不这样做呢?printsent.string.strip+'.'。或者打印'{}.'.formatsent.string.strip,printf'{sent.string.strip}.'的副本
import en_core_web_sm
sents = "many price increase options\nstill believe us need prove consistently\naim please delay end displeasingich\nresponds wuickly\n"
sents = sents.replace('\n', '.\n')

nlp = en_core_web_sm.load()
text = nlp(sents)

for sent in text.sents:
  sentence = sent
  print(sentence)