gpt4 book ai didi

python - 返回给定字符数下的句子的函数

转载 作者:行者123 更新时间:2023-11-28 20:26:22 26 4
gpt4 key购买 nike

让我们假设我有以下段落:

"This is the first sentence. This is the second sentence? This is the third
sentence!"

我需要创建一个函数,它只返回给定字符数下的句子数。如果小于一个句子,则返回第一个句子的所有字符。

例如:

>>> reduce_paragraph(100)
"This is the first sentence. This is the second sentence? This is the third
sentence!"

>>> reduce_paragraph(80)
"This is the first sentence. This is the second sentence?"

>>> reduce_paragraph(50)
"This is the first sentence."

>>> reduce_paragraph(5)
"This "

我从这样的事情开始,但我似乎无法弄清楚如何完成它:

endsentence = ".?!"
sentences = itertools.groupby(text, lambda x: any(x.endswith(punct) for punct in endsentence))
for number,(truth, sentence) in enumerate(sentences):
if truth:
first_sentence = previous+''.join(sentence).replace('\n',' ')
previous = ''.join(sentence)

最佳答案

由于英语的句法结构,处理句子非常困难。正如有人已经指出的那样,即使是最好的正则表达式,缩写等问题也会引起无休止的头痛。

您应该考虑 Natural Laungauge Toolkit .特别是 punkt模块。它是一个句子分词器,它将为您完成繁重的工作。

关于python - 返回给定字符数下的句子的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12030470/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com