-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdialog.go
151 lines (130 loc) · 3.1 KB
/
dialog.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package neocortex
import (
"fmt"
"time"
"github.com/rs/xid"
)
type InputRecord struct {
At time.Time `json:"at"`
Input Input `json:"input"`
}
type OutputRecord struct {
At time.Time `json:"at"`
Output Output `json:"output"`
}
type ContextRecord struct {
At time.Time `json:"at"`
Context Context `json:"context"`
}
type Dialog struct {
ID string `json:"id" bson:"id"`
StartAt time.Time `json:"start_at" bson:"start_at"`
LastActivity time.Time `json:"last_activity" bson:"last_activity"`
EndAt time.Time `json:"end_at" bson:"end_at"`
Ins []*InputRecord `json:"ins" bson:"ins"`
Outs []*OutputRecord `json:"outs" bson:"outs"`
Contexts []*ContextRecord `json:"contexts" bson:"contexts"`
Performance float64 `json:"performance" bson:"performance"`
}
func newDialog() *Dialog {
return &Dialog{
ID: xid.New().String(),
LastActivity: time.Now(),
StartAt: time.Now(),
EndAt: time.Time{},
Ins: []*InputRecord{},
Outs: []*OutputRecord{},
Contexts: []*ContextRecord{},
Performance: 0.0,
}
}
// TODO: To optimize, please find all at the same time (that has better performance)
func (dialog *Dialog) HasEntity(entity string) bool {
totalIns := len(dialog.Ins)
totalOuts := len(dialog.Outs)
diff := totalIns - totalOuts
fmt.Println(diff)
if diff == 0 {
for i := 0; i < totalIns; i++ {
for _, ent := range dialog.Ins[i].Input.Entities {
if ent.Value == entity {
return true
}
}
for _, ent := range dialog.Outs[i].Output.Entities {
if ent.Value == entity {
return true
}
}
}
return false
}
for _, in := range dialog.Ins {
for _, ent := range in.Input.Entities {
if ent.Value == entity {
return true
}
}
}
for _, out := range dialog.Outs {
for _, ent := range out.Output.Entities {
if ent.Value == entity {
return true
}
}
}
return false
}
func (dialog *Dialog) HasIntent(intent string) bool {
totalIns := len(dialog.Ins)
totalOuts := len(dialog.Outs)
diff := totalIns - totalOuts
if diff == 0 {
for i := 0; i < totalIns; i++ {
for _, intn := range dialog.Ins[i].Input.Intents {
if intn.Intent == intent {
return true
}
}
for _, intn := range dialog.Outs[i].Output.Intents {
if intn.Intent == intent {
return true
}
}
}
return false
}
for _, in := range dialog.Ins {
for _, intn := range in.Input.Intents {
if intn.Intent == intent {
return true
}
}
}
for _, out := range dialog.Outs {
for _, intn := range out.Output.Intents {
if intn.Intent == intent {
return true
}
}
}
return false
}
func (dialog *Dialog) HasDialogNode(node string) bool {
for _, out := range dialog.Outs {
for _, visited := range out.Output.VisitedNodes {
if visited.Name == node || visited.Title == node {
return true
}
}
}
return false
}
func (dialog *Dialog) HasContextVar(contextVar string) bool {
for _, c := range dialog.Contexts {
if _, ok := c.Context.Variables[contextVar]; ok {
return true
}
}
return false
}