【问题标题】:Create synonyms and use regular expressions to find keyword创建同义词并使用正则表达式查找关键字
【发布时间】:2019-05-23 17:59:07
【问题描述】:

背景:

我想使用正则表达式来搜索关键字。但是,我的关键字有多个同义词。例如,关键字positive 可以包含以下我认为等于positive 的词:"+", "pos", "POS", "Positive", "POSITIVE"

我尝试过查看Create a dataframe with NLTK synonymshttp://www.nltk.org/howto/wordnet.html,但我认为这不是我要找的东西

目标:

1) 为给定关键字创建同义词(例如positive

2) 使用正则表达式在语料库中搜索关键字(例如positive

示例:

toy_corpus = 'patient is POS which makes them ideal to treatment '

我认为获得它的步骤如下所示:

1) 定义positive 的同义词 例如positive = ["pos", "POS", "Positive", "POSITIVE", "+"]

2) 使用正则表达式查找关键字POS

问题

我该如何实现这一目标?

【问题讨论】:

    标签: regex python-3.x nlp pattern-matching corpus


    【解决方案1】:

    试试看:

    import re
    question = "patient is POS which makes them ideal to treatment. And the the positive"
    find=["pos","POS","positive"]
    
    words=re.findall("\n+",question)
    result = [words   for words in find if words in question.split()]
    print(result)
    ['POS', 'positive']
    

    其中 \n 是单词边界。 维基:word boundary 更多示例:stackoverflow.com 最好的问候!

    【讨论】:

    • 快问:“\n+”是做什么的?
    • 欢迎来到 SO 并感谢您的回答。为了清楚起见,没有必要重新“安装”——它内置在 Python 中。此外,您的解决方案并没有真正使用正则表达式。使用[w for w in find if w in question.split()] 会得到相同的结果。
    • 感谢 ggorlen,无论如何 EER,您也可以尝试使用 \b ..is 单词边界 (regular-expressions.info/wordboundaries.html) 这里有更多示例:stackoverflow.com/questions/37543724/… Best Regards!
    • 看了你的链接还是不明白+\n之后的目的是什么?
    猜你喜欢
    • 1970-01-01
    • 2018-08-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2015-05-13
    • 1970-01-01
    • 2011-05-20
    • 2012-05-11
    相关资源
    最近更新 更多