private static void RecursivelyExtractSemanticProperties(List <ResultPropertiesRef> propertyList, int semanticsOffset, IntPtr phraseBuffer, RuleNode ruleTree, IList <RecognizedWordUnit> words, bool isSapi53Header) { IntPtr ptr = new IntPtr((long)phraseBuffer + semanticsOffset); SPSERIALIZEDPHRASEPROPERTY sPSERIALIZEDPHRASEPROPERTY = (SPSERIALIZEDPHRASEPROPERTY)Marshal.PtrToStructure(ptr, typeof(SPSERIALIZEDPHRASEPROPERTY)); string propertyName; SemanticValue semanticValue = ExtractSemanticValueInformation(semanticsOffset, sPSERIALIZEDPHRASEPROPERTY, phraseBuffer, isSapi53Header, out propertyName); RuleNode ruleNode = ruleTree.Find(sPSERIALIZEDPHRASEPROPERTY.ulFirstElement, sPSERIALIZEDPHRASEPROPERTY.ulCountOfElements); if (propertyName == "SemanticKey") { ruleNode._name = (string)semanticValue.Value; ruleNode._hasName = true; } else { propertyList.Add(new ResultPropertiesRef(propertyName, semanticValue, ruleNode)); } if (sPSERIALIZEDPHRASEPROPERTY.pFirstChildOffset != 0) { RecursivelyExtractSemanticProperties(propertyList, (int)sPSERIALIZEDPHRASEPROPERTY.pFirstChildOffset, phraseBuffer, ruleTree, words, isSapi53Header); } if (sPSERIALIZEDPHRASEPROPERTY.pNextSiblingOffset != 0) { RecursivelyExtractSemanticProperties(propertyList, (int)sPSERIALIZEDPHRASEPROPERTY.pNextSiblingOffset, phraseBuffer, ruleTree, words, isSapi53Header); } }
private void RecursivelyExtractSemanticValue(IntPtr phraseBuffer, int semanticsOffset, SemanticValue semanticValue, IList <RecognizedWordUnit> words, bool isSapi53Header, GrammarOptions semanticTag) { IntPtr ptr = new IntPtr((long)phraseBuffer + semanticsOffset); SPSERIALIZEDPHRASEPROPERTY sPSERIALIZEDPHRASEPROPERTY = (SPSERIALIZEDPHRASEPROPERTY)Marshal.PtrToStructure(ptr, typeof(SPSERIALIZEDPHRASEPROPERTY)); string propertyName; SemanticValue semanticValue2 = ExtractSemanticValueInformation(semanticsOffset, sPSERIALIZEDPHRASEPROPERTY, phraseBuffer, isSapi53Header, out propertyName); if (propertyName == "_value" && semanticValue != null) { semanticValue.Value = semanticValue2.Value; if (sPSERIALIZEDPHRASEPROPERTY.pFirstChildOffset != 0) { semanticValue2 = semanticValue; } } else { InsertSemanticValueToDictionary(semanticValue, propertyName, semanticValue2, semanticTag, ref _dupItems); } if (sPSERIALIZEDPHRASEPROPERTY.pFirstChildOffset != 0) { RecursivelyExtractSemanticValue(phraseBuffer, (int)sPSERIALIZEDPHRASEPROPERTY.pFirstChildOffset, semanticValue2, words, isSapi53Header, semanticTag); } if (sPSERIALIZEDPHRASEPROPERTY.pNextSiblingOffset != 0) { RecursivelyExtractSemanticValue(phraseBuffer, (int)sPSERIALIZEDPHRASEPROPERTY.pNextSiblingOffset, semanticValue, words, isSapi53Header, semanticTag); } }
public SemanticValueDebugDisplay(SemanticValue value) { _value = value.Value; _dictionary = value._dictionary; _name = value.KeyName; _confidence = value.Confidence; }
private void AppendAttributes(XmlElement propertyNode, SemanticValue semanticValue) { foreach (KeyValuePair <string, SemanticValue> item in (IEnumerable <KeyValuePair <string, SemanticValue> >)semanticValue) { if (propertyNode.Attributes[item.Key] == null) { propertyNode.SetAttribute(item.Key, item.Value.Value.ToString()); } } }
private bool CalcILSemantics(IntPtr phraseBuffer) { if ((_grammarOptions & GrammarOptions.SemanticInterpretation) != 0 || _grammarOptions == GrammarOptions.KeyValuePairs) { IList <RecognizedWordUnit> words = Words; _semantics = new SemanticValue("<ROOT>", null, _confidence); if (_serializedPhrase.PropertiesOffset != 0) { RecursivelyExtractSemanticValue(phraseBuffer, (int)_serializedPhrase.PropertiesOffset, _semantics, words, _isSapi53Header, _grammarOptions & GrammarOptions.TagFormat); } return(true); } return(false); }
private static bool TryExecuteOnParse(RuleNode ruleRef, SemanticValue value, IList <RecognizedWordUnit> words, out object newValue, ref Exception exceptionThrown) { newValue = null; bool result = false; Grammar grammar = ruleRef._grammar; if (grammar != null && grammar._scripts != null) { try { if (exceptionThrown != null) { if (!ExecuteOnError(grammar, ruleRef, exceptionThrown)) { return(result); } exceptionThrown = null; return(result); } result = ExecuteOnParse(grammar, ruleRef, value, words, out newValue); return(result); } catch (Exception ex) { if (exceptionThrown == null) { exceptionThrown = ex; try { if (!ExecuteOnError(grammar, ruleRef, exceptionThrown)) { return(result); } exceptionThrown = null; return(result); } catch (Exception ex2) { Exception ex3 = exceptionThrown = ex2; return(result); } } return(result); } } return(result); }
private static bool ExecuteOnParse(Grammar grammar, RuleNode ruleRef, SemanticValue value, IList <RecognizedWordUnit> words, out object newValue) { ScriptRef[] scripts = grammar._scripts; bool result = false; newValue = null; foreach (ScriptRef scriptRef in scripts) { if (!(ruleRef._rule == scriptRef._rule) || scriptRef._method != RuleMethodScript.onParse) { continue; } RecognizedWordUnit[] array = new RecognizedWordUnit[ruleRef._count]; for (int j = 0; j < ruleRef._count; j++) { array[j] = words[j]; } object[] parameters = new object[2] { value, array }; if (grammar._proxy != null) { Exception exceptionThrown; newValue = grammar._proxy.OnParse(scriptRef._rule, scriptRef._sMethod, parameters, out exceptionThrown); if (exceptionThrown != null) { throw exceptionThrown; } } else { MethodInfo onParse; Grammar ruleInstance; GetRuleInstance(grammar, scriptRef._rule, scriptRef._sMethod, out onParse, out ruleInstance); newValue = onParse.Invoke(ruleInstance, parameters); } result = true; } return(result); }
private static void InsertSemanticValueToDictionary(SemanticValue semanticValue, string propertyName, SemanticValue thisSemanticValue, GrammarOptions semanticTag, ref Collection <SemanticValue> dupItems) { string text = propertyName; if ((text == "$" && semanticTag == GrammarOptions.MssV1) || (text == "=" && (semanticTag == GrammarOptions.KeyValuePairSrgs || semanticTag == GrammarOptions.KeyValuePairs)) || (thisSemanticValue.Count == -1 && semanticTag == GrammarOptions.W3cV1)) { if ((semanticTag & GrammarOptions.SemanticInterpretation) == 0 && semanticValue._valueFieldSet && !semanticValue.Value.Equals(thisSemanticValue.Value)) { throw new InvalidOperationException(SR.Get(SRID.DupSemanticValue, semanticValue.KeyName)); } semanticValue.Value = thisSemanticValue.Value; semanticValue._valueFieldSet = true; } else if (!semanticValue._dictionary.ContainsKey(text)) { semanticValue._dictionary.Add(text, thisSemanticValue); } else if (!semanticValue._dictionary[text].Equals(thisSemanticValue)) { if (semanticTag == GrammarOptions.KeyValuePairSrgs) { throw new InvalidOperationException(SR.Get(SRID.DupSemanticKey, propertyName, semanticValue.KeyName)); } int num = 0; do { text = propertyName + string.Format(CultureInfo.InvariantCulture, "_{0}", new object[1] { num++ }); }while (semanticValue._dictionary.ContainsKey(text)); semanticValue._dictionary.Add(text, thisSemanticValue); if (dupItems == null) { dupItems = new Collection <SemanticValue>(); } SemanticValue item = semanticValue._dictionary[text]; dupItems.Add(item); } }
private void CalcSemantics(Grammar grammar) { if (_semantics == null && _serializedPhrase.SemanticErrorInfoOffset == 0) { GCHandle gCHandle = GCHandle.Alloc(_phraseBuffer, GCHandleType.Pinned); try { IntPtr phraseBuffer = gCHandle.AddrOfPinnedObject(); if (!CalcILSemantics(phraseBuffer)) { IList <RecognizedWordUnit> words = Words; RuleNode ruleNode = ExtractRules(grammar, _serializedPhrase.Rule, phraseBuffer); List <ResultPropertiesRef> properties = BuildRecoPropertyTree(_serializedPhrase, phraseBuffer, ruleNode, words, _isSapi53Header); _semantics = RecursiveBuildSemanticProperties(words, properties, ruleNode, _grammarOptions & GrammarOptions.TagFormat, ref _dupItems); _semantics.Value = TryExecuteOnRecognition(grammar, _recoResult, ruleNode._rule); } } finally { gCHandle.Free(); } } }
private void AppendPropertiesSML(XmlDocument document, XmlElement alternateNode, SemanticValue semanticsNode, NumberFormatInfo nfo) { if (semanticsNode != null) { foreach (KeyValuePair <string, SemanticValue> item in (IEnumerable <KeyValuePair <string, SemanticValue> >)semanticsNode) { if (item.Key == "_attributes") { AppendAttributes(alternateNode, item.Value); if (string.IsNullOrEmpty(alternateNode.InnerText) && semanticsNode.Value != null) { XmlText newChild = document.CreateTextNode(semanticsNode.Value.ToString()); alternateNode.AppendChild(newChild); } } else { string name = item.Key; if (_dupItems != null && _dupItems.Contains(item.Value)) { name = RemoveTrailingNumber(item.Key); } XmlElement xmlElement = document.CreateElement(name); xmlElement.SetAttribute("confidence", semanticsNode[item.Key].Confidence.ToString("f", nfo)); alternateNode.AppendChild(xmlElement); if (item.Value.Count > 0) { if (item.Value.Value != null) { XmlText newChild2 = document.CreateTextNode(item.Value.Value.ToString()); xmlElement.AppendChild(newChild2); } AppendPropertiesSML(document, xmlElement, item.Value, nfo); } else if (item.Value.Value != null) { XmlText newChild3 = document.CreateTextNode(item.Value.Value.ToString()); xmlElement.AppendChild(newChild3); } } } } }
internal ResultPropertiesRef(string name, SemanticValue value, RuleNode ruleNode) { _name = name; _value = value; _ruleNode = ruleNode; }
private static SemanticValue RecursiveBuildSemanticProperties(IList <RecognizedWordUnit> words, List <ResultPropertiesRef> properties, RuleNode ruleTree, GrammarOptions semanticTag, ref Collection <SemanticValue> dupItems) { SemanticValue semanticValue = new SemanticValue(ruleTree._name, null, ruleTree._confidence); for (RuleNode ruleNode = ruleTree._child; ruleNode != null; ruleNode = ruleNode._next) { SemanticValue semanticValue2 = RecursiveBuildSemanticProperties(words, properties, ruleNode, semanticTag, ref dupItems); if (!ruleNode._hasName) { foreach (KeyValuePair <string, SemanticValue> item in semanticValue2._dictionary) { InsertSemanticValueToDictionary(semanticValue, item.Key, item.Value, semanticTag, ref dupItems); } if (semanticValue2.Value != null) { if ((semanticTag & GrammarOptions.SemanticInterpretation) == 0 && semanticValue._valueFieldSet && !semanticValue.Value.Equals(semanticValue2.Value)) { throw new InvalidOperationException(SR.Get(SRID.DupSemanticValue, ruleTree._name)); } semanticValue.Value = semanticValue2.Value; semanticValue._valueFieldSet = true; } } else { if (!semanticValue2._valueFieldSet && semanticValue2.Count == 0) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < ruleNode._count; i++) { if (stringBuilder.Length > 0) { stringBuilder.Append(" "); } stringBuilder.Append(words[(int)ruleNode._firstElement + i].Text); } semanticValue2._valueFieldSet = true; semanticValue2.Value = stringBuilder.ToString(); } semanticValue._dictionary.Add(ruleNode._name, semanticValue2); } } foreach (ResultPropertiesRef property in properties) { if (property._ruleNode == ruleTree) { InsertSemanticValueToDictionary(semanticValue, property._name, property._value, semanticTag, ref dupItems); } } Exception exceptionThrown = null; object newValue; bool flag = TryExecuteOnParse(ruleTree, semanticValue, words, out newValue, ref exceptionThrown); if (exceptionThrown != null) { throw exceptionThrown; } if (flag) { semanticValue._dictionary.Clear(); semanticValue.Value = newValue; semanticValue._valueFieldSet = true; } return(semanticValue); }