Advertisement
Guest User

indexer

a guest
Sep 26th, 2012
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.08 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3.  
  4. public class Indexer {
  5.     private HashMap<String, ArrayList<String>> index;
  6.    
  7.     public Indexer() {
  8.         index = new HashMap<String, ArrayList<String>>();
  9.     }
  10.    
  11.     public void addEntry(String location, String[] words) {
  12.         for (int i = 0; i < words.length; i++) {
  13.             String w = words[i].toLowerCase();
  14.             if (index.containsKey(w)) {
  15.                 ArrayList<String> locations = index.get(w);
  16.                 if (!locations.contains(location)) {
  17.                     locations.add(location);
  18.                 }
  19.             }
  20.             else {
  21.                 ArrayList<String> locations = new ArrayList<String>();
  22.                 locations.add(location);
  23.                 index.put(w, locations);
  24.             }
  25.         }
  26.     }
  27.    
  28.     public String search(String word) {
  29.         String searchTerm = word.toLowerCase();
  30.         ArrayList<String> locations = index.get(searchTerm);
  31.  
  32.         String result = "";
  33.         if (locations == null)
  34.             return result;
  35.        
  36.         for (int i = 0; i < locations.size(); i++) {
  37.             String location = locations.get(i);
  38.             result = result + location + ", ";
  39.         }
  40.        
  41.         result = result.substring(0, (result.length()-2));
  42.         return result;
  43.     }
  44.  
  45.    
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement