Ejemplo n.º 1
0
  /**
   * Auxiliary method to sort a Map by value.
   *
   * @param unsortedMap The Map to be sorted.
   * @param stemHandler A StemUtils object.
   */
  public final void sortMapByValue(HashMap<String, Integer> unsortedMap, StemUtils stemHandler) {
    // Initialize variables
    Entry<String, Integer> entry;
    String currentKey;
    int currentValue;

    // Get the 5 greatest tokens by value
    // If the HashMap has less than 5 elements, just sort them
    int size = (unsortedMap.keySet().size() < 5 ? unsortedMap.size() : 5);
    for (int i = 0; i < size; i++) {
      entry = unsortedMap.entrySet().iterator().next();
      currentKey = entry.getKey();
      currentValue = entry.getValue();
      for (String key : unsortedMap.keySet()) {
        if (unsortedMap.get(key) > currentValue) {
          currentValue = unsortedMap.get(key);
          currentKey = key;
        }
      }
      commonTerms.add(stemHandler.getOriginalWord(currentKey));
      unsortedMap.remove(currentKey);
    }
  }