By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,654 Members | 1,517 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,654 IT Pros & Developers. It's quick & easy.

Implementing inverted index in Java

P: 8
I am trying to implement inverted index in java from few days..but I am unable to implement it.the term and term frequencies are coming nicely but I am unable to retrieve the document Id's.I am not getting the idea how to use two treemap, or how to wrap one treemap inside another treemap.

I am attaching the code here.
Expand|Select|Wrap|Line Numbers
  1. import java.util.*;  
  2. import java.io.*;  
  3.  
  4. public class invertindex{
  5.  
  6. public static void main (String[] args)
  7. {
  8.      TreeMap <String, Integer> t1 = new TreeMap<String, Integer>();
  9.    // TreeMap <String, TreeSet> t2 = new TreeMap<String, TreeSet>();
  10.      readFile(t1);  
  11.      //print(t1);  
  12. }
  13.  
  14. public static int getWord
  15.         (String word, TreeMap <String, Integer> t1 )
  16. {
  17.  if (t1.containsKey(word))
  18.  {
  19.     return t1.get(word);
  20.  }
  21.  else {
  22.      return 0;
  23.  }
  24. }
  25.  
  26.  
  27. public static void readFile(TreeMap <String, Integer> t1 )
  28. {
  29. //    t1.clear();
  30.     Scanner File;
  31.     String word; 
  32.     Integer count;
  33.     String Docs [] = {"words.txt", "words2.txt","words3.txt", "words4.txt",};
  34.    try  
  35.       {      
  36. for (int x=0; x<Docs.length; x++)  
  37.    t1.clear();
  38.  
  39.           File f= new File(Docs[x]);
  40.           BufferedReader br= new BufferedReader(new FileReader(f));
  41.  
  42.         // File = new Scanner(new FileReader(Docs[x]));  
  43.  
  44.    String str="";
  45.       while ((str=br.readLine())!=null)  
  46.       {   
  47. //  word = File.next( );  
  48.           StringTokenizer stk=new StringTokenizer(str, " ,.-");
  49.           while(stk.hasMoreTokens())
  50.           {
  51.              word=stk.nextToken();
  52.           word = word.toLowerCase(); 
  53.  
  54.   count = getWord(word, t1) + 1;  
  55.   t1.put(word, count);  
  56.           }
  57.       }
  58.  
  59.    print(t1);
  60.    }
  61.        } 
  62.  
  63.       catch (Exception e)  
  64.       {  
  65.  System.err.println(e);  
  66.  return;  
  67.       }
  68.    }
  69.  
  70. public static void print(TreeMap<String, Integer> t1)
  71. {
  72.     System.out.println("(Term, TermFrequency)");
  73.     System.out.println("--------------------");
  74.  
  75.      for(String word : t1.keySet( ))  
  76.       {  
  77.          System.out.printf("(%s,%d);", word, t1.get(word));  
  78.       }  
  79.  
  80. }
  81. }
  82.  
Aug 19 '13 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.