internal CfgSerializedHeader(Stream stream) { BinaryReader binaryReader = new BinaryReader(stream); ulTotalSerializedSize = binaryReader.ReadUInt32(); if (ulTotalSerializedSize < 100 || ulTotalSerializedSize > int.MaxValue) { XmlParser.ThrowSrgsException(SRID.UnsupportedFormat); } FormatId = new Guid(binaryReader.ReadBytes(16)); if (FormatId != _SPGDF_ContextFree) { XmlParser.ThrowSrgsException(SRID.UnsupportedFormat); } GrammarGUID = new Guid(binaryReader.ReadBytes(16)); LangID = binaryReader.ReadUInt16(); pszSemanticInterpretationGlobals = binaryReader.ReadUInt16(); cArcsInLargestState = binaryReader.ReadInt32(); cchWords = binaryReader.ReadInt32(); cWords = binaryReader.ReadInt32(); pszWords = binaryReader.ReadUInt32(); if (pszWords < 100 || pszWords > ulTotalSerializedSize) { XmlParser.ThrowSrgsException(SRID.UnsupportedFormat); } cchSymbols = binaryReader.ReadInt32(); pszSymbols = binaryReader.ReadUInt32(); cRules = binaryReader.ReadInt32(); pRules = binaryReader.ReadUInt32(); cArcs = binaryReader.ReadInt32(); pArcs = binaryReader.ReadUInt32(); pWeights = binaryReader.ReadUInt32(); cTags = binaryReader.ReadInt32(); tags = binaryReader.ReadUInt32(); ulReservered1 = binaryReader.ReadUInt32(); ulReservered2 = binaryReader.ReadUInt32(); if (pszWords > 100) { cScripts = binaryReader.ReadInt32(); pScripts = binaryReader.ReadUInt32(); cIL = binaryReader.ReadInt32(); pIL = binaryReader.ReadUInt32(); cPDB = binaryReader.ReadInt32(); pPDB = binaryReader.ReadUInt32(); ulRootRuleIndex = binaryReader.ReadUInt32(); GrammarOptions = (GrammarOptions)binaryReader.ReadUInt32(); cBasePath = binaryReader.ReadUInt32(); GrammarMode = binaryReader.ReadUInt32(); ulReservered3 = binaryReader.ReadUInt32(); ulReservered4 = binaryReader.ReadUInt32(); } }
internal void InitializeFromSerializedBuffer(RecognitionResult recoResult, SPSERIALIZEDPHRASE serializedPhrase, IntPtr phraseBuffer, int phraseLength, bool isSapi53Header, bool hasIPAPronunciation) { _recoResult = recoResult; _isSapi53Header = isSapi53Header; _serializedPhrase = serializedPhrase; _confidence = _serializedPhrase.Rule.SREngineConfidence; _grammarId = _serializedPhrase.ullGrammarID; _homophoneGroupId = _serializedPhrase.wHomophoneGroupId; _hasIPAPronunciation = hasIPAPronunciation; _phraseBuffer = new byte[phraseLength]; Marshal.Copy(phraseBuffer, _phraseBuffer, 0, phraseLength); _grammarOptions = ((recoResult.Grammar != null) ? recoResult.Grammar._semanticTag : GrammarOptions.KeyValuePairSrgs); CalcSemantics(recoResult.Grammar); }
private void Init(string id, CfgRule cfgRule, int iSerialize, GrammarOptions SemanticFormat, ref int cImportedRules) { _id = id; _cfgRule = cfgRule; _firstState = null; _cfgRule.DirtyRule = true; _iSerialize = iSerialize; _fHasExitPath = false; _fHasDynamicRef = false; _fIsEpsilonRule = false; _fStaticRule = false; if (_cfgRule.Import) { cImportedRules++; } }
internal static GrammarOptions TagFormat2GrammarOptions(SrgsTagFormat value) { GrammarOptions result = GrammarOptions.KeyValuePairs; switch (value) { case SrgsTagFormat.KeyValuePairs: result = GrammarOptions.KeyValuePairSrgs; break; case SrgsTagFormat.MssV1: result = GrammarOptions.MssV1; break; case SrgsTagFormat.W3cV1: result = GrammarOptions.W3cV1; break; } return(result); }
internal static GrammarOptions TagFormat2GrammarOptions(SrgsTagFormat value) { GrammarOptions newValue = 0; switch (value) { case SrgsTagFormat.KeyValuePairs: newValue = GrammarOptions.KeyValuePairSrgs; break; case SrgsTagFormat.MssV1: newValue = GrammarOptions.MssV1; break; case SrgsTagFormat.W3cV1: newValue = GrammarOptions.W3cV1; break; } return(newValue); }
internal static SrgsTagFormat GrammarOptions2TagFormat(GrammarOptions value) { SrgsTagFormat result = SrgsTagFormat.Default; switch (value & GrammarOptions.TagFormat) { case GrammarOptions.MssV1: result = SrgsTagFormat.MssV1; break; case GrammarOptions.W3cV1: result = SrgsTagFormat.W3cV1; break; case GrammarOptions.KeyValuePairs: case GrammarOptions.KeyValuePairSrgs: result = SrgsTagFormat.KeyValuePairs; break; } return(result); }
internal Rule(Backend backend, string name, int offsetName, SPCFGRULEATTRIBUTES attributes, int id, int iSerialize, GrammarOptions SemanticFormat, ref int cImportedRules) : base(backend, null) { _rule = this; Init(name, new CfgRule(id, offsetName, attributes), iSerialize, SemanticFormat, ref cImportedRules); }
internal Rule(Backend backend, string name, CfgRule cfgRule, int iSerialize, GrammarOptions SemanticFormat, ref int cImportedRules) : base(backend, null) { _rule = this; Init(name, cfgRule, iSerialize, SemanticFormat, ref cImportedRules); }
// Pulls the required data out of a stream containing a cfg. // Stream must point to start of cfg on entry and is reset to same point on exit. private static string CheckRuleName(Stream stream, string rulename, bool isImportedGrammar, bool stgInit, out bool sapi53Only, out GrammarOptions grammarOptions) { sapi53Only = false; long initialPosition = stream.Position; CfgGrammar.CfgHeader header; using (StreamMarshaler streamHelper = new(stream)) // Use StreamMarshaler which helps deserialize certain data types { CfgGrammar.CfgSerializedHeader serializedHeader = null; header = CfgGrammar.ConvertCfgHeader(streamHelper, false, true, out serializedHeader); StringBlob symbols = header.pszSymbols; // Calc the root rule string rootRule = header.ulRootRuleIndex != 0xffffffff && header.ulRootRuleIndex < header.rules.Length ? symbols.FromOffset(header.rules[header.ulRootRuleIndex]._nameOffset) : null; // Get if we have semantic interpretation sapi53Only = (header.GrammarOptions & (GrammarOptions.MssV1 | GrammarOptions.W3cV1 | GrammarOptions.STG | GrammarOptions.IpaPhoneme)) != 0; // Check that the rule name is valid if (rootRule == null && string.IsNullOrEmpty(rulename)) { throw new ArgumentException(SR.Get(SRID.SapiErrorNoRulesToActivate)); } if (!string.IsNullOrEmpty(rulename)) { // Convert the CFG script reference to ScriptRef bool fFoundRule = false; foreach (CfgRule cfgRule in header.rules) { if (symbols.FromOffset(cfgRule._nameOffset) == rulename) { // Private rule are not allowed fFoundRule = cfgRule.Export || stgInit || (!isImportedGrammar ? cfgRule.TopLevel || rulename == rootRule : false); break; } } // check that the name exists if (!fFoundRule) { throw new ArgumentException(SR.Get(SRID.RecognizerRuleNotFoundStream, rulename)); } } else { rulename = rootRule; } grammarOptions = header.GrammarOptions & GrammarOptions.TagFormat; } stream.Position = initialPosition; return(rulename); }
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 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); } }
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); }
private static string CheckRuleName(Stream stream, string rulename, bool isImportedGrammar, bool stgInit, out bool sapi53Only, out GrammarOptions grammarOptions) { sapi53Only = false; long position = stream.Position; using (StreamMarshaler streamHelper = new StreamMarshaler(stream)) { CfgGrammar.CfgSerializedHeader cfgSerializedHeader = null; CfgGrammar.CfgHeader cfgHeader = CfgGrammar.ConvertCfgHeader(streamHelper, false, true, out cfgSerializedHeader); StringBlob pszSymbols = cfgHeader.pszSymbols; string text = (cfgHeader.ulRootRuleIndex != uint.MaxValue && cfgHeader.ulRootRuleIndex < cfgHeader.rules.Length) ? pszSymbols.FromOffset(cfgHeader.rules[cfgHeader.ulRootRuleIndex]._nameOffset) : null; sapi53Only = ((cfgHeader.GrammarOptions & (GrammarOptions.MssV1 | GrammarOptions.IpaPhoneme | GrammarOptions.W3cV1 | GrammarOptions.STG)) != 0); if (text == null && string.IsNullOrEmpty(rulename)) { throw new ArgumentException(SR.Get(SRID.SapiErrorNoRulesToActivate)); } if (!string.IsNullOrEmpty(rulename)) { bool flag = false; CfgRule[] rules = cfgHeader.rules; for (int i = 0; i < rules.Length; i++) { CfgRule cfgRule = rules[i]; if (pszSymbols.FromOffset(cfgRule._nameOffset) == rulename) { flag = (cfgRule.Export || stgInit || (!isImportedGrammar && (cfgRule.TopLevel || rulename == text))); break; } } if (!flag) { throw new ArgumentException(SR.Get(SRID.RecognizerRuleNotFoundStream, rulename)); } } else { rulename = text; } grammarOptions = (cfgHeader.GrammarOptions & GrammarOptions.TagFormat); } stream.Position = position; return(rulename); }
internal void InitFromBinaryGrammar(StreamMarshaler streamHelper) { CfgGrammar.CfgHeader header = CfgGrammar.ConvertCfgHeader(streamHelper); _words = header.pszWords; _symbols = header.pszSymbols; _grammarOptions = header.GrammarOptions; State[] array = new State[header.arcs.Length]; SortedDictionary <int, Rule> sortedDictionary = new SortedDictionary <int, Rule>(); int count = _rules.Count; BuildRulesFromBinaryGrammar(header, array, sortedDictionary, count); Arc[] array2 = new Arc[header.arcs.Length]; bool flag = true; CfgArc cfgArc = default(CfgArc); State state = null; IEnumerator <KeyValuePair <int, Rule> > enumerator = sortedDictionary.GetEnumerator(); if (enumerator.MoveNext()) { KeyValuePair <int, Rule> current = enumerator.Current; Rule value = current.Value; for (int i = 1; i < header.arcs.Length; i++) { CfgArc cfgArc2 = header.arcs[i]; if (cfgArc2.RuleRef) { value._listRules.Add(_rules[(int)cfgArc2.TransitionIndex]); } if (current.Key == i) { value = current.Value; if (enumerator.MoveNext()) { current = enumerator.Current; } } if (flag || cfgArc.LastArc) { if (array[i] == null) { uint nextHandle = CfgGrammar.NextHandle; array[i] = new State(value, nextHandle, i); AddState(array[i]); } state = array[i]; } int nextStartArcIndex = (int)cfgArc2.NextStartArcIndex; State end = null; if (state != null && nextStartArcIndex != 0) { if (array[nextStartArcIndex] == null) { uint nextHandle2 = CfgGrammar.NextHandle; array[nextStartArcIndex] = new State(value, nextHandle2, nextStartArcIndex); AddState(array[nextStartArcIndex]); } end = array[nextStartArcIndex]; } float flWeight = (header.weights != null) ? header.weights[i] : 1f; Arc arc; if (cfgArc2.RuleRef) { Rule ruleRef = _rules[(int)cfgArc2.TransitionIndex]; arc = new Arc(null, ruleRef, _words, flWeight, 0, null, MatchMode.AllWords, ref _fNeedWeightTable); } else { int transitionIndex = (int)cfgArc2.TransitionIndex; int num = (transitionIndex == 4194302 || transitionIndex == 4194301 || transitionIndex == 4194303) ? transitionIndex : 0; arc = new Arc((int)((num == 0) ? cfgArc2.TransitionIndex : 0), flWeight, cfgArc2.LowConfRequired ? (-1) : (cfgArc2.HighConfRequired ? 1 : 0), num, MatchMode.AllWords, ref _fNeedWeightTable); } arc.Start = state; arc.End = end; AddArc(arc); array2[i] = arc; flag = false; cfgArc = cfgArc2; } } int j = 1; int k = 0; for (; j < header.arcs.Length; j++) { CfgArc cfgArc3 = header.arcs[j]; if (!cfgArc3.HasSemanticTag) { continue; } for (; k < header.tags.Length && header.tags[k].StartArcIndex == j; k++) { CfgSemanticTag cfgTag = header.tags[k]; Tag tag = new Tag(this, cfgTag); _tags.Add(tag); array2[tag._cfgTag.StartArcIndex].AddStartTag(tag); array2[tag._cfgTag.EndArcIndex].AddEndTag(tag); if (cfgTag._nameOffset > 0) { tag._cfgTag._nameOffset = _symbols.OffsetFromId(_symbols.Find(_symbols.FromOffset(cfgTag._nameOffset))); } else { tag._cfgTag._valueOffset = _symbols.OffsetFromId(_symbols.Find(_symbols.FromOffset(cfgTag._valueOffset))); } } } _fNeedWeightTable = true; if (header.BasePath != null) { SetBasePath(header.BasePath); } _guid = header.GrammarGUID; _langId = header.langId; _grammarMode = header.GrammarMode; _fLoadedFromBinary = true; }
public GenerationOptionsForm() { InitializeComponent(); mOpt = new GrammarOptions(); }