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

com.bbn.bue.common.evaluation.EquivalenceBasedProvenancedAligner Maven / Gradle / Ivy

There is a newer version: 4.1.2
Show newest version
package com.bbn.bue.common.evaluation;

import com.google.common.annotations.Beta;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.Multimaps;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Creates {@link ProvenancedAlignment}s based on grouping items into equivalence classes. The items
 * aligned are the equivalence classes and the provenances are the original items.
 */
@Beta
public final class EquivalenceBasedProvenancedAligner
    implements ProvenancedAligner {

  private final Function leftEqClassFunction;
  private final Function rightEqClassFunction;

  private EquivalenceBasedProvenancedAligner(
      final Function leftEqClassFunction,
      final Function rightEqClassFunction) {
    this.leftEqClassFunction = checkNotNull(leftEqClassFunction);
    this.rightEqClassFunction = checkNotNull(rightEqClassFunction);
  }

  // the generic cast is always safe because it is just discarding extra bounds information
  // we don't need
  @SuppressWarnings("unchecked")
  /**
   * Creates an aligner given a single function from items to equivalence classes (requires left
   * and right items to both be of types compatible with this function).
   */
  public static  EquivalenceBasedProvenancedAligner forEquivalenceFunction(
      Function equivalenceFunction) {
    return new EquivalenceBasedProvenancedAligner(
        (Function) equivalenceFunction,
        (Function) equivalenceFunction);
  }

  // the generic cast is always safe because it is just discarding extra bounds information
  // we don't need
  @SuppressWarnings("unchecked")
  /**
   * Creates an aligner given two separate functions for mapping the left and right items to
   * (presumably the same set of) equivalence classes.
   */
  public static  EquivalenceBasedProvenancedAligner forKeyAndTestEquivalenceFunctions(
      Function leftEquivalenceFunction,
      Function rightEquivalenceFunction) {
    return new EquivalenceBasedProvenancedAligner(
        (Function) leftEquivalenceFunction,
        (Function) rightEquivalenceFunction);
  }

  public EquivalenceBasedProvenancedAlignment align(
      Iterable left,
      Iterable right) {
    final ImmutableListMultimap
        leftEquivalenceClassToProvenance =
        Multimaps.index(left, leftEqClassFunction);
    final ImmutableListMultimap
        rightEquivalenceClassToProvenance =
        Multimaps.index(right, rightEqClassFunction);

    return EquivalenceBasedProvenancedAlignment.fromEquivalenceClassMaps(
        leftEquivalenceClassToProvenance, rightEquivalenceClassToProvenance);
  }

  @Override
  public Function, ? extends Iterable>, ProvenancedAlignment> asFunction() {
    return new Function, ? extends Iterable>, ProvenancedAlignment>() {
      @Override
      public ProvenancedAlignment apply(final
      EvalPair, ? extends Iterable> input) {
        return align(input.key(), input.test());
      }
    };
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy