java - 使用哪种数据结构或算法来安排字典数据以进行序列搜索?

标签 java algorithm data-structures dictionary

我有一本字典,里面有将近一百万个单词。我必须设计用于快速搜索字符序列的算法。

例如。如果用户键入 and,应用程序必须返回具有类似 random,sand,stand 序列的单词 ...ETC。

我现有的解决方案是在所有现有的单词中搜索匹配的正则表达式,但效率不高。 如果需要,我愿意重组现有数据库、字典缓存或在任何级别工作或者是否有一些现成的 java api?

最佳答案

http://lucene.apache.org/core/

看看这个,应该能满足你的要求。

final File INDEX_DIR = new File("index");  
try{  
    Class.forName("com.mysql.jdbc.Driver").newInstance();  
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "password");  
    StandardAnalyzer analyzer = new StandardAnalyzer();  
    IndexWriter writer = new IndexWriter(INDEX_DIR, analyzer, true);  
    System.out.println("Indexing to directory '" + INDEX_DIR + "'...");  
    indexDocs(writer, conn);  
    writer.optimize();  
    writer.close();  
} catch (Exception e) {  
    e.printStackTrace();  
}  

void indexDocs(IndexWriter writer, Connection conn) throws Exception {  
String sql = "select id, name, color from pet";  
Statement stmt = conn.createStatement();  
ResultSet rs = stmt.executeQuery(sql);  
while (rs.next()) {  
    Document d = new Document();  
    d.add(new Field("id", rs.getString("id"), Field.Store.YES, Field.Index.NO));  
    d.add(new Field("name", rs.getString("name"), Field.Store.NO,  Field.Index.TOKENIZED));  
    d.add(new Field("address", rs.getString("address"),Field.Store.NO, Field.Index.TOKENIZED));  
    writer.addDocument(d);  
  }  
}  

关于java - 使用哪种数据结构或算法来安排字典数据以进行序列搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15832925/

相关文章:

java - 使用数据网格源 2 的错误 Tapestry

java - 将 Java 转换为 Objective-C : Exception Handling

java - 数据报包接收缓冲区大小-java

java - configuration.yml 出现错误 : * Unrecognized field at: driverClass - DROPWIZARD

algorithm - 自选团队

algorithm - 棒切割算法的替代方法(递归)

algorithm - 特定数据结构的无碰撞散列函数

string - 字符串中模式的符号表示,并找到 "similar"子模式

c# - 从顶点组合中找到最小的不规则多边形(性能关键)

algorithm - 斯卡拉 : Sorting list of number based on another list