# Comparing strings within a list of strings

I have a single list containing strings like this:

list={"house", "cat", "dog" ,"horse", "horses", "tree"};


How can I compare these strings within a list and return those strings which are similar (e.g. by using threshold values defined by SmithWatermanSimilarity). Thus, the output should be

{"house", "horse", "horses"}

• This may get you started GatherBy[list, SmithWatermanSimilarity[#, ##] &]. – High Performance Mark Mar 27 at 10:47

c = ClusterClassify[list, 4];

GatherBy[list, c]


{{"house", "horse", "horses"}, {"cat"}, {"dog"}, {"tree"}}

First[%]


{"house", "horse", "horses"}

• That is nice, but here I need to define n in ClusterClassify(4). Is there an unsupervised way? – M.A. Mar 27 at 18:47
• There is no "proper" assignment of $n$---it depends upon your task and application. In some cases you want every distinct element to be in its own category, and in other cases you want all elements to be the in same category. There is no way around this. Even if you use some off-the-shelf code for unsupervised clustering, that code is, in effect, choosing $n$ for you... just in an opaque way, or one that minimizes some global criterion function (such as sum of squared distances...). – David G. Stork Mar 27 at 18:52
• my first idea was to use a combination of StringLengthand SmithWatermanSimilarityto automatically detect similar strings. However my problem is, I have no Idea how this can be applied WITHIN the elements of a list... – M.A. Mar 27 at 19:02
• I have no Idea how this can be applied ... you follow the approach illustrated in my comment above, and write a two-argument function (combining your desired functions) which scores the similarity of pairs of elements from the list. – High Performance Mark Mar 27 at 21:33

This may be overkill, but it looks like a clique problem: look for cliques of strings that all have a mutual similarity of at least threshold:

list = {"house", "cat", "dog", "horse", "horses", "tree"};

With[{threshold = 3},

list[[#]] & /@ cliques