Git Product home page Git Product logo

go-fuzzy's Introduction

go-fuzzy

Go-fuzzy is a Golang fuzzy search implementation that for given keys returns objects with the closest values to an input.

A shameless port of Fuse.

Installation

go get github.com/antoan-angelov/go-fuzzy

Example

type Laptop struct {
  Manufacturer string
  HasFan bool
  Processor CPU
}

type CPU struct {
  Manufacturer string
  Series string
  Cores int
  Frequency int
}

processor1 := &CPU{Manufacturer: "Intel", Series: "i7", Cores: 4, Frequency: 4}
laptop1 := &Laptop{Manufacturer: "Acer", HasFan: true, Processor: processor}

processor2 := &CPU{Manufacturer: "AMD", Series: "Athlon", Cores: 4, Frequency: 4}
laptop2 := &Laptop{Manufacturer: "Lenovo", HasFan: true, Processor: processor}

laptops := []Laptop { laptop1, laptop2 }

fuzzyDemo := fuzzy.NewFuzzy()
fuzzyDemo.Set(laptops)
fuzzyDemo.Кeys = []string{"Manufacturer", "Processor.Manufacturer"}

results := fuzzyDemo.Search("Inet") // returns [laptop1]

Options

Кeys (type: []string, default: nil)
The list of properties to use fuzzy search on. It supports nested properties via dot notation.


Id (type: string, default: "")
Name of the identifier property. If set, instead of returning the objects themselves, it will return the specified identifier of the objects.


CaseSensitive (type: bool, default: false)
Whether comparisons should be case sensitive.


ShouldSort (type: bool, default: true)
Whether to sort the result list by score.


SearchFn (type: Searchable, default: defaults.BitapSearcher)
The search function to use. The object must implement Searchable interface:

type Searchable interface {
    SetPattern(pattern string, options *fuzzy.BitapOptions)
    Search(text string) *fuzzy.SearchResult
}

GetFn (type: func(object interface{}, path string) interface{}, default: defaults.DefaultGet)
The method used to access an object's properties. The default implementation handles dot notation nesting (i.e. a.b.c).


SortFn (type: func(object1 interface{}, object2 interface{}) int, default: defaults.DefaultComparator)
The function that is used for sorting the result list.

Bitap specific options

Location (type: int, default: 0)
Determines approximately where in the text is the pattern expected to be found.


Threshold (type: float32, default: 0.6)
At what point the match algorithm gives up. A threshold of 0.0 requires a perfect match (of both letters and location), a threshold of 1.0 would match anything.


Distance (type: int, default: 100)
Determines how close the match must be to the fuzzy location (specified by location). An exact letter match which is distance characters away from the fuzzy location would score as a complete mismatch. A distance of 0 requires the match be at the exact location specified, a threshold of 1000 would require a perfect match to be within 800 characters of the location to be found using a threshold of 0.8.


MaxPatternLength (type: int, default: 32)
The maximum length of the pattern. The longer the pattern, the more intensive the search operation will be. Whenever the pattern exceeds the maxPatternLength, an error will be thrown.

Methods

func Search(pattern string) []interface{}

@param {string} pattern The pattern string to fuzzy search on. @return A list of all search matches.

Searches for all the items whose keys (fuzzy) match the pattern.

func Set(list []interface{}) []interface{}

@param list @return The newly set list

Sets a new list for GoFuzzy to match against.

License

The MIT License (MIT)

Copyright (c) 2016 Antoan Angelov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

go-fuzzy's People

Contributors

photown avatar

Watchers

Doychin Atanasov avatar James Cloos avatar  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.