正则表达式和bigrams

时间:2015-07-27 19:00:54

标签: python regex

我有一个bigrams列表:

  

['只希望','请修理','请添加','只请求','只是希望& #39]

和字符串列表:

  

['这是一个很棒的实用工具。我的唯一愿望是获得新的同步   功能。','效果不佳,请修复问题。','很棒,   工作良好。 只是希望他们不断添加新的实用工具。','我的仅限   请求是否添加了新的ui']

我需要在字符串列表中搜索这些bigrams(假设我可以处理大写/小写),我不确定正则表达式是否是在字符串列表中查找这些bigrams的最佳方式,任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

没有正则表达式的一种方法:

bigrams = ['only wish', 'please fix', 'please add', 'only request', 'just hope']
text = ['this is a wonderful utility. My only wish is to get a new sync feature.', 'Does not work well, please fix the problem.', 'Great, works fine. just hope they keep adding new utilities.', 'My only request is they add a new ui']

for string in text:
    for bigram in bigrams:
        if bigram in string.lower():
            print bigram + ' in ' + string