如果句子被剪切,则从字符串中删除最后一句

问题描述 投票:0回答:1

我正在使用 Python OpenAI API 生成来自 GPT-4 的响应。问题是有时回复中的最后一句话会被删掉。如果最后一句话被删掉,我想把它删掉。例如,

This is a full sentence. A second sentence. This sentence is cu
应生成
This is a full sentence. A second sentence.
This is another example. Here the last punctuation is missing
应生成
This is another example.
此外,
Here we are again. How are you doing?
不应被剪切。

如何实现这一目标?这可以使用 nltk 或正则表达式来实现吗?

python python-3.x regex nltk
1个回答
0
投票
text='This is a full sentence. A second sentence. hello'
text=text.strip()+' '
text=text.replace('. ','. <new_line>')
sentences=text.split('<new_line>')
if  '. ' not in sentences[-1]:
    sentences.pop()
out=''.join(sentences)
© www.soinside.com 2019 - 2024. All rights reserved.