Java implements the "are you looking for" function for Google and Baidu

Source: Internet
Author: User
ArticleDirectory
    • References

Background:

When you use the search engine and e-commerce search functions, you must have encountered the following situation: I want to search for a blog site, but I may lose my chance to become a blogger, you don't have to worry about not finding the desired results, because the search engine built on big data will help you automatically correct the results. In this example, Google and Baidu return to me:

The following query results are displayed:BlogParkAnd  Are you looking:Blog Park,They all implement automatic error correction,I have written about automatic error correction before.A rough articleAt that time, it was self-implemented n-gram model, but the effect was not very good, mainly for Different CorpusAlgorithmThe accuracy is different. I want to try another algorithm. Currently, the mainstream distance between strings is calculated (on the contrary, you can also think of it as similarity ).Levenshtein: To Realize That Lucene has already done this, let's grow on the shoulders of giants.

Reference package:

Lucene-core-3.1.0.jar + lucene-spellchecker-3.1.0.jar, you canHere we get

Example:

Add the following to the main method of the class spellcorrector:Code

 1   //  Create directory  2 File dict = New File ("" );  3 Directory directory = Fsdirectory. Open (dict );  4  5   //  Instantiate the spelling checker  6 Spellchecker sp = New  Spellchecker (directory );   8   9   //  Create a dictionary  10 File dictionary = New File (spellcorrecter. Class . Getresource ("dictionary.txt" ). GetFile ());  11  12   //  Index a dictionary  13 Sp. indexdictionary ( New  Plaintextdictionary (dictionary ));  15   16   //  Search with typos  17 String search = "do not disturb" ;  18    19   20   // Suggested number. Here I only want the nearest one. You can set it to another number, for example, 3.  21   Int Suggestionnumber = 1 ;  22    24   //  Obtain recommended keywords  25 String [] suggestions = Sp. suggestsimilar (search, suggestionnumber );  27   28   //  Display result  29 System. Out. println ("Search:" +Search );  31   32   For  (String word: Suggestions ){  33 System. Out. println ("are you looking for?" + Word );  34   }  35   

Note: before that, you need to have a corpus. Here is a file that stores the correct video name. The format is as follows:

1 Red and bloody tears2 Fire on ice3 At the enemy's hand4 Chi Feng competing ship Wang second5 Golden Turtle6 No. 1 Xiaoxiang Road7 Season 28 Prairie wolf jazz9Saving big soldier Ryan

Now, let's run it directly. See:

Complete code and dictionary here(For work reasons, the dictionary only keeps some movie names. You can use your own corpus)

If you have any questions, contact jeexianwu # gmail.com.

References
    • http://lucene.apache.org/java/docs/
    • http://today.java.net/pub/a/today/2005/08/09/didyoumean.html
    • http://archsofty.blogspot.com/2009/12/adicione-o-recurso-voce-quis-dizer-nas.html
    • http://lucene.apache.org/java/3_0_0/api/contrib-spellchecker/index.html
    • http://en.wikipedia.org/wiki/Edit_distance
    • http://en.wikipedia.org/wiki/Levenshtein_distance
    • http://en.wikipedia.org/wiki/Jaro-Winkler_distance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.