我使用文本blob在Python中编写了用于lemmatize句子的代码,但我没有得到预期的结果:
def get_lemmatize_text(transcript):
transcript = transcript.strip()
blob = TextBlob(transcript)
for word in blob:
expected_str = Word(word)
expected_str = expected_str.lemmatize()
return expected_str
print(get_lemmatize_text("he had not received the four letters we d sent him as he had been travelling for the whole of august and hadn t received any call or text from us . he has just arrived today and has called us straight away . he has also just of his account when he had asked for it to be cancelled before it switched from the first additions datestr . he says he received contact from us that we were looking into this but doesnot have that to hand"))
我得到以下输出:
d
出了什么问题?任何人都可以帮助我或纠正我吗?
答案 0 :(得分:0)
您的代码以明智的字符.split()
迭代句子将会处理它。然后你没有保留你的结果,这就是为什么你得到最后一次迭代的结果list
追加将会处理它。如果你解决了这两个问题你的代码将正常工作:)
试试这个,
blob = TextBlob(transcript).split()
result=[]
for word in blob:
expected_str = Word(word)
expected_str = expected_str.lemmatize()
result.append(expected_str)
return result #or try this return ' '.join(result)