Beispiel #1
0
        public void AppendTextWithPronunciation(string textToSpeak, string pronunciation)
        {
            Helpers.ThrowIfEmptyOrNull(textToSpeak, "textToSpeak");
            Helpers.ThrowIfEmptyOrNull(pronunciation, "pronunciation");
            ValidateElement(_elementStack.Peek(), SsmlElement.Text);
            PhonemeConverter.ValidateUpsIds(pronunciation);
            Element element = new Element(ElementType.Phoneme, textToSpeak);

            _elements.Add(element);
            element._attributes = new Collection <AttributeItem>();
            element._attributes.Add(new AttributeItem("ph", pronunciation));
        }
        IToken IElementFactory.CreateToken(IElement parent, string content, string pronunciation, string display, float reqConfidence)
        {
            SrgsToken token = new(content);

            if (!string.IsNullOrEmpty(pronunciation))
            {
                // Check if the pronunciations are ok
                string sPron = pronunciation;
                for (int iCurPron = 0, iDeliminator = 0; iCurPron < sPron.Length; iCurPron = iDeliminator + 1)
                {
                    // Find semi-colon delimiter and replace with null
                    iDeliminator = pronunciation.IndexOfAny(s_pronSeparator, iCurPron);
                    if (iDeliminator == -1)
                    {
                        iDeliminator = sPron.Length;
                    }

                    string sSubPron = sPron.Substring(iCurPron, iDeliminator - iCurPron);

                    // make sure this goes through
                    switch (_grammar.PhoneticAlphabet)
                    {
                    case AlphabetType.Sapi:
                        sSubPron = PhonemeConverter.ConvertPronToId(sSubPron, _grammar.Culture.LCID);
                        break;

                    case AlphabetType.Ipa:
                        PhonemeConverter.ValidateUpsIds(sSubPron);
                        break;

                    case AlphabetType.Ups:
                        sSubPron = PhonemeConverter.UpsConverter.ConvertPronToId(sSubPron);
                        break;
                    }
                }

                token.Pronunciation = pronunciation;
            }

            if (!string.IsNullOrEmpty(display))
            {
                token.Display = display;
            }

            if (reqConfidence >= 0)
            {
                throw new NotSupportedException(SR.Get(SRID.ReqConfidenceNotSupported));
            }
            return(token);
        }
        public void AppendTextWithPronunciation(string textToSpeak, string pronunciation)
        {
            Helpers.ThrowIfEmptyOrNull(textToSpeak, nameof(textToSpeak));
            Helpers.ThrowIfEmptyOrNull(pronunciation, nameof(pronunciation));

            // check for well formed document
            ValidateElement(_elementStack.Peek(), SsmlElement.Text);

            // validate the pronunciation
            PhonemeConverter.ValidateUpsIds(pronunciation);

            Element phoneElement = new(ElementType.Phoneme, textToSpeak);

            _elements.Add(phoneElement);

            phoneElement._attributes = new Collection <AttributeItem>();
            phoneElement._attributes.Add(new AttributeItem("ph", pronunciation));
        }
        IToken IElementFactory.CreateToken(IElement parent, string content, string pronunciation, string display, float reqConfidence)
        {
            SrgsToken srgsToken = new SrgsToken(content);

            if (!string.IsNullOrEmpty(pronunciation))
            {
                int num  = 0;
                int num2 = 0;
                while (num < pronunciation.Length)
                {
                    num2 = pronunciation.IndexOfAny(_pronSeparator, num);
                    if (num2 == -1)
                    {
                        num2 = pronunciation.Length;
                    }
                    string text = pronunciation.Substring(num, num2 - num);
                    switch (_grammar.PhoneticAlphabet)
                    {
                    case AlphabetType.Sapi:
                        text = PhonemeConverter.ConvertPronToId(text, _grammar.Culture.LCID);
                        break;

                    case AlphabetType.Ipa:
                        PhonemeConverter.ValidateUpsIds(text);
                        break;

                    case AlphabetType.Ups:
                        text = PhonemeConverter.UpsConverter.ConvertPronToId(text);
                        break;
                    }
                    num = num2 + 1;
                }
                srgsToken.Pronunciation = pronunciation;
            }
            if (!string.IsNullOrEmpty(display))
            {
                srgsToken.Display = display;
            }
            if (reqConfidence >= 0f)
            {
                throw new NotSupportedException(SR.Get(SRID.ReqConfidenceNotSupported));
            }
            return(srgsToken);
        }
