forked from clyhs/gopcp.v2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
chinese.go
68 lines (59 loc) · 1.5 KB
/
chinese.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
package chatbot
import (
"fmt"
"strings"
)
// simpleCN 代表针对中文的演示级聊天机器人。
type simpleCN struct {
name string
talk Talk
}
// NewSimpleCN 用于创建针对中文的演示级聊天机器人。
func NewSimpleCN(name string, talk Talk) Chatbot {
return &simpleCN{
name: name,
talk: talk,
}
}
// Name 是Chatbot接口的实现的一部分。
func (robot *simpleCN) Name() string {
return robot.name
}
// Begin 是Chatbot接口的实现的一部分。
func (robot *simpleCN) Begin() (string, error) {
return "请输入你的名字:", nil
}
// Hello 是Talk接口的实现的一部分。
func (robot *simpleCN) Hello(userName string) string {
userName = strings.TrimSpace(userName)
if robot.talk != nil {
return robot.talk.Hello(userName)
}
return fmt.Sprintf("你好,%s!我可以为你做些什么?", userName)
}
// Talk 是Talk接口的实现的一部分。
func (robot *simpleCN) Talk(heard string) (saying string, end bool, err error) {
heard = strings.TrimSpace(heard)
if robot.talk != nil {
return robot.talk.Talk(heard)
}
switch heard {
case "":
return
case "没有", "再见":
saying = "再见!"
end = true
return
default:
saying = "对不起,我没听懂你说的。"
return
}
}
// ReportError 是Chatbot接口的实现的一部分。
func (robot *simpleCN) ReportError(err error) string {
return fmt.Sprintf("发生了一个错误: %s\n", err)
}
// End 是Chatbot接口的实现的一部分。
func (robot *simpleCN) End() error {
return nil
}