/// <summary> /// Method to extract all entities contained within a SemanticValue. /// </summary> /// <param name="semanticValue">The SemanticValue object.</param> /// <returns>The list of extracted entities.</returns> private List <Entity> ExtractEntities(SemanticValue semanticValue) { List <Entity> entityList = new List <Entity>(); foreach (var entry in semanticValue) { // We currently only consider leaf nodes (whose underlying // value is of type string) as entities. if (entry.Value.Value is string) { // Extract the entity's type (key), value and confidence score. entityList.Add(new Entity() { Type = entry.Key, Value = (string)entry.Value.Value, Score = entry.Value.Confidence }); } else { // Keep looking for leaf nodes. entityList.AddRange(this.ExtractEntities(entry.Value)); } } return(entityList); }
private static List <string> parseSemanticList(string key, SemanticValue semantic) { if (semantic.ContainsKey(key)) { List <string> list = new List <string>(); // remove weird object Object from the ToString int index = semantic[key].Value.ToString().IndexOf("[object Object]"); string result = (index < 0) ? semantic[key].Value.ToString() : semantic[key].Value.ToString().Remove(index, "[object Object]".Length); string[] resultPieces = result.Split('{', '}', ' '); foreach (string piece in resultPieces) { // go through each piece of the semantic and add the non-trivial pieces to our list if (piece != "") { list.Add(piece); } } return(list); } return(null); }
/// <summary> /// Method to construct the IntentData (intents and entities) from /// a SemanticValue. /// </summary> /// <param name="semanticValue">The SemanticValue object.</param> /// <returns>An IntentData object containing the intents and entities.</returns> public static IntentData BuildIntentData(SemanticValue semanticValue) { List <Intent> intentList = new List <Intent>(); if (semanticValue.Value != null) { intentList.Add(new Intent() { Value = semanticValue.Value.ToString(), Score = semanticValue.Confidence }); } // Consider top-level semantics to be intents. foreach (var entry in semanticValue) { intentList.Add(new Intent() { Value = entry.Key, Score = entry.Value.Confidence }); } List <Entity> entityList = ExtractEntities(semanticValue); return(new IntentData() { Intents = intentList.ToArray(), Entities = entityList.ToArray() }); }
public List <String> SpeechRecognized(SemanticValue semantics, string sentence) { List <String> contentSentence = new List <string>(); List <String> contentSemantic = new List <string>(); Dictionary <String, List <String> > dico = new Dictionary <string, List <String> >(); //Fill the dictionary contentSentence.Add(sentence); dico.Add("sentence", contentSentence); foreach (KeyValuePair <String, SemanticValue> child in semantics) { contentSemantic.Add(semantics[child.Key].Value.ToString()); } dico.Add("semantics", contentSemantic); if (dico.Count != 0) { string json = JsonConvert.SerializeObject(dico); this.network.SendString(json, "recognized_speech"); return(contentSemantic); } else { return(null); } }
private static List <string> ParseSemanticList(string key, SemanticValue semantic) { if (semantic.ContainsKey(key)) { List <string> list = new List <string>(); int index = semantic[key].Value.ToString().IndexOf("[object Object]"); string result = (index < 0) ? semantic[key].Value.ToString() : semantic[key].Value.ToString().Remove(index, "[object Object]".Length); string[] resultPieces = result.Split('{', '}', ' '); foreach (string piece in resultPieces) { if (piece != "") { list.Add(piece); } } return(list); } return(null); }
public static void Print(SemanticValue value) { if (value.type == 'i') { EmitCode("stloc {0}", "_i"); EmitCode("ldstr \" Result: {0}{1}\""); EmitCode("ldloc {0}", "_i"); EmitCode("box [mscorlib]System.{0}", "Int32"); EmitCode("ldstr \"{0}\"", "i"); EmitCode("call void [mscorlib]System.Console::WriteLine(string,object,object)"); } else if (value.type == 'r') { EmitCode("stloc {0}", "_r"); EmitCode("ldstr \" Result: {0}{1}\""); EmitCode("ldloc {0}", "_r"); EmitCode("box [mscorlib]System.{0}", "Double"); EmitCode("ldstr \"{0}\"", "r"); EmitCode("call void [mscorlib]System.Console::WriteLine(string,object,object)"); } else if (value.type == 'b') { EmitCode("stloc {0}", "_i"); EmitCode("ldstr \" Result: {0}\""); EmitCode("ldloc {0}", "_i"); EmitCode("box [mscorlib]System.{0}", "Boolean"); EmitCode("call void [mscorlib]System.Console::WriteLine(string,object)"); } //if (value.type == 'b') // Console.WriteLine(value.val); //else // Console.WriteLine("{0}{1}", value.val, value.type); }
public static SemanticValue NegationOp(SemanticValue value) { if (!genCode) { return(new SemanticValue()); } if (value.error != null) { return(value); } if (value.type != 'b') { return new SemanticValue { error = " int / real not allowed in negation ops" } } ; EmitCode("ldc.i4 {0}", 0); EmitCode("ceq"); bool tmp = bool.Parse(value.val); SemanticValue res = new SemanticValue(); res.type = 'b'; res.val = (!tmp).ToString(); return(res); } }
/// <summary> /// Initializes a new instance of the SemanticValue class. /// </summary> /// <param name="value">The semantic value from the recognition engine.</param> public SpeechSemanticValue(SemanticValue value) { this.value = value.Value != null?value.Value.ToString() : string.Empty; this.confidence = value.Confidence; this.items = new SpeechSemanticChildren(value); }
private static string parseSemantic(string key, SemanticValue semantic) { if (semantic.ContainsKey(key)) { // remove weird object Object from the ToString int index = semantic[key].Value.ToString().IndexOf("[object Object]"); string result = (index < 0) ? semantic[key].Value.ToString() : semantic[key].Value.ToString().Remove(index, "[object Object]".Length); string[] resultPieces = result.Split('{', '}', ' '); foreach (string piece in resultPieces) { // go through each piece of the semantic and add the non-trivial pieces to our list if (piece != "") { // return the first valid piece, shouldn't be a list return(piece); } } } return(null); }
private void SpeechRecognized(object sender, SpeechRecognizedEventArgs e) { TB_recognizedText.Background = Brushes.LightGreen; TB_recognizedText.Text = Capitalize(e.Result.Text); SemanticValue semantics = e.Result.Semantics; // La frase ¿Hay algun vuelo para mañana? añade una variable a la semantica para indicar que se ha reconocido esa frase // Si dicha variable se encuentra en la gramatica se muestran los destinos disponibles y si no se extrae más información de la semantica if (semantics.ContainsKey("TomorrowFlights")) { SP_destinationsAvaliable.Visibility = Visibility.Visible; } else { ChangeStackPanelsVisibility(true); // Por defecto Castellón como origen, una mejora seria indicarlo en la gramatica TB_origin.Text = "Castellón"; TB_destination.Text = semantics["Destination"].Value.ToString(); TB_type.Text = semantics["Type"].Value.ToString(); if (semantics.ContainsKey("Price")) { TB_price.Text = semantics["Price"].Value.ToString(); } } B_speak.IsEnabled = true; }
/// <summary> /// Handles the SpeechRecognized event from the Recognition Engine /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public virtual void SpeechRecognized_Handler(object sender, SpeechRecognizedEventArgs e) { string text = e.Result.Text; SemanticValue semantics = e.Result.Semantics; NewInput = true; LastResult = e.Result; }
public static List<KeyValuePair<string, string>> Convert(SemanticValue aSemantics) { List<KeyValuePair<string, string>> semanticsToDict = new List<KeyValuePair<string, string>>(); foreach (var s in aSemantics) { semanticsToDict.Add(new KeyValuePair<string, string>(s.Key.ToString(), s.Value.Value.ToString())); } return semanticsToDict; }
private void oRecognizer_SpeechRecognized(object sender, System.Speech.Recognition.SpeechRecognizedEventArgs e) { RecognitionResult result = e.Result; SemanticValue semantics = e.Result.Semantics; string scriptParameter = ""; if (e.Result.Semantics.ContainsKey("PARAM1")) { string temp = e.Result.Semantics["PARAM1"].Value.ToString().Replace("'s", "").Replace("'S", ""); if (temp.ToUpper() == "I" || temp.ToUpper() == "ME" || temp.ToUpper() == "MY") { temp = gUser; } if (temp.ToUpper() == "YOU" || temp.ToUpper() == "YOUR") { temp = gSystemName; } scriptParameter = temp; if (e.Result.Semantics.ContainsKey("PARAM2")) { temp = e.Result.Semantics["PARAM2"].Value.ToString().Replace("'s", "").Replace("'S", ""); if (temp.ToUpper() == "I" || temp.ToUpper() == "ME" || temp.ToUpper() == "MY") { temp = gUser; } if (temp.ToUpper() == "YOU" || temp.ToUpper() == "YOUR") { temp = gSystemName; } scriptParameter += "," + temp; if (e.Result.Semantics.ContainsKey("PARAM3")) { temp = e.Result.Semantics["PARAM3"].Value.ToString().Replace("'s", "").Replace("'S", ""); if (temp.ToUpper() == "I" || temp.ToUpper() == "ME" || temp.ToUpper() == "MY") { temp = gUser; } if (temp.ToUpper() == "YOU" || temp.ToUpper() == "YOUR") { temp = gSystemName; } scriptParameter += "," + temp; } } } // scriptParameter = scriptParameter.Replace(); if (result.Grammar.Name.ToString() == "Direct Match") { ProcessInput(result.Text, result.Text, scriptParameter); } else { ProcessInput(result.Text, result.Grammar.Name.ToString(), scriptParameter); } }
/// <summary> /// Handles the SpeechRecognized event from the Recognition Engine /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public virtual void SpeechRecognized_Handler(object sender, SpeechRecognizedEventArgs e) { string text = e.Result.Text; SemanticValue semantics = e.Result.Semantics; NewInput = true; LastResult = e.Result; AerDebug.LogSpeech(e.Result.Text, e.Result.Confidence); }
private string GetValue(SemanticValue Semantics, string keyName) { string result = ""; if (Semantics.ContainsKey(keyName)) { result = Semantics[keyName].Value.ToString(); } return(result); }
private string GetConfidence(SemanticValue Semantics, string keyName) { string result = ""; if (Semantics.ContainsKey(keyName)) { result = Semantics[keyName].Confidence.ToString("0.0000"); } return(result); }
public static SemanticValue LogicalOp(SemanticValue left, SemanticValue right, Tokens t) { if (!genCode) { return(new SemanticValue()); } if (right.error != null) { return(right); } if (left.error != null) { return(left); } if (left.type != 'b' || right.type != 'b') { return new SemanticValue { error = " int / real not allowed in boolean ops" } } ; SemanticValue res = new SemanticValue(); bool l = bool.Parse(left.val); bool r = bool.Parse(right.val); bool result = false; switch (t) { case Tokens.And: EmitCode("and"); EmitCode("ldc.i4 {0}", 0); EmitCode("cgt"); result = l && r; break; case Tokens.Or: EmitCode("Or"); EmitCode("ldc.i4 {0}", 0); EmitCode("cgt"); result = l || r; break; default: return(new SemanticValue { error = " internal error" }); } res.type = 'b'; res.val = result.ToString(); return(res); }
private string[] getTags(SemanticValue s) { List <string> tags = new List <string>(); foreach (var result in s) { string value = (string)result.Value.Value; tags.Add(value); } return(tags.ToArray()); }
/// <summary> /// Constructor that creates an instance from a SemanticValue object's /// internal data. The newly created instance aims to represent the same /// data a SemanticValue object does. /// </summary> /// <param name="keyName">The key under which this semantic value instance is /// referenced by its parent. Can be null for the the root value</param> /// <param name="semanticValue"></param> public RecognizedSemanticValue(string keyName, SemanticValue semanticValue) { this.KeyName = keyName; this.Confidence = semanticValue.Confidence; this.Value = semanticValue.Value; // Copy children as well Children = new DssDictionary <string, RecognizedSemanticValue>(); foreach (KeyValuePair <string, SemanticValue> child in semanticValue) { Children.Add(child.Key, new RecognizedSemanticValue(child.Key, child.Value)); } }
private void SpeechRecognitionService_SpeechRecognized(System.Speech.Recognition.SpeechRecognizedEventArgs e) { SemanticValue semantics = e.Result.Semantics; if (semantics.ContainsKey(NEW_KEY)) { if (semantics.ContainsKey(LEVEL_KEY)) { switch (semantics[LEVEL_KEY].Value.ToString()) { case "facil": RB_Easy.IsChecked = true; break; case "media": RB_Med.IsChecked = true; break; case "dificil": RB_Hard.IsChecked = true; break; } } NuevaPartida(); } else if (semantics.ContainsKey(RESTART_KEY)) { ReiniciarPartida(); } else if (semantics.ContainsKey(PROBABLE_KEY)) { CB_VerPosibles.IsChecked = mostrarPosibles = bool.Parse(semantics[PROBABLE_KEY].Value.ToString()); ActualizaPosibles(); } else { int newNumber = 0; if (semantics.ContainsKey(NUMBER_KEY)) { newNumber = int.Parse(semantics[NUMBER_KEY].Value.ToString()); } int row = LEFT_HEADER.IndexOf(semantics["Row"].Value.ToString()); int column = int.Parse(semantics["Column"].Value.ToString()) - 1; PonSelecciónEn(row, column); _s[_filaActual, _columnaActual] = newNumber; } }
public static SemanticValue Create(string val, char type) { if (!genCode) { return(new SemanticValue()); } SemanticValue res = new SemanticValue(); res.type = type; if (type == 'b') { res.val = val; EmitCode("ldc.i4 {0}", bool.Parse(val) ? 1 : 0); } if (type == 'i') { int tmp; if (int.TryParse(val, out tmp)) { res.val = tmp.ToString(CultureInfo.InvariantCulture); EmitCode("ldc.i4 {0}", res.val); } else { return new SemanticValue { error = string.Format(" value {0} not fitted in int", val) } }; } if (type == 'r') { double tmp; if (double.TryParse(val, NumberStyles.Float, CultureInfo.InvariantCulture, out tmp)) { res.val = tmp.ToString(CultureInfo.InvariantCulture); EmitCode("ldc.r8 {0}", res.val); } else { return new SemanticValue { error = string.Format(" value {0} not fitted in double", val) } }; } return(res); }
private SemanticStruct createStruct(SemanticValue init) { SemanticStruct result = new SemanticStruct(); if (init.Value != null) { result.value = init.Value; result.confidence = init.Confidence; } foreach (KeyValuePair <String, SemanticValue> child in init) { result.Add(child.Key, createStruct(child.Value)); } return(result); }
private static Dictionary<string, string> GetPluginParameters(SemanticValue semantics) { Dictionary<string, string> dictionary = null; if (semantics["params"] != null && semantics["params"].Count > 0) { dictionary = new Dictionary<string, string>(); foreach (var semantic in semantics["params"]) { var semanticValue = semantic.Value; dictionary.Add(semantic.Key, (string)semanticValue.Value); } } return dictionary; }
/// <summary> /// Creates a SemanticValue instance from this object /// </summary> /// <returns>The created semantic value instance or null if this object's /// value is not set</returns> public SemanticValue ToSemanticValue() { if (TypeOfValue == RecognizedValueType.Undefined) { return(null); } SemanticValue semanticValue = new SemanticValue(KeyName, Value, Confidence); foreach (KeyValuePair <string, RecognizedSemanticValue> child in Children) { semanticValue[child.Key] = child.Value.ToSemanticValue(); } return(semanticValue); }
private static void _recognizerHandler(object sender, SpeechRecognizedEventArgs e) { using (DatabaseContext context = new DatabaseContext()) { if (context.Grammars.Any(x => x.Name.Equals(e.Result.Text))) { DIM_Vision_Entities.Entities.Grammar action = context.Grammars.FirstOrDefault(x => x.Name.Equals(e.Result.Text)); if (action != null) { MethodInfo met = typeof(WindowsInteraction).GetMethod(action.Value); if (met != null) { var response = met.Invoke(null, null); synth.Speak((string)response); } } } else if (context.Choices.Any(x => x.Name.Equals(e.Result.Text))) { DIM_Vision_Entities.Entities.Choice action = context.Choices.FirstOrDefault(x => x.Name.Equals(e.Result.Text)); if (action != null) { MethodInfo met = typeof(WindowsInteraction).GetMethod(action.Value); if (met != null) { met.Invoke(null, null); } } } else { SemanticValue semantics = e.Result.Semantics; if (semantics.Any(y => context.Choices.Any(x => x.Value.Equals(y.Key)))) { var sem = semantics.FirstOrDefault(y => context.Choices.Any(x => x.Value.Equals(y.Key))); MethodInfo met = typeof(WindowsInteraction).GetMethod(sem.Value.Value.ToString()); if (met != null) { var response = met.Invoke(null, null); synth.Speak((string)response); } } } } // synth.Speak(string.Join(",",listApps)); }
/** * @method prConstructSemanticsJSON * * Construct a JSON semantics object manually so that it can be read in javascript. * * @param {SemanticValue} Semantics object returned by the recognition engine. * @returns {string} String in JSON format with serialized semantics object. */ private string ConstructSemanticsJSON(SemanticValue sem) { string semantics = null; foreach (KeyValuePair <String, SemanticValue> child in sem) { semantics += (semantics == null) ? "{" : ","; semantics += "\"" + child.Key + "\":\"" + child.Value.Value + "\""; } if (semantics != null) { semantics += "}"; } return(semantics); }
private SortedList ParseResult(SemanticValue value) { System.Collections.SortedList htResult = new SortedList(); foreach (var item in value) { if (item.Value.Count > 0) { htResult.Add(item.Key, ParseResult(item.Value)); } else { htResult.Add(item.Key, item.Value.Value.ToString()); } } return(htResult); }
/// <summary> Event handler tasked with routing the recognized voice commands. </summary> /// <param name="sender"> The object that caused the event. </param> /// <param name="e"> The speech recognized commands. </param> private void Recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) { SemanticValue semantics = e.Result.Semantics; this.logger.LogInformation("Recognized Text: " + e.Result.Text); if (semantics.ContainsKey("PowerOn")) { this.PowerOn(); } if (semantics.ContainsKey("PowerOff")) { this.PowerOff(); } if (semantics.ContainsKey("CurrentTemperature")) { this.CurrentTemperatureCommand(); } if (semantics.ContainsKey("HeatRoom")) { SemanticValue heatRoomSemanticValue = semantics["HeatRoom"]; double targetTemperature = double.Parse(heatRoomSemanticValue["TargetTemp"].Value.ToString()); Task.WaitAny(this.StartHeatingMode(targetTemperature)); } if (semantics.ContainsKey("CoolRoom")) { SemanticValue coolRoomSemanticValue = semantics["CoolRoom"]; double targetTemperature = double.Parse(coolRoomSemanticValue["TargetTemp"].Value.ToString()); Task.WaitAny(this.StartCoolingMode(targetTemperature)); } if (semantics.ContainsKey("ChangeRoomTemp")) { SemanticValue coolRoomSemanticValue = semantics["ChangeRoomTemp"]; double targetTemperature = double.Parse(coolRoomSemanticValue["TargetTemp"].Value.ToString()); Task.WaitAny(this.ChangeRoomTemperatureCommand(targetTemperature)); } }
private SortedList ParseResult(SemanticValue value) { System.Collections.SortedList htResult = new SortedList(); foreach (var item in value) { if (item.Value.Count > 0) { htResult.Add(item.Key, ParseResult(item.Value)); } else { htResult.Add(item.Key, item.Value.Value.ToString()); } } return htResult; }
string RecInterpret(SemanticValue sem) { if (sem.Count == 1 && (sem.First().Value.Value == null || sem.First().Value.Value.ToString() == String.Empty)) { return "\\" + sem.First().Key + RecInterpret(sem.First().Value); } else if (sem.Count > 0) { StringBuilder sb = new StringBuilder("?"); foreach (var item in sem) { sb.Append(item.Key + "=" + item.Value.Value + "&"); } return sb.ToString(); } return String.Empty; }
/** * @method prConstructSemanticsJSON * * Construct a JSON semantics object manually so that it can be read in javascript. * * @param {SemanticValue} Semantics object returned by the recognition engine. * @returns {string} String in JSON format with serialized semantics object. */ private string ConstructSemanticsJSON(SemanticValue sem) { string semantics = null; foreach (KeyValuePair <String, SemanticValue> child in sem) { semantics += (semantics == null) ? "[" : ","; semantics += "{\"" + child.Key + "\":\"" + child.Value.Value + "\"}"; //Console.WriteLine("Los litros a repostar son: {0} {1}", child.Key, child.Value.Value ?? "null"); } if (semantics != null) { semantics += "]"; } return(semantics); }
void _recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) { //obtenemos un diccionario con los elementos semánticos SemanticValue semantics = e.Result.Semantics; string rawText = e.Result.Text; RecognitionResult result = e.Result; if (semantics.ContainsKey("rgb")) { this.BackColor = Color.FromArgb((int)semantics["rgb"].Value); Update(); } if (semantics.ContainsKey("escribir")) { this.label1.Text = "Esto es un texto."; Update(); } else if (semantics.ContainsKey("remove")) { if (semantics["remove"].Value.ToString() == "text") { this.label1.Text = ""; } else if (semantics["remove"].Value.ToString() == "time") { this.label2.Text = ""; } } else if (semantics.ContainsKey("time")) { var now = DateTime.Now; this.label2.Text = now.ToString("HH:mm"); Update(); synth.Speak("Son las " + now.Hour + " y " + now.Minute); } else { this.label1.Text = "Comando de voz no admitido."; } }
public static void Mem(string id, SemanticValue value) { if (!_identificators.ContainsKey(id)) { throw new ErrorException(string.Format(" variable {0} not declared", id)); } if (_identificators[id].Item1 != value.type && !(_identificators[id].Item1 == 'r' && value.type == 'i')) { throw new ErrorException(" types doesn't match"); } if (_identificators[id].Item1 == 'r' && value.type == 'i') { EmitCode("conv.r8"); } EmitCode("stloc {0}", id); _identificators[id] = new Tuple <char, string>(_identificators[id].Item1, value.val); }
/// <summary> /// Method to construct the IntentData (intents and entities) from /// a SemanticValue. /// </summary> /// <param name="semanticValue">The SemanticValue object.</param> /// <returns>An IntentData object containing the intents and entities.</returns> private IntentData BuildIntentData(SemanticValue semanticValue) { List <Intent> intentList = new List <Intent>(); // Consider top-level semantics to be intents. foreach (var entry in semanticValue) { intentList.Add(new Intent() { Value = entry.Key, Score = entry.Value.Confidence }); } List <Entity> entityList = this.ExtractEntities(semanticValue); return(new IntentData() { Intents = intentList.ToArray(), Entities = entityList.ToArray() }); }
// Creates the semantic items recursively. private TreeViewItem CreateSemanticItem(string key, SemanticValue semanticValue) { if (semanticValue == null) { return null; } else { // The semantics item will be displayed as "key = value". If there's no // value then just the "key" will be displayed TreeViewItem item = new TreeViewItem(); string header = key; if (semanticValue.Value != null) { header += " = " + semanticValue.Value.ToString(); } item.Header = header; // Expand the item item.IsExpanded = true; // Generate the child items recursively foreach (KeyValuePair<String, SemanticValue> child in semanticValue) { item.Items.Add(CreateSemanticItem(child.Key, child.Value)); } return item; } }
public static string DumpSemantics(SemanticValue semanticValue) { StringBuilder builder = new StringBuilder(); if (null == semanticValue) { builder.Append("Null SemanticValue argument"); } else { builder.AppendFormat("SemanticValue.Count: {0}\n", semanticValue.Count); var keys = from sv in semanticValue select sv.Key; foreach (var key in keys) { builder.Append("Key: ").AppendLine(key); builder.Append("Value: "); SemanticValue obj = semanticValue[key]; builder.AppendLine( null == obj ? "Null Value" : obj.Value.ToString() ); builder.AppendLine("==============="); } } return builder.ToString(); }
internal static bool Process(SemanticValue semanticValue) { return false; }
private static string parseSemantic(string key, SemanticValue semantic) { if (semantic.ContainsKey(key)) { // remove weird object Object from the ToString int index = semantic[key].Value.ToString().IndexOf("[object Object]"); string result = (index < 0) ? semantic[key].Value.ToString() : semantic[key].Value.ToString().Remove(index, "[object Object]".Length); string[] resultPieces = result.Split('{', '}', ' '); foreach (string piece in resultPieces) { // go through each piece of the semantic and add the non-trivial pieces to our list if (piece != "") { // return the first valid piece, shouldn't be a list return piece; } } } return null; }
private static List<string> parseSemanticList(string key, SemanticValue semantic) { if (semantic.ContainsKey(key)) { List<string> list = new List<string>(); // remove weird object Object from the ToString int index = semantic[key].Value.ToString().IndexOf("[object Object]"); string result = (index < 0) ? semantic[key].Value.ToString() : semantic[key].Value.ToString().Remove(index, "[object Object]".Length); string[] resultPieces = result.Split('{', '}', ' '); foreach (string piece in resultPieces) { // go through each piece of the semantic and add the non-trivial pieces to our list if (piece != "") { list.Add(piece); } } return list; } return null; }
public static void Execute(SemanticValue semantics) { // get the subject pieces List<string> subjects = parseSemanticList("subject", semantics); // get the command string command = parseSemantic("command", semantics); // get the directObject, if any string directObject = parseSemantic("directObject", semantics); if (subjects != null) { // execute subject keypresses foreach (string subject in subjects) { Trace.WriteLine(subject); if (subjectObject.KeyLookup.ContainsKey(subject)) { DirectInputEmulator.SendKeyPresses(subjectObject.KeyLookup[subject], 75); } } } if (command != null && command != "") { // execute command keypresses Trace.WriteLine(command); DirectInputEmulator.SendKeyPresses(commandObjects[command].KeyLookup[command], 75); // execute direct object keypresses (if needed) if (directObject != null && directObject != "") { Trace.WriteLine(directObject); DirectInputEmulator.SendKeyPresses(commandObjects[command].KeyLookup[directObject], 75); } } }
public static Tuple<string, string> GetSpeciesAndLength(SemanticValue semanticValue) { if (!HasSpeciesAndLength(semanticValue)) return Tuple.Create(String.Empty, String.Empty); // As long as the grammar is valid, there should be no need for a defensive // null check. If it is throwing an NRE, then check the grammar definition. // Don't ask me how I know... var species = semanticValue[SpeciesKey].Value.ToString(); var length = semanticValue[LengthKey].Value.ToString(); System.Diagnostics.Debug.WriteLine("Species: [{0}], Length: [{1}]", species, length); return Tuple.Create(species, length); }
/// <summary> /// Check the recognition result for both a species name and a length value /// </summary> /// <param name="semanticValue"></param> /// <returns></returns> public static bool HasSpeciesAndLength(SemanticValue semanticValue) { bool hasSpecies = semanticValue.ContainsKey(SpeciesKey); bool hasLength = semanticValue.ContainsKey(LengthKey); System.Diagnostics.Debug.WriteLine("HasSpecies? {0} HasLength? {1}", hasSpecies, hasLength); return hasSpecies && hasLength; }
// First clears the semantics tree view and then create the nodes using semanticValue private void DisplaySemantics(SemanticValue semanticValue) { _treeViewSemantics.Items.Clear(); if (semanticValue != null) { // Create the root item _treeViewSemantics.Items.Add(CreateSemanticItem("root", semanticValue)); } }
public static async Task ExecuteAsync(SemanticValue semantics) { // get the subject pieces List<string> subjects = parseSemanticList("subject", semantics); // get the command string command = parseSemantic("command", semantics); // get the directObject, if any string directObject = parseSemantic("directObject", semantics); if (subjects != null) { // execute subject keypresses foreach (string subject in subjects) { Trace.WriteLine(subject); if (subjectObject.KeyLookup.ContainsKey(subject)) { await DirectInputEmulator.SendInputAsync(subjectObject.KeyLookup[subject].SpaceOperations(Core.Instance.Configuration.KeyPressDelay)); } } } if (command != null && command != "") { // execute command keypresses Trace.WriteLine(command); await DirectInputEmulator.SendInputAsync(commandObjects[command].KeyLookup[command].SpaceOperations(Core.Instance.Configuration.KeyPressDelay)); // execute direct object keypresses (if needed) if (directObject != null && directObject != "") { Trace.WriteLine(directObject); await DirectInputEmulator.SendInputAsync(commandObjects[command].KeyLookup[directObject].SpaceOperations(Core.Instance.Configuration.KeyPressDelay)); } } }