Coder Social home page Coder Social logo

fuzzy-rs's Introduction

sublime_fuzzy sublime_fuzzy on crates.io

Fuzzy matching algorithm based on Sublime Text's string search. Iterates through characters of a search string and calculates a score.

The score is based on several factors:

  • Word starts like the t in some_thing get a bonus (bonus_word_start)
  • Consecutive matches get an accumulative bonus for every consecutive match (bonus_consecutive)
  • Matches that also match case (T -> T instead of t -> T) in case of a case-insensitive search get a bonus (bonus_match_case)
  • The distance between two matches will be multiplied with the penalty_distance penalty and subtracted from the score

The default scoring is configured to give a lot of weight to word starts. So a pattern scc will match SoccerCartoonController, not SoccerCartoonController.

Match Examples

With default weighting.

Pattern Target string Result
scc SoccerCartoonController SoccerCartoonController
something some search thing some search thing

Usage

Basic usage:

use sublime_fuzzy::best_match;

let result = best_match("something", "some search thing");

assert!(result.is_some());

Match::continuous_matches returns an iter of consecutive matches. Based on those the input string can be formatted.

format_simple provides a simple formatting that wraps matches in tags:

use sublime_fuzzy::{best_match, format_simple};

let target = "some search thing";

let result = best_match("something", target).unwrap();

assert_eq!(
    format_simple(&result, target, "<span>", "</span>"),
    "<span>some</span> search <span>thing</span>"
);

The weighting of the different factors can be adjusted:

use sublime_fuzzy::{FuzzySearch, Scoring};

// Or pick from one of the provided `Scoring::...` methods like `emphasize_word_starts`
let scoring = Scoring {
    bonus_consecutive: 128,
    bonus_word_start: 0,
    ..Scoring::default()
};

let result = FuzzySearch::new("something", "some search thing")
    .case_sensitive()
    .score_with(&scoring)
    .best_match();

assert!(result.is_some())

Note: Any whitespace in the pattern ('something' in the examples above) will be removed.

Documentation

Check out the documentation at docs.rs.

fuzzy-rs's People

Contributors

schlechtwetterfront avatar pajn avatar ocboogie avatar vallentin avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.