Skip to content

Instantly share code, notes, and snippets.

@drmfinlay
Last active June 20, 2020 07:10
Show Gist options
  • Save drmfinlay/83925796816229a5f6c2b7a568f71211 to your computer and use it in GitHub Desktop.
Save drmfinlay/83925796816229a5f6c2b7a568f71211 to your computer and use it in GitHub Desktop.
Custom Dragonfly integer examples
"""
Example using custom English language integers.
"""
from dragonfly import Key, MappingRule, Grammar
from custom_integers_en import MyIntegerRef
rule = MappingRule(
mapping={
"number <n>": Key("%(n)d")
},
extras=[
MyIntegerRef("n", 0, 10)
]
)
grammar = Grammar("CustomIntegersEN")
grammar.add_rule(rule)
grammar.load()
# Unload function which will be called by natlink at unload time.
def unload():
global grammar
if grammar: grammar.unload()
grammar = None
# coding: utf-8
"""
Example using custom Chinese language integers.
Note: I don't actually speak Chinese, so sorry if my integer command is
grammatically incorrect.
"""
from dragonfly import Key, MappingRule, Grammar
from custom_integers_zh import MyIntegerRef
rule = MappingRule(
mapping={
# 数 (Shùzì) — number
u"数 <n>": Key("%(n)d")
},
extras=[
MyIntegerRef("n", 0, 10)
]
)
grammar = Grammar("CustomIntegersZH")
grammar.add_rule(rule)
grammar.load()
# Unload function which will be called by natlink at unload time.
def unload():
global grammar
if grammar: grammar.unload()
grammar = None
#
# This file is part of Dragonfly.
# (c) Copyright 2007, 2008 by Christo Butcher
# Licensed under the LGPL.
#
# Dragonfly is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Dragonfly is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with Dragonfly. If not, see
# <http://www.gnu.org/licenses/>.
#
"""
Custom English language implementation of integer classes
============================================================================
This implementation is almost the same as the default one. It just redefines
'int_0' to exclude the "oh" alternative for the number zero.
"""
from dragonfly import RuleWrap
from dragonfly.language.base.integer import Integer
from dragonfly.language.base.integer_internal import (MapIntBuilder,
IntegerContentBase)
from dragonfly.language.en.number import (
int_1_9, int_10_19, int_20_99, int_100s, int_100big, int_1000s,
int_1000000s
)
#---------------------------------------------------------------------------
# Custom integer content.
int_0 = MapIntBuilder({
"zero": 0,
})
class MyIntegerContent(IntegerContentBase):
builders = [int_0, int_1_9, int_10_19, int_20_99,
int_100s, int_100big, int_1000s, int_1000000s]
#---------------------------------------------------------------------------
# Integer reference class.
class MyIntegerRef(RuleWrap):
def __init__(self, name, min, max, default=None):
element = Integer(None, min, max, content=MyIntegerContent)
RuleWrap.__init__(self, name, element, default=default)
# coding: utf-8
"""
Custom Chinese language implementation of integer classes
============================================================================
"""
from dragonfly import RuleWrap
from dragonfly.language.base.integer import Integer
from dragonfly.language.base.integer_internal import (
MapIntBuilder, CollectionIntBuilder, MagnitudeIntBuilder,
IntegerContentBase
)
#---------------------------------------------------------------------------
# Custom integer content.
int_0 = MapIntBuilder({
u"零": 0, # Líng
})
int_1_9 = MapIntBuilder({
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ǔ
})
class MyIntegerContent(IntegerContentBase):
builders = [int_0, int_1_9]
#---------------------------------------------------------------------------
# Integer reference class.
class MyIntegerRef(RuleWrap):
def __init__(self, name, min, max, default=None):
element = Integer(None, min, max, content=MyIntegerContent)
RuleWrap.__init__(self, name, element, default=default)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment