A simple implementation of inverted indexes

Source: Internet
Author: User

a simple implementation of inverted indexes
Inverted index is a commonly used search engine algorithm, mainly used to achieve full text searching, the establishment of keywords and documents in the mapping relationship, a lot of powerful functions are built on this basis, about inverted index detailed description can see Wikipedia.  The following is done according to your own ideas, just to understand the workings of this data structure. TODO: If you want to search the full occurrence of a sentence, such as "What is it" can be searched for these words and then see the same file in the continuous location of the results can be, set operation.
package Mythought.invertedindex; import java.io.BufferedReader; import java.io.FileReader; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; public class Invertedindex { //key word <----> doc file Private map<string, set<string>> indexs = new hashmap<string, set< String>> (); //This is assumed to be small files Public void addfile (string fileName, string content) {string[] words = content. Split ("");For ( int i = 0; I < words. length; I+ +) {String Word = words[i]; //Only record first appeared position set<string> Wordindex = indexs. Get (word); if (wordindex = = null) { Wordindex = new hashset<string> (); indexs. Put (word, wordindex);            } wordindex. Add ("+fileName + ", " + I+")");        }    }    Public void addfile (String fileName) throws exception{BufferedReader br = new bufferedreader ( new filereader (fileName)); try {StringBuilder SB = new StringBuilder ();String line = br. ReadLine ();while (line! = null) { sb. Append (line); sb. Append (""); //per line direct connection Line = br. ReadLine ();            }This . AddFile (fileName, sb. toString ());} Catch(Exception e) { e. Printstacktrace ();} finally { BR. Close ();        }    }Public set<string> search (String keyword) {set<string> Results = indexs. Get (keyword); return new hashset<string> (results);    }Public static void main (string[] args) throws exception{ invertedindex Test = new invertedindex (); test. AddFile ("File1", "Hello Fuck World Todotodo"); test. AddFile ("File2", "go to get it if you want It"); test. AddFile ("C:/data/hello.txt");System. Out. println (test. Search ("it")); System. println (test. Search ("You")); System. Out. println (test. Search ("Vonzhou"));     }}
Operation Result:[(file2,7), (file2,3)][(file2,5)][(c:/data/hello.txt,4)]
Reference: 1.http://en.wikipedia.org/wiki/inverted_index

A simple implementation of inverted indexes

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.