用于近似字符串匹配的示例java代码或用于近似字符串匹配的boyer moore扩展代码
我需要在一首乐曲(例如,存储在表中的音符音高[字符串值])与参考乐曲之间找到1.不匹配(音符播放不正确)、2.插入(额外播放)和3.删除(遗漏的音符) 这可以通过精确字符串匹配算法或动态规划/近似字符串匹配算法实现。然而,我意识到,由于识别不匹配、插入和删除注释,近似字符串匹配更适合我的问题。或Boyer moore的扩展版本,以支持近似字符串匹配用于近似字符串匹配的示例java代码或用于近似字符串匹配的boyer moore扩展代码,java,algorithm,dynamic-programming,string-matching,approximate,Java,Algorithm,Dynamic Programming,String Matching,Approximate,我需要在一首乐曲(例如,存储在表中的音符音高[字符串值])与参考乐曲之间找到1.不匹配(音符播放不正确)、2.插入(额外播放)和3.删除(遗漏的音符) 这可以通过精确字符串匹配算法或动态规划/近似字符串匹配算法实现。然而,我意识到,由于识别不匹配、插入和删除注释,近似字符串匹配更适合我的问题。或Boyer moore的扩展版本,以支持近似字符串匹配 Dictionary<char, int> ShiftSizeTable = new Dictionary<char, int&g
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
}
是否有关于示例java代码的链接,我可以尝试近似字符串匹配?我发现了复杂的解释和公式——但我希望我能用一些示例代码和简单的解释做得很好。或者我可以在boyer moore上找到任何扩展为近似字符串匹配的java代码示例吗?我理解boyer-moore的概念,但在调整它以支持近似字符串匹配(即支持不匹配、插入和删除)时遇到困难
还有什么是最有效的近似字符串匹配算法(如精确字符串匹配算法中的boyer moore)
非常感谢您的见解/建议。
非常感谢您可以从上的Wikipedia页面开始 问题是,这是一个复杂的字段,仅仅查看/复制一些示例代码可能无法帮助您理解正在发生的事情 编辑-此外,我不知道Boyer Moore会如何适应近似字符串匹配。这里是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
}
Dictionary ShiftSizeTable=new Dictionary();
//计算模式文本中每个元素的移位/跳过计数。这样我们可以在搜索时跳过给定文本中的许多字符。
public void预处理BMSBADMatchTable(字符[]模式字符)
{
ShiftSizeTable.Clear();
int totalCharacters=patternCharacters.Length;
对于(int lpIndex=0;lpIndex
谢谢Stephen。是的,我在维基百科上看过。在没有伪代码的情况下,很难在代码中找到答案。你是否知道至少有一些Papers或网站与修改的boyer moore有关的字符串匹配算法。?感谢您的timeBoyer Moore是一个精确的字符串匹配算法。可以修改它以支持近似字符串匹配。我甚至找到了一些关于这方面的论文和资料。一旦调整为支持近似字符串匹配,它类似于Boyer Moore Horspool算法。谢谢