簡體   English   中英

在java中查找模式匹配詞的最快方法

[英]Fastest way to lookup pattern matching words in java

給定一個總字數在 100,000-500,000 之間的字典,查找模式/掩碼的最快方法是什么? 其中 '-' 是一個未知的字母,即 s--t- 將返回鹽、咸、糞、蘇格蘭等......

目前使用的 trie 非常適合填充首字母的單詞,但是當存在諸如 ---st 或 -tr- 之類的模式時,trie 的好處就完全喪失了。

我正在搜索的單詞的分布基本上是均勻分布的,其中填充了第一個字母,而那些沒有填充。

將單詞加載到 SQL 數據庫中然后使用 SQL 通配符搜索功能是否有意義? 或者我只是手動搜索每個可能的字母組合以獲得空白字母的哈希圖怎么樣?

希望您能提供任何見解。

下面的小方法利用String#matches()方法以及動態創建的正則表達式,該正則表達式基於搜索條件字符串中提供的通配符。 它將返回一個字符串列表( List<String> ),其中包含找到的與提供的條件字符串匹配的任何單詞。

我通過(使用BufferedReader(FileReader) )運行搜索條件字符串( "s--t-" )的單詞列表文件包含 370,108 個單詞,通常在大約 250 毫秒或 0.25 秒(平均)內完成任務。

對於通配符,最常用的通配符是星號 ( * ),通常表示字符串中的零個或多個字符,以及問號 ( ? ),通常表示任何一個字符。 您顯然想使用連字符 (-) 代替通常的問號,這是可以的。 提供的方法可以根據您的特定目的處理同一條件字符串中的所有三種通配符類型( *?- )。

public static List<String> searchForWord(String dictionaryFilePath, 
                                         String searchCriteria) {
    // This method ignores letter case!
    List<String> foundList = new ArrayList<>();  // To hold all found words.

    // Convert the supplied criteria string to a Regular Expression 
    // for the String#matches() method located in the 'while' loop.
    String regEx = searchCriteria.replace("?", ".").replace("-", ".").replace("*", ".*?").toLowerCase();

    // 'Try With Resources' use here to auto-close the reader.
    try (BufferedReader reader = new BufferedReader(new FileReader(dictionaryFilePath))) {
        String line = "";
        while ((line = reader.readLine()) != null) {
            line = line.trim().toLowerCase();
            if (line.matches(regEx)) {
                foundList.add(line);  // There's a match...add to the List.
            }
        }
    }
    // catch Exceptions (if any).
    catch (FileNotFoundException ex) {
        System.err.println(ex);
    }
    catch (IOException ex) {
        System.err.println(ex);
    }
    return foundList;  // Return the List.
} 

要使用此方法:

List<String> list = searchForWord("WordFile.txt", "s--t-");
for (String str : list) {
    System.out.println(str);
}

從我使用的單詞列表中找到的匹配項:

saeta    saite    saith    sakti    salta
salts    salty    santa    santo    santy
saute    sauty    scats    scatt    scote
scots    scott    scuta    scute    scuts
scyth    seats    sects    seity    senti
sents    septa    septi    septs    serta
sesti    sexto    sexts    sheth    shita
shits    shote    shots    shott    shute
shuts    sidth    sifts    silts    silty
sinto    sintu    sitta    sixte    sixth
sixty    skate    skats    skete    skite
skits    skyte    slate    slath    slats
slaty    slete    slite    slits    slote
sloth    slots    sluts    smeth    smite
smith    smote    smuts    smyth    snath
snite    snits    snitz    snots    softa
softs    softy    sooth    soots    sooty
sorts    sorty    south    sowte    spate
spath    spats    spete    spite    spits
spitz    spots    sputa    spute    sruti
state    stats    stets    stite    stith
stott    suets    suety    suite    suits
suity    sutta    swath    swati    swats
swith    swots    syftn

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM