【问题标题】:I have an array of strings; is there a way to see which one an argument string is closest to?我有一个字符串数组;有没有办法查看参数字符串最接近哪一个?
【发布时间】:2015-03-29 15:20:23
【问题描述】:

我正在使用 Python,我决定分解并制作大量短语,以供语音识别模块的结果进行比较。到目前为止,我得到了:

phrases = [
    "what time is it",
    "what's the weather",
    "what's the date",
    "hello",
    "hi",
    "what's up",
    "how are you"
]

(我几分钟前才开始做这个,所以我还没有很多......主要只是一个大纲)但无论如何,我想要一个这样的函数......

def match(phrase):
    #match_greatest will start at zero but continuously update if the string
    #being compared has a higher percentage match
    match_greatest = 0

    #match will store the actual string that is closest
    match = ""

    for i in phrases:
        #this is the part I need help with...
        match_current = #somehow get the percentage that the argument phrase matches the phrase it's comparing to

        #if the current phrase is a closer match than before, update it
        if match_current > match_greatest:
            match_greatest = match_current
            match = i

    return match

...举个例子,如果我调用 match("what time it a") 或 match("what time sit") -- 这些是语音识别可能给出的误读示例 -- 并使用我当前的一组短语,它会返回“现在几点”。

【问题讨论】:

  • 迭代每个字符,如果两个位置的字符相同,即在输入的字符串和匹配的字符串中,增加一些变量并在末尾将该变量除以字符串的总长度你想匹配。换句话说,创建一个百分比匹配
  • 你用什么麦克风输入?

标签: python compare speech-recognition phrase


【解决方案1】:

字符串之间的合理距离之一是“编辑距离”或Levenshtein 距离。它计算将一个字符串转换为另一个字符串的编辑量(插入、删除和替换)。

Python实现在这里,需要动态编程

https://pypi.python.org/pypi/python-Levenshtein/

你也可以自己实现算法,很简单。

如果您想要面向语音的距离,值得考虑soundex,它是 Levenshtein 的一个特定扩展,用于解释单词的语音属性。见

https://pypi.python.org/pypi/Fuzzy

您可以遍历字符串并找到具有最小编辑距离的字符串。

【讨论】:

    【解决方案2】:

    这是我将如何做的一个例子。

    def match(phrase):
        phrases = [
        "what time is it",
        "what's the weather",
        "what's the date",
        "hello",
        "hi",
        "what's up",
        "how are you"
    ]
    
    
        match_word_dict = {}
        for element in phrases:
            sameness = 0
            for index in range(len(element)):
                if len(phrase) == index:
                    break
                if phrase[index] == element[index]:
                    sameness += 1
    
    
    
            percent = (sameness * 1.0 / len(element) * 1.0) * 100
            match_word_dict[element] = percent
        return match_word_dict
    
    print match("hello")
    print match("hel")
    

    我返回一个显示短语和百分比匹配的字典 这也是我将如何只打印匹配百分比最高的短语

    key, value = max(match("hello").iteritems(), key=lambda x:x[1])
    print key, value 
    

    【讨论】:

    • 随着复杂度的增加,您可以编写一个返回匹配百分比高于某个匹配值的方法。
    【解决方案3】:

    这只是一个尝试。我们可以包含许多可能性和其他测试用例,它们会导致比我在下面所做的更复杂的逻辑。

    phrases = {
        1: "what time is it",
        2: "what's the weather",
        3: "what's the date",
        4: "hello",
        5: "hi",
        6: "what's up",
        7: "how are you"
        }
    
    
    def match(phrase):
        """
        """
        phr_list = phrase.split()
        max_count = 0
        key = None
    
        for k, v in phrases.iteritems():
            count = sum(1 for word in phr_list if word.lower() in v.split())
    
            if count > max_count:
                count = max_count
                key = k
    
        if key:
            return phrases.get(key)
        return phrase
    
    
    print match("what time it a")
    
    print match("what time sit")    
    
    print match(" how you good")
    

    产量:

    what time is it
    what time is it
    how are you
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-18
      • 1970-01-01
      • 1970-01-01
      • 2016-03-04
      • 2011-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多