-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar_query.go
117 lines (104 loc) · 2.88 KB
/
grammar_query.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package search
import (
"gopkg.in/olivere/elastic.v6"
"github.com/Bnei-Baruch/archive-backend/utils"
)
const (
GRAMMAR_BOOST = 100.0
GRAMMAR_SUGGEST_SIZE = 30
GRAMMAR_SEARCH_SIZE = 2000
)
func createGrammarQuery(q *Query) elastic.Query {
boolQuery := elastic.NewBoolQuery()
if simpleQuery(q) != "" {
boolQuery = boolQuery.Should(
elastic.NewDisMaxQuery().Query(
elastic.NewMatchPhraseQuery("rules.language", simpleQuery(q)).Slop(SLOP).Boost(GRAMMAR_BOOST),
elastic.NewMatchPhraseQuery("rules", simpleQuery(q)).Slop(SLOP).Boost(GRAMMAR_BOOST),
),
)
}
return boolQuery
}
func NewSuggestGammarV2Request(query *Query, language string, preference string) *elastic.SearchRequest {
fetchSourceContext := elastic.NewFetchSourceContext(true).Include("intent", "variables", "values", "rules")
source := elastic.NewSearchSource().
Query(createGrammarQuery(query)).
FetchSourceContext(fetchSourceContext).
Size(GRAMMAR_SEARCH_SIZE).
Explain(query.Deb)
return elastic.NewSearchRequest().
SearchSource(source).
Index(GrammarIndexNameForServing(language)).
Preference(preference)
}
func NewResultsSuggestGrammarV2CompletionRequest(query *Query, language string, preference string) *elastic.SearchRequest {
fetchSourceContext := elastic.NewFetchSourceContext(true).Include("intent", "variables", "values", "rules")
source := elastic.NewSearchSource().
FetchSourceContext(fetchSourceContext).
Suggester(
elastic.NewCompletionSuggester("rules_suggest").
Field("rules_suggest").
Text(simpleQuery(query)).
Size(GRAMMAR_SUGGEST_SIZE).
SkipDuplicates(true)).
Suggester(
elastic.NewCompletionSuggester("rules_suggest.language").
Field("rules_suggest.language").
Text(simpleQuery(query)).
Size(GRAMMAR_SUGGEST_SIZE).
SkipDuplicates(true)).
Explain(query.Deb)
return elastic.NewSearchRequest().
SearchSource(source).
Index(GrammarIndexNameForServing(language)).
Preference(preference)
}
func wordToHist(word string) map[rune]int {
ret := make(map[rune]int)
for _, r := range word {
ret[r]++
}
return ret
}
func simpleQuery(q *Query) string {
if q.Term == "" && len(q.ExactTerms) == 1 {
return q.ExactTerms[0]
}
return q.Term
}
func cmpWordHist(a, b map[rune]int) float64 {
common := 0
diff := 0
for r, countA := range a {
if countB, ok := b[r]; ok {
min, max := utils.MinMax(countA, countB)
common += min
diff += max - min
} else {
diff += countA
}
}
for r, countB := range b {
if _, ok := a[r]; !ok {
diff += countB
}
}
return float64(common) / float64(common+diff)
}
func chooseRule(query *Query, rules []string) string {
if len(rules) == 0 {
return ""
}
queryHist := wordToHist(simpleQuery(query))
max := float64(0)
maxIndex := 0
for i := range rules {
ruleHist := wordToHist(rules[i])
if cur := cmpWordHist(queryHist, ruleHist); cur > max {
max = cur
maxIndex = i
}
}
return rules[maxIndex]
}