【问题标题】:sample java code for approximate string matching or boyer-moore extended for approximate string matching用于近似字符串匹配的示例 java 代码或用于近似字符串匹配的 boyer-moore 扩展
【发布时间】:2011-03-03 08:06:38
【问题描述】:

我需要在乐曲中找到 1.mismatch(错误演奏的音符)、2.insertion(附加演奏)和 3.deletion(遗漏的音符)(例如,存储在表格中的音符音高 [字符串值])对照参考乐曲。

这可以通过精确字符串匹配算法或动态编程/近似字符串匹配算法来实现。但是我意识到,由于识别不匹配、插入、删除注释,近似字符串匹配更适合我的问题。或 Boyer-moore 的扩展版本以支持大约。字符串匹配。

是否有任何示例 java 代码的链接我可以尝试近似字符串匹配?我找到了复杂的解释和方程式——但我希望我能用一些示例代码和简单的解释做得很好。或者我可以在 boyer-moore 上找到任何示例 java 代码扩展约。字符串匹配?我理解 boyer-moore 的概念,但是在调整它以支持大约。字符串匹配(即支持不匹配、插入、删除)。

还有什么是最有效的。字符串匹配算法(如精确字符串匹配算法中的 boyer-moore)?

非常感谢任何见解/建议。 非常感谢提前

【问题讨论】:

    标签: java algorithm dynamic-programming string-matching approximate


    【解决方案1】:

    您可以从approximate string matching 上的维基百科页面开始。

    问题在于这个是一个复杂的领域,仅仅查看/复制一些示例代码可能无法帮助您理解发生了什么。

    编辑 - 此外,我看不出 Boyer-Moore 如何适应近似字符串匹配。

    【讨论】:

    • 谢谢斯蒂芬。是的,我在维基百科中浏览过。在代码中很难弄清楚 - 至少没有伪代码。您对至少一些与修改后的博耶摩尔相关的论文或网站有任何想法吗?字符串匹配算法。?感谢您的宝贵时间
    • Boyer-Moore 是一种精确的字符串匹配算法。可以对其进行修改以支持近似字符串匹配。我什至在这方面找到了一些论文和资源。一旦调整到支持大约。字符串匹配它类似于 Boyer-Moore-Horspool 算法。谢谢
    【解决方案2】:

    这里是 C# Boyer-More 代码,可以将其转换为 BMH 或近似匹配。

    Dictionary<char, int> ShiftSizeTable = new Dictionary<char, int>();
    
            //Calculate Shifit/Skip count for each element in pattern text. So that we can skip that many no of Characters in given text while searching.
            public void PreProcessBMSBadMatchTable(char[] patternCharacters)
            {
                ShiftSizeTable.Clear();
    
                int totalCharacters = patternCharacters.Length;
    
                for (int lpIndex = 0; lpIndex < totalCharacters; lpIndex++)
                {
                    //Calculate the shift size for each character in the string or char array.
                    int ShiftSize = Math.Max(1, (totalCharacters - 1) - lpIndex);
    
                    //If the charater is already exists in the ShiftSize table then replace it else add it to ShiftSize table.
                    if (ShiftSizeTable.ContainsKey(patternCharacters[lpIndex]))
                    {
                        ShiftSizeTable.Remove(patternCharacters[lpIndex]);
                    }
    
                    ShiftSizeTable.Add(patternCharacters[lpIndex], ShiftSize);
                }
            }
    
            //Use the PreProcessed Shift/Skip table to find the pattern Characters in text and skip the bad Characters in the text.
            public int BoyerMooreSearch1UsingDictionary(char[] textCharacters, char[] patternCharacters)
            {        
                PreProcessBMSBadMatchTable(patternCharacters);
    
                int SkipLength;
                int patternCharactersLenght = patternCharacters.Length;
                int textCharactersLenght = textCharacters.Length;
    
                // Step2. Use Loop through each character in source text use ShiftArrayTable to skip the elements.
                for (int lpTextIndex = 0; lpTextIndex <= (textCharactersLenght - patternCharactersLenght); lpTextIndex += SkipLength)
                {
                    SkipLength = 0;
    
                    for (int lpPatIndex = patternCharactersLenght - 1; lpPatIndex >= 0; lpPatIndex--)
                    {
                        if (patternCharacters[lpPatIndex] != textCharacters[lpTextIndex + lpPatIndex])
                        {
                            SkipLength = Math.Max(1, lpPatIndex - ShiftSizeTable[patternCharacters[lpPatIndex]]);
                            break;
                        }
                    }
                    if (SkipLength == 0)
                    {
                        return lpTextIndex;    // Found
                    }
                }
                return -1; // Not found
            } 
    

    【讨论】:

      猜你喜欢
      • 2011-05-11
      • 2013-07-10
      • 1970-01-01
      • 2020-07-31
      • 2010-09-08
      • 2016-03-28
      • 2013-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多