[英]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.