package de.jungblut.ner;

import de.jungblut.distance.CosineDistance;
import de.jungblut.distance.DistanceMeasurer;
import de.jungblut.distance.SimilarityMeasurer;
import de.jungblut.math.dense.DenseDoubleMatrix;
import de.jungblut.math.tuple.Tuple3;

/**
 * Iterative similarity aggregation for named entity recognition and set
 * expansion based on the paper
 * "SEISA: Set Expansion by Iterative Similarity Aggregation". Those who wonder
 * what the package name "ner" stands for, it is "named entity recognition".
 * 
 * @author thomas.jungblut
 * 
 */
public final class IterativeSimilarityAggregation {

  private final String[] seedTokens;
  private final Tuple3<String[], String[], DenseDoubleMatrix> bipartiteGraph;
  private final double alpha;
  private final SimilarityMeasurer similarityMeasurer;

  /**
   * Constructs the similarity aggregation by seed tokens to expand and a given
   * bipartite graph. The bipartite graph is represented as a three tuple, which
   * consists of the vertices (called (candidate-) terms or entities) on the
   * first item, the context vertices on the second item and the edges between
   * those is a NxM matrix, where n is the entity tokens count and m is the
   * number of the context vertices. Alpha is set to 0.5 and the cosine distance
   * is used.
   */
  public IterativeSimilarityAggregation(String[] seedTokens,
      Tuple3<String[], String[], DenseDoubleMatrix> bipartiteGraph) {
    this(seedTokens, bipartiteGraph, 0.5d, new CosineDistance());
  }

  /**
   * Constructs the similarity aggregation by seed tokens to expand and a given
   * bipartite graph. The bipartite graph is represented as a three tuple, which
   * consists of the vertices (called (candidate-) terms or entities) on the
   * first item, the context vertices on the second item and the edges between
   * those is a NxM matrix, where n is the entity tokens count and m is the
   * number of the context vertices. Alpha is the constant weighting factor used
   * throughout the paper (usually 0.5). The distance measurer to be used must
   * be also defined.
   */
  public IterativeSimilarityAggregation(String[] seedTokens,
      Tuple3<String[], String[], DenseDoubleMatrix> bipartiteGraph,
      double alpha, DistanceMeasurer distance) {
    this.seedTokens = seedTokens;
    this.bipartiteGraph = bipartiteGraph;
    this.alpha = alpha;
    this.similarityMeasurer = new SimilarityMeasurer(distance);
  }

  /**
   * Starts the static thresholding algorithm and returns the
   */
  public String[] startStaticThresholding(double similarityThreshold) {
    return seedTokens;
  }

}

@@ -680,68 +680,76 @@
 nal 
-Tuple3%3CString%5B%5D, String%5B%5D, DenseDoubleMatrix%3E bip
+double alpha;%0D%0A  private final SimilarityMeasurer simil
 ar
-t
 it
-eGraph
+yMeasurer
 ;%0D%0A 
@@ -761,82 +761,103 @@
 ate 
-final double alpha;%0D%0A  private final SimilarityMeasurer similarityMeasurer
+String%5B%5D termNodes;%0D%0A  private String%5B%5D contextNodes;%0D%0A  private DenseDoubleMatrix weightMatrix
 ;%0D%0A%0D
@@ -2395,16 +2395,28 @@
    this.
+termNodes = 
 bipartit
@@ -2421,33 +2421,132 @@
 iteGraph
- = bipartiteGraph
+.getFirst();%0D%0A    this.contextNodes = bipartiteGraph.getSecond();%0D%0A    this.weightMatrix = bipartiteGraph.getThird()
 ;%0D%0A    t

unroll the tuple