3

我正在研究 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 加载语法”。您可以将其中任何一个作为评论来查看差异。

4

1 回答 1

0
<rule id="_1" scope="private">

您的所有作用域在您的 XML 中都是私有的。将它们设置为公开。

于 2015-07-03T23:35:58.333 回答