“Did you mean?” feature in Lucene.net

匿名 (未验证) 提交于 2019-12-03 03:06:01

问题:

Can someone please let me know how do I implement "Did you mean" feature in Lucene.net?

Thanks!

回答1:

You should look into the SpellChecker module in the contrib dir. It's a port of Java lucene's SpellChecker module, so its documentation should be helpful.

(From the javadocs:)

Example Usage:

  import org.apache.lucene.search.spell.SpellChecker;    SpellChecker spellchecker = new SpellChecker(spellIndexDirectory);   // To index a field of a user index:   spellchecker.indexDictionary(new LuceneDictionary(my_lucene_reader, a_field));   // To index a file containing words:   spellchecker.indexDictionary(new PlainTextDictionary(new File("myfile.txt")));   String[] suggestions = spellchecker.suggestSimilar("misspelt", 5); 


回答2:

AFAIK Lucene supports proximity-search, meaning that if you use something like:

field:stirng~0.5

(it s a tilde-sign)

will match "string". the float is how "tolerant" the search would be, where 1.0 is exact match and 0.0 is match everything (sort of).

Different parsers will however implement this differently.

A proximity-search is much slower than a fuzzy-search (stri*) so use it with caution. In your case, one would assume that if you find no matches on a regular search, you try a proximity-search to see what you find, and present "did you mean" based on the result somehow.

Might be useful to cache this sort of lookups for very common mispellings, for performance reasons.



回答3:

Google's "Did you mean?" is (probably; they're secretive, of course) implemented by consulting their query log. Look to see if people who searched for the query you're processing searched for something very similar soon after; if so, it indicates they made a mistake, and realized what they ought to be searching for.

Since you probably don't have a huge query log, you could approximate it. Take the query, split up the terms, see if there are any similar terms in the database (by edit distance, whatever); replace your terms with those nearby terms, and rerun the query. If you get more hits, that was probably a better query. Suggest it to the user. (And since you've already got the hits, and most people only look at the top 2 results, show them those.)



回答4:

Take a look at google code project called semanticvectors. There's a decent amount of discussion on the Lucene mailing lists for doing functionality like what you're after using it - however it is written in java.

You will probably have to parse and use some machine learning algorithms on your search logs to build a feature like this!



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!