代码之家  ›  专栏  ›  技术社区  ›  Ninja Warrior 11

将输入中的至少两个单词与语句匹配

  •  1
  • Ninja Warrior 11  · 技术社区  · 6 年前

    我正在努力编写一个regex,它至少匹配两个单词,以防1匹配a到B does 或者输入A中的任何字典单词,因此在案例2中没有问题这个 Wakanda exist 如果1-A应该匹配B,假设 do , in ,和 the 已删除。

    CASE 1
    A -> Do Wakanda exist in the world?
    B -> Does Wakanda exist?
    >> A should match B
    
    exclude = ['do', 'in', 'the']
    A = "Do Wakanda exist in the world?"
    B = "Does Wakanda exist?"
    split_A = A.lower().split()
    final_A = [i if i not in exclude else '' for i in split_A]
    A = " ".join(' '.join(final_A).strip().split())
    
    CASE 1
    A -> wakanda exist world?
    B -> Does Wakanda exist?
    >> A should match B
    

    CASE 2
    A -> Does Atlantis exist in our world?
    B -> Does Wakanda exist?
    >> A should not match B
    
    2 回复  |  直到 6 年前
        1
  •  2
  •   Andrej Kesely    6 年前

    你可以用 set 检查两个句子是否匹配的操作(不需要使用regex,但需要进行一些预处理-remove ? ,将句子小写等):

    A = "Do Wakanda exist in the world?"
    B = "Does Wakanda exist?"
    
    A2 = "Does Atlantis exist in our world?"
    B2 = "Does Wakanda exist?"
    
    exclude = ['do', 'in', 'the', 'does']
    
    def a_match_b(a, b):
        a = set(a.replace('?', '').lower().split()) - set(exclude)
        b = set(b.replace('?', '').lower().split()) - set(exclude)
        return len(a.intersection(b)) > 1
    
    print(a_match_b(A, B))
    print(a_match_b(A2, B2))
    

    输出为:

    True
    False
    

    编辑:

    正如@tobias_k所说,您可以使用regexp来查找单词,因此您也可以使用:

    import re
    
    A = "Do Wakanda exist in the world?"
    B = "Does Wakanda exist?"
    
    A2 = "Does Atlantis exist in our world?"
    B2 = "Does Wakanda exist?"
    
    exclude = ['do', 'in', 'the', 'does']
    
    def a_match_b(a, b):
        words_a = re.findall(r'[\w]+', a.lower())
        words_b = re.findall(r'[\w]+', b.lower())
        a = set(words_a) - set(exclude)
        b = set(words_b) - set(exclude)
        return len(a.intersection(b)) > 1
    
    print(a_match_b(A, B))
    print(a_match_b(A2, B2))
    
        2
  •  0
  •   Code Jockey    6 年前

    编辑:

    这是一个更“纯”的regex解决方案,如果它在您使用的任何regex解析器中运行:

    将字符串连接到“||”并尝试与此正则表达式匹配:

    (?i).*?(\b\w+\b).*?(\b\w+\b).*?\|\|(?:.*\b\1\b.*\b\2\b.*|.*\b\2\b.*\b\1\b.*)

    所以,用绳子跑 wakanda exist world||Does Wakanda exist? 它将与两组匹配: wakanda exist

    如果你穿上它 wakanda xist ello world||does exist wakanda hello 两个不匹配,因为 瓦坎达 比赛。。。

    其他更详细和可扩展的解决方案:

    转弯 "wakanda exist world?" “\ bWakaDa\b\\b\\bWork\b”,但是您喜欢,并在第二个字符串上运行它,得到匹配,像 瓦坎达 ,然后删除 瓦坎达 从你的列表中再次运行它如果你有第二场比赛,那你就很好。

    因为您还没有将Python指定为语言标记,而且我不知道Python,所以我将提供JavaScript来实现这一点,如果需要的话,您可以修改它

    var simplifiedSentence1 = "wakanda exist world?";
    var simplifiedSentence2 = "Does Wakanda exist?"
    
    matchExp = new RegExp(".*?("
        + simplifiedSentence1
            .replace(/\W+/g,"|")
            .replace(/^\||\|$/,"")
            .replace(/(\w+)/g,"\\b$1\\b")
        + ")","i");
    match = matchExp.exec(simplifiedSentence2)[1];
    matchExp2 = new RegExp("\\b" + match + "\\b\\W*", "i");
    TwoWordsMatched = matchExp.test(simplifiedSentence2.replace(matchExp2, ""));
    

    TwoWordsMatched 如果两个单词在两个语句之间匹配,则为true;如果一个或更少的单词匹配,则为false