[英]exact and case insensitive match for a multi word token in a string python
我有一个包含一个单词和一个多单词令牌的列表。
brand_list = ['ibm','microsoft','abby softwate', 'tata computer services']
我需要搜索标题字符串中存在的所有这些单词。 我能够找到一个单词。 但是对于多单词令牌,我的代码失败。 这是我的代码。 请帮帮我。 这是我的解决方案。
import string
def check_firm(test_title):
translator = str.maketrans('', '', string.punctuation)
title = test_title.translate(translator)
if any(one_word.lower() in title.lower().split(' ') for one_word in brand_list):
status_code_value = 0
print("OEM word found")
else:
status_code_value = 1
print("OEM word not found")
print("current value of status code ------------>", status_code_value)
改变这个 :
if any(one_word.lower() in title.lower().split(' ') for one_word in brand_list):
对此 :
if title.lower() in brand_list:
因此 :
import string
brand_list = ['ibm','Microsoft','abby softwate', 'TATA computer services']
brand_list = [x.lower() for x in brand_list] # ['ibm', 'microsoft', 'abby softwate',
# 'tata computer services']
def check_firm(test_title):
translator = str.maketrans('', '', string.punctuation)
title = test_title.translate(translator)
if title.lower() in brand_list:
status_code_value = 0
print("OEM word found")
else:
status_code_value = 1
print("OEM word not found")
print("current value of status code ------------>", status_code_value)
check_firm('iBM')
check_firm('Tata Computer SERVICES')
check_firm('Khan trading Co.')
输出 :
OEM word found
current value of status code ------------> 0
OEM word found
current value of status code ------------> 0
OEM word not found
current value of status code ------------> 1
注意:我使用以下命令将列表中的所有元素转换为
lower()
:
brand_list = [x.lower() for x in brand_list]
这样可以确保正确进行比较。
编辑 :
OP : 但是我的输入图块是标题字符串。 例如“塔塔计算机服务获利x美元”。 在这种情况下,我们如何找到字符串?
在这种情况下,我会选择在传递给函数之前先分割字符串:
inp_st1 = 'iBM'
inp_st2 = 'Tata Computer SERVICES made a profit of x dollars'
inp_st3 = 'Khan trading Co.'
check_firm(inp_st1)
check_firm(" ".join(inp_st2.split()[:3])) # Tata Computer SERVICES
check_firm(inp_st3)
由于此代码,您将永远找不到两个单词:
title.lower().split(' ')
假设您的标题是tata计算机服务 ,那么执行该代码时,您将得到:
["tata", "computer", "services"]
然后在for循环中,您将仅搜索每个单数词,从本质上讲,您已经将标题分解为一些您无法匹配的东西。
用人类语言编写for循环 :
any(one_word.lower() in title.lower().split(' ') for one_word in brand_list)
如果在数组[“ tata”,“ computer”,“ services”]中可以找到brand_list中的任何单词,则为真。
如您所见, brand_list中的任何单词都无法匹配,因为该单词实际上由三个单词和空格“ tata计算机服务”组成。
要执行您想要的:
更改此:
if any(one_word.lower() in title.lower().split(' ') for one_word in brand_list):
至:
if any(one_word.lower() in title.lower() for one_word in brand_list):
这样,您就可以在标题中的brand_list中查找每个单词。 您的代码如下所示:
brand_list = ['ibm','microsoft','abby softwate', 'tata computer services']
import string
def check_firm(test_title):
translator = str.maketrans('', '', string.punctuation)
title = test_title.translate(translator)
if any(one_word.lower() in title.lower() for one_word in brand_list):
status_code_value = 0
print("OEM word found")
else:
status_code_value = 1
print("OEM word not found")
print("current value of status code ------------>", status_code_value)
check_firm("ibm")
check_firm("abby software")
check_firm("abby softwate apple")
具有以下输出:
OEM word found
current value of status code ------------> 0
OEM word not found
current value of status code ------------> 1
OEM word found
current value of status code ------------> 0
编辑
OP : 我尝试过您的解决方案。 问题是它对于“ tata computer servicessssssssss”之类的输入也同样适用。 解决这个问题的任何想法。 谢谢
在注释中强调指出,此代码将允许传递诸如tat computer servicesss之类的标题。 为了避免这种情况,我建议使用正则表达式,例如:
brand_list = ['ibm','microsoft','abby softwate', 'tata computer services']
import string
import re
def check_firm(test_title):
translator = str.maketrans('', '', string.punctuation)
title = test_title.translate(translator)
if any(re.search(r'\b' + one_word.lower() + r'\b', title) for one_word in brand_list):
status_code_value = 0
print("OEM word found")
else:
status_code_value = 1
print("OEM word not found")
print("current value of status code ------------>", status_code_value)
check_firm("tata computer services")
check_firm("tata computer servicessssss")
check_firm("tata computer services something else")
产量
OEM word found
current value of status code ------------> 0
OEM word not found
current value of status code ------------> 1
OEM word found
current value of status code ------------> 0
感兴趣的部分是:
any(re.search(r'\b' + one_word.lower() + r'\b', title) for one_word in brand_list):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.