Last active
February 12, 2023 06:42
-
-
Save drmfinlay/6b0651504fb02c1bbed464cbd67f546b to your computer and use it in GitHub Desktop.
Test Chinese Dragonfly grammar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
# (c) Copyright 2020 by Dane Finlay | |
# Licensed under the LGPL. | |
""" | |
This is a simple grammar I put together for testing Dragonfly's Windows | |
Speech Recognition (WSR) engine with Chinese. | |
It should work with WSR if you have the Windows display and speech input | |
language set to Chinese (Simplified). You will need to download and install | |
the speech model in the Windows language settings: | |
**Settings** > **Time & Language** > **Language**. | |
You can use this module either with a module loader script in the | |
dragonfly/examples folder or with the following command:: | |
python -m dragonfly load -e sapi5inproc _test_dragonfly_zh.py | |
Nothing in this grammar is specific to WSR. You can use it with Dragonfly's | |
CMU Pocket Sphinx backend instead, provided you have a compatible | |
pronunciation dictionary, language model and acoustic model. This Mandarin | |
model and dictionary worked for me: | |
https://sourceforge.net/projects/cmusphinx/files/Acoustic%20and%20Language%20Models/Mandarin/ | |
In order to use the above Mandarin model, Pocket Sphinx must first be | |
recompiled with the `FSG_PNODE_CTXT_BVSZ' constant manually set to 6 or | |
higher. | |
Note: I only put this together for testing. I don't actually speak Chinese, | |
so I've undoubtedly made some obvious grammatical errors. | |
""" | |
from dragonfly import (Grammar, MappingRule, Key, Text, Mimic, | |
Choice, Dictation, CompoundRule) | |
# Create the Grammar object. | |
grammar = Grammar("Chinese test grammar") | |
class CommandRule(MappingRule): | |
""" | |
Command Rule. | |
This class contains a mapping of spoken commands to executed actions. | |
""" | |
mapping = { | |
# Common words. | |
# Note: Both Key and Text are used here to test ActionSeries. | |
u"你好": Key(u"你,好,space") + Text(u"(Nǐ hǎo) — Hello!"), | |
u"我": Text(u"我 (Wǒ) — I/me/myself"), | |
# Type numbers. | |
# This tests a Dragonfly Choice extra and optional word. | |
# 数 (Shùzì) — number | |
u"数 <数> [数]": Key(u"%(数)d"), | |
# Clipboard commands. | |
u"剪切": Key("c-x"), # Jiǎn qiè — cut | |
u"复制": Key("c-c"), # Fùzhì — copy | |
u"粘贴": Key("c-v"), # Zhāntiē — paste | |
# Mimic spoken text. | |
# Mimicking doesn't work. It actually crashed Python when I tried | |
# to use it! | |
# u"摹拟 <text>": Mimic(extra="text"), # Mónǐ — mimic | |
} | |
# Extra elements available for use in the commands above. | |
extras = [ | |
# 数 (Shùzì) — number | |
Choice(u"数", { | |
u"零": 0, # Líng | |
u"一": 1, # Yī | |
u"二": 2, # Èr | |
u"三": 3, # Sān | |
u"四": 4, # Sì | |
u"五": 5, # Wǔ | |
u"六": 6, # Liù | |
u"七": 7, # Qī | |
u"八": 8, # Bā | |
u"九": 9, # Jiǔ | |
}), | |
# Dictation("text") | |
] | |
class DictationRule(CompoundRule): | |
""" | |
Dictation rule that types what is heard. | |
This will only be used if what was said didn't match a command. | |
""" | |
spec = "<text>" | |
extras = [Dictation("text")] | |
def _process_recognition(self, node, extras): | |
Text(u"{}".format(extras["text"])).execute() | |
# Add rules to the grammar. | |
grammar.add_rule(CommandRule()) | |
grammar.add_rule(DictationRule()) | |
# Load the grammar. | |
grammar.load() | |
# Print the engine language. | |
language = grammar.engine.language | |
print("Engine language: {}".format(language)) | |
# Display a warning if the language isn't Chinese. | |
if language != "zh": | |
print("The engine language is NOT Chinese ('zh')!\n" | |
"This grammar might not work!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment