Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:如何在字符串列表中找到最可能的字符串?_Java_String_Soundex - Fatal编程技术网

Java:如何在字符串列表中找到最可能的字符串?

Java:如何在字符串列表中找到最可能的字符串?,java,string,soundex,Java,String,Soundex,我有一个Java字符串列表,其中包含拼写不同(并非完全不同)的人的名字。例如,John可能拼写为Jon、Jawn、Jaun等。如何检索此列表中最合适的字符串。如果有人能在这种情况下建议一种使用Soundex的方法,那将是非常有帮助的 您已经使用了算法,有几种策略可以实现这一点。Blur是基于Trie的Java实现,基于Levenshtein单词距离进行近似字符串匹配 还有另一种实现它的策略,称为boyer-moore近似字符串匹配算法 使用此算法和Levenshtein单词距离解决这些问题的常用

我有一个Java字符串列表,其中包含拼写不同(并非完全不同)的人的名字。例如,John可能拼写为Jon、Jawn、Jaun等。如何检索此列表中最合适的字符串。如果有人能在这种情况下建议一种使用Soundex的方法,那将是非常有帮助的

您已经使用了算法,有几种策略可以实现这一点。Blur是基于Trie的Java实现,基于Levenshtein单词距离进行近似字符串匹配

还有另一种实现它的策略,称为boyer-moore近似字符串匹配算法

使用此算法和Levenshtein单词距离解决这些问题的常用方法是将输入与可能的输出进行比较,并选择与所需输出之间距离最小的输出。

如果在索引文本时使用


搜索是索尔的专长。并搜索类似发音的单词。但是,如果您只是想要这个,而不想要solr提供的其他功能,那么您可以使用可用的源代码。

有一个jar文件用于匹配近似字符串

通过链接下载frej.jar

这个jar文件中有一个方法

Fuzzy.equals("jon","john");

在这种类型的近似字符串中,它将返回true。

有很多理论和方法来估计两个字符串的匹配

给出一个直截了当的真/假结果似乎很奇怪,因为“jon”实际上并不等于“john”,虽然很接近,但并不匹配

实现许多估算方法的一项伟大的学术工作叫做“SecondString.jar”——

大多数实现的方法都会给比赛打分,这个分数取决于所使用的方法

例如: 让我们将“编辑距离”定义为str1中到达str2所需的字符更改数 在本例中,“jon”->“john”需要添加1个字符
当然,对于这种方法,分数越低越好

本文提供了关于基于Trie的近似字符串匹配Java实现的详细解释和完整代码:

搜索函数返回小于给定值的所有单词的列表 与目标词的最大距离 def搜索(word、maxCost):

上面的搜索函数使用这个递归帮助器。它假定 上一行已填写完毕。 def searchRecursive(节点、字母、单词、上一行、结果、最大成本):

columns=len(word)+1
currentRow=[previousRow[0]+1]
#为字母构建一行,目标中的每个字母都有一列
#word,第0列的空字符串加1
对于xrange中的列(1,列):
insertCost=currentRow[列-1]+1
deleteCost=上一行[列]+1
如果word[第1列]!=信函:
替换成本=上一行[列-1]+1
其他:
replaceCost=上一行[列-1]
currentRow.append(最小值(插入成本、删除成本、替换成本))
#如果行中的最后一个条目指示最佳成本小于
#最大成本,并且在这个trie节点中有一个单词,然后添加它。
如果当前行[-1]
# build first row
currentRow = range( len(word) + 1 )

results = []

# recursively search each branch of the trie
for letter in trie.children:
    searchRecursive( trie.children[letter], letter, word, currentRow, 
        results, maxCost )

return results
columns = len( word ) + 1
currentRow = [ previousRow[0] + 1 ]

# Build one row for the letter, with a column for each letter in the target
# word, plus one for the empty string at column 0
for column in xrange( 1, columns ):

    insertCost = currentRow[column - 1] + 1
    deleteCost = previousRow[column] + 1

    if word[column - 1] != letter:
        replaceCost = previousRow[ column - 1 ] + 1
    else:                
        replaceCost = previousRow[ column - 1 ]

    currentRow.append( min( insertCost, deleteCost, replaceCost ) )

# if the last entry in the row indicates the optimal cost is less than the
# maximum cost, and there is a word in this trie node, then add it.
if currentRow[-1] <= maxCost and node.word != None:
    results.append( (node.word, currentRow[-1] ) )

# if any entries in the row are less than the maximum cost, then 
# recursively search each branch of the trie
if min( currentRow ) <= maxCost:
    for letter in node.children:
        searchRecursive( node.children[letter], letter, word, currentRow, 
            results, maxCost )