次の段落があるとします。
"This is the first sentence. This is the second sentence? This is the third
sentence!"
特定の文字数以下の文の数のみを返す関数を作成する必要があります。1 文に満たない場合は、最初の文のすべての文字が返されます。
例えば:
>>> 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)