所以我最近决定再次重做我的Jarvis AI的基础设施。现在我正在使用混合代码(谷歌和微软)。由于某种原因,Google部分代码不会触发,并且在麦克风中说话时会显示任何文字或文字。
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;
}
}
}
此处的代码全部来自this website。
在没有错误之后它仍然没有返回或做任何事情,请帮忙!
答案 0 :(得分:1)
使用正则表达式解析JSON通常会导致问题。考虑使用像JSON.NET这样的库来将字符串解析为对象。