1. Sara Magliacane
  2. recoprov

Commits

Sara Magliacane  committed e254429

adding top-k edges filter

  • Participants
  • Parent commits b50112f
  • Branches master

Comments (0)

Files changed (3)

File src/main/java/nl/vu/recoprov/baseclasses/EdgeScoreComparator.java

View file
  • Ignore whitespace
+package nl.vu.recoprov.baseclasses;
+
+import java.util.Comparator;
+
+public class EdgeScoreComparator implements Comparator<LabelledEdge> {
+
+
+	public int compare(LabelledEdge o1, LabelledEdge o2) {
+		return (o1.getScore() < o2.getScore() ? -1 : (o1.getScore() == o2
+				.getScore() ? 0 : 1));
+	}
+}
+	
+

File src/main/java/nl/vu/recoprov/baseclasses/LabelledEdge.java

View file
  • Ignore whitespace
+package nl.vu.recoprov.baseclasses;
+
+import java.io.Serializable;
+
+public class LabelledEdge implements Serializable {
+	/**
+		 * 
+		 */
+	private static final long serialVersionUID = 1L;
+	private int id;
+	private String label;
+	private double score = 0.0;
+
+	public LabelledEdge(Integer i, String s, double score) {
+		setId(i);
+		setLabel(s);
+		this.score = score;
+	}
+
+	public double getScore() {
+		return score;
+	}
+
+	public void setScore(double score) {
+		this.score = score;
+	}
+
+	public int getId() {
+		return id;
+	}
+
+	public void setId(int id) {
+		this.id = id;
+	}
+
+	public String getLabel() {
+		return label;
+	}
+
+	public void setLabel(String label) {
+		this.label = label;
+	}
+
+	public String toString() {
+		return label + " -> " + id + " [ score: " + score + "]";
+	}
+
+}

File src/main/java/nl/vu/recoprov/signalfilters/TopKEdges.java

View file
  • Ignore whitespace
+package nl.vu.recoprov.signalfilters;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import nl.vu.recoprov.abstractclasses.SignalFilterer;
+import nl.vu.recoprov.baseclasses.DependencyGraph;
+import nl.vu.recoprov.baseclasses.EdgeScoreComparator;
+import nl.vu.recoprov.baseclasses.LabelledEdge;
+import nl.vu.recoprov.baseclasses.DependencyNode;
+import nl.vu.recoprov.signalaggregators.WeightedSumAggregator;
+
+
+public class TopKEdges extends SignalFilterer {
+
+	public static int K = 10;
+	public static String label = WeightedSumAggregator.FINAL_SCORE;
+
+	public TopKEdges(String label) {
+		this.label = label;
+	}
+
+	public TopKEdges(String label, int K) {
+		this.label = label;
+		this.K = K;
+	}
+
+	public TopKEdges() {}
+
+	@Override
+	public DependencyGraph filterNode(DependencyGraph input, DependencyNode d) {
+		if (d == null) {
+			System.out.println("No node with name " + d);
+			return input;
+		}
+
+		// get all its outcoming edges
+		ArrayList<LabelledEdge> edgearray = input.getAllEdges(d
+				.getLuceneDocNumber(), label);
+
+		if (edgearray == null)
+			return input;
+
+		Collections.sort(edgearray, new EdgeScoreComparator());
+
+		int count = 0;
+
+		// for each of its outcoming edges
+		for (LabelledEdge edge : edgearray) {
+
+			if (count >= K) {
+				break;
+			}
+			input.removeEdge(d.getId(), edge.getId(), edge.getLabel());
+			count++;
+
+		}
+
+		return input;
+	}
+
+}