Implementing a Fuzzy Search Algorithm in Java with possible to extend by adding similarity calculation strategy.
extends ScoreWeight
implements ScoreStrategy
Create similarity calculation strategy.
WordsScoreStrategy wordsScoreStrategyInstance = new WordsScoreStrategy();
StartsWithScoreStrategy startsWithScoreStrategyInstance = new StartsWithScoreStrategy();
CharsScoreStrategy charsScoreStrategyInstance = new CharsScoreStrategy();
Method setWeight(int value)
Set score weight.
Param | Type | Description |
---|---|---|
value | int |
Score weight |
wordsScoreStrategyInstance.setWeight(2);
startsWithScoreStrategyInstance.setWeight(5);
charsScoreStrategyInstance.setWeight(3);
Class FuzzySearch
implements FuzzySearchable
Create searcher.
FuzzySearch Searcher = new FuzzySearch();
Add Similarity calculation strategy.
Param | Type | Description |
---|---|---|
strategy | ScoreStrategy |
Similarity calculation strategy |
Searcher.addScoreStrategy(wordsScoreStrategyInstance);
Searcher.addScoreStrategy(startsWithScoreStrategyInstance);
Searcher.addScoreStrategy(charsScoreStrategyInstance);
Search.
Param | Type | Description |
---|---|---|
list | String[] |
Source list of strings |
input | String |
Search query string |
limit | int |
Maximum number of results |
Param | Type |
---|---|
String[] |
Sorted list of strings |
String[] sourceList = new String[]{
"Java",
"JavaScript",
"C++"
};
String[] resultList = Searcher.search(sourceList, "Java", 10));
package ru.shcoder.fuzzysearch;
/**
* <h1>Class CustomScoreStrategy</h1>
* Similarity calculation strategy for two strings
*
* @author Vasiliy Shilov (https://github.com/uxter)
* @version 1.0
* @since 2017-07-31
*/
public class CustomScoreStrategy extends ScoreWeight implements ScoreStrategy {
/**
* Get similarity score
* @param str1 First string
* @param str2 Second string
* @return Similarity Score
*/
public double getScore(String str1, String str2) {
double result;
// Some logic
return result;
}
}
- Fork the project.
- Make your feature addition or bug fix.
- Send me a pull request.
© 2017 Vasily Shilov