normalize

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 23, 2021 License: MIT Imports: 4 Imported by: 2

README

normalize

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
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AreStringsSimilar

func AreStringsSimilar(one string, other string, threshold float64, normalizers ...Option) bool

AreStringsSimilar returns true if relative distance between 2 strings after normalization is lower than provided threshold.

func Many

func Many(strings []string, normalizers ...Option) []string

Many normalizes slice of strings returning new slice with normalized elements.

func Normalize

func Normalize(str string, normalizers ...Option) string

Normalize returns normalized string. If not normalizers specified default set of normalizers is used.

Types

type Option

type Option func(string) string

func WithCyrillicToLatinLookAlike

func WithCyrillicToLatinLookAlike() Option

WithCyrillicToLatinLookAlike Latin/cyrillic look-alike pairs are normalized to latin letters so `В (в)` becomes `B (b)`, etc.

func WithFixRareCyrillicChars

func WithFixRareCyrillicChars() Option

WithFixRareCyrillicChars rare cyrillic letters `ё` and `й` are replaced with common equivalents `е` and `и`

func WithLowerCase

func WithLowerCase() Option

WithLowerCase converts string to lowercase

func WithNoNormalization

func WithNoNormalization() Option

WithNoNormalization applies no changes to string

func WithRemoveSpecialChars

func WithRemoveSpecialChars() Option

WithRemoveSpecialChars any char except latin/cyrillic letters, German umlauts (`ä`, `ö`, `ü`) and digits are removed

func WithRuneMapping

func WithRuneMapping(mapping map[rune]rune) Option

WithRuneMapping configures arbitrary rune mapping, case sensitive

func WithUmlautToLatinLookAlike

func WithUmlautToLatinLookAlike() Option

WithUmlautToLatinLookAlike german umlauts `ä`, `ö`, `ü` get converted to latin `a`, `o`, `u`

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL