本文整理汇总了C#中Grammar类的典型用法代码示例。如果您正苦于以下问题:C# Grammar类的具体用法?C# Grammar怎么用?C# Grammar使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Grammar类属于命名空间,在下文中一共展示了Grammar类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SpeechRecognizer
public SpeechRecognizer(string file, KinectSensor sensor)
{
this.grammarFile = file;
this.kinectSensor = sensor;
audioSource = kinectSensor.AudioSource;
audioSource.AutomaticGainControlEnabled = false;
audioSource.BeamAngleMode = BeamAngleMode.Adaptive;
Func<RecognizerInfo, bool> matchingFunc = r =>
{
string value;
r.AdditionalInfo.TryGetValue("Kinect", out value);
return "True".Equals(value, StringComparison.InvariantCultureIgnoreCase) && "en-US".Equals(r.Culture.Name, StringComparison.InvariantCultureIgnoreCase);
};
var recognizerInfo = SpeechRecognitionEngine.InstalledRecognizers().Where(matchingFunc).FirstOrDefault();
if (recognizerInfo == null)
return;
speechRecognitionEngine = new SpeechRecognitionEngine(recognizerInfo.Id);
var grammar = new Grammar(grammarFile);
speechRecognitionEngine.LoadGrammar(grammar);
audioStream = audioSource.Start();
speechRecognitionEngine.SetInputToAudioStream(audioStream, new SpeechAudioFormatInfo(EncodingFormat.Pcm, 16000, 16, 1, 32000, 2, null));
speechRecognitionEngine.AudioStateChanged += onAudioStateChanged;
speechRecognitionEngine.SpeechRecognized += onSpeechRecognized;
speechRecognitionEngine.RecognizeCompleted += onSpeechRecognizeCompleted;
speechRecognitionEngine.EmulateRecognizeCompleted += onEmulateRecognizeCompleted;
}
示例2: CreateSpeechRecognizer
//Speech recognizer
private SpeechRecognitionEngine CreateSpeechRecognizer()
{
RecognizerInfo ri = GetKinectRecognizer();
SpeechRecognitionEngine sre;
sre = new SpeechRecognitionEngine(ri.Id);
//words we need the program to recognise
var grammar = new Choices();
grammar.Add(new SemanticResultValue("moustache", "MOUSTACHE"));
grammar.Add(new SemanticResultValue("top hat", "TOP HAT"));
grammar.Add(new SemanticResultValue("glasses", "GLASSES"));
grammar.Add(new SemanticResultValue("sunglasses", "SUNGLASSES"));
grammar.Add(new SemanticResultValue("tie", "TIE"));
grammar.Add(new SemanticResultValue("bow", "BOW"));
grammar.Add(new SemanticResultValue("bear", "BEAR"));
//etc
var gb = new GrammarBuilder { Culture = ri.Culture };
gb.Append(grammar);
var g = new Grammar(gb);
sre.LoadGrammar(g);
//Events for recognising and rejecting speech
sre.SpeechRecognized += SreSpeechRecognized;
sre.SpeechRecognitionRejected += SreSpeechRecognitionRejected;
return sre;
}
示例3: btnRecognize_Click
private void btnRecognize_Click(object sender, RoutedEventArgs e)
{
text = txtInput.Text.ToLower();
text = CleanText(text);
words = text.Split(' ');
wcount = words.Length;
//words = words.Distinct().ToArray();
txtOutput.Text = text;
Choices choices = new Choices(words);
GrammarBuilder gb = new GrammarBuilder(new GrammarBuilder(choices), 0, wcount);
//GrammarBuilder gb = new GrammarBuilder(txtInput.Text.Trim());
gb.Culture = new CultureInfo("es-MX");
Grammar grammar = new Grammar(gb);
//recognizer = new SpeechRecognitionEngine("SR_MS_es-MX_TELE_11.0");
//recognizer = new SpeechRecognitionEngine(new CultureInfo("es-MX"));
recognizer.LoadGrammar(grammar);
recognizer.SetInputToWaveFile(@"E:\Proyectos\Audio Timestamps\chapter01.wav");
//recognizer.SetInputToDefaultAudioDevice();
recognizer.RecognizeCompleted += new EventHandler<RecognizeCompletedEventArgs>(RecognizeCompletedHandler);
recognizer.RecognizeAsync(RecognizeMode.Multiple);
}
示例4: initRS
public void initRS()
{
try
{
SpeechRecognitionEngine sre = new SpeechRecognitionEngine(new CultureInfo("en-US"));
var words = new Choices();
words.Add("Hello");
words.Add("Jump");
words.Add("Left");
words.Add("Right");
var gb = new GrammarBuilder();
gb.Culture = new System.Globalization.CultureInfo("en-US");
gb.Append(words);
Grammar g = new Grammar(gb);
sre.LoadGrammar(g);
sre.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized);
sre.SetInputToDefaultAudioDevice();
sre.RecognizeAsync(RecognizeMode.Multiple);
}
catch (Exception e)
{
label1.Text = "init RS Error : " + e.ToString();
}
}
示例5: Parse
public Node Parse(Grammar grammar, IEnumerable<Token> tokens)
{
_chart.Clear();
_chart.Add(new State(grammar.GetStart().Earley(0)));
using (var enumerator = tokens.GetEnumerator())
{
var token = enumerator.Current;
for (int i = 0; enumerator.MoveNext(); i++)
{
if (i >= _chart.Count) SyntaxError(token);
token = enumerator.Current;
var rules = _chart[i];
for (int j = 0; j < rules.Count; j++)
{
if (!rules[j].IsFinal)
if (rules[j].NextTerm.GetType() == typeof (NonTerminal))
Predict(grammar, i, j);
else
Scan(token, i, j);
else
Complete(i, j);
}
}
}
int n = _chart.Count - 1;
var complete = _chart[n].FinalRules(grammar.Start).FirstOrDefault();
if (complete == null) SyntaxError();
return BuildTree(complete, n);
}
示例6: Start
// Use this for initialization
void Start()
{
grammar = new Grammar();
grammar.AddTag("story", new string[] {
"#story_beginning# #story_problem# #story_climax# #story_ending#"
});
grammar.AddTag("story_beginning", new string[] {
"Once upon a time there was a valiant #animal#"
});
grammar.AddTag("story_problem", new string[] {
"that never #difficulty_verb#.",
"that one day heard some strange words: #strange_calling#"
});
grammar.AddTag("story_climax", new string[] {
"Suddenly, he decided to #resolution_verb#."
});
grammar.AddTag("story_ending", new string[] {
"Finally he could #result_verb# without worries."
});
grammar.AddTag("difficulty_verb", new string[] {"slept", "danced", "talked"});
grammar.AddTag("resolution_verb", new string[] {"run", "sing", "give up"});
grammar.AddTag("result_verb", new string[] {"sleep", "dance", "talk freely"});
grammar.AddTag("strange_calling", new string[] {"Hello #name#!", "Hello my #writer_object#!"});
grammar.AddTag("animal", new string[] {"dolphin", "dog", "cat", "lamb", "lion"});
grammar.AddTag("name", new string[] {"Mr. Gil", "Madame", "Masked Man"});
grammar.AddTag("writer_object", new string[] {"text", "book", "beloved code"});
InvokeRepeating("TestEvaluation", 1f, 2f);
}
示例7: BuildSpeechEngine
void BuildSpeechEngine(RecognizerInfo rec)
{
_speechEngine = new SpeechRecognitionEngine(rec.Id);
var choices = new Choices();
choices.Add("venus");
choices.Add("mars");
choices.Add("earth");
choices.Add("jupiter");
choices.Add("sun");
var gb = new GrammarBuilder { Culture = rec.Culture };
gb.Append(choices);
var g = new Grammar(gb);
_speechEngine.LoadGrammar(g);
//recognized a word or words that may be a component of multiple complete phrases in a grammar.
_speechEngine.SpeechHypothesized += new EventHandler<SpeechHypothesizedEventArgs>(SpeechEngineSpeechHypothesized);
//receives input that matches any of its loaded and enabled Grammar objects.
_speechEngine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(_speechEngineSpeechRecognized);
//receives input that does not match any of its loaded and enabled Grammar objects.
_speechEngine.SpeechRecognitionRejected += new EventHandler<SpeechRecognitionRejectedEventArgs>(_speechEngineSpeechRecognitionRejected);
//C# threads are MTA by default and calling RecognizeAsync in the same thread will cause an COM exception.
var t = new Thread(StartAudioStream);
t.Start();
}
示例8: BuildGrammar
public Grammar BuildGrammar()
{
Choices choiceBuilder = new Choices();
// Songs
if (SongHelper.SongCount() > 0) // it freaks out if there's nothing in the one-of bit.
{
GrammarBuilder songBuilder = new GrammarBuilder();
songBuilder.Append("play song");
songBuilder.Append(SongHelper.GenerateSongChoices());
choiceBuilder.Add(songBuilder);
}
GrammarBuilder shuffleBuilder = new GrammarBuilder();
shuffleBuilder.Append("shuffle all songs");
choiceBuilder.Add(shuffleBuilder);
// Playlists
if (SongHelper.PlaylistCount() > 0)
{
GrammarBuilder playListBuilder = new GrammarBuilder();
playListBuilder.Append("play playlist");
playListBuilder.Append(SongHelper.GeneratePlaylistChoices());
choiceBuilder.Add(playListBuilder);
GrammarBuilder shufflePlayListBuilder = new GrammarBuilder();
shufflePlayListBuilder.Append("shuffle playlist");
shufflePlayListBuilder.Append(SongHelper.GeneratePlaylistChoices());
choiceBuilder.Add(shufflePlayListBuilder);
}
Grammar gram = new Grammar(new GrammarBuilder(choiceBuilder));
return gram;
}
示例9: SpeechEngine
public SpeechEngine(KinectHandler handler)
{
_kinectHandler = handler;
_commands = new GrammarBuilder();
_grammar = new Grammar(_commands);
_grammar.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(_grammar_SpeechRecognized);
}
示例10: CreateGrammar
public void CreateGrammar()
{
var b = new GrammarBuilder();
b.Append(Config.StopListening);
Grammar = new Grammar(b);
}
示例11: LoadGrammar
/// <summary>
/// Loads the grammar in to the recognition engine.
/// </summary>
protected virtual void LoadGrammar(string pathToGrammar, EventHandler<LoadGrammarCompletedEventArgs> GrammarLoaded)
{
NMDebug.Log("Loading Grammar...");
Grammar grammar = new Grammar(pathToGrammar + @"Default.xml");
RecognitionEngine.LoadGrammarCompleted += GrammarLoaded;
RecognitionEngine.LoadGrammarAsync(grammar);
}
示例12: StartListening
public void StartListening()
{
if (null != _ri)
{
_speechEngine = new SpeechRecognitionEngine(_ri.Id);
// Create a grammar from grammar definition XML file.
using (var memoryStream = new MemoryStream(Encoding.ASCII.GetBytes(KAIT.Kinect.Service.Properties.Resources.SpeechGrammar)))
{
var g = new Grammar(memoryStream);
_speechEngine.LoadGrammar(g);
}
_speechEngine.SpeechRecognized += _speechEngine_SpeechRecognized;
_speechEngine.SpeechRecognitionRejected += _speechEngine_SpeechRecognitionRejected;
// let the convertStream know speech is going active
_convertStream.SpeechActive = true;
_speechEngine.SetInputToAudioStream(
_convertStream, new SpeechAudioFormatInfo(EncodingFormat.Pcm, 16000, 16, 1, 32000, 2, null));
_speechEngine.RecognizeAsync(RecognizeMode.Multiple);
}
}
示例13: LoadCurrentSyllabus
internal void LoadCurrentSyllabus(SyllabusTracker syllabusTracker)
{
if (_speechRecognitionEngine == null) return; // not currently running recognition
_speechRecognitionEngine.RequestRecognizerUpdate();
_speechRecognitionEngine.UnloadAllGrammars();
// new choices consolidation for commands - one command per syllabus file line
var commandLoad = new Choices();
foreach (var baseSyllabus in syllabusTracker.Syllabi)
{
foreach (var command in baseSyllabus.Commands)
{
commandLoad.Add(command);
}
}
// add commands - should be per input language, but now English
VoiceCommands.AddCommands(commandLoad);
var gBuilder = new GrammarBuilder();
gBuilder.Append(commandLoad);
var grammar = new Grammar(gBuilder) { Name = "Syllabus" };
_speechRecognitionEngine.LoadGrammar(grammar);
var dictgrammar = new DictationGrammar("grammar:dictation#pronunciation") { Name = "Random" };
_speechRecognitionEngine.LoadGrammar(dictgrammar);
}
示例14: SpeechRecogniser
public SpeechRecogniser()
{
RecognizerInfo ri = SpeechRecognitionEngine.InstalledRecognizers().Where(r => r.Id == RecognizerId).FirstOrDefault();
if (ri == null)
return;
sre = new SpeechRecognitionEngine(ri.Id);
// Build a simple grammar of shapes, colors, and some simple program control
var instruments = new Choices();
foreach (var phrase in InstrumentPhrases)
instruments.Add(phrase.Key);
var objectChoices = new Choices();
objectChoices.Add(instruments);
var actionGrammar = new GrammarBuilder();
//actionGrammar.AppendWildcard();
actionGrammar.Append(objectChoices);
var gb = new GrammarBuilder();
gb.Append(actionGrammar);
var g = new Grammar(gb);
sre.LoadGrammar(g);
sre.SpeechRecognized += sre_SpeechRecognized;
sre.SpeechHypothesized += sre_SpeechHypothesized;
sre.SpeechRecognitionRejected += new EventHandler<SpeechRecognitionRejectedEventArgs>(sre_SpeechRecognitionRejected);
var t = new Thread(StartDMO);
t.Start();
valid = true;
}
示例15: IntroGrammar
public IntroGrammar()
{
Choices majors = new Choices();
majors.Add(new SemanticResultValue("Computer Science", "CSC"));
SemanticResultKey majorKey = new SemanticResultKey(Slots.Major.ToString(), majors);
Choices years = new Choices();
for (int i = 2001; i < 2020; i++)
{
years.Add(new SemanticResultValue(i.ToString(), i));
}
SemanticResultKey year = new SemanticResultKey(Slots.GradYear.ToString(), years);
Choices yesOrNo = new Choices();
yesOrNo.Add(new SemanticResultValue("yes", "yes"));
yesOrNo.Add(new SemanticResultValue("yeah", "yes"));
yesOrNo.Add(new SemanticResultValue("yep", "yes"));
yesOrNo.Add(new SemanticResultValue("no", "no"));
yesOrNo.Add(new SemanticResultValue("nope", "no"));
SemanticResultKey yesNo = new SemanticResultKey(Slots.YesNo.ToString(), yesOrNo);
Choices options = new Choices();
options.Add(majorKey);
options.Add(year);
options.Add(yesNo);
GrammarBuilder builder = new GrammarBuilder();
builder.Append(options);
grammar = new Grammar(builder);
}