<commit_before>﻿using Microsoft.Speech.Recognition;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CrewChiefV2.Events;

namespace CrewChiefV2
{
    class SpeechRecogniser
    {
        private SpeechRecognitionEngine sre;

        private String location = UserSettings.GetUserSettings().getString("speech_recognition_location");

        // externalise these?
        public static String FUEL = "fuel";
        public static String TYRE_WEAR = "tyre wear";
        public static String TYRE_TEMPS = "tyre temps";
        public static String AERO = "aero";
        public static String TRANSMISSION = "transmission";
        public static String ENGINE = "engine";
        public static String PACE = "pace";
        public static String GAP_IN_FRONT = "gap in front";
        public static String GAP_AHEAD = "gap ahead";
        public static String GAP_BEHIND = "gap behind";
        public static String LAST_LAP_TIME = "last lap time";
        public static String LAP_TIME = "lap time";
        public static String LAST_LAP = "last lap";
        public static String POSITION = "position";

        private static String KEEP_QUIET = "keep quiet";
        private static String SHUT_UP = "shut up";
        private static String I_KNOW_WHAT_IM_DOING = "I know what I'm doing";
        private static String LEAVE_ME_ALONE = "leave me alone";        

        private static String KEEP_ME_UPDATED = "keep me updated";
        private static String KEEP_ME_INFORMED = "keep me informed";
        private static String KEEP_ME_POSTED = "keep me posted";

        private static String HOW_LONGS_LEFT = "how long's left";
        private static String HOW_MANY_LAPS_LEFT = "how many laps left";
        private static String HOW_MANY_LAPS_TO_GO = "how many laps to go";

        private static String SPOT = "spot";
        private static String DONT_SPOT = "don't spot";

        private float confidenceLimit = 0.5f;

        private CrewChief crewChief;

        public Boolean initialised = false;

        public SpeechRecogniser(CrewChief crewChief)
        {
            this.crewChief = crewChief;
        }

        public void initialiseSpeechEngine()
        {
            initialised = false;
            System.Globalization.CultureInfo cultureInfo = null;
            try
            {
                cultureInfo = new System.Globalization.CultureInfo(location);
            }
            catch (Exception e)
            {
                Console.WriteLine("Unable to initialise culture info object for location " + location +
                    ". Check that MSSpeech_SR_" + location + "_TELE.msi is installed");
                Console.WriteLine("Exception message: " + e.Message);
                return;
            }
            try
            {
                this.sre = new SpeechRecognitionEngine(cultureInfo);
            }
            catch (Exception e)
            {
                Console.WriteLine("Unable to initialise speech recognition engine, check that SpeechPlatformRuntime.msi is installed");
                Console.WriteLine("Exception message: " + e.Message);
                return;
            }
            
            sre.SetInputToDefaultAudioDevice();
            Choices info1 = new Choices();
            info1.Add(new string[] { FUEL, TYRE_WEAR, TYRE_TEMPS, AERO, TRANSMISSION, ENGINE, PACE });
            GrammarBuilder gb1 = new GrammarBuilder("how is my");
            gb1.Append(info1);
            Grammar g1 = new Grammar(gb1);

            Choices info2 = new Choices();
            info2.Add(new string[] { GAP_IN_FRONT, GAP_AHEAD, GAP_BEHIND, LAST_LAP, LAP_TIME, LAST_LAP_TIME, POSITION });
            GrammarBuilder gb2 = new GrammarBuilder("what's my");
            gb2.Append(info2);
            Grammar g2 = new Grammar(gb2);

            Choices info3 = new Choices();
            info3.Add(new string[] { KEEP_QUIET, SHUT_UP, I_KNOW_WHAT_IM_DOING, LEAVE_ME_ALONE });
            GrammarBuilder gb3 = new GrammarBuilder();
            gb3.Append(info3);
            Grammar g3 = new Grammar(gb3);

            Choices info4 = new Choices();
            info4.Add(new string[] { KEEP_ME_INFORMED, KEEP_ME_POSTED, KEEP_ME_UPDATED });
            GrammarBuilder gb4 = new GrammarBuilder();
            gb4.Append(info4);
            Grammar g4 = new Grammar(gb4);

            Choices info5 = new Choices();
            info5.Add(new string[] { HOW_LONGS_LEFT, HOW_MANY_LAPS_LEFT, HOW_MANY_LAPS_TO_GO });
            GrammarBuilder gb5 = new GrammarBuilder();
            gb5.Append(info5);
            Grammar g5 = new Grammar(gb5);

            Choices info6 = new Choices();
            info6.Add(new string[] { SPOT, DONT_SPOT });
            GrammarBuilder gb6 = new GrammarBuilder();
            gb6.Append(info6);
            Grammar g6 = new Grammar(gb6);

            sre.LoadGrammar(g1);
            sre.LoadGrammar(g2);
            sre.LoadGrammar(g3);
            sre.LoadGrammar(g4);
            sre.LoadGrammar(g5);
            sre.LoadGrammar(g6);
            sre.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized);
            initialised = true;
        }<commit_msg>added more exception logging<commit_after>﻿using Microsoft.Speech.Recognition;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CrewChiefV2.Events;

namespace CrewChiefV2
{
    class SpeechRecogniser
    {
        private SpeechRecognitionEngine sre;

        private String location = UserSettings.GetUserSettings().getString("speech_recognition_location");

        // externalise these?
        public static String FUEL = "fuel";
        public static String TYRE_WEAR = "tyre wear";
        public static String TYRE_TEMPS = "tyre temps";
        public static String AERO = "aero";
        public static String TRANSMISSION = "transmission";
        public static String ENGINE = "engine";
        public static String PACE = "pace";
        public static String GAP_IN_FRONT = "gap in front";
        public static String GAP_AHEAD = "gap ahead";
        public static String GAP_BEHIND = "gap behind";
        public static String LAST_LAP_TIME = "last lap time";
        public static String LAP_TIME = "lap time";
        public static String LAST_LAP = "last lap";
        public static String POSITION = "position";

        private static String KEEP_QUIET = "keep quiet";
        private static String SHUT_UP = "shut up";
        private static String I_KNOW_WHAT_IM_DOING = "I know what I'm doing";
        private static String LEAVE_ME_ALONE = "leave me alone";        

        private static String KEEP_ME_UPDATED = "keep me updated";
        private static String KEEP_ME_INFORMED = "keep me informed";
        private static String KEEP_ME_POSTED = "keep me posted";

        private static String HOW_LONGS_LEFT = "how long's left";
        private static String HOW_MANY_LAPS_LEFT = "how many laps left";
        private static String HOW_MANY_LAPS_TO_GO = "how many laps to go";

        private static String SPOT = "spot";
        private static String DONT_SPOT = "don't spot";

        private float confidenceLimit = 0.5f;

        private CrewChief crewChief;

        public Boolean initialised = false;

        public SpeechRecogniser(CrewChief crewChief)
        {
            this.crewChief = crewChief;
        }

        public void initialiseSpeechEngine()
        {
            initialised = false;
            System.Globalization.CultureInfo cultureInfo = null;
            try
            {
                cultureInfo = new System.Globalization.CultureInfo(location);
            }
            catch (Exception e)
            {
                Console.WriteLine("Unable to initialise culture info object for location " + location +
                    ". Check that MSSpeech_SR_" + location + "_TELE.msi is installed");
                Console.WriteLine("Exception message: " + e.Message);
                return;
            }
            try
            {
                this.sre = new SpeechRecognitionEngine(cultureInfo);
            }
            catch (Exception e)
            {
                Console.WriteLine("Unable to initialise speech recognition engine, check that SpeechPlatformRuntime.msi is installed");
                Console.WriteLine("Exception message: " + e.Message);
                return;
            }

            try
            {
                sre.SetInputToDefaultAudioDevice();
            }
            catch (Exception e)
            {
                Console.WriteLine("Unable to set default audio device");
                Console.WriteLine("Exception message: " + e.Message);
                return;
            }
            try
            {
                Choices info1 = new Choices();
                info1.Add(new string[] { FUEL, TYRE_WEAR, TYRE_TEMPS, AERO, TRANSMISSION, ENGINE, PACE });
                GrammarBuilder gb1 = new GrammarBuilder("how is my");
                gb1.Append(info1);
                Grammar g1 = new Grammar(gb1);

                Choices info2 = new Choices();
                info2.Add(new string[] { GAP_IN_FRONT, GAP_AHEAD, GAP_BEHIND, LAST_LAP, LAP_TIME, LAST_LAP_TIME, POSITION });
                GrammarBuilder gb2 = new GrammarBuilder("what's my");
                gb2.Append(info2);
                Grammar g2 = new Grammar(gb2);

                Choices info3 = new Choices();
                info3.Add(new string[] { KEEP_QUIET, SHUT_UP, I_KNOW_WHAT_IM_DOING, LEAVE_ME_ALONE });
                GrammarBuilder gb3 = new GrammarBuilder();
                gb3.Append(info3);
                Grammar g3 = new Grammar(gb3);

                Choices info4 = new Choices();
                info4.Add(new string[] { KEEP_ME_INFORMED, KEEP_ME_POSTED, KEEP_ME_UPDATED });
                GrammarBuilder gb4 = new GrammarBuilder();
                gb4.Append(info4);
                Grammar g4 = new Grammar(gb4);

                Choices info5 = new Choices();
                info5.Add(new string[] { HOW_LONGS_LEFT, HOW_MANY_LAPS_LEFT, HOW_MANY_LAPS_TO_GO });
                GrammarBuilder gb5 = new GrammarBuilder();
                gb5.Append(info5);
                Grammar g5 = new Grammar(gb5);

                Choices info6 = new Choices();
                info6.Add(new string[] { SPOT, DONT_SPOT });
                GrammarBuilder gb6 = new GrammarBuilder();
                gb6.Append(info6);
                Grammar g6 = new Grammar(gb6);

                sre.LoadGrammar(g1);
                sre.LoadGrammar(g2);
                sre.LoadGrammar(g3);
                sre.LoadGrammar(g4);
                sre.LoadGrammar(g5);
                sre.LoadGrammar(g6);
            }
            catch (Exception e)
            {
                Console.WriteLine("Unable to configure speech engine grammar");
                Console.WriteLine("Exception message: " + e.Message);
                return;
            }
            try
            {
                sre.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized);
            }
            catch (Exception e)
            {
                Console.WriteLine("Unable to add event handler to speech engine");
                Console.WriteLine("Exception message: " + e.Message);
                return;
            }
            initialised = true;
        }
{'fraction_non_alphanumeric': 0.08213944603629417, 'fraction_numerical': 0.009030129373968916, 'mean_word_length': 1.9255778511557022, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6976807', 'n_tokens_mistral': 3379, 'n_tokens_neox': 3152, 'n_words': 837}