-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
85 lines (67 loc) · 1.67 KB
/
Copy pathmain.go
File metadata and controls
85 lines (67 loc) · 1.67 KB
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
package main
import (
_ "embed"
logiAst "github.com/tislib/logi/pkg/ast/logi"
"github.com/tislib/logi/pkg/vm"
"log"
)
//go:embed chat-bot.lg
var logiContent string
//go:embed chat-bot.lgm
var macroContent string
func main() {
v := vm.New()
if err := v.LoadMacroContent(macroContent); err != nil {
log.Fatal(err)
}
if _, err := v.LoadLogiContent(logiContent); err != nil {
log.Fatal(err)
}
definition, err := v.GetDefinitionByName("MyChatbot")
if err != nil {
log.Fatal(err)
}
implementer := &chatBotImplementer{
intents: make(map[string]intent),
}
// Execute the definition
if err := v.Execute(definition, implementer); err != nil {
log.Fatal(err)
}
log.Println(implementer.intents)
// map[Farewell:{Goodbye See you later!} Greeting:{Hello Hi there!}]
}
type intent struct {
pattern string
response string
}
type chatBotImplementer struct {
intents map[string]intent
currentIntent string
}
func (c *chatBotImplementer) Call(vm vm.VirtualMachine, statement logiAst.Statement) error {
if statement.Scope == "" {
switch statement.Command {
case "intent":
c.currentIntent = statement.GetParameter("name").AsString()
for _, subStatement := range statement.SubStatements[0] {
if err := c.Call(vm, subStatement); err != nil {
return err
}
}
}
}
if statement.Scope == "conversation" {
switch statement.Command {
case "pattern":
i := c.intents[c.currentIntent]
i.pattern = statement.GetParameter("pattern").AsString()
c.intents[c.currentIntent] = i
case "response":
i := c.intents[c.currentIntent]
i.response = statement.GetParameter("response").AsString()
c.intents[c.currentIntent] = i
}
}
return nil
}