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

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

The newest version!
package com.bbn.bue.common.evaluation;

import com.google.common.annotations.Beta;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.common.collect.Ordering;

import java.util.Map;

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

@Beta
public final class BrokenDownSummaryConfusionMatrix {

  private ImmutableMap data;

  private BrokenDownSummaryConfusionMatrix(Map data) {
    this.data = ImmutableMap.copyOf(data);
  }

  public static  BrokenDownSummaryConfusionMatrix fromMap(
      Map data) {
    return new BrokenDownSummaryConfusionMatrix(data);
  }

  public Map asMap() {
    return data;
  }

  /**
   * Provides a builder for a {@link BrokenDownSummaryConfusionMatrix}
   * where the keys of the result will be ordered by {@code ordering}.  We require the ordering to
   * maintain deterministic output.
   */
  public static  Builder builder(Ordering ordering) {
    return new Builder(ordering);
  }

  public static final class Builder {

    private final Ordering keyOrdering;
    private final Map data = Maps.newHashMap();

    private Builder(final Ordering keyOrdering) {
      this.keyOrdering = checkNotNull(keyOrdering);
    }

    public Builder combine(
        BrokenDownSummaryConfusionMatrix dataToAdd) {
      for (final Map.Entry entry : dataToAdd.asMap()
          .entrySet()) {
        final SignatureType signature = entry.getKey();
        if (!data.containsKey(signature)) {
          data.put(signature, SummaryConfusionMatrices.builder());
        }
        data.get(signature).accumulate(entry.getValue());
      }
      return this;
    }

    public BrokenDownSummaryConfusionMatrix build() {
      final ImmutableMap.Builder ret =
          ImmutableMap.builder();

      for (final SignatureType key : keyOrdering.sortedCopy(data.keySet())) {
        ret.put(key, data.get(key).build());
      }

      return BrokenDownSummaryConfusionMatrix.fromMap(ret.build());
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy