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

Removing last sentence from a string if sentence is cut [closed]

提问人:BlackHawk 提问时间:10/28/2023 更新时间:10/28/2023 访问量:54

问:


想改进这个问题吗?更新问题,使其仅通过编辑这篇文章来关注一个问题。

25天前关闭。

我正在使用 Python OpenAI API 从 GPT-4 生成响应。问题是有时响应中的最后一句话被剪掉了。如果这句话被删掉了,我想删除最后一句话。例如,should result in 和 should result in Further,不应剪切。This is a full sentence. A second sentence. This sentence is cuThis is a full sentence. A second sentence.This is another example. Here the last punctuation is missingThis is another example.Here we are again. How are you doing?

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

蟒蛇 python-3.x 正则表达式 nltk

评论

1赞 John Gordon 10/28/2023
如果字符串不以 结尾,则删除最后一个 .难点是什么?.!?.!?
1赞 InSync 10/28/2023
如果响应根本不包含此类标点符号怎么办?

答:

0赞 Ajay Pun Magar 10/28/2023 #1
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)