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. 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): 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 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): # 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