Deepu
Deepu

Reputation: 2616

Best String matching algorithm to implement in Java?

I want to implement an algorithm in Java to find the nearest similar Strings.

I have station_names in mysql database like - 23 ST, 233 ST, 21 ST, 14 St Times Sq, 24 ST

and if user enters a search string like 23rd station then I should return 23 ST and 233 ST or if user enters like Times Square then result should be 14 St Times Sq.

I found many algorithms on internet but I m confused to which one to use.

Can you please suggest me the best algorithm that can I implement in Java?

Thanks in advance

Upvotes: 3

Views: 4496

Answers (2)

Sandi Hrvić
Sandi Hrvić

Reputation: 221

To answer your question, there is no best algorithm in general, only the one that works best in your specific case.

You will want to define one or more metrics to measure differences between the input and the strings you have in the DB and then sort the results by score (see String metric).

The problem is that the most similar string is not always the closest address. That's why I said you have to define your own metric.

Upvotes: 2

Peter Lawrey
Peter Lawrey

Reputation: 533790

There are many possible ways to do this. For example you might say that 21 ST is closer to 23rd station than 233 ST. You have to work out what you want and find the approach which will match it best.

It is likely you might need multiple approaches and then score the results. This is what I would do.

You can test the different approach by providing a large sample data test suite and finding out which of the approaches (or a combination) give you the highest success rate.

Upvotes: 1

Related Questions