Example #1
0
        // TODO Clean this up
        public void OnSpeechRecognition(RecognizedSpeechEventArgs e)
        {
            lock (padLock)
            {
                switch (e.Type)
                {
                case RecognizedSpeechType.Hypothesis:
                case RecognizedSpeechType.Stop:
                    if (lastTs != DateTime.MinValue && DateTime.UtcNow - lastTs > TimeSpan.FromSeconds(1))
                    {
                        segment = "";
                    }

                    if (e.Words.Length > 0)
                    {
                        segment += " " + string.Join(" ", e.Words);
                    }

                    segment = segment.Trim();

                    segment = ExecuteCommandPhrase(segment, e.Type == RecognizedSpeechType.Stop);
                    break;

                case RecognizedSpeechType.Correction:
                {
                    int lastIdx = segment.LastIndexOf(' ');
                    segment = lastIdx == -1 ? "" : segment.Substring(0, lastIdx);

                    if (e.Words.Length > 0)
                    {
                        segment += " " + string.Join(" ", e.Words);
                    }

                    segment = segment.Trim();
                    break;
                }
                }

                lastTs = DateTime.UtcNow;
            }
        }
Example #2
0
        private void DispatchResult(RecognitionResult result, RecognizedSpeechType type)
        {
            var curRecognitionResult = result;

            if (prevRecognitionResult != null &&
                Math.Abs(curRecognitionResult.Confidence - prevRecognitionResult.Confidence) > 0.25)
            {
                Console.WriteLine("! Abandoned");
                return;
            }
            else
            {
                Console.Write("! DispatchResult ");
            }


            var prevText = prevRecognitionResult == null ? "" : prevRecognitionResult.Text;
            var curText  = curRecognitionResult.Text;
            int i        = 0;

            for (; i < prevText.Length && i < curText.Length; ++i)
            {
                if (prevText[i] != curText[i])
                {
                    break;
                }
            }
            bool isCorrection = prevText != "" && i < curText.Length && curText[i] != ' ';

            for (; i > 0 && i < curText.Length; --i)
            {
                if (curText[i] == ' ')
                {
                    break;
                }
            }

            curText = curText.Substring(i, curText.Length - i);
            curText = curText.Replace(",", " comma ");
            var words = curText.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            for (int j = 0; j < words.Length; ++j)
            {
                words[j] = new string(words[j].Where(char.IsLetterOrDigit).Select(char.ToLowerInvariant).ToArray());

                foreach (var correction in corrections)
                {
                    words[j] = words[j].Replace(correction.Key, correction.Value);
                }
            }

            Console.WriteLine("{0} - {1}", result.Confidence, curText);

            prevRecognitionResult = curRecognitionResult;

            var args = new RecognizedSpeechEventArgs
            {
                Type  = isCorrection ? RecognizedSpeechType.Correction : type,
                Words = words
            };

            foreach (var service in services.Values)
            {
                service.OnSpeechRecognition(args);
            }
        }