所以我有一个文本流,其中下一个项目是所有先前生成的文本+一些附加文本,这些文本可能是新单词、字母或单词的一部分。我试图以一种允许我在流式传输时完全重建文本的方式有效地分割文本/排列文本。这是一个例子:
original text = "this is some prepended text which keeps showing up with the stream"
现在假设流是这样输入的,请注意注释字符串,它包含一个字母,该字母是以下示例中单词的延续,而不是独立的字母:
def dummy_generator():
yield 'I'm sorry to hear that you're feeling sad'
time.sleep(0.2)
yield '. Here are some suggestions that may help'
time.sleep(0.2)
yield 'uplift your mood:\n\n1. Take a walk outside'
time.sleep(0.2)
yield 'Fresh air and natural surroundings can do w' #For example, here there's a single letter which is continued in the next piece of text
time.sleep(0.2)
yield 'onders for your mood.\n\n2. Listen to music'
time.sleep(0.2)
yield 'Music has a powerful effect on our emotions'
time.sleep(0.2)
yield 'and can help lift our spirits.\n\n3. Practice'
time.sleep(0.2)
yield 'mindfulness: Taking a few minutes to focus on'
鉴于此,有没有一种方法可以在文本生成时有效地将其拆分为正确的单词?
看起来输入不明确,您需要一个对英语有一点了解的函数,并且可以选择是否将一行的最后一个单词与下一行的第一个单词合并,或者不合并。例如,该函数会比较
'w onders'
和 'wonders'
并决定 'wonders'
是更好的英语;同样,它会比较 'emotions and'
和 'emotionsand'
并决定 'emotions and'
是更好的英语。
对于
'outside Fresh'
和'music Music'
可以使用大写字母来决定不应该合并单词;但对于 'w onders'
和 'emotions and'
则需要参考英语词典。
可以帮助您找到合适词典的相关问题: