因此,罪魁祸首是它当前没有将输入的最后部分写到
output(例如,尝试“ Michael is Singing
stuff”-它应该正确地写所有内容并省略“
stuff”)。可能有一种更优雅的方式来处理此问题,但是您可以尝试的一件事是
else在
for循环中添加一个子句。由于问题在于最终单词未包含在中
output,因此我们可以
else用来确保在
for循环完成时添加最终单词:
def algorithm(input): print input p = PorterStemmer() while 1: output = '' word = '' if input == '': break for c in input: if c.isalpha(): word += c.lower() elif word: output += p.stem(word, 0,len(word)-1) word = '' output += c.lower() else: output += p.stem(word, 0, len(word)-1) print output return output
这已经通过两个测试用例进行了广泛的测试,因此很明显它是防弹的:)可能有一些边缘案例在那附近爬行,但是希望它可以帮助您入门。



