70 lines
1.3 KiB
Go
70 lines
1.3 KiB
Go
package matcher
|
|
|
|
import (
|
|
"os"
|
|
"regexp"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
type Matcher struct {
|
|
tokens []TokenCompiled
|
|
}
|
|
|
|
type TokenCompiled struct {
|
|
token string
|
|
regex []*regexp.Regexp
|
|
}
|
|
|
|
func createToken(token string, regexList []string) TokenCompiled {
|
|
result := new(TokenCompiled)
|
|
result.token = token
|
|
result.regex = make([]*regexp.Regexp, len(regexList))
|
|
for i, regex := range regexList {
|
|
result.regex[i] = regexp.MustCompile(regex)
|
|
}
|
|
return *result
|
|
}
|
|
|
|
func (t TokenCompiled) match(input string) (bool, string) {
|
|
for _, regex := range t.regex {
|
|
if regex.MatchString(input) {
|
|
return true, t.token
|
|
}
|
|
}
|
|
return false, ""
|
|
}
|
|
|
|
func (m *Matcher) Tokenize(input string) []string {
|
|
result := make([]string, 0, 10)
|
|
for _, token := range m.tokens {
|
|
ok, matchedToken := token.match(input)
|
|
if ok {
|
|
result = append(result, matchedToken)
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
func InitMatcher(config string) *Matcher {
|
|
configData, err := os.ReadFile(config)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
var configMap map[string][]string
|
|
|
|
err = yaml.Unmarshal(configData, &configMap)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
tokens := make([]TokenCompiled, 0, len(configMap))
|
|
|
|
for k, v := range configMap {
|
|
tokensCompiled := createToken(k, v)
|
|
tokens = append(tokens, tokensCompiled)
|
|
}
|
|
result := new(Matcher)
|
|
result.tokens = tokens
|
|
return result
|
|
}
|