forked from Krucify/CleverNAO-Form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
244 lines (222 loc) · 7.58 KB
/
Form1.cs
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech.Recognition;
using Aldebaran.Proxies;
using ChatterBotAPI;
namespace CleverNAO_Form
{
public partial class Form1 : Form
{
SpeechRecognitionEngine sre;
private bool recognitionRunning;
private bool connectedToNAO;
private bool connectedToCleverBot;
ChatterBotFactory factory;
ChatterBot robot;
ChatterBotSession session;
TextToSpeechProxy tts;
BackgroundWorker bw;
public Form1()
{
InitializeComponent();
speechOnOff.Enabled = false;
ErrorLabel.Text = "";
clearConvoBtn.Click += new System.EventHandler(clearConvoBtn_click);
initializeConvoBox();
initializeBackgroundWorker();
startSpeech();
startCleverbot();
startNAO();
}
#region Initialization Functions
public void initializeConvoBox()
{
//string start = "Hold Shift to speak, then release.";
convoBox.ReadOnly = true;
convoBox.BackColor = System.Drawing.SystemColors.Window;
//insertConvoLine(start, true);
}
public void initializeBackgroundWorker()
{
bw = new BackgroundWorker();
bw.WorkerReportsProgress = true;
bw.WorkerSupportsCancellation = true;
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
}
public void startSpeech()
{
System.Speech.Recognition.DictationGrammar dg = new System.Speech.Recognition.DictationGrammar();
dg.Enabled = true;
sre = new SpeechRecognitionEngine();
sre.LoadGrammar(dg);
sre.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
sre.SetInputToDefaultAudioDevice();
}
public void startCleverbot()
{
factory = new ChatterBotFactory();
robot = factory.Create(ChatterBotType.CLEVERBOT);
session = robot.CreateSession();
}
public void startNAO()
{
try
{
tts = new TextToSpeechProxy("192.168.0.102", 9559);
connectedToNAO = true;
}
catch (Exception e)
{
connectedToNAO = false;
ErrorLabel.Text = "No Connection to NAO";
}
}
#endregion
#region Helper Functions
public void outputToConvoBox(string line, int type)
{
convoBox.AppendText("(" + DateTime.Now.ToLongTimeString() + ") ");
int length = convoBox.TextLength;
switch (type)
{
case 1: // From robot
convoBox.AppendText("Robot: " + line + "\n");
convoBox.SelectionStart = length;
convoBox.SelectionLength = line.Length + 8;
convoBox.SelectionColor = Color.Blue;
if (connectedToNAO)
sendToNAO(line);
break;
case 2: // From human
convoBox.AppendText("Human: " + line + "\n");
convoBox.SelectionStart = length;
convoBox.SelectionLength = line.Length + 8;
convoBox.SelectionColor = Color.Black;
break;
case 3: // System messages
convoBox.AppendText(line + "\n");
convoBox.SelectionStart = length;
convoBox.SelectionLength = line.Length + 1;
convoBox.SelectionColor = Color.Red;
break;
default:
break;
}
inputBox.Enabled = true;
inputBox.Focus();
convoBox.SelectionStart = convoBox.Text.Length;
convoBox.ScrollToCaret();
}
public string getResponse(string input)
{
try
{
connectedToCleverBot = true;
return session.Think(input);
}
catch (Exception e)
{
connectedToCleverBot = false;
return "Error: " + e.Message;
}
}
public void sendToNAO(string input)
{
tts.say(input);
}
#endregion
#region EVENT HANDLERS
void pushToTalk_keyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.ControlKey && !recognitionRunning)
{
try
{
sre.RecognizeAsync();
speechOnOff.BackColor = Color.LimeGreen;
speechOnOff.Text = "On";
e.Handled = true;
e.SuppressKeyPress = true;
recognitionRunning = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
void pushToTalk_keyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.ControlKey)
{
sre.RecognizeAsyncCancel();
speechOnOff.BackColor = System.Drawing.SystemColors.Control;
speechOnOff.Text = "Off";
recognitionRunning = false;
}
}
void inputBox_enter(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
if (!bw.IsBusy)
{
outputToConvoBox(inputBox.Text, 2);
bw.RunWorkerAsync(inputBox.Text);
inputBox.Clear();
inputBox.Enabled = false;
}
}
}
void clearConvoBtn_click(object sender, EventArgs e)
{
convoBox.Clear();
}
// Speech Event Handlers
void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
if (!bw.IsBusy && e.Result.Confidence > 0.3)
{
outputToConvoBox(e.Result.Text, 2);
bw.RunWorkerAsync(e.Result.Text);
}
else
{
outputToConvoBox("Try Again. Speak Clearly.", 3);
}
}
void recognizer_UnrecognizedSpeech(object sender, SpeechRecognitionRejectedEventArgs e)
{
if (!bw.IsBusy)
{
outputToConvoBox("Speech not recognized. " + e.Result.Text, 3);
}
}
//Background Worker Event Handlers
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
e.Result = getResponse((string)e.Argument);
}
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (!connectedToCleverBot)
outputToConvoBox((string)e.Result, 3);
else
outputToConvoBox((string)e.Result, 1);
}
private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
}
#endregion
}
}