Created
March 22, 2022 00:16
-
-
Save Theoistic/dd95003211f61603c51a5a20bd6171f3 to your computer and use it in GitHub Desktop.
MyBot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
good morning | |
good evening | |
good afternoon | |
hello there | |
### | |
speak("hi there"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using SharpNL.DocumentCategorizer; | |
using SharpNL.Extensions; | |
using SharpNL.Tokenize; | |
using SharpNL.Utility; | |
[Serializable] | |
public class IntentModel : IObjectStream<DocumentSample> | |
{ | |
public string Intent { get; set; } | |
public string[] Samples { get; set; } | |
private int Position { get; set; } = 0; | |
public DocumentSample Read() | |
{ | |
if (Position >= Samples.Length) | |
return null; | |
string[] tokens = WhitespaceTokenizer.Instance.Tokenize(Samples[Position]); | |
List<string> vector = new List<string>(tokens.Length); | |
bool skip = false; | |
foreach (string token in tokens) | |
{ | |
if (token.StartsWith("<", StringComparison.Ordinal)) | |
{ | |
skip = !skip; | |
} | |
else if (!skip) | |
{ | |
vector.Add(token); | |
} | |
} | |
tokens = new string[vector.Count]; | |
vector.CopyTo(tokens); | |
Position++; | |
return new DocumentSample(Intent, tokens); | |
} | |
public void Reset() => Position = 0; | |
public void Dispose() => Samples = null; | |
} | |
public class CompIntent<T> : IObjectStream<T> | |
{ | |
private IObjectStream<T>[] streams; | |
private int streamIndex = 0; | |
public CompIntent(IObjectStream<T>[] streams) => this.streams = streams; | |
public T Read() | |
{ | |
T obj = default(T); | |
while (streamIndex < streams.Length && obj == null) | |
{ | |
obj = streams[streamIndex].Read(); | |
if (obj == null) | |
{ | |
streamIndex++; | |
} | |
} | |
return obj; | |
} | |
public void Reset() | |
{ | |
streamIndex = 0; | |
streams.ForEach(x => x.Reset()); | |
} | |
public void Dispose() => streams.ForEach(x => x.Dispose()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Jint; | |
using SharpNL.DocumentCategorizer; | |
using SharpNL.Extensions; | |
using SharpNL.Utility; | |
using System.Speech.Recognition; | |
using System.Speech.Synthesis; | |
string[] data = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.ai"); | |
IObjectStream<DocumentSample>[] CategoryStreams = new IObjectStream<DocumentSample>[data.Length]; | |
Dictionary<string, string> IntentSnippet = new Dictionary<string, string>(); | |
data.ForEach((x, i) => | |
{ | |
var d = File.ReadAllText(x).Split("###"); | |
string intentName = Path.GetFileNameWithoutExtension(x); | |
string[] samples = d[0].Replace("\r", "").Split("\n"); | |
string code = d[1].Replace("\r", "").Replace("\n", Environment.NewLine); | |
IntentSnippet.Add(intentName, code); | |
CategoryStreams[i] = new IntentModel | |
{ | |
Intent = intentName, | |
Samples = samples | |
}; | |
}); | |
var param = new TrainingParameters(); | |
param.Set(Parameters.Iterations, "70"); | |
param.Set(Parameters.Cutoff, "1"); | |
DocumentCategorizerModel categorizerModel = DocumentCategorizerME.Train("vi", | |
new CompIntent<DocumentSample>(CategoryStreams), param, new DocumentCategorizerFactory()); | |
Engine engine = new Engine().SetValue("speak", new Action<string>(x => | |
{ | |
SpeechSynthesizer synth = new SpeechSynthesizer(); | |
synth.SetOutputToDefaultAudioDevice(); | |
synth.Speak(x); | |
})); | |
SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine( | |
new System.Globalization.CultureInfo("en-US")); | |
recognizer.LoadGrammar(new DictationGrammar()); | |
recognizer.SpeechRecognized += (sender, e) => | |
{ | |
if(e.Result.Confidence > 0.25) | |
{ | |
DocumentCategorizerME categorizer = new DocumentCategorizerME(categorizerModel); | |
var outcome = categorizer.Categorize(e.Result.Text); | |
var intent = categorizer.GetBestCategory(outcome); | |
engine.Execute(IntentSnippet[intent]); | |
} | |
}; | |
recognizer.SetInputToDefaultAudioDevice(); | |
recognizer.RecognizeAsync(RecognizeMode.Multiple); | |
while (true) { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
what time is it | |
do you know the time | |
do you have the time | |
can you give me the time | |
### | |
speak("buy a watch you fool."); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment