我正在学习Microsoft.Speech库。它类似于System.Speech库。我遇到了问题,我认为这可能是一个错误。
我试图使用e.Result.Semantic.ContainsKey("DestinationCity")
来确定它是否有密钥。当我从GrammarBuilder加载语法时,我得到一个true
来说“我想从纽约飞往芝加哥”。然后我将语法写入xml文件并再次从该文件加载语法。这一次,当我说“我想从纽约飞往芝加哥”时,我得到false
。我不知道为什么会这样。
这是我的代码:
class Program
{
static void Main(string[] args)
{
//Create GrammarBuilder gb
Choices cities = new Choices(new string[] {
"Los Angeles", "New York", "Chicago", "San Francisco", "Miami", "Dallas" });
GrammarBuilder gb = new GrammarBuilder();
gb.Append("I would like to fly from");
gb.Append(new SemanticResultKey("DepartureCity", cities));
gb.Append("to");
gb.Append(new SemanticResultKey("DestinationCity", cities));
//Put GrammarBuilder gb into xml file
SrgsDocument doc = new SrgsDocument(gb);
System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(@"C:\Visual Studio Projects\SpeechSemanticsTest\SpeechSemanticsTest\passwordGrammar.xml");
doc.WriteSrgs(writer);
writer.Close();
//Create SpeechRecognitionEngine
SpeechRecognitionEngine sre = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));
sre.SetInputToDefaultAudioDevice();
sre.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized);
//Load Grammar from xml
sre.LoadGrammar(new Grammar(@"C:\Visual Studio Projects\SpeechSemanticsTest\SpeechSemanticsTest\passwordGrammar.xml"));
//load Grammar from GrammarBuilder
//sre.LoadGrammar(new Grammar(gb));
//Start Recognizing
sre.RecognizeAsync(RecognizeMode.Multiple);
Console.ReadKey();
}
static void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
Console.WriteLine(e.Result.Text);
Console.WriteLine(e.Result.Semantics.ContainsKey("DestinationCity"));
}
}
创建的xml文件位于:
<?xml version="1.0" encoding="utf-8"?>
<grammar xml:lang="en-US" root="root" tag-format="properties-ms/1.0" version="1.0" xmlns="http://www.w3.org/2001/06/grammar" xmlns:sapi="http://schemas.microsoft.com/Speech/2002/06/SRGSExtensions">
<rule id="_1" scope="private">
<one-of>
<item>Los Angeles</item>
<item>New York</item>
<item>Chicago</item>
<item>San Francisco</item>
<item>Miami</item>
<item>Dallas</item>
</one-of>
</rule>
<rule id="DepartureCity" scope="private">
<ruleref uri="#_1" />
</rule>
<rule id="DestinationCity" scope="private">
<ruleref uri="#_1" />
</rule>
<rule id="root" scope="private">
I would like to fly from<ruleref uri="#DepartureCity" />to<ruleref uri="#DestinationCity" />
</rule>
</grammar>
请注意,有“加载语法形式xml”和“从GrammarBuilder加载语法”。您可以作为评论来查看差异。
答案 0 :(得分:0)
[
'1.js',
'2.js'
].forEach(function (src) {
var script = document.createElement('script');
script.src = src;
script.async = false;
document.head.appendChild(script);
});
您的XML中的所有作用域都是私有的。将它们设置为公开。