SpeechAnalyzer > AnalysisContext lack of documentation

I'm using the new SpeechAnalyzer framework to detect certain commands and want to improve accuracy by giving context. Seems like AnalysisContext is the solution for this, but couldn't find any usage example. So I want to make sure that I'm doing it right or not.

       let context = AnalysisContext()
       context.contextualStrings = [
           AnalysisContext.ContextualStringsTag("commands"): [
               "set speed level",
               "set jump level",
               "increase speed",
               "decrease speed",
                ...
           ],
           AnalysisContext.ContextualStringsTag("vocabulary"): [
               "speed", "jump", ...
           ]
       ]

        try await analyzer.setContext(context)

With this implementation, it still gives outputs like "Set some speed level", "It's speed level", etc.

Also, is it possible to make it expect number after those commands, in order to eliminate results like "set some speed level to" (instead of two).

Yes, that's the right syntax.

However, currently, contextual strings only help transcriptions from the DictationTranscriber module. The SpeechTranscriber module does not currently take contextual strings into account.

There is no way to tell the transcribers to expect a number using this contextual string mechanism. However, you could include numbers in the contextual strings to bias the transcriber towards them.

Alternatively, you can consider using SFSpeechLanguageModel as described in the Improve Accuracy section of the documentation. You could provide a number of sample commands that include numbers to bias the transcriber towards recognizing them.

Additionally, let me point out that you can create an extension on ContextualStringsTag to simplify that code to this:

[ .commands: [ "set speech level", … ],
  .vocabulary: … ]
SpeechAnalyzer > AnalysisContext lack of documentation
 
 
Q