Beispiel #5
0
        internal override void Validate(SrgsGrammar grammar)
        {
            if (_pronunciation != null || _display != null)
            {
                grammar.HasPronunciation = true;
            }

            // Validate the pronunciation if any
            if (_pronunciation != null)
            {
                for (int iCurPron = 0, iDeliminator = 0; iCurPron < _pronunciation.Length; iCurPron = iDeliminator + 1)
                {
                    // Find semi-colon delimiter and replace with null
                    iDeliminator = _pronunciation.IndexOf(';', iCurPron);
                    if (iDeliminator == -1)
                    {
                        iDeliminator = _pronunciation.Length;
                    }

                    string subPronunciation = _pronunciation.Substring(iCurPron, iDeliminator - iCurPron);

                    // Convert the pronunciation, will throw if error
                    switch (grammar.PhoneticAlphabet)
                    {
                    case AlphabetType.Sapi:
                        PhonemeConverter.ConvertPronToId(subPronunciation, grammar.Culture.LCID);
                        break;

                    case AlphabetType.Ups:
                        PhonemeConverter.UpsConverter.ConvertPronToId(subPronunciation);
                        break;

                    case AlphabetType.Ipa:
                        PhonemeConverter.ValidateUpsIds(subPronunciation.ToCharArray());
                        break;
                    }
                }
            }

            base.Validate(grammar);
        }
        internal override void Validate(SrgsGrammar grammar)
        {
            if (_pronunciation != null || _display != null)
            {
                grammar.HasPronunciation = true;
            }
            if (_pronunciation != null)
            {
                int num  = 0;
                int num2 = 0;
                while (num < _pronunciation.Length)
                {
                    num2 = _pronunciation.IndexOf(';', num);
                    if (num2 == -1)
                    {
                        num2 = _pronunciation.Length;
                    }
                    string text = _pronunciation.Substring(num, num2 - num);
                    switch (grammar.PhoneticAlphabet)
                    {
                    case AlphabetType.Sapi:
                        PhonemeConverter.ConvertPronToId(text, grammar.Culture.LCID);
                        break;

                    case AlphabetType.Ups:
                        PhonemeConverter.UpsConverter.ConvertPronToId(text);
                        break;

                    case AlphabetType.Ipa:
                        PhonemeConverter.ValidateUpsIds(text.ToCharArray());
                        break;
                    }
                    num = num2 + 1;
                }
            }
            base.Validate(grammar);
        }
        private void ParseToken(ParseElementCollection parent, string sToken, string pronunciation, string display, float reqConfidence)
        {
            int requiredConfidence = parent?._confidence ?? 0;

            sToken = Backend.NormalizeTokenWhiteSpace(sToken);
            if (string.IsNullOrEmpty(sToken))
            {
                return;
            }
            parent._confidence = 0;
            if (reqConfidence < 0f || reqConfidence.Equals(0.5f))
            {
                parent._confidence = 0;
            }
            else if ((double)reqConfidence < 0.5)
            {
                parent._confidence = -1;
            }
            else
            {
                parent._confidence = 1;
            }
            if (pronunciation != null || display != null)
            {
                string text  = EscapeToken(sToken);
                string text2 = (display == null) ? text : EscapeToken(display);
                if (pronunciation != null)
                {
                    OneOf oneOf = (pronunciation.IndexOf(';') >= 0) ? new OneOf(parent._rule, _backend) : null;
                    int   num   = 0;
                    int   num2  = 0;
                    while (num < pronunciation.Length)
                    {
                        num2 = pronunciation.IndexOf(';', num);
                        if (num2 == -1)
                        {
                            num2 = pronunciation.Length;
                        }
                        string text3 = pronunciation.Substring(num, num2 - num);
                        string text4 = null;
                        switch (_backend.Alphabet)
                        {
                        case AlphabetType.Sapi:
                            text4 = PhonemeConverter.ConvertPronToId(text3, _grammar.Backend.LangId);
                            break;

                        case AlphabetType.Ipa:
                            text4 = text3;
                            PhonemeConverter.ValidateUpsIds(text4);
                            break;

                        case AlphabetType.Ups:
                            text4 = PhonemeConverter.UpsConverter.ConvertPronToId(text3);
                            break;
                        }
                        string sWord = string.Format(CultureInfo.InvariantCulture, "/{0}/{1}/{2};", new object[3]
                        {
                            text2,
                            text,
                            text4
                        });
                        if (oneOf != null)
                        {
                            oneOf.AddArc(_backend.WordTransition(sWord, 1f, requiredConfidence));
                        }
                        else
                        {
                            parent.AddArc(_backend.WordTransition(sWord, 1f, requiredConfidence));
                        }
                        num = num2 + 1;
                    }
                    ((IElement)oneOf)?.PostParse((IElement)parent);
                }
                else
                {
                    string sWord2 = string.Format(CultureInfo.InvariantCulture, "/{0}/{1};", new object[2]
                    {
                        text2,
                        text
                    });
                    parent.AddArc(_backend.WordTransition(sWord2, 1f, requiredConfidence));
                }
            }
            else
            {
                parent.AddArc(_backend.WordTransition(sToken, 1f, requiredConfidence));
            }
        }
        // Disable parameter validation check

        /// <summary>
        /// Add transition representing the normalized token.
        ///
        /// White Space Normalization - Trim leading/trailing white spaces.
        ///                             Collapse white space sequences to a single ' '.
        /// Restrictions - Normalized token cannot be empty.
        ///                Normalized token cannot contain double-quote.
        ///
        /// If (Parent == Token) And (Parent.SAPIPron.Length > 0) Then
        ///     Escape normalized token.  "/" -> "\/", "\" -> "\\"
        ///     Build /D/L/P; form from the escaped token and SAPIPron.
        ///
        /// SAPIPron may be a semi-colon delimited list of pronunciations.
        /// In this case, a transition for each of the pronunciations will be added.
        ///
        /// AddTransition(NormalizedToken, Parent.EndState, NewState)
        /// Parent.EndState = NewState
        /// </summary>
        private void ParseToken(ParseElementCollection parent, string sToken, string pronunciation, string display, float reqConfidence)
        {
            int requiredConfidence = (parent != null) ? parent._confidence : CfgGrammar.SP_NORMAL_CONFIDENCE;

            // Performs white space normalization in place
            sToken = Backend.NormalizeTokenWhiteSpace(sToken);
            if (string.IsNullOrEmpty(sToken))
            {
                return;
            }

            // "sapi:reqconf" Attribute
            parent._confidence = CfgGrammar.SP_NORMAL_CONFIDENCE;  // Default to normal

            if (reqConfidence < 0 || reqConfidence.Equals(0.5f))
            {
                parent._confidence = CfgGrammar.SP_NORMAL_CONFIDENCE;  // Default to normal
            }
            else if (reqConfidence < 0.5)
            {
                parent._confidence = CfgGrammar.SP_LOW_CONFIDENCE;
            }
            else
            {
                parent._confidence = CfgGrammar.SP_HIGH_CONFIDENCE;
            }

            // If SAPIPron is specified, use /D/L/P; as the transition text, for each of the pronunciations.
            if (pronunciation != null || display != null)
            {
                // Escape normalized token.  "/" -> "\/", "\" -> "\\"
                string sEscapedToken = EscapeToken(sToken);
                string sDisplayToken = display == null ? sEscapedToken : EscapeToken(display);

                if (pronunciation != null)
                {
                    // Garbage transition is optional whereas Wildcard is not.  So we need additional epsilon transition.
                    OneOf oneOf = pronunciation.IndexOf(';') >= 0 ? new OneOf(parent._rule, _backend) : null;

                    for (int iCurPron = 0, iDeliminator = 0; iCurPron < pronunciation.Length; iCurPron = iDeliminator + 1)
                    {
                        // Find semi-colon delimiter and replace with null
                        iDeliminator = pronunciation.IndexOf(';', iCurPron);
                        if (iDeliminator == -1)
                        {
                            iDeliminator = pronunciation.Length;
                        }

                        string pron     = pronunciation.Substring(iCurPron, iDeliminator - iCurPron);
                        string sSubPron = null;
                        switch (_backend.Alphabet)
                        {
                        case AlphabetType.Sapi:
                            sSubPron = PhonemeConverter.ConvertPronToId(pron, _grammar.Backend.LangId);
                            break;

                        case AlphabetType.Ipa:
                            sSubPron = pron;
                            PhonemeConverter.ValidateUpsIds(sSubPron);
                            break;

                        case AlphabetType.Ups:
                            sSubPron = PhonemeConverter.UpsConverter.ConvertPronToId(pron);
                            break;
                        }

                        // Build /D/L/P; form for this pronunciation.
                        string sDLP = string.Format(CultureInfo.InvariantCulture, "/{0}/{1}/{2};", sDisplayToken, sEscapedToken, sSubPron);

                        // Add /D/L/P; transition to the new state.
                        if (oneOf != null)
                        {
                            oneOf.AddArc(_backend.WordTransition(sDLP, 1.0f, requiredConfidence));
                        }
                        else
                        {
                            parent.AddArc(_backend.WordTransition(sDLP, 1.0f, requiredConfidence));
                        }
                    }

                    if (oneOf != null)
                    {
                        ((IOneOf)oneOf).PostParse(parent);
                    }
                }
                else
                {
                    // Build /D/L; form for this pronunciation.
                    string sDLP = string.Format(CultureInfo.InvariantCulture, "/{0}/{1};", sDisplayToken, sEscapedToken);

                    // Add /D/L; transition to the new state.
                    parent.AddArc(_backend.WordTransition(sDLP, 1.0f, requiredConfidence));
                }
            }
            else
            {
                // Add transition to the new state with normalized token.
                parent.AddArc(_backend.WordTransition(sToken, 1.0f, requiredConfidence));
            }
        }