-
Notifications
You must be signed in to change notification settings - Fork 2
/
matcher.go
132 lines (113 loc) · 2.37 KB
/
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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package neocortex
type Match struct {
Is string
Confidence float64
}
type CMatch struct {
Name string
Value interface{}
}
type DialogNodeMatch struct {
Title string
Name string
}
type Matcher struct {
DialogNode DialogNodeMatch
Entity Match
Intent Match
ContextVariable CMatch
AND *Matcher
OR *Matcher
}
func (out *Output) Match(c *Context, matcher *Matcher) bool {
ok := false
for _, i := range out.Intents {
if i.Intent == matcher.Intent.Is && i.Confidence > matcher.Intent.Confidence {
ok = true
}
}
for _, e := range out.Entities {
if e.Entity == matcher.Entity.Is && e.Confidence > matcher.Entity.Confidence {
ok = true
}
}
if c.Variables != nil {
for varName, varValue := range c.Variables {
if matcher.ContextVariable.Name == varName {
if matcher.ContextVariable.Value == varValue {
ok = true
}
}
}
}
if matcher.DialogNode.Title != "" || matcher.DialogNode.Name != "" {
for _, n := range out.VisitedNodes {
if matcher.DialogNode.Name != "" {
if matcher.DialogNode.Title != "" {
if n.Name == matcher.DialogNode.Name && n.Title == matcher.DialogNode.Title {
ok = true
}
}
if n.Name == matcher.DialogNode.Name {
ok = true
}
} else if matcher.DialogNode.Title != "" {
if n.Title == matcher.DialogNode.Title {
ok = true
}
}
}
}
if matcher.AND != nil {
if out.Match(c, matcher.AND) && ok {
ok = true
} else {
ok = false
}
}
if matcher.OR != nil {
if out.Match(c, matcher.OR) || ok {
ok = true
} else {
ok = false
}
}
return ok
}
func (in *Input) Match(c *Context, matcher *Matcher) bool {
ok := false
for _, i := range in.Intents {
if i.Intent == matcher.Intent.Is && i.Confidence > matcher.Intent.Confidence {
ok = true
}
}
for _, e := range in.Entities {
if e.Entity == matcher.Entity.Is && e.Confidence > matcher.Entity.Confidence {
ok = true
}
}
if c.Variables != nil {
for varName, varValue := range c.Variables {
if matcher.ContextVariable.Name == varName {
if matcher.ContextVariable.Value == varValue {
ok = true
}
}
}
}
if matcher.AND != nil {
if in.Match(c, matcher.AND) && ok {
ok = true
} else {
ok = false
}
}
if matcher.OR != nil {
if in.Match(c, matcher.OR) || ok {
ok = true
} else {
ok = false
}
}
return ok
}