-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar_variables_matcher.go
65 lines (61 loc) · 1.7 KB
/
grammar_variables_matcher.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
62
63
64
65
package search
import (
// log "github.com/Sirupsen/logrus"
"github.com/Bnei-Baruch/archive-backend/cache"
"github.com/Bnei-Baruch/archive-backend/consts"
)
func GrammarFilterVariablesMatch(intent string, variablesByPhrase VariablesByPhrase, cm cache.CacheManager) {
for phrase, vMap := range variablesByPhrase {
if !GrammarVariablesMatch(intent, vMap, cm) {
delete(variablesByPhrase, phrase)
}
}
}
func GrammarVariablesMatch(intent string, vMap map[string][]string, cm cache.CacheManager) bool {
if intent == consts.GRAMMAR_INTENT_LANDING_PAGE_CONVENTIONS {
location := ""
year := ""
for variable, values := range vMap {
if variable == consts.VAR_YEAR {
if len(values) != 1 {
return false
}
year = values[0]
} else if variable == consts.VAR_CONVENTION_LOCATION {
if len(values) != 1 {
return false
}
location = values[0]
}
}
// Validate vMap fits convention $ConventionLocation and $Year existing values.
// log.Infof("location: %s, year: %s => %t", location, year, cm.SearchStats().DoesConventionExist(location, year))
// Uninitialized, usually for tests. Return false.
if cm == nil {
return false
}
return cm.SearchStats().DoesConventionExist(location, year)
} else if intent == consts.GRAMMAR_INTENT_LANDING_PAGE_HOLIDAYS {
year := ""
holiday := ""
for variable, values := range vMap {
if variable == consts.VAR_YEAR {
if len(values) != 1 {
return false
}
year = values[0]
} else if variable == consts.VAR_HOLIDAYS {
if len(values) != 1 {
return false
}
holiday = values[0]
}
}
if cm == nil {
return false
}
return cm.SearchStats().DoesHolidayExist(holiday, year)
} else {
return true
}
}