TokenPairDistanceMeasure extends TextSimilarityMeasureBase { @Override public double getSimilarity(Collection<String> stringList1, Collection<String> stringList2) throws SimilarityException { List<String> sl1 = new LinkedList<String>(); for (String s : stringList1) { sl1.add(s.toLowerCase()); } List<String> sl2 = new LinkedList<String>(); for (String s : stringList2) { sl2.add(s.toLowerCase()); } Set<String> strings1 = new LinkedHashSet<String>(sl1); Set<String> strings2 = new LinkedHashSet<String>(sl2); List<String> commonStrings = new LinkedList<String>(strings1); commonStrings.retainAll(strings2); Set<Pair> pairs = new LinkedHashSet<Pair>(); for (String s1 : commonStrings) { for (String s2 : commonStrings) { if (!s1.equals(s2)) { Pair p = new Pair(s1,s2); pairs.add(p); } } } if (pairs.size() > 1) { double[] v1 = new double[pairs.size()]; double[] v2 = new double[pairs.size()]; List<Pair> pairList = new LinkedList<Pair>(pairs); for (int i = 0; i < pairList.size(); i++) { Pair p = pairList.get(i); int idx1a = sl1.indexOf(p.getString1()); int idx1b = sl1.indexOf(p.getString2()); int idx1diff = transform(idx1a - idx1b); int idx2a = sl2.indexOf(p.getString1()); int idx2b = sl2.indexOf(p.getString2()); int idx2diff = transform(idx2a - idx2b); v1[i] = idx1diff; v2[i] = idx2diff; } PearsonsCorrelation pearson = new PearsonsCorrelation(); return pearson.correlation(v1, v2); } return 0.0; }  @Override double getSimilarity(Collection<String> stringList1,
			Collection<String> stringList2); int transform(int diff);  }
@Test public void run() throws Exception { TextSimilarityMeasure comparator = new TokenPairDistanceMeasure(); List<String> doc1 = new LinkedList<String>(); doc1.add("one"); doc1.add("two"); doc1.add("three"); doc1.add("four"); List<String> doc2 = new LinkedList<String>(); doc2.add("two"); doc2.add("one"); assertEquals(0.0, comparator.getSimilarity(doc1, doc2), epsilon); doc2 = new LinkedList<String>(); doc2.add("one"); doc2.add("two"); assertEquals(0.0, comparator.getSimilarity(doc1, doc2), epsilon); doc2 = new LinkedList<String>(); doc2.add("one"); doc2.add("a"); doc2.add("b"); doc2.add("c"); doc2.add("d"); doc2.add("e"); doc2.add("f"); doc2.add("two"); assertEquals(0.0, comparator.getSimilarity(doc1, doc2), epsilon); doc2 = new LinkedList<String>(); doc2.add("one"); doc2.add("three"); doc2.add("two"); doc2.add("four"); assertEquals(0.478, comparator.getSimilarity(doc1, doc2), epsilon); doc2 = new LinkedList<String>(); doc2.add("one"); doc2.add("three"); doc2.add("six"); doc2.add("seven"); doc2.add("two"); doc2.add("four"); assertEquals(0.330, comparator.getSimilarity(doc1, doc2), epsilon); }