Getting the closest string match

后端 未结 13 898
难免孤独
难免孤独 2020-11-22 10:57

I need a way to compare multiple strings to a test string and return the string that closely resembles it:

TEST STRING: THE BROWN FOX JUMPED OVER THE RED COW         


        
13条回答
  •  再見小時候
    2020-11-22 11:33

    The problem is hard to implement if the input data is too large (say millions of strings). I used elastic search to solve this.

    Quick start : https://www.elastic.co/guide/en/elasticsearch/client/net-api/6.x/elasticsearch-net.html

    Just insert all the input data into DB and you can search any string based on any edit distance quickly. Here is a C# snippet which will give you a list of results sorted by edit distance (smaller to higher)

    var res = client.Search(s => s
        .Query(q => q
        .Match(m => m
            .Field(f => f.VariableName)
            .Query("SAMPLE QUERY")
            .Fuzziness(Fuzziness.EditDistance(5))
        )
    ));
    

提交回复
热议问题