def load(self, gramSpec, allResults=0, hypothesis=0, grammarName=None): if type(gramSpec) == types.StringType: gramSpec = [gramSpec] elif type(gramSpec) != types.ListType: raise TypeError("grammar definition must be a list of strings") elif type(gramSpec[0]) != types.StringType: raise TypeError("grammar definition must be a list of strings") gramparser.splitApartLines(gramSpec) parser = gramparser.GramParser(gramSpec, grammarName=grammarName) parser.doParse() parser.checkForErrors() gramBin = gramparser.packGrammar(parser) self.scanObj = parser.scanObj # for later error messages. GramClassBase.load(self, gramBin, allResults, hypothesis) # we want to keep a list of the rules which can be activated and the # known lists so we can catch errors earlier self.validRules = parser.exportRules.keys() self.validLists = parser.knownLists.keys() # we reverse the rule dictionary so we can convert rule numbers back # to rule names during recognition self.ruleMap = {} for x in parser.knownRules.keys(): self.ruleMap[parser.knownRules[x]] = x return 1
def load(self,gramSpec,allResults=0,hypothesis=0, grammarName=None): try: # print 'loading grammar %s, gramspec type: %s'% (grammarName, type(gramSpec)) # code upper ascii characters with latin1 if they were in the process entered as unicode if not type(gramSpec) in (str, list): raise TypeError( "grammar definition of %s must be a string or a list of strings, not %s"% (grammarName, type(gramSpec))) parser = gramparser.GramParser(gramSpec, grammarName=grammarName) parser.doParse() parser.checkForErrors() gramBin = gramparser.packGrammar(parser) # self.scanObj = parser.scanObj # for later error messages # no mention to GrammarError after the loading now.. try: GramClassBase.load(self,gramBin,allResults,hypothesis) except natlink.BadGrammar: print('GrammarBase, cannot load grammar, BadGrammar:\n%s\n'% gramSpec) raise # we want to keep a list of the rules which can be activated and the # known lists so we can catch errors earlier self.validRules = list(parser.exportRules.keys()) self.validLists = list(parser.knownLists.keys()) # we reverse the rule dictionary so we can convert rule numbers back # to rule names during recognition self.ruleMap = {} for ruleNum, knownRule in parser.knownRules.items(): self.ruleMap[ knownRule ] = ruleNum return 1 except: print("Unexpected error load GramClassBase:", sys.exc_info()) print(traceback.print_exc())
def ParseGrammarDefinitions(gramSpec, gramName, Lists, Dicts, activeRules, All=1, Exclusive=0, exclusiveState=0): if type(gramSpec) != type([]): gramSpec = [gramSpec] gramparser.splitApartLines(gramSpec) ## Parser = natlinkutils.GramParser(gramSpec) ## Parser.doParse() # with gramparserlexyacc: Parser = gramparser.GramParser(gramSpec) print '%s, type gramSpec: %s, ' % (gramName, type(gramSpec)) if type(gramSpec) == types.ListType: gramSpec = [checkForBinary(g) for g in gramSpec] Parser.doParse() ParserInfo = (InverseDict(Parser.knownWords), InverseDict(Parser.knownRules), InverseDict(Parser.knownLists), Parser.importRules) stack = [] for name in Parser.ruleDefines.keys(): ParseRuleDefinitions(name, stack, Parser, ParserInfo, Lists, Dicts) DefRules = {} for x in stack: DefRules[x.Name] = x UsedRules = [] for x in stack: x.FillInRules(DefRules, UsedRules) x.FoldLongAlternatives(0) Grammar = GrammarElement() Grammar.Init(RuleCode, gramName) if Exclusive: if not exclusiveState: return # if asking for exclusive, only show the activerules All = 0 if All: Obsolete = GrammarElement() Obsolete.Init(RuleCode, 'Obsolete') for rule in stack: if Parser.exportRules.has_key(rule.Name): Grammar.Insert(rule) elif not rule.Name in UsedRules: Obsolete.Insert(rule) if len(Obsolete.Included) != 0: Grammar.Append(Obsolete) elif activeRules: for rule in stack: if (rule.Name in activeRules): Grammar.Insert(rule) else: return # nothing if no active rules QH return Grammar
def load(self, gramSpec, allResults=0, hypothesis=0, grammarName=None): # print 'loading grammar %s, gramspec type: %s'% (grammarName, type(gramSpec)) # code upper ascii characters with latin1 if they were in the process entered as unicode if not type(gramSpec) in (six.text_type, six.binary_type, types.ListType): raise TypeError( "grammar definition of %s must be a string or a list of strings, not %s" % (grammarName, type(gramSpec))) # print 'loading %s, type: %s'% (grammarName, type(gramSpec) ) if type(gramSpec) == types.ListType: for i, grampart in enumerate(gramSpec): line = grampart if type(line) == six.binary_type: line = utilsqh.convertToUnicode(line) if type(line) == six.text_type: line = utilsqh.convertToBinary(line) if line != grampart: gramSpec[i] = line if type(gramSpec) == six.binary_type: gramSpec = utilsqh.convertToUnicode(gramSpec) if type(gramSpec) == six.text_type: gramSpec = utilsqh.convertToBinary(gramSpec) gramSpec = gramSpec.split('\n') gramSpec = [g.rstrip() for g in gramSpec] gramparser.splitApartLines(gramSpec) parser = gramparser.GramParser(gramSpec, grammarName=grammarName) parser.doParse() parser.checkForErrors() gramBin = gramparser.packGrammar(parser) self.scanObj = parser.scanObj # for later error messages. try: GramClassBase.load(self, gramBin, allResults, hypothesis) except natlink.BadGrammar: print 'GrammarBase, cannot load grammar, BadGrammar:\n%s\n' % gramSpec raise # we want to keep a list of the rules which can be activated and the # known lists so we can catch errors earlier self.validRules = parser.exportRules.keys() self.validLists = parser.knownLists.keys() # we reverse the rule dictionary so we can convert rule numbers back # to rule names during recognition self.ruleMap = {} for x in parser.knownRules.keys(): self.ruleMap[parser.knownRules[x]] = x return 1
def load(self, gramSpec, allResults=0, hypothesis=0, grammarName=None): # print 'loading grammar %s, gramspec type: %s'% (grammarName, type(gramSpec)) # code upper ascii characters with latin1 if they were in the process entered as unicode if type(gramSpec) == types.ListType: for grampart in gramSpec: if type(grampart) == types.UnicodeType: newGramSpec = [g.encode('latin1') for g in gramSpec] gramSpec = newGramSpec break elif type(gramSpec) == types.UnicodeType: gramSpec = [gramSpec.encode('latin1')] elif type(gramSpec) == types.StringType: gramSpec = [gramSpec] else: raise TypeError( "grammar definition must be a string or a list of strings, not %s" % gramSpec) gramparser.splitApartLines(gramSpec) parser = gramparser.GramParser(gramSpec, grammarName=grammarName) parser.doParse() parser.checkForErrors() gramBin = gramparser.packGrammar(parser) self.scanObj = parser.scanObj # for later error messages. try: GramClassBase.load(self, gramBin, allResults, hypothesis) except natlink.BadGrammar: print 'GrammarBase, cannot load grammar, BadGrammar:\n%s\n' % gramSpec raise # we want to keep a list of the rules which can be activated and the # known lists so we can catch errors earlier self.validRules = parser.exportRules.keys() self.validLists = parser.knownLists.keys() # we reverse the rule dictionary so we can convert rule numbers back # to rule names during recognition self.ruleMap = {} for x in parser.knownRules.keys(): self.ruleMap[parser.knownRules[x]] = x return 1