[英]What can I use for finding names words in two list? Python
I am interested in the finding of the same words in two lists.我有兴趣在两个列表中找到相同的单词。 I have two lists of words in the text_list I also stemmed the words.
我在 text_list 中有两个单词列表,我也词干了这些单词。
text_list = [['i', 'am', 'interest' ,'for', 'this', 'subject'], ['this', 'is', 'a', 'second', 'sentence']]
words_list = ['a', 'word', 'sentence', 'interesting']
So I need this output:所以我需要这个 output:
same_words= ['a', 'sentence', 'interest']
You need to apply stemming to both the lists, There are discrepancies for example interesting
and interest
and if you apply stemming to only words_list
then Sentence
becomes sentenc
so, therefore, apply stemmer to both the lists and then find the common elements:您需要对两个列表都应用词干提取,例如
interesting
和interest
之间存在差异,如果您仅对words_list
应用词干提取,则Sentence
变为sentenc
,因此,将词干分析器应用于两个列表,然后找到共同元素:
from nltk.stem import PorterStemmer
text_list = [['i', 'am', 'interest','for', 'this', 'subject'], ['this', 'is', 'a', 'second', 'sentence']]
words_list = ['a', 'word', 'sentence', 'interesting']
ps = PorterStemmer()
words_list = [ps.stem(w) for w in words_list]
text_list = [list(map(ps.stem,i)) for i in text_list]
answer = []
for i in text_list:
answer.append(list(set(words_list).intersection(set(i))))
output = sum(answer, [])
print(output)
>>> ['interest', 'a', 'sentenc']
There is a package called fuzzywuzzy
which allows you to match the string from a list with the strings from another list with approximation.有一个名为
fuzzywuzzy
的package,它允许您将列表中的字符串与另一个列表中的字符串进行近似匹配。
First of all, you will need to flatten your nested list to a list/set with unique strings.首先,您需要将嵌套列表展平为具有唯一字符串的列表/集。
from itertools import chain
newset = set(chain(*text_list))
{'sentence', 'i', 'interest', 'am', 'is', 'for', 'a', 'second', 'subject', 'this'}
Next, from the fuzzywuzzy
package, we import the fuzz
function.接下来,从
fuzzywuzzy
package,我们导入fuzz
function。
from fuzzywuzzy import fuzz
result = [max([(fuzz.token_set_ratio(i,j),j) for j in newset]) for i in words_list]
[(100, 'a'), (57, 'for'), (100, 'sentence'), (84, 'interest')]
by looking at here, the fuzz.token_set_ratio
actually helps you to match the every element from the words_list
to all the elements in newset
and gives the percentage of matching alphabets between the two elements.通过查看此处,
fuzz.token_set_ratio
实际上可以帮助您将 words_list 中的每个元素与words_list
中的所有元素进行newset
,并给出两个元素之间匹配字母的百分比。 You can remove the max
to see the full list of it.您可以删除
max
以查看它的完整列表。 (Some alphabets in for
is in the word
, that's why it's shown in this tuple list too with 57% of matching. You can later use a for loop and a percentage tolerance to remove those matches below the percentage tolerance) (
for
中的一些字母在word
中,这就是为什么它在这个元组列表中也显示了 57% 的匹配。稍后您可以使用 for 循环和百分比容差来删除低于百分比容差的匹配)
Finally, you will use map
to get your desired output.最后,您将使用
map
来获得您想要的 output。
similarity_score, fuzzy_match = map(list,zip(*result))
fuzzy_match
Out[40]: ['a', 'for', 'sentence', 'interest']
If your input is not the usual ASCII standard, you can put another argument in the fuzz.token_set_ratio
如果您的输入不是通常的 ASCII 标准,您可以在
fuzz.token_set_ratio
中放置另一个参数
a = ['У', 'вас', 'є', 'чашка', 'кави?']
b = ['ви']
[max([(fuzz.token_set_ratio(i, j, force_ascii= False),j) for j in a]) for i in b]
Out[9]: [(67, 'кави?')]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.