Skip to content

👓 Implementing a Fuzzy Search Algorithm in Java with possible to extend by adding similarity calculation strategy

License

Notifications You must be signed in to change notification settings

uxter/fuzzysearch

Repository files navigation

Build Status Coverage Status Join the chat at https://gitter.im/fuzzysearch

FuzzySearch

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();

Set score weight.

Parameters
Param Type Description
value int Score weight
wordsScoreStrategyInstance.setWeight(2);
startsWithScoreStrategyInstance.setWeight(5);
charsScoreStrategyInstance.setWeight(3);

implements FuzzySearchable

Create searcher.

FuzzySearch Searcher = new FuzzySearch();

Add Similarity calculation strategy.

Parameters
Param Type Description
strategy ScoreStrategy Similarity calculation strategy
Searcher.addScoreStrategy(wordsScoreStrategyInstance);
Searcher.addScoreStrategy(startsWithScoreStrategyInstance);
Searcher.addScoreStrategy(charsScoreStrategyInstance);

Search.

Parameters
Param Type Description
list String[] Source list of strings
input String Search query string
limit int Maximum number of results
Return
Param Type
String[] Sorted list of strings
String[] sourceList = new String[]{
    "Java",
    "JavaScript",
    "C++"
};
String[] resultList = Searcher.search(sourceList, "Java", 10));

Interfaces

FuzzySearchable
ScoreStrategy

Custom similarity calculation strategy

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;
    }

}

Note on Patches/Pull Requests

  1. Fork the project.
  2. Make your feature addition or bug fix.
  3. Send me a pull request.

© 2017 Vasily Shilov

About

👓 Implementing a Fuzzy Search Algorithm in Java with possible to extend by adding similarity calculation strategy

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages