Question

So i recently decided to again redo the infrastructure of my Jarvis AI. Now i'm using a mix of the code (Google and Microsoft). The Google part of the code will not trigger for some reason and display any words or text when spoken into microphone.

public const int DEFAULT_BIT_RATE = 8000;
public const string DEFAULT_LANGUAGE = "en-US";
static string client = "Jarvis";
public class SpeechInputResult
{
        static public string ID;
        public int status;

        public class Hypothesis
        {
            public string utterance;
            public double confidence = -1.0d;//-1 = No Value
            public override string ToString()
            {
                return "'" +utterance + "'" + ((confidence == -1) ? "" : "@" + confidence);
            }
            public List<Hypothesis> hypotheses = new List<Hypothesis>();

            public Hypothesis getBestHypothesis()
            {
                if (hypotheses.Count() <=0)
                    return null;
                Hypothesis H = hypotheses[0];
                foreach (Hypothesis h in hypotheses)
                {
                    if (h.confidence>=H.confidence)
                    {
                        H = h;
                    }
                    return H;
                }
                return null;
            }
            public string json_men = "";
            public void FromJSON(String JSON)
            {
                json_men = JSON;
                JSON = JSON.Replace("\n","").Trim();
                Match M;

                //status
                M = new Regex("\\\"status\\\"\\:([0-9]*),", RegexOptions.IgnoreCase).Match(JSON);

                //ID
                M = new Regex ("\\\"id\\\"\\:\\\"([^\\\"]*)\\\",", RegexOptions.IgnoreCase).Match(JSON);
                ID = M.Groups[1].Value;

                //Hypotheses
                int l1 = JSON.IndexOf("hypotheses");
                    l1 = JSON.IndexOf("[",l1);
                int r1 = JSON.LastIndexOf("]");
                string JSON2 = JSON.Substring(l1, r1-l1+1);

                MatchCollection m2 = new Regex("{([^\\}]*)}", RegexOptions.IgnoreCase).Matches(JSON2);
                foreach (Match g in m2)
                {
                    string s = g.Value;
                    SpeechInputResult.Hypothesis h = new SpeechInputResult.Hypothesis();

                    M = new Regex("\\\"utterance\\\"\\:\\\"([^\\\"]*)\\\"", RegexOptions.IgnoreCase).Match(s);
                    h.utterance  = M.Groups[1].Value;

                    M = new Regex("\\\"confidence\\\"\\:([0-9\\.]*)", RegexOptions.IgnoreCase).Match(s);
                    string confidence = M.Groups[1].Value;
                    confidence = confidence.Replace(".", ",");
                    if (confidence != "")
                    {
                        h.confidence = float.Parse(confidence);
                    }
                    hypotheses.Add(h);
                }
            }
        }
        public static SpeechInputResult ProcessFlacFile(string FlacFileName, int BIT_RATE = DEFAULT_BIT_RATE, string language = DEFAULT_LANGUAGE, uint maxresults = 1)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://www.google.com/speech-api/v1/recognize?xjerr=1" + "&client=" + client + "&lang=" + language + "&maxresults=" + maxresults + "&pfilter=0");
            FileStream fStream = new FileStream(FlacFileName, FileMode.Open, FileAccess.Read);
            request.Proxy = null;
            request.Timeout = 60000;
            request.KeepAlive = true;
            request.Method = "POST";
            request.ContentType = "audio/x-flac; rate=8000";
            //bitrate must = .flac file
            request.UserAgent = client;
            FileInfo fInfo = new FileInfo(FlacFileName);
            long numbytes = fInfo.Length;
            byte[] data = null;
            using (FileStream fstream = new FileStream(FlacFileName, FileMode.Open, FileAccess.Read))
                data = new byte[fstream.Length];
            fStream.Read(data, 0, Convert.ToInt32(fStream.Length));
            fStream.Close();
            using (Stream wrStream = request.GetRequestStream())
            {
                wrStream.Write(data, 0, data.Length);
            }
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                dynamic resp = response.GetResponseStream();
                if (resp != null)
                {
                    StreamReader sr = new StreamReader(resp);
                    MessageBox.Show(sr.ReadToEnd());
                    //resp.Close();
                    //resp.Dispose();
                }
            }
            catch (System.Exception ee)
            {
                MessageBox.Show("hi"+ee);
            }
            return null;
        }
    }
}

The code here is all from this website.

After getting it to no errors it still doesn't return or do anything, please help!

Was it helpful?

Solution

Parsing JSON with regular expressions will usually cause you problems. Consider using a library like JSON.NET to parse the string into an object instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top