簡體   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