def createGrammar(): sommatoriaExpansion = Literal("sommatoria") sommatoriaExpansion.tag = '\\sum_{}^{}' sommatoriaRule = PublicRule("sommatoria_main", sommatoriaExpansion) indexExpansion = Literal("sommatoria da") indexExpansion.tag = None indexRule = PublicRule("summation_index", indexExpansion) intervalExpansion = Literal("sommatoria da a") intervalExpansion.tag = None intervalRule = PublicRule("summation_interval", intervalExpansion) #setattr section setattr(sommatoriaRule, 'node_type', NODE_TYPE.INTERNO) setattr(sommatoriaRule, 'request_new_layer', False) setattr(sommatoriaRule, 'next_rules_trigger_words', ['da']) setattr(sommatoriaRule, 'is_entry_rule', True) #------------------------- setattr(indexRule, 'node_type', NODE_TYPE.INTERNO) setattr(indexRule, 'request_new_layer', True) setattr(indexRule, 'next_rules_trigger_words', ['a']) setattr(indexRule, 'is_entry_rule', False) setattr( indexRule, 'go_to_begin', len('\\sum_{}^{}')) #attributo che specifica se fare carry-home. #------------------------------- setattr(intervalRule, 'node_type', NODE_TYPE.INTERNO) setattr(intervalRule, 'request_new_layer', True) setattr( intervalRule, 'next_rules_trigger_words', [] ) #non mettere None se no salta tutto perchè None non è iterabile setattr(intervalRule, 'is_entry_rule', False) #grammar creation section g = Grammar() g.add_rules(sommatoriaRule, indexRule, intervalRule) return g
def createGrammar(): integraleDefinitoExpansion = Literal("integrale da") integraleDefinitoExpansion.tag = "\\int\\limits_{}^{}" definedIntegralRule = PublicRule("defined_integral_main", integraleDefinitoExpansion) integraleDefinitoUpLimitExpansion = Literal("integrale da a") integraleDefinitoUpLimitExpansion.tag = None upLimitRule = PublicRule("defined_integral_limit", integraleDefinitoUpLimitExpansion) #setattr section setattr(definedIntegralRule, 'node_type', NODE_TYPE.INTERNO) setattr(definedIntegralRule, 'request_new_layer', True) setattr(definedIntegralRule, 'next_rules_trigger_words', [ 'a' ]) #non mettere None se no salta tutto perchè None non è iterabile setattr(definedIntegralRule, 'is_entry_rule', True) setattr(definedIntegralRule, 'go_to_begin', len('\\int\\limits_{}^{}') ) #attributo che specifica se fare carry-home. #------------------------------ setattr(upLimitRule, 'node_type', NODE_TYPE.INTERNO) setattr(upLimitRule, 'request_new_layer', True) setattr( upLimitRule, 'next_rules_trigger_words', [] ) #non mettere None se no salta tutto perchè None non è iterabile setattr(upLimitRule, 'is_entry_rule', False) #grammar creation section g = Grammar() g.add_rules(definedIntegralRule, upLimitRule) return g
def main(): # Create a grammar and add some rules to it grammar = Grammar() name = HiddenRule("name", AlternativeSet("john", "bob", "anna")) # greeting is either: 'hey', 'hey there' or 'hello' greeting = HiddenRule( "greeting", AlternativeSet(Sequence("hey", OptionalGrouping("there")), "hello")) # parting_phrase is either: 'good bye' or 'see you' parting_phrase = HiddenRule("parting_phrase", AlternativeSet("good bye", "see you")) # greet is a greeting followed by a name greet = PublicRule("greet", Sequence(RuleRef(greeting), RuleRef(name))) # goodbye is a parting phrase followed by a name goodbye = PublicRule("goodbye", Sequence(RuleRef(parting_phrase), RuleRef(name))) grammar.add_rules(name, greeting, parting_phrase, greet, goodbye) print("Grammar compiles to the following:") print(grammar.compile()) # Try matching some speech strings print_matching(grammar, "hey john") print_matching(grammar, "hey there john") print_matching(grammar, "see you john") # Try matching some hidden rules print_matching(grammar, "bob") print_matching(grammar, "hey there") print_matching(grammar, "good bye")
def createGrammar(): limiteMainExpansion = Literal("limite per") limiteMainExpansion.tag = "\\lim{ \\to }" limiteMainRule = PublicRule("limite_main", limiteMainExpansion) limiteUpP1Expansion = Literal("limite per che") limiteUpP1Expansion.tag = None limiteUpP1Rule = PublicRule("limite_up_p1", limiteUpP1Expansion) limiteUpP2Expansion = Literal("limite per che tende") limiteUpP2Expansion.tag = None limiteUpP2Rule = PublicRule("limite_up_p2", limiteUpP2Expansion) limiteUpP3Expansion = Literal("limite per che tende a") limiteUpP3Expansion.tag = None limiteUpP3Rule = PublicRule("limite_up_p3", limiteUpP3Expansion) #setattr section setattr(limiteMainRule, 'node_type', NODE_TYPE.INTERNO) setattr(limiteMainRule, 'request_new_layer', True) setattr(limiteMainRule, 'next_rules_trigger_words', [ 'che' ]) #non mettere None se no salta tutto perchè None non è iterabile setattr(limiteMainRule, 'is_entry_rule', True) setattr( limiteMainRule, 'go_to_begin', len('\\lim{ \\to }')) #attributo che specifica se fare carry-home. #------------------------------ setattr(limiteUpP1Rule, 'node_type', NODE_TYPE.INTERNO) setattr(limiteUpP1Rule, 'request_new_layer', True) setattr(limiteUpP1Rule, 'next_rules_trigger_words', [ 'tende' ]) #non mettere None se no salta tutto perchè None non è iterabile setattr(limiteUpP1Rule, 'is_entry_rule', False) #-------------------------------- setattr(limiteUpP2Rule, 'node_type', NODE_TYPE.INTERNO) setattr(limiteUpP2Rule, 'request_new_layer', True) setattr(limiteUpP2Rule, 'next_rules_trigger_words', [ 'a' ]) #non mettere None se no salta tutto perchè None non è iterabile setattr(limiteUpP2Rule, 'is_entry_rule', False) #------------------------------------ setattr(limiteUpP3Rule, 'node_type', NODE_TYPE.INTERNO) setattr(limiteUpP3Rule, 'request_new_layer', True) setattr( limiteUpP3Rule, 'next_rules_trigger_words', [] ) #non mettere None se no salta tutto perchè None non è iterabile setattr(limiteUpP3Rule, 'is_entry_rule', False) #grammar creation section g = Grammar() g.add_rules(limiteMainRule, limiteUpP1Rule, limiteUpP2Rule, limiteUpP3Rule) return g
def main(): # Create a hidden (private) rule rule1 = HiddenRule("hello", "hello") # Create a public rule referencing rule1 rule2 = PublicRule("greet", RuleRef(rule1)) # Create a grammar and add the new rules to it grammar = Grammar("g") grammar.add_rules(rule1, rule2) # Compile the grammar using compile() print("Grammar '%s' compiles to:" % grammar.name) print(grammar.compile()) # Find rules matching 'hello' # rule2 will be found, but not rule1 because it is hidden print("Matching rule: %s" % grammar.find_matching_rules("hello")[0])
def main(): # The Repeat expansion requires one or more matches for its child expansion. # The KleeneStar requires zero or more matches. The Sequence expansion # requires all of its children expansions to be spoken in sequence. # Create a public rule using a Repeat expansion and another using the # KleeneStar expansion. rule1 = PublicRule("repeat", Sequence(Repeat("please"), "don't crash")) rule2 = PublicRule("kleene", Sequence(KleeneStar("please"), "don't crash")) # Create a grammar and add the new rules to it grammar = Grammar("g") grammar.add_rules(rule1, rule2) # Compile the grammar using compile() print("Grammar '%s' compiles to:" % grammar.name) print(grammar.compile()) # Find rules in the grammar that match some speech strings print_matching(grammar, "don't crash") # only kleene will match print_matching(grammar, "please don't crash") # both will match print_matching(grammar, "please please don't crash") # both again
def createGrammar(): fracExpansion = Literal("frazione") fracExpansion.tag = '\\frac{}{}' fracRule = PublicRule("fraction_main", fracExpansion) numeratorExpansion = Literal("frazione numeratore") numeratorExpansion.tag = None numeratorRule = PublicRule("fraction_numerator", numeratorExpansion) denominatorExpansion = Literal("frazione numeratore denominatore") denominatorExpansion.tag = None denominatorRule = PublicRule("fraction_denominator", denominatorExpansion) #setattr section setattr(fracRule, 'node_type', NODE_TYPE.INTERNO) setattr(fracRule, 'request_new_layer', False) setattr(fracRule, 'next_rules_trigger_words', [ 'numeratore' ]) #non mettere None se no salta tutto perchè None non è iterabile setattr(fracRule, 'is_entry_rule', True) #------------------- setattr(numeratorRule, 'node_type', NODE_TYPE.INTERNO) setattr(numeratorRule, 'request_new_layer', True) setattr(numeratorRule, 'next_rules_trigger_words', [ 'denominatore' ]) #non mettere None se no salta tutto perchè None non è iterabile setattr(numeratorRule, 'is_entry_rule', False) setattr( numeratorRule, 'go_to_begin', len('\\frac{}{}')) #attributo che specifica se fare carry-home. #------------------------ setattr(denominatorRule, 'node_type', NODE_TYPE.INTERNO) setattr(denominatorRule, 'request_new_layer', True) setattr( denominatorRule, 'next_rules_trigger_words', [] ) #non mettere None se no salta tutto perchè None non è iterabile setattr(denominatorRule, 'is_entry_rule', False) #grammar creation section g = Grammar() g.add_rules(fracRule, numeratorRule, denominatorRule) return g