繁体   English   中英

soundex算法的数据结构?

[英]Data structure for soundex algorithm?

谁能为我建议在soundex算法程序中使用哪种数据结构? 使用的语言是Java。 如果有人以前用Java进行过这项工作。 该程序应具有以下功能:能够读取大约50,000个单词,应该能够阅读一个单词并返回具有相同soundex的相关单词

我不希望程序实现只提供关于使用哪种数据结构的建议。

提示:如果将SQL用作数据后端,则可以让SQL使用两个SQL函数SOUNDEX和DIFFERENCE处理它。

也许不是您想要的,但是许多人不知道MSsql具有这两个功能。

好了soundex可以通过直接传递给字符串来实现,因此不需要任何特殊的操作。

之后,可以将4个字符的代码视为整数键。

然后,只需构建一个字典即可存储由该整数键索引的单词集。 50,000个单词应该很容易装入内存,因此不需要花哨的东西。

然后浏览字典,每个存储桶都是一组相似的发音的单词。

实际上,这是perl中的整个程序:

#!/usr/bin/perl
use Text::Soundex;
use Data::Dumper;
open(DICT,"</usr/share/dict/linux.words");
my %dictionary = ();
while (<DICT>) {
        chomp();
        chomp();
        push @{$dictionary{soundex($_)}},$_;
}
close(DICT);
while (<>) {
        my @words = split / +/;
        foreach (@words) {
            print Dumper $dictionary{soundex($_)};
        }
}

我相信您只需要将原始字符串转换为soundex键,再转换为哈希表即可; 表中每个条目的值将是映射到该soundex的原始字符串的集合。

Google收藏夹中的MultiMap收藏界面(及其实现)将对您有用。

class SpellChecker
{

  interface Hash {
    String hash(String);
  }

  private final Hash hash;

  private final Map<String, Set<String>> collisions;

  SpellChecker(Hash hash) {
    this.hash = hash;
    collisions = new TreeSet<String, Set<String>>();
  }

  boolean addWord(String word) {
    String key = hash.hash(word);
    Set<String> similar = collisions.get(key);
    if (similar == null)
      collisions.put(key, similar = new TreeSet<String>());
    return similar.add(word);
  }

  Set<String> similar(String word) {
    Set<String> similar = collisions.get(hash.hash(word));
    if (similar == null)
      return Collections.emptySet();
    else
      return Collections.unmodifiableSet(similar);
  }

}

哈希策略可以是Soundex,Metaphone或您拥有的东西。 有些策略可能是可调的(输出多少个字符,等等)。

由于soundex是哈希,因此我将使用以soundex为键的哈希表。

您需要一个4字节的整数。

soundex算法始终返回4个字符的代码,如果使用ANSI输入,则会返回4个字节(以4个字母表示)。

因此,将返回的代码存储在哈希表中,将您的单词转换为代码,然后在哈希表中查找它。 真的很容易。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM