Forked from pedro-ramirez-suarez/CustomGrammar.cs
Last active
September 24, 2016 00:12
-
-
Save pedrogk/cdd48782a3a909d72d6e29f544108c2c to your computer and use it in GitHub Desktop.
Intelligent Personal Assistant
This file contains hidden or 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
//Terminals | |
var cuantos = ToTerm("cuantos"); | |
var cuantas = ToTerm("cuantas"); | |
var cual = ToTerm("cual"); | |
var que = ToTerm("que"); | |
var cuando = ToTerm("cuando"); | |
var tiene = ToTerm("tiene"); | |
/* más aquí */ | |
var number = new NumberLiteral("number"); | |
var number2 = new NumberLiteral("number2"); | |
/* más aquí */ | |
//non terminals | |
var cuantosStatement = new NonTerminal("cuantosStatement"); | |
var queStatement = new NonTerminal("queStatement"); | |
var actualizaStatement = new NonTerminal("actualizaStatement"); | |
var operacionesStatement = new NonTerminal("operacionesStatement"); | |
/* más aquí */ | |
//Cuanto pregunta | |
cuantosStatement.Rule = (cuantoscuantas + tabla + tiene + tabla2 + whereId) | | |
(en + cuantoscuantas + tabla + esta + tabla2 + whereId); | |
//que pregunta | |
queStatement.Rule = (que + campo + tiene + tabla + whereId) | | |
(que + tabla + campo + antesdespues + de +whereId ); | |
/* más aquí */ | |
//actualiza comando | |
actualizaStatement.Rule = actualiza + ella + tabla + whereCampo + cambia + su + campo + por + valueSet; | |
//operaciones matematicas | |
operacionesStatement.Rule = cuanto + es + number + (multiplicacion | suma | resta | division) + number2; | |
//todos los comandos posibles de nuestro lenguaje | |
comando.Rule = cuantosStatement | queStatement | cuandoStatement | cualMasMenosStatement | | |
cualMayorMenorStatement | muestraTodoStatement | muestraUnoStatement | | |
actualizaStatement | operacionesStatement; |
This file contains hidden or 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
/* más aquí */ | |
ParseTree tree; | |
Parser parser = new Parser(lang); | |
tree = parser.Parse(query); | |
//Si por alguna razón el comando no fue reconocido, agregar comilla sencilla en la palabra final e intentar de nuevo | |
if (tree.Root == null || tree.Root.ChildNodes[0] == null) | |
{ | |
var words = query.Split(new char[] { ' ' }); | |
words[words.Length - 1] = "'" + words[words.Length - 1] + "'"; | |
query = string.Join(" ", words); | |
//if is still null, return an error | |
tree = parser.Parse(query); | |
if (tree.Root == null || tree.Root.ChildNodes[0] == null) | |
{ | |
return new List<object>() { new { error = "Lo siento, no puedo entenderte, pero la respuesta a la pregunta última sobe la vida, el universo y todo es 42" } }; | |
} | |
} | |
try | |
{ | |
var execute = lang.QueryStatement(tree.Root.ChildNodes[0]); | |
var db = new CustomSearch.data.DataAccess("default"); | |
var result = db.ExecuteQuery(execute.Item1, execute.Item2, execute.Item3); | |
return result; | |
} | |
catch | |
{ | |
return new List<object>() { new { error = "Lo siento, no puedo entenderte, pero la respuesta a la pregunta última sobe la vida, el universo y todo es 42" } }; | |
} |
This file contains hidden or 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
public string LocalSearch(string query) | |
{ | |
var deepThought = new DeepThoughtMachine(); | |
return JsonConvert.SerializeObject(deepThought.Search(query)); | |
} |
This file contains hidden or 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
public void LaunchSearch() { | |
var client = new RestClient(AppResources.DeepThoughtRoot); | |
client.AddHandler("application/json", new DynamicSerializer()); | |
client.ExecuteAsync<dynamic>(new RestRequest(AppResources.DeepThoughtSearch + txtPregunta.Text), (res) => { | |
var data = JsonConvert.DeserializeObject<dynamic>(res.Content); | |
SpeechSynthesizer synth = new SpeechSynthesizer(); | |
VoiceInformation spvoice; | |
bool found = true; | |
// show results | |
foreach(var e in data) { | |
string result = e.ToString(); | |
result = result.Replace("{", "").Replace("}", "").Replace("\r\n","").Replace("\"", ""); | |
var lines = result.Split(new char[] { ','}); | |
result = string.Empty; | |
foreach (var l in lines) { | |
if (l.Contains("no puedo entenderte")) { | |
found = false; | |
break; | |
} | |
if (!l.Trim().ToLower().StartsWith("id")) | |
result += l + "|"; | |
} | |
var record = result.Split(new char[] { '|'}); | |
var row = new StackPanel { Background = new SolidColorBrush(Color.FromArgb(255, 40,40,40)), | |
Margin = new Thickness(2,2,2,8) }; | |
foreach (var f in record) { | |
if (string.IsNullOrWhiteSpace(f)) | |
continue; | |
var sp = new StackPanel { Orientation = System.Windows.Controls.Orientation.Horizontal, | |
Margin = new Thickness(2, 2, 2, 8) }; | |
var item = f.Split(new char[] { ':' }); | |
if (!string.IsNullOrWhiteSpace(item[0])) { | |
var field = new TextBlock { Text = item[0], Foreground = new SolidColorBrush(Color.FromArgb(255, 30, 144, 255)), | |
FontSize = 30 }; | |
sp.Children.Add(field); | |
} | |
var value = new TextBlock { Text = item[1], Foreground = new SolidColorBrush(Color.FromArgb(255, 211, 211, 211)), | |
FontSize = 30 }; | |
sp.Children.Add(value); | |
row.Children.Add(sp); | |
} | |
results.Children.Add(row); | |
} | |
//set voice and message | |
string msg = string.Empty; | |
if (found) { | |
msg = "Esto es lo que encontré"; | |
spvoice = InstalledVoices.All | |
.Where(voice => voice.Language.Equals("es-ES") & voice.Gender == VoiceGender.Female) | |
.FirstOrDefault(); | |
} | |
else { | |
msg = "Lo siento, no puedo entenderte, intenta de nuevo."; | |
spvoice = InstalledVoices.All | |
.Where(voice => voice.Language.Equals("es-ES") & voice.Gender == VoiceGender.Male) | |
.FirstOrDefault(); | |
} | |
results.Children.Add(new StackPanel { Height= 60 }); | |
synth.SetVoice(spvoice); | |
synth.SpeakTextAsync(msg); | |
}); | |
} |
This file contains hidden or 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
function goSearch() { | |
$.post( | |
searchUrl, | |
{ query: $('#query').val() }, | |
function (result) { | |
var jsonData = JSON.parse(result); | |
$('#results').html(''); | |
$('#results').hide('fast'); | |
$('#results').append('<h2>Esto es lo que encontré</h2>'); | |
var item = ''; | |
if (jsonData.length == 0) { | |
siriError.load(); | |
siriError.play(); | |
$('#results').append('<h2><label class="label label-info" style="white-space:normal!important">La búsqueda no produjo resultados, pero la respuesta a la pregunta última sobe la vida, el universo y todo es 42</label><h2>'); | |
$('#results').show('slow'); | |
return; | |
} else if (jsonData[0].error != undefined && jsonData[0].error != null) { | |
siriError.load(); | |
siriError.play(); | |
$('#results').append('<h1><label class="label label-danger" style="white-space:normal!important">' + jsonData[0].error + '</label><h1>'); | |
$('#results').show('slow'); | |
return; | |
} | |
siriFound.load(); | |
siriFound.play(); | |
for (var x in jsonData) { | |
var i = jsonData[x]; | |
var names = Object.keys(i) | |
item = '<div class="col-md-3"><div class="well">'; | |
for (var j in names) { | |
if (names[j] == 'ID' || names[j] == 'id' || names[j] == 'Id') | |
continue; | |
item += '<p><h4 class="label label-default fieldName">' + names[j] + '</h4> ' + i[names[j]] + '</p>'; | |
} | |
item += '</div></div>'; | |
$('#results').append(item); | |
} | |
$('#results').show('slow'); | |
}); | |
} |
This file contains hidden or 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
var Language = (from language in InstalledSpeechRecognizers.All | |
where language.Language == "es-ES" | |
select language).FirstOrDefault(); | |
SpeechRecognizerUI speechRecognition = new SpeechRecognizerUI(); | |
speechRecognition.Recognizer.SetRecognizer(Language); | |
SpeechRecognitionUIResult recoResult = await speechRecognition.RecognizeWithUIAsync(); | |
if (recoResult.ResultStatus == SpeechRecognitionUIStatus.Succeeded) { | |
txtPregunta.Text = recoResult.RecognitionResult.Text.Replace(".",""); | |
LaunchSearch(); | |
} |
This file contains hidden or 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
recognition = new webkitSpeechRecognition(); | |
recognition.lang = "es-MX"; | |
recognition.continuous = true; | |
recognition.interimResults = true; | |
recognition.onresult = function (event) { | |
var interim_transcript = ''; | |
for (var i = event.resultIndex; i < event.results.length; ++i) { | |
if (event.results[i].isFinal) { | |
final_transcript += event.results[i][0].transcript; | |
} else { | |
if (event.results[i][0].transcript != '') | |
interim_transcript += event.results[i][0].transcript; | |
} | |
} | |
if (interim_transcript != '') { | |
$('#query').val(interim_transcript); | |
if ($.trim(final_transcript) != $.trim(interim_transcript)) | |
final_transcript = interim_transcript; | |
} | |
} | |
recognition.start(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment