-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatbotExamples.cs
170 lines (131 loc) · 4.88 KB
/
ChatbotExamples.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
using System;
//add assembly reference for these
using System.Speech.Synthesis;
using System.Speech.Recognition;
//installed with Nuget
using Cleverbot.Net;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Threading;
namespace ChatbotExamples
{
public class ChatBot
{
SpeechSynthesizer synthesizer;
SpeechRecognitionEngine recognizer;
public ChatBot()
{
synthesizer = new SpeechSynthesizer();
synthesizer.Volume = 100; // (0 - 100)
synthesizer.Rate = 0; // (-10 - 10)
// Create an in-process speech recognizer for the en-US locale.
System.Globalization.CultureInfo en = new System.Globalization.CultureInfo("en-US");
recognizer = new SpeechRecognitionEngine(en);
// Create and load a dictation grammar.
recognizer.LoadGrammar(new DictationGrammar());
// Add a handler for the speech recognized event.
recognizer.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
// Configure input to the speech recognizer.
recognizer.SetInputToDefaultAudioDevice();
}
// Handle the SpeechRecognized event.
static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
Console.WriteLine("Recognized text: " + e.Result.Text);
}
public void ChatLoop()
{
// Console.Write("Please say something: ");
// recognizer.Recognize();
while (true)
{
string answer = CallCleverbot();
//clean input, check speed of this
answer = Regex.Replace(answer, "[^a-zA-Z0-9% ._]", string.Empty);
var words = answer.Split(' ');
foreach (string word in words)
{
int syllables = CalculateSyllables(word);
//use this info to send bytecode to Arduino
Console.WriteLine(word + " contains: " + syllables + " syllables.");
// synthesizer.Speak(word);
//synthesizer.SpeakAsync(word);
//synthesizer.Speak("");
}
}
}
public string CallCleverbot()
{
string question, answer = "";
//API User and Pass Keys
var session = CleverbotSession.NewSession("0VrnWzbEZNTu267C", "SQ6OtXq07kede794AByhwee7pMA6aewT");
Console.Write("Ask Cleverbot Something:");
question = Console.ReadLine();
answer = session.Send(question);
Console.WriteLine(answer);
return answer;
}
public int CalculateSyllables(string word)
{
char[] vowels = { 'a', 'e', 'i', 'o', 'u', 'y' };
int vowelCount = 0;
word = word.ToLower();
bool lastCharWasVowel = false;
foreach (char letter in word)
{
bool foundVowel = false;
foreach (char vowel in vowels)
{
//if last letter was a vowel...
//example: coin
//only count as one
if (vowel == letter)
{
foundVowel = true;
if (lastCharWasVowel)
{
lastCharWasVowel = true;
break;
}
else
{
lastCharWasVowel = true;
vowelCount++;
break;
}
}
}
//if full cycle and no vowel found, set lastWasVowel to false;
if (!foundVowel)
lastCharWasVowel = false;
}
//if word ends with 'e' or 'es' a syllable can be subtracted as this is usually silent.
if(word.Length > 1)
{
if(new[] { "es", "e"}.Equals(word.Substring(word.Length-2)))
{
vowelCount--;
}
}
//If no Vowels, assume 1 Syllable.
//ex: 'hmmm'
if (vowelCount < 1)
{
return 1;
}
else
{
return vowelCount;
}
}
}
class Program
{
static void Main(string[] args)
{
ChatBot chat = new ChatBot();
chat.ChatLoop();
}
}
}