ProductPromotion
Logo

Go.Lang

made by https://0x3d.site

GitHub - avito-tech/normalize
Contribute to avito-tech/normalize development by creating an account on GitHub.
Visit Site

GitHub - avito-tech/normalize

GitHub - avito-tech/normalize

normalize

License: MIT Go Reference ci codecov Go Report Card

Simple library for fuzzy text sanitizing, normalizing and comparison.

Why

People type differently. This may be a problem if you need to associate user input with some internal entity or compare two inputs of different users. Say abc-01 and ABC 01 must be considered to be the same strings in your system. There are many heuristics we can apply to make this work:

  • Remove special characters.
  • Convert everything to lowercase.
  • etc.

This library is essentially an easily configurable set of useful helpers implementing all these transformations.

Installation

go get -u github.com/avito-tech/normalize 

Features

Normalize fuzzy text

package main 

import (
	"fmt"
	"github.com/avito-tech/normalize"
)

func main() {
	fuzzy := "VAG-1101"
	clean := normalize.Normalize(fuzzy)
	fmt.Print(clean) // vag1101

	manyFuzzy := []string{"VAG-1101", "VAG-1102"}
	manyClean := normalize.Many(manyFuzzy)
	fmt.Print(manyClean) // {"vag1101", "vag1102"}
}

Default rules (in order of actual application):

  • Any char except latin/cyrillic letters, German umlauts (ä, ö, ü) and digits are removed.
  • Rare cyrillic letters ё and й are replaced with common equivalents е and и.
  • Latin/cyrillic look-alike pairs are normalized to latin letters, so В (в) becomes B (b). Please check all replacement pairs in WithCyrillicToLatinLookAlike normalizer in normalizers.go.
  • German umlauts ä, ö, ü get converted to latin a, o, u.
  • The whole string gets lower cased.

Compare fuzzy texts

Compare two strings with all normalizations described above applied. Provide threshold parameters to tweak how similar strings must be to make the function return true. threshold is relative value, so 0.5 roughly means "strings are 50% different after all normalizations applied".

Levenstein distance is used under the hood to compute distance between strings.

package main

import (
    "fmt"
    "github.com/avito-tech/normalize"
)

func main() {
	fuzzy := "Hyundai-Kia"
	otherFuzzy := "HYUNDAI"
	similarityThreshold := 0.3
	result := normalize.AreStringsSimilar(fuzzy, otherFuzzy, similarityThreshold)

	// distance(hyundaikia, hyundai) = 3
	// 3 / len(hyundaikia) = 0.3 
	fmt.Print(result) // true
}

Default rules

  • Apply default normalization (described above).
  • Calculate Levenstein distance and return true if distance / strlen <= threshold.

Configuration

Both AreStringsSimilar and Normalize accept arbitrary number of normalizers as an optional parameter. Normalizer is any function that accepts string and returns string.

For example, following option will leave string unchanged.

package main

import "github.com/avito-tech/normalize"

func WithNoNormalization() normalize.Option {
	return func(str string) string {
		return str
	}
}

You can configure normalizing to use only those options you need. For example, you can use only lower casing and cyr2lat conversion during normalization. Note that the order of options matters.

package main

import (
	"fmt"
	"github.com/avito-tech/normalize"
)

func main() {
	fuzzy := "АВ-123"
	clean := normalize.Normalize(fuzzy, normalize.WithLowerCase(), normalize.WithCyrillicToLatinLookAlike())
	fmt.Print(clean) // ab-123
}

Articles
to learn more about the golang concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about GoLang.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory