在句子中搜索关键字 (Python)
Search key words in a sentence (Python)
我有以下代码:
import re
sentences=['the Research and development cost of the year']
rdterms=['research and development']
rdterms_regex = [re.compile(r'\b' + term + r'\b')
for term in rdterms]
def rdsentence(sentence:str):
"""Checks whether a sentence is R&D-oriented."""
for term in rdterms_regex:
if term.search(sentence, re.IGNORECASE):
return True
return False
for sentence in sentences:
print(rdsentence(sentence))
代码的目的是检测rdterms中的关键字(即'research and development')是否出现在句子中(即'the Research and development cost of the year')。
如果句子是“当年的研发费用”--本期报表“正确”--正确。
如果句子是“当年的研发成本”--当前报告“假”--我要它报告“真”。
如果句子是“research and development”--当前报告“False”--我希望它报告“True”。
我的代码有什么问题?谢谢!
编译正则表达式时指定re.IGNORECASE
,而不是在搜索中:
rdterms_regex = [re.compile(r'\b' + term + r'\b', re.IGNORECASE)
for term in rdterms]
def rdsentence(sentence:str):
"""Checks whether a sentence is R&D-oriented."""
for term in rdterms_regex:
if term.search(sentence):
return True
return False
我有以下代码:
import re
sentences=['the Research and development cost of the year']
rdterms=['research and development']
rdterms_regex = [re.compile(r'\b' + term + r'\b')
for term in rdterms]
def rdsentence(sentence:str):
"""Checks whether a sentence is R&D-oriented."""
for term in rdterms_regex:
if term.search(sentence, re.IGNORECASE):
return True
return False
for sentence in sentences:
print(rdsentence(sentence))
代码的目的是检测rdterms中的关键字(即'research and development')是否出现在句子中(即'the Research and development cost of the year')。
如果句子是“当年的研发费用”--本期报表“正确”--正确。
如果句子是“当年的研发成本”--当前报告“假”--我要它报告“真”。
如果句子是“research and development”--当前报告“False”--我希望它报告“True”。
我的代码有什么问题?谢谢!
编译正则表达式时指定re.IGNORECASE
,而不是在搜索中:
rdterms_regex = [re.compile(r'\b' + term + r'\b', re.IGNORECASE)
for term in rdterms]
def rdsentence(sentence:str):
"""Checks whether a sentence is R&D-oriented."""
for term in rdterms_regex:
if term.search(sentence):
return True
return False