Files

28 lines
746 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package region
import (
"regexp"
"strings"
)
var spaceRE = regexp.MustCompile(`\s+`)
var edgePunctRE = regexp.MustCompile(`^[\s\.,!\?;:"'«»\(\)\[\]\{\}]+|[\s\.,!\?;:"'«»\(\)\[\]\{\}]+$`)
func Normalize(input string) string {
s := strings.TrimSpace(strings.ToLower(input))
s = strings.ReplaceAll(s, "ё", "е")
s = strings.ReplaceAll(s, "-", " ")
s = strings.ReplaceAll(s, "г.", "город ")
s = strings.ReplaceAll(s, "обл.", "обл")
s = edgePunctRE.ReplaceAllString(s, "")
s = strings.Map(func(r rune) rune {
switch r {
case '.', ',', '!', '?', ';', ':', '"', '\'', '«', '»', '(', ')', '[', ']', '{', '}':
return ' '
default:
return r
}
}, s)
return spaceRE.ReplaceAllString(strings.TrimSpace(s), " ")
}