-
Notifications
You must be signed in to change notification settings - Fork 0
/
PatternFinder.go
61 lines (55 loc) · 1.55 KB
/
PatternFinder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"regexp"
"strings"
)
type StringFinder struct {
haystack string
needle string
}
func normaliseString(needle string) string {
// Normalise all football club representation
commonPattern := []string{"FC", "F.C.", "F.C"}
workerString := needle
for _, val := range commonPattern {
workerString = strings.Replace(workerString, val, "", -1)
}
return workerString
}
func regexMatchForShortForms(haystack string, needle string) bool {
re := regexp.MustCompile("\\b[A-Z]")
if re != nil {
shortForm := re.FindAllString(haystack, -1)
if strings.ToLower(strings.Replace(needle, " ", "", -1)) == strings.ToLower(strings.Join(shortForm, "")) {
return true
} else {
shortForm = re.FindAllString(haystack, 2) //Atleast first 2 characters match in short form
if strings.ContainsAny(haystack, needle) {
if strings.HasPrefix(strings.ToLower(needle), strings.ToLower(strings.Join(shortForm, ""))) {
return true
}
}
}
}
return false
}
func (sf *StringFinder) MatchString() int {
//Handle empty needle
if len(strings.Replace(sf.needle, " ", "", -1)) == 0 {
return 0
}
switch {
case strings.Contains(strings.ToLower(sf.haystack), strings.ToLower(sf.needle)):
if len(strings.Split(sf.needle, " ")) > 1 { //Normally more than one word matches will be a hit
return 1
}
case strings.Contains(strings.ToLower(sf.haystack), strings.ToLower(normaliseString(sf.needle))):
return 1
case regexMatchForShortForms(sf.haystack, sf.needle):
return 1
}
return 0
}
func main() {
//fmt.Println("Testing pattern match")
}