4

您好,我有一个 32mb 的文件。它是一个简单的字典文件,编码为 1250,其中包含 280 万行。每一行只有一个唯一的单词:

cat
dog
god
...

我想使用 Lucene 搜索特定单词字典中的每个字谜。例如:

我想搜索单词dog和 lucene 的每个字谜应该搜索我的字典并返回doggod。在我的 webapp 中,我有一个 Word 实体:

public class Word {
    private Long id;
    private String word;
    private String baseLetters;
    private String definition;
}

baseLetters 是按字母顺序排序的变量,用于搜索此类字谜[神和狗词将具有相同的 baseLetters:dgo]。我成功地在不同的服务中使用这个 baseLetters 变量从我的数据库中搜索了这样的字谜,但是我在创建字典文件的索引时遇到了问题。我知道我必须添加到字段:

word 和 baseLetters 但我不知道该怎么做 :( 有人可以告诉我一些实现这个目标的方向吗?

现在我只有这样的东西:

public class DictionaryIndexer {

private static final Logger logger = LoggerFactory.getLogger(DictionaryIndexer.class);

@Value("${dictionary.path}")
private String dictionaryPath;

@Value("${lucene.search.indexDir}")
private String indexPath;

public void createIndex() throws CorruptIndexException, LockObtainFailedException {
    try {
        IndexWriter indexWriter = getLuceneIndexer();
        createDocument();           
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    }       
 }

private IndexWriter getLuceneIndexer() throws CorruptIndexException, LockObtainFailedException, IOException {
    StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
    IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_36, analyzer);
    indexWriterConfig.setOpenMode(OpenMode.CREATE_OR_APPEND);
    Directory directory = new SimpleFSDirectory(new File(indexPath));
    return new IndexWriter(directory, indexWriterConfig);
}

private void createDocument() throws FileNotFoundException {
    File sjp = new File(dictionaryPath);
    Reader reader = new FileReader(sjp);

    Document dictionary = new Document();
    dictionary.add(new Field("word", reader));
}

}

PS:还有一个问题。如果我在 Spring 中将 DocumentIndexer 注册为 bean,每次我重新部署我的 webapp 时,索引是否会创建/附加?未来的 DictionarySearcher 也会如此吗?

4

2 回答 2

8

Lucene 不是最好的工具,因为你不是在做搜索:你是在做查找。所有实际工作都发生在“索引器”中,然后您只需存储所有工作的结果。在任何散列类型的存储机制中,查找都可以是 O(1)。

这是您的索引器应该执行的操作:

  1. 将整个字典读成一个简单的结构,如SortedSetorString[]
  2. 创建一个空的HashMap<String,List<String>>(可能大小相同,以提高性能)用于存储结果
  3. 按字母顺序遍历字典(实际上任何顺序都可以,只要确保你点击了所有条目)
    1. 对单词中的字母进行排序
    2. 在您的存储集合中查找已排序的字母
    3. 如果查找成功,则将当前单词添加到列表中;否则,创建一个包含该单词的新列表并将其放入存储中Map
  4. 如果以后需要这张地图,把地图存到磁盘上;否则,将其保存在内存中
  5. 丢弃字典

这是您的查找过程应该执行的操作:

  1. 对示例单词中的字母进行排序
  2. 在您的存储集合中查找已排序的字母
  3. 打印List从查找返回的(或 null),注意从输出中省略示例单词

如果要节省堆空间,请考虑使用DAWG。你会发现你可以用几百千字节而不是 32MiB 来表示整个英语单词词典。我将把它作为练习留给读者。

祝你的家庭作业好运。

于 2012-12-28T16:35:19.840 回答
4

函数 createDocument() 应该是

private void createDocument() throws FileNotFoundException {
    File sjp = new File(dictionaryPath);
    BufferedReader reader = new BufferedReader(new FileReader(sjp));

    String readLine = null;
    while((readLine = reader.readLine() != null)) {
        readLine = readLine.trim();
        Document dictionary = new Document();
        dictionary.add(new Field("word", readLine));
        // toAnagram methods sorts the letters in the word. Also makes it
        // case insensitive.
        dictionary.add(new Field("anagram", toAnagram(readLine)));
        indexWriter.addDocument(dictionary);
    }
}

如果您将 Lucene 用于很多功能,请考虑使用Apache Solr,这是一个构建在 Lucene 之上的搜索平台。

您还可以为每个字谜组仅使用一个条目来建模您的索引。

{"anagram" : "scare", "words":["cares", "acres"]}
{"anagram" : "shoes", "words":["hoses"]}
{"anagram" : "spore", "words":["pores", "prose", "ropes"]}

这将需要在处理您的字典文件时更新索引中的现有文档。在这种情况下,Solr 将有助于使用更高级别的 API。例如,IndexWriter 不支持更新文档。Solr 支持更新。

这样的索引将为每个字谜搜索提供一个结果文档。

希望能帮助到你。

于 2013-01-01T11:11:36.777 回答