【发布时间】:2021-12-26 03:34:47
【问题描述】:
假设我有以下字符串:
string = 'machine learning ml is a type of artificial intelligence ai that allows software applications to become more accurate at predicting outcomes without being explicitly programmed to do so machine12 learning algorithms use historical data as input to predict new output values machines learning is good'
进一步假设我有一个标签定义为:
tag = 'machine learning'
现在我希望在我的字符串中找到标签。从我的string 可以看出,我有三个位置machine learning,一个位于string 的开头,一个位于machine12 learning,最后一个位于machines learning。我希望找到所有这些并将输出列表设为
['machine learning', 'machine12 learning', 'machines learning']
为了能够做到这一点,我尝试使用 nltk 标记我的标签。那是
tag_token = nltk.word_tokenize(tag)
然后我将拥有['machine','learning']。然后我会搜索tag[0]。
我知道string.find(tag_token[0]) 和data.rfind(tag_token[0]) 将给出machine 的位置,用于第一个和最后一个查找,但如果我在文本中有更多machine learning 怎么办(这里我们有3 个)?
在那种情况下,我将无法将它们全部提取出来。所以我最初的想法是找到所有出现的machine 然后learning 会失败。我希望使用fuzzywuzzy 来分析['machine learning', 'machine12 learning', 'machines learning'] 的标签。
所以我的问题是 string 我有,我如何搜索标签及其近似值并将它们列出如下?
['machine learning', 'machine12 learning', 'machines learning']
更新:我现在知道我可以做到以下几点:
pattern = re.compile(r"(machine[\s0-9]+learning)",re.IGNORECASE)
matches = pattern.findall(data)
#[output]: ['machine learning', 'machine12 learning']
如果我这样做了
pattern = re.compile(r"(machine[\sA-Za-z]+learning)",re.IGNORECASE)
matches = pattern.findall(data)
#[output]: ['machine learning', 'machines learning']
但可以肯定的是,这并不是一个可推广的解决方案。所以我想知道在这种情况下是否有一种智能的搜索方式?
【问题讨论】:
标签: python regex full-text-search fuzzy-search fuzzywuzzy