示例用于近似字符串匹配的java代码或扩展用于近似字符串匹配的boyer-moore

时间:2010-06-13 23:48:21

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

我需要找到1.mismatch(错误播放音符),2。插入(附加演奏),& 3.删除(遗漏音符),在乐曲中(例如,存储在表格中的音符音符[字符串值])与参考音乐作品。

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

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

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

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

3 个答案:

答案 0 :(得分:1)

here,请参阅此结果页的第一个(Boyer.java)

答案 1 :(得分:1)

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

问题是这个 是一个复杂的字段,只是查看/复制一些示例代码可能无法帮助您理解正在发生的事情。

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

答案 2 :(得分:0)

这是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
        }