繁体   English   中英

使用该短语的所有可能近似值在字符串中搜索单词/短语

[英]Searching for a word/phrase in a string with all the possible approximations of the phrase

假设我有以下字符串:

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']

但当然,这不是一个普遍的解决方案。 所以我想知道在这种情况下是否有一种聪明的搜索方式?

也许使用这样的模式 (string\\w*)?

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_token=['machine','learning']

pattern='('+''.join(e+'\w* ' for e in tag_token).rstrip()+')'

rgx=re.compile(pattern,re.IGNORECASE)
rgx.findall(string)
#output
#['machine learning', 'machine12 learning', 'machines learning']

更难找到与标签中单词位置变化的匹配项

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM