initial prototype, only responds with tokens
This commit is contained in:
parent
d2fd718f22
commit
0ab510adef
5 changed files with 128 additions and 0 deletions
70
matcher/matcher.go
Normal file
70
matcher/matcher.go
Normal file
|
@ -0,0 +1,70 @@
|
|||
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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue