[英]Loosely searching multiple words in a sentence using python
I am trying to search words in a string but my output is false because "men" and "shirt" does not match in the string because of plural factor. 我正在尝试搜索字符串中的单词,但是我的输出为false,因为由于复数因素,字符串中的“ men”和“ shirt”不匹配。 What i am really looking for is to match " men " with " mens " and " shirt " with " shirts ".
我真正想要的是将“ 男装 ”与“ 男装 ”匹配,将“ 衬衫 ”与“ 衬衫 ”匹配。 How can I do that and if there is a easy way to accomplish this in python then please share.
我该怎么做,如果有简单的方法可以在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)
Output 输出量
False
您可以在NTLK
库中使用词形NTLK
(删除' NTLK
'等),也可以使用FUZZYWUZZY
库进行模糊字符串匹配。
One possibility is using Python's builtin difflib
module. 一种可能性是使用Python的内置
difflib
模块。 The function get_close_matches()
( doc ) might need some tuning: 函数
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))
Prints: 印刷品:
Word: "men" Close matches: ['mens']
Word: "shirt" Close matches: ['shirts']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.