Guest User

Untitled

a guest
Mar 13th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.91 KB | None | 0 0
  1. /**
  2.      * Merges the keywords for a single document into the master keywordsIndex
  3.      * hash table. For each keyword, its Occurrence in the current document
  4.      * must be inserted in the correct place (according to descending order of
  5.      * frequency) in the same keyword's Occurrence list in the master hash table.
  6.      * This is done by calling the insertLastOccurrence method.
  7.      *
  8.      * @param kws Keywords hash table for a document
  9.      */
  10.     public void mergeKeyWords(HashMap<String,Occurrence> kws) {
  11.         // COMPLETE THIS METHOD
  12.        
  13.        
  14.         // keywordsIndex.get(key).add(Occurrence)
  15.         // ketwordsIndex.get(key).insertLastOccurence()
  16.     }
  17.  
  18.  
  19. /**
  20.      * Inserts the last occurrence in the parameter list in the correct position in the
  21.      * same list, based on ordering occurrences on descending frequencies. The elements
  22.      * 0..n-2 in the list are already in the correct order. Insertion is done by
  23.      * first finding the correct spot using binary search, then inserting at that spot.
  24.      *
  25.      * @param occs List of Occurrences
  26.      * @return Sequence of mid point indexes in the input list checked by the binary search process,
  27.      *         null if the size of the input list is 1. This returned array list is only used to test
  28.      *         your code - it is not used elsewhere in the program.
  29.      */
  30.     public ArrayList<Integer> insertLastOccurrence(ArrayList<Occurrence> occs) {
  31.         if (occs.isEmpty() || occs.size() == 1)
  32.             return null;
  33.        
  34.         Occurrence last = occs.get(occs.size()-1);
  35.        
  36.         ArrayList<Integer> checked = new ArrayList();
  37.        
  38.         int hi = occs.size() - 2; //last position is element to be sorted
  39.         int lo = 0;
  40.         int mid = 0;
  41.         while (lo <= hi) {
  42.             mid = (lo + hi)/2;
  43.             checked.add(mid);
  44.             if(last.frequency == occs.get(mid).frequency)
  45.                 break;
  46.             else if (last.frequency > occs.get(mid).frequency)
  47.                 hi = mid - 1;
  48.             else
  49.                 lo = mid + 1;
  50.         }
  51.         occs.add(mid, last);
  52.         occs.remove(occs.size()-1);
  53.  
  54.         return checked;
  55.     }
Add Comment
Please, Sign In to add comment