我有以下字符串:
"The boy went to twn and bought sausage and chicken. He then picked a tddy for his sister"
Run Code Online (Sandbox Code Playgroud)
要提取的单词列表:
["town","teddy","chicken","boy went"]
Run Code Online (Sandbox Code Playgroud)
注意:town 和 teddy 在给定的句子中拼写错误。
我尝试了以下方法,但我得到了不属于答案的其他词:
import difflib
sent = "The boy went to twn and bought sausage and chicken. He then picked a tddy for his sister"
list1 = ["town","teddy","chicken","boy went"]
[difflib.get_close_matches(x.lower().strip(), sent.split()) for x in list1 ]
Run Code Online (Sandbox Code Playgroud)
我得到以下结果:
[['twn', 'to'], ['tddy'], ['chicken.', 'picked'], ['went']]
Run Code Online (Sandbox Code Playgroud)
代替:
'twn', 'tddy', 'chicken','boy went'
Run Code Online (Sandbox Code Playgroud)