1
- use serde:: { Serialize , Deserialize } ;
2
- use std:: collections:: HashMap ;
3
- use std:: fs:: File ;
4
- use std:: io:: { self , Write , BufReader } ;
5
-
6
- /**
7
- * @file main.rs
8
- * @brief Rusty: A simple chatbot implemented in Rust.
9
- */
10
-
11
-
12
- pub struct Chatbot {
13
- responses : HashMap < String , String > ,
14
- }
15
-
16
- impl Chatbot {
17
- pub fn new ( ) -> Self {
18
- Chatbot {
19
- responses : HashMap :: new ( ) ,
20
- }
21
- }
22
-
23
- pub fn respond ( & self , input : & str ) -> String {
24
- let input = input. trim ( ) . to_lowercase ( ) ;
25
-
26
- for ( key, value) in & self . responses {
27
- if input. contains ( & key. to_lowercase ( ) ) {
28
- return value. clone ( ) ;
29
- }
30
- }
31
-
32
- String :: from ( "I'm not sure how to respond to that. Could you please rephrase?" )
33
- }
34
-
35
- pub fn load_responses ( & mut self , filename : & str ) -> Result < ( ) , io:: Error > {
36
- let file = File :: open ( filename) ?;
37
- let reader = BufReader :: new ( file) ;
38
- let responses: Vec < UserInput > = serde_json:: from_reader ( reader) ?;
39
-
40
- for response in responses {
41
- self . responses . insert ( response. key , response. value ) ;
42
- }
43
- Ok ( ( ) )
44
- }
45
- }
46
-
47
- #[ derive( Debug , Serialize , Deserialize ) ]
48
- pub struct UserInput {
49
- key : String ,
50
- value : String ,
51
- }
52
-
53
- fn main ( ) -> io:: Result < ( ) > {
54
- let mut chatbot = Chatbot :: new ( ) ;
55
-
56
- if let Err ( e) = chatbot. load_responses ( "chatbot_responses.json" ) {
57
- eprintln ! ( "Error loading responses: {}" , e) ;
58
- return Err ( e) ;
59
- }
60
-
61
- println ! ( "Chatbot initialized. Type 'exit' to quit." ) ;
62
-
63
- loop {
64
- print ! ( "You: " ) ;
65
- io:: stdout ( ) . flush ( ) ?;
66
-
67
- let mut input = String :: new ( ) ;
68
- io:: stdin ( ) . read_line ( & mut input) ?;
69
-
70
- let input = input. trim ( ) ;
71
-
72
- if input. eq_ignore_ascii_case ( "exit" ) {
73
- println ! ( "Goodbye!" ) ;
74
- break ;
75
- }
76
-
77
- let response = chatbot. respond ( input) ;
78
- println ! ( "Bot: {}" , response) ;
79
- }
80
-
81
- Ok ( ( ) )
82
- }
83
-
84
- #[ cfg( test) ]
85
- mod tests {
86
- use super :: * ;
87
-
88
- #[ test]
89
- fn test_chatbot_respond ( ) {
90
- let mut chatbot = Chatbot :: new ( ) ;
91
- chatbot. responses . insert ( "hello" . to_string ( ) , "Hi there!" . to_string ( ) ) ;
92
- chatbot. responses . insert ( "bye" . to_string ( ) , "Goodbye!" . to_string ( ) ) ;
93
-
94
- assert_eq ! ( chatbot. respond( "Hello" ) , "Hi there!" ) ;
95
- assert_eq ! ( chatbot. respond( "Goodbye" ) , "Goodbye!" ) ;
96
- assert_eq ! ( chatbot. respond( "How are you?" ) , "I'm not sure how to respond to that. Could you please rephrase?" ) ;
97
- }
98
-
99
- #[ test]
100
- fn test_chatbot_load_responses ( ) {
101
- let mut chatbot = Chatbot :: new ( ) ;
102
- chatbot. load_responses ( "chatbot_responses.json" ) . unwrap ( ) ;
103
-
104
- assert_eq ! ( chatbot. responses. len( ) , 4 ) ;
105
- assert_eq ! ( chatbot. responses. get( "hello" ) . unwrap( ) , "Hi there! How can I help you?" ) ;
106
- assert_eq ! ( chatbot. responses. get( "bye" ) . unwrap( ) , "Goodbye! Have a great day!" ) ;
107
- assert_eq ! ( chatbot. responses. get( "how are you" ) . unwrap( ) , "I'm a bot, so I don't have feelings, but thanks for asking!" ) ;
108
- assert_eq ! ( chatbot. responses. get( "Who are you" ) . unwrap( ) , "I'm YOU, you MOFO!!" ) ;
109
- }
110
- }
1
+ use serde:: { Deserialize , Serialize } ;
2
+ use std:: collections:: HashMap ;
3
+ use std:: fs:: File ;
4
+ use std:: io:: { self , BufReader , Write } ;
5
+
6
+ /**
7
+ * @file main.rs
8
+ * @brief Rusty: A simple chatbot implemented in Rust.
9
+ */
10
+
11
+ pub struct Chatbot {
12
+ responses : HashMap < String , String > ,
13
+ }
14
+
15
+ impl Chatbot {
16
+ pub fn new ( ) -> Self {
17
+ Chatbot {
18
+ responses : HashMap :: new ( ) ,
19
+ }
20
+ }
21
+
22
+ pub fn respond ( & self , input : & str ) -> String {
23
+ let input = input. trim ( ) . to_lowercase ( ) ;
24
+
25
+ for ( key, value) in & self . responses {
26
+ if input. contains ( & key. to_lowercase ( ) ) {
27
+ return value. clone ( ) ;
28
+ }
29
+ }
30
+
31
+ String :: from ( "I'm not sure how to respond to that. Could you please rephrase?" )
32
+ }
33
+
34
+ pub fn load_responses ( & mut self , filename : & str ) -> Result < ( ) , io:: Error > {
35
+ let file = File :: open ( filename) ?;
36
+ let reader = BufReader :: new ( file) ;
37
+ let responses: Vec < UserInput > = serde_json:: from_reader ( reader) ?;
38
+
39
+ for response in responses {
40
+ self . responses . insert ( response. key , response. value ) ;
41
+ }
42
+ Ok ( ( ) )
43
+ }
44
+ }
45
+
46
+ #[ derive( Debug , Serialize , Deserialize ) ]
47
+ pub struct UserInput {
48
+ key : String ,
49
+ value : String ,
50
+ }
51
+
52
+ fn main ( ) -> io:: Result < ( ) > {
53
+ let mut chatbot = Chatbot :: new ( ) ;
54
+
55
+ if let Err ( e) = chatbot. load_responses ( "chatbot_responses.json" ) {
56
+ eprintln ! ( "Error loading responses: {}" , e) ;
57
+ return Err ( e) ;
58
+ }
59
+
60
+ println ! ( "Chatbot initialized. Type 'exit' to quit." ) ;
61
+
62
+ loop {
63
+ print ! ( "You: " ) ;
64
+ io:: stdout ( ) . flush ( ) ?;
65
+
66
+ let mut input = String :: new ( ) ;
67
+ io:: stdin ( ) . read_line ( & mut input) ?;
68
+
69
+ let input = input. trim ( ) ;
70
+
71
+ if input. eq_ignore_ascii_case ( "exit" ) {
72
+ println ! ( "Goodbye!" ) ;
73
+ break ;
74
+ }
75
+
76
+ let response = chatbot. respond ( input) ;
77
+ println ! ( "Bot: {}" , response) ;
78
+ }
79
+
80
+ Ok ( ( ) )
81
+ }
82
+
83
+ #[ cfg( test) ]
84
+ mod tests {
85
+ use super :: * ;
86
+
87
+ #[ test]
88
+ fn test_chatbot_respond ( ) {
89
+ let mut chatbot = Chatbot :: new ( ) ;
90
+ chatbot
91
+ . responses
92
+ . insert ( "hello" . to_string ( ) , "Hi there!" . to_string ( ) ) ;
93
+ chatbot
94
+ . responses
95
+ . insert ( "bye" . to_string ( ) , "Goodbye!" . to_string ( ) ) ;
96
+
97
+ assert_eq ! ( chatbot. respond( "Hello" ) , "Hi there!" ) ;
98
+ assert_eq ! ( chatbot. respond( "Goodbye" ) , "Goodbye!" ) ;
99
+ assert_eq ! (
100
+ chatbot. respond( "How are you?" ) ,
101
+ "I'm not sure how to respond to that. Could you please rephrase?"
102
+ ) ;
103
+ }
104
+
105
+ #[ test]
106
+ fn test_chatbot_load_responses ( ) {
107
+ let mut chatbot = Chatbot :: new ( ) ;
108
+ chatbot. load_responses ( "chatbot_responses.json" ) . unwrap ( ) ;
109
+
110
+ assert_eq ! ( chatbot. responses. len( ) , 4 ) ;
111
+ assert_eq ! (
112
+ chatbot. responses. get( "hello" ) . unwrap( ) ,
113
+ "Hi there! How can I help you?"
114
+ ) ;
115
+ assert_eq ! (
116
+ chatbot. responses. get( "bye" ) . unwrap( ) ,
117
+ "Goodbye! Have a great day!"
118
+ ) ;
119
+ assert_eq ! (
120
+ chatbot. responses. get( "how are you" ) . unwrap( ) ,
121
+ "I'm a bot, so I don't have feelings, but thanks for asking!"
122
+ ) ;
123
+ assert_eq ! (
124
+ chatbot. responses. get( "Who are you" ) . unwrap( ) ,
125
+ "I'm YOU, you MOFO!!"
126
+ ) ;
127
+ }
128
+ }
0 commit comments