[英]Loosely searching multiple words in a sentence using python
我正在尝试搜索字符串中的单词,但是我的输出为false,因为由于复数因素,字符串中的“ men”和“ shirt”不匹配。 我真正想要的是将“ 男装 ”与“ 男装 ”匹配,将“ 衬衫 ”与“ 衬衫 ”匹配。 我该怎么做,如果有简单的方法可以在python中完成,请分享。
strings = ['get-upto-70-off-on-mens-t-shirts']
words = ['men','shirt']
print map(lambda x: all(map(lambda y:y in x.split(),words)),strings)
输出量
False
您可以在NTLK
库中使用词形NTLK
(删除' NTLK
'等),也可以使用FUZZYWUZZY
库进行模糊字符串匹配。
一种可能性是使用Python的内置difflib
模块。 函数get_close_matches()
( doc )可能需要一些调整:
import difflib
strings = ['get-upto-70-off-on-mens-t-shirts']
words = ['men','shirt']
for w in words:
for s in strings:
s = s.split('-')
m = difflib.get_close_matches(w, s)
print('Word: "{}" Close matches: {}'.format(w, m))
印刷品:
Word: "men" Close matches: ['mens']
Word: "shirt" Close matches: ['shirts']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.