Unexpected result with apoc.text.sorensenDiceSimilarity?

Viewed 90

A bit confused in regards to string similarity using Sorensen-Dice. Apparently it makes a difference in what order parameters are being passed.

WITH
apoc.text.sorensenDiceSimilarity("+46xxxxx2260", "+46xxxxx2226") as score1,
apoc.text.sorensenDiceSimilarity("+46xxxxx2226", "+46xxxxx2260") as score2
RETURN
score1, score2

One of these scores (i.e. similarity coefficients) will say 1.0, the other 0.909090...

Does not make sense to me, but perhaps there's something with the algorithm I'm not aware of? Any insight is appreciated.

P.S. "Neo4j Kernel", "3.5.9", "community"

1 Answers

This is definitely a bug and a good catch!

As alternative you can do below query which uses apoc functions as toSet and intersection and text function, split. There is hack on the query that uses ROUND(10^4/10^4) to use 4-decimal places. If you like my answer, please vote and accept it. Thanks.

WITH apoc.coll.toSet(split("+46xxxxx2260","")) as set1, apoc.coll.toSet(split("+46xxxxx2226","")) as set2
WITH set1, set2, apoc.coll.intersection(set1, set2) as common 
RETURN ROUND(2*size(common)*10^4/(size(set1)+size(set2)))/10^4 as sorensenDiceSimilarity

Result:
0.9091
Related