コード例 #1
0
        /// <summary>
        /// Populates and returns a SpeechToTextResult object from a given Watson SpeechResult object.
        /// </summary>
        /// <param name="watsonResult">Watson SpeechResult object</param>
        /// <returns>A SpeechToTextResult object</returns>
        public SpeechToTextResult CreateSpeechToTextResult(SpeechResult watsonResult)
        {
            var textResult = new SpeechToTextResult();

            textResult.IsFinal          = watsonResult.Final;
            textResult.TextAlternatives = new TextAlternative[watsonResult.Alternatives.Length];
            for (int i = 0; i < textResult.TextAlternatives.Length; ++i)
            {
                SpeechAlt watsonAlternative = watsonResult.Alternatives[i];
                var       alternative       = new WatsonTextAlternative();
                alternative.Text                 = watsonAlternative.Transcript;
                alternative.Confidence           = (float)watsonAlternative.Confidence;
                alternative.TimeStamps           = watsonAlternative.Timestamps;
                alternative.WordConfidenceValues = watsonAlternative.WordConfidence;
                textResult.TextAlternatives[i]   = alternative;
            }
            return(textResult);
        }
コード例 #2
0
        private SpeechResultList ParseRecognizeResponse(IDictionary resp)
        {
            if (resp == null)
            {
                return(null);
            }

            try
            {
                List <SpeechResult> results = new List <SpeechResult>();
                IList iresults = resp["results"] as IList;
                if (iresults == null)
                {
                    return(null);
                }

                foreach (var r in iresults)
                {
                    IDictionary iresult = r as IDictionary;
                    if (iresults == null)
                    {
                        continue;
                    }

                    SpeechResult result = new SpeechResult();
                    result.Final = (bool)iresult["final"];

                    IList ialternatives = iresult["alternatives"] as IList;
                    if (ialternatives == null)
                    {
                        continue;
                    }

                    List <SpeechAlt> alternatives = new List <SpeechAlt>();
                    foreach (var a in ialternatives)
                    {
                        IDictionary ialternative = a as IDictionary;
                        if (ialternative == null)
                        {
                            continue;
                        }

                        SpeechAlt alternative = new SpeechAlt();
                        alternative.Transcript = (string)ialternative["transcript"];
                        if (ialternative.Contains("confidence"))
                        {
                            alternative.Confidence = (double)ialternative["confidence"];
                        }

                        if (ialternative.Contains("timestamps"))
                        {
                            IList itimestamps = ialternative["timestamps"] as IList;

                            TimeStamp[] timestamps = new TimeStamp[itimestamps.Count];
                            for (int i = 0; i < itimestamps.Count; ++i)
                            {
                                IList itimestamp = itimestamps[i] as IList;
                                if (itimestamp == null)
                                {
                                    continue;
                                }

                                TimeStamp ts = new TimeStamp();
                                ts.Word       = (string)itimestamp[0];
                                ts.Start      = (double)itimestamp[1];
                                ts.End        = (double)itimestamp[2];
                                timestamps[i] = ts;
                            }

                            alternative.Timestamps = timestamps;
                        }
                        if (ialternative.Contains("word_confidence"))
                        {
                            IList iconfidence = ialternative["word_confidence"] as IList;

                            WordConfidence[] confidence = new WordConfidence[iconfidence.Count];
                            for (int i = 0; i < iconfidence.Count; ++i)
                            {
                                IList iwordconf = iconfidence[i] as IList;
                                if (iwordconf == null)
                                {
                                    continue;
                                }

                                WordConfidence wc = new WordConfidence();
                                wc.Word       = (string)iwordconf[0];
                                wc.Confidence = (double)iwordconf[1];
                                confidence[i] = wc;
                            }

                            alternative.WordConfidence = confidence;
                        }

                        alternatives.Add(alternative);
                    }
                    result.Alternatives = alternatives.ToArray();
                    results.Add(result);
                }

                return(new SpeechResultList(results.ToArray()));
            }
            catch (Exception e)
            {
                Log.Error("SpeechToText", "ParseJsonResponse exception: {0}", e.ToString());
                return(null);
            }
        }