1   package eu.fbk.dkm.pikes.resources.ecb;
2   
3   import eu.fbk.utils.eval.PrecisionRecall;
4   
5   import java.util.*;
6   
7   /**
8    * Created by alessio on 28/01/16.
9    */
10  
11  public class ClusteringEvaluation {
12  
13      static class Pair<T> {
14  
15          T s1, s2;
16  
17          public Pair(T s1, T s2) {
18              this.s1 = s1;
19              this.s2 = s2;
20          }
21  
22          @Override public boolean equals(Object o) {
23              if (this == o) {
24                  return true;
25              }
26              if (!(o instanceof Pair)) {
27                  return false;
28              }
29  
30              Pair pair = (Pair) o;
31  
32              return (s1.equals(pair.s1) && s2.equals(pair.s2)) ||
33                      (s1.equals(pair.s2) && s2.equals(pair.s1));
34          }
35  
36          @Override public int hashCode() {
37              int result = s1.hashCode();
38              result = 31 * result + s2.hashCode();
39              return result;
40          }
41  
42          @Override public String toString() {
43              return "Pair{" +
44                      "s1='" + s1 + '\'' +
45                      ", s2='" + s2 + '\'' +
46                      '}';
47          }
48      }
49  
50      static Map<PrecisionRecall.Measure, Double> pairWise(Set<Set> goldS, Set<Set> classifiedS) {
51  
52          Set<List> gold = new HashSet<>();
53          Set<List> classified = new HashSet<>();
54  
55          for (Set set : goldS) {
56              gold.add(new ArrayList<>(set));
57          }
58          for (Set set : classifiedS) {
59              classified.add(new ArrayList<>(set));
60          }
61  
62          Set<Pair> goldPairs = new HashSet<>();
63          Set<Pair> classifiedPairs = new HashSet<>();
64  
65          for (List objects : gold) {
66              for (int i = 0; i < objects.size(); i++) {
67                  for (int j = i + 1; j < objects.size(); j++) {
68                      goldPairs.add(new Pair(objects.get(i), objects.get(j)));
69                  }
70              }
71          }
72          for (List objects : classified) {
73              for (int i = 0; i < objects.size(); i++) {
74                  for (int j = i + 1; j < objects.size(); j++) {
75                      classifiedPairs.add(new Pair(objects.get(i), objects.get(j)));
76                  }
77              }
78          }
79  
80          Set<Pair> intersection = new HashSet<>(goldPairs);
81          intersection.retainAll(classifiedPairs);
82  
83          double commons = intersection.size() * 1.0;
84          double goldPairCount = goldPairs.size() * 1.0;
85          double classifiedPairCount = classifiedPairs.size() * 1.0;
86  
87          double p = commons / classifiedPairCount;
88          double r = commons / goldPairCount;
89  
90          Map<PrecisionRecall.Measure, Double> ret = new HashMap<>();
91          ret.put(PrecisionRecall.Measure.PRECISION, p);
92          ret.put(PrecisionRecall.Measure.RECALL, r);
93          ret.put(PrecisionRecall.Measure.F1, 2 * p * r / (p + r));
94  
95          return ret;
96      }
97  
98      public static void main(String[] args) {
99  
100         Set<Set> m = new HashSet<>();
101         Set<String> m1 = new HashSet<>();
102         m1.add("a");
103         m1.add("b");
104         m1.add("c");
105         Set<String> m2 = new HashSet<>();
106         m2.add("d");
107         m2.add("e");
108         m2.add("f");
109         m.add(m1);
110         m.add(m2);
111 
112         Set<Set> c = new HashSet<>();
113         Set<String> c1 = new HashSet<>();
114         c1.add("a");
115         c1.add("b");
116         Set<String> c2 = new HashSet<>();
117         c2.add("c");
118         c2.add("d");
119         c2.add("e");
120         Set<String> c3 = new HashSet<>();
121         c3.add("f");
122         c.add(c1);
123         c.add(c2);
124         c.add(c3);
125 
126         System.out.println(pairWise(m, c));
127     }
128 }