All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.stanford.nlp.util.Comparators Maven / Gradle / Ivy

Go to download

Stanford Parser processes raw text in English, Chinese, German, Arabic, and French, and extracts constituency parse trees.

There is a newer version: 3.9.2
Show newest version
package edu.stanford.nlp.util;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;


  // Originally from edu.stanford.nlp.natlog.util
public class Comparators {

  private Comparators() {} // class of static methods

  /**
   * Returns a new {@code Comparator} which is the result of chaining the
   * given {@code Comparator}s.  If the first Comparator
   * considers two objects unequal, its result is returned; otherwise, the
   * result of the second Comparator is returned.  Facilitates
   * sorting on primary and secondary keys.
   */
  public static  Comparator chain(final Comparator c1,
                                        final Comparator c2) {
    return (o1, o2) -> {
      int x = c1.compare(o1, o2);
      return (x == 0 ? c2.compare(o1, o2) : x);
    };
  }

  /**
   * Returns a new Comparator which is the result of chaining the
   * given Comparators.  Facilitates sorting on multiple keys.
   */
  public static  Comparator chain(final List> c) {
    return (o1, o2) -> {
      int x = 0;
      Iterator> it = c.iterator();
      while (x == 0 && it.hasNext()) {
        x = it.next().compare(o1, o2);
      }
      return x;
    };
  }

  @SafeVarargs
  public static  Comparator chain(Comparator... c) {
    return chain(Arrays.asList(c));
  }

  /**
   * Returns a new Comparator which is the reverse of the
   * given Comparator.
   */
  public static  Comparator reverse(final Comparator c) {
    return (o1, o2) -> -c.compare(o1, o2);
  }

  public static > Comparator nullSafeNaturalComparator() {
    return Comparators::nullSafeCompare;
  }

  /**
   * Returns a consistent ordering over two elements even if one of them is null
   * (as long as compareTo() is stable, of course).
   *
   * There's a "trickier" solution with xor at http://stackoverflow.com/a/481836
   * but the straightforward answer seems better.
   */
  public static > int nullSafeCompare(final T one, final T two) {
    if (one == null) {
      if (two == null) {
        return 0;
      }
      return -1;
    } else {
      if (two == null) {
        return 1;
      }
      return one.compareTo(two);
    }
  }

    private static > int compareLists(List list1,
                                   List list2) {
      // if (list1 == null && list2 == null) return 0;  // seems better to regard all nulls as out of domain or none, not some
      if (list1 == null || list2 == null) {
        throw new IllegalArgumentException();
      }
      int size1 = list1.size();
      int size2 = list2.size();
      int size = Math.min(size1, size2);
      for (int i = 0; i < size; i++) {
        int c = list1.get(i).compareTo(list2.get(i));
        if (c != 0) return c;
      }
      if (size1 < size2) return -1;
      if (size1 > size2) return 1;
      return 0;
    }

    public static  Comparator> getListComparator() {
      return (list1, list2) -> compareLists(list1, list2);
    }

    /**
     * A Comparator that compares objects by comparing their
     * String representations, as determined by invoking
     * toString() on the objects in question.
     */
    public static Comparator getStringRepresentationComparator() {
      return new Comparator() {
          public int compare(Object o1, Object o2) {
            return o1.toString().compareTo(o2.toString());
          }
        };
    }

    public static Comparator getBooleanArrayComparator() {
      return (a1, a2) -> ArrayUtils.compareBooleanArrays(a1, a2);
    }

    public static  Comparator getArrayComparator() {
      return (a1, a2) -> ArrayUtils.compareArrays(a1, a2);
    }

  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy