我不明白为什么尽管SpeechResult是正确的,但返回给布尔值以启动动作的布尔值返回false,对我来说grammar.xaml似乎是正确的,但随后SemanticInterpretation将其返回错误。
MainPage.cs:
public MainPage()
{
this.InitializeComponent();
this.Loaded += OnLoaded;
this.actions = new Dictionary<string, Dictionary<string, Action>>()
{
[ASCOLTO_RULE] = new Dictionary<string, Action>()
{
[ASCOLTO_OPTION] = OnGreen,
},
};
}
private void OnGreen()
{
Background = new SolidColorBrush(Colors.Green);
}
async void OnLoaded(object sender, RoutedEventArgs e)
{
this.speechRecognizer = new SpeechRecognizer();
this.speechRecognizer.Timeouts.BabbleTimeout = TimeSpan.FromSeconds(0);
this.speechRecognizer.Timeouts.InitialSilenceTimeout = TimeSpan.FromSeconds(0);
this.speechRecognizer.Timeouts.EndSilenceTimeout = TimeSpan.FromSeconds(0);
var grammarFile = await StorageFile.GetFileFromApplicationUriAsync(
new Uri("ms-appx:///grammar.xml"));
this.speechRecognizer.Constraints.Add(
new SpeechRecognitionGrammarFileConstraint(grammarFile));
var result = await speechRecognizer.CompileConstraintsAsync();
if (result.Status == SpeechRecognitionResultStatus.Success)
{
while (true)
{
var speechResult = await speechRecognizer.RecognizeAsync();
if ((speechResult.Confidence == SpeechRecognitionConfidence.Medium) ||
(speechResult.Confidence == SpeechRecognitionConfidence.High))
{
string spokenCommand = string.Empty;
var lastRulePath = speechResult.RulePath.Last();
IReadOnlyList<string> values = null;
if (speechResult?.SemanticInterpretation?.Properties.TryGetValue(
ACTION_IDENTIFIER, out values) == (bool)true)
{
var action = values.FirstOrDefault();
// Ok, we have a rule and an action. Need to execute on it.
this.actions[lastRulePath]?[action]?.Invoke();
}
}
}
}
}
SpeechRecognizer speechRecognizer;
Dictionary<string, Dictionary<string, Action>> actions;
static readonly string ASCOLTO_RULE = "avviaascolto";
static readonly string ASCOLTO_OPTION = "avviacomandovocale";
static readonly string ACTION_IDENTIFIER = "action";
static readonly int TRANSLATION = 100;
static readonly float SCALE = 0.5f;
grammar.xml:
<?xml version="1.0" encoding="utf-8"?>
<grammar
version="1.0" mode="voice" root="comando"
xml:lang="it-IT" tag-format="semantics/1.0"
xmlns="http://www.w3.org/2001/06/grammar">
<rule id="comando">
<one-of>
<item>
<ruleref uri="#avviaascolto"/>
</item>
</one-of>
</rule>
<rule id="avviaascolto">
<one-of>
<item>
<tag>out="ascoltoavviato";</tag>
avvia ascolto
<tag> out.action="avviacomandovocale"; </tag>
</item>
</one-of>
</rule>
</grammar>
我想了解的是错误在哪里,如果是grammar.xaml或后面的代码中。
预先感谢