def execute(self,Model,instructID): CaughtError = None self.initialize(instructID) try: self._execute(Model) except (KeyError,LookupError,ValueError,AttributeError,RuntimeError,TypeError),e: logger.error('%s on %s', e.__class__.__name__, e) CaughtError = e
def findMissingSenses(): for k,v in Senses.items(): for pos,senseList in v.items(): for s in senseList: try: if pos!='see': pywordnet.getSense(k,pos,s-1) except (KeyError,TypeError),err: # Inflected form logger.errror('Trying inflected form b/c of Error %s',err) logger.error('%s',pywordnet.getSense(s[0],pos,s[1][0]-1)) except: logger.error('Cannot find %s, %s, %s', k,pos,s)
def findMissingSenses(): for k, v in Senses.items(): for pos, senseList in v.items(): for s in senseList: try: if pos != "see": pywordnet.getSense(k, pos, s - 1) except (KeyError, TypeError), err: # Inflected form logger.errror("Trying inflected form b/c of Error %s", err) logger.error("%s", pywordnet.getSense(s[0], pos, s[1][0] - 1)) except: logger.error("Cannot find %s, %s, %s", k, pos, s)
def recImmediatePicture(cls,desc,viewCache,side): match = False picture = desc.value if hasattr(desc,'Detail') and desc.Detail: if isinstance(desc.Detail,list): picture = desc.Detail[0] else: picture = desc.Detail if isinstance(picture,Thing): picture = picture.value if side in (FrontLeft,FrontRight,At,Front): match = viewCache[Front].match(picture,picture.ViewPosition) if not match: side = Front elif side in (Back,Left,Right): match = viewCache[side].match(picture,picture.ViewPosition) else: logger.error('recImmediatePicture(%r): Unknown side %r',desc,side) return match,side
def readCorrFrame(parses,instructID): CaughtError=None CaughtErrorTxt='' frames=[] for frame in open('Directions/ContentFrames/ContentFrame-'+instructID).readlines(): frame = str(frame) #Escape if not frame or frame == '\n\n': return [],Failed,EOFError,'Empty instruction file' try: frames.append(SurfaceSemanticsStructure.parse(frame)) except ValueError,e: CaughtErrorTxt = "Can't parse: " + str(e) logger.error("%s.",CaughtErrorTxt) if str(e).startswith("Error parsing field structure"): CaughtError = 'EOFError' else: CaughtError = 'ValueError'
def readCorrFrame(parses, instructID): CaughtError = None CaughtErrorTxt = "" frames = [] for frame in open("Directions/ContentFrames/ContentFrame-" + instructID).readlines(): frame = str(frame) # Escape if not frame or frame == "\n\n": return [], Failed, EOFError, "Empty instruction file" try: frames.append(SurfaceSemanticsStructure.parse(frame)) except ValueError, e: CaughtErrorTxt = "Can't parse: " + str(e) logger.error("%s.", CaughtErrorTxt) if str(e).startswith("Error parsing field structure"): CaughtError = "EOFError" else: CaughtError = "ValueError"
def run(self, files, instructionQ=None, successes=0, attempts=0, Results={}, Conditions=[]): global InstructionText for file in files: while (instructionQ and not instructionQ.empty()): type, message = instructionQ.get() print 'TimedFollower.run :: got', type, message if type == 'State': print 'Got state message' elif type == 'ID': file = message elif type == 'Text': InstructionText = message else: logger.error( 'TimedFollower.run::instructionQ:' ' Unknown message type: %s %s.', type, message) Options.reset() results, error, resultTxt = self.follow(file) if Options.Statistics: logger.runComplete('Statistics: %s', Options.stats()) name = file.split('-')[1] stats = open( 'MarcoLogs/Statistics/' + '-'.join(Conditions) + '-' + logger.timeStamp + '-' + name, 'w') for opt, count in Options.stats().items(): stats.write('%s: %d\n' % (opt, count)) stats.close() attempts += 1 if resultTxt == Success: successList = Results.setdefault(resultTxt, []) successList.append((resultTxt, file)) Results[resultTxt] = successList successes += 1 else: errorList = Results.setdefault(error, []) errorList.append((resultTxt, file)) Results[error] = errorList
def extractSurfaceSemantics(token,parent): global Senses POS=getPartOfSpeech(token,parent) tokenSenses = {} text = token['TEXT'].lower() default = token['TEXT'].upper() if POS in ['N', 'V', 'ADV', 'ADJ']: try: #Redo as test = foo while not tokenSensesword: try: foo ; except KeyError: foo = next foo tokenSenses = Senses[text] except KeyError: logger.warning('extractSurfaceSemantics : Text not in tagged senses: %s', text) try: #logger.warning('extractSurfaceSemantics : Previously unseen word but in WordNet?: %s', text) # stringified range of possible senses without spaces tokenSenses = {POS : range(1,len(pywordnet.getWord(text,POS).getSenses())+1)} except KeyError: try: logger.warning('extractSurfaceSemantics : Inflected version of WordNet word? %s', text) if text.endswith('s'): text = text[:-1] tokenSenses = Senses[text] else: stemmer = PorterStemmer() # Update WordNetStemmer to NLTK 1.4 API stemmer.stem(token) text = token['STEM'] tokenSenses = Senses[text] except KeyError: text = token['TEXT'].lower() try: logger.warning('extractSurfaceSemantics : Misspelling / typo of WordNet word? %s', text) spellchecker = enchant.DictWithPWL('en_US', Lexicon) s = '' for s in spellchecker.suggest(text): if s in Senses: tokenSenses = Senses[s] break if not tokenSenses and spellchecker.suggest(text): s = spellchecker.suggest(text)[0] tokenSenses = {POS : range(1,len(pywordnet.getWord(s,POS).getSenses())+1)} if s and Options.Spellcheck: logger.warning('extractSurfaceSemantics : Found spelling correction %s for %s', s,text) text = s #logger.debug('*** extractSurfaceSemantics : Implement spelling correction. *** ') #raise KeyError except KeyError: logger.error('extractSurfaceSemantics : Unknown token: %s', text) return default # Handle experienced typos. if 'see' in tokenSenses: ### FIXME adding to dict for typos that are other words text = tokenSenses['see'] try: tokenSenses = Senses[text] except: return default # Handle morphology variants that wordnet understands. elif isinstance(tokenSenses, tuple): text,tokenSenses[POS] = tokenSenses[POS] try: return '_'.join([text,POS,','.join([str(i) for i in tokenSenses[POS]])]) except KeyError: #logger.warning('extractSurfaceSemantics : Expected POS %s for token %s, Got %s, Using %s', # POS, token, tokenSenses.keys(), tokenSenses.keys()[0]) if tokenSenses.keys(): POS = token['POS'] = tokenSenses.keys()[0] return '_'.join([text,POS,','.join([str(i) for i in tokenSenses.values()[0]])]) except Exception,e: logger.error('extractSurfaceSemantics: %s: Could not find sense %s for token %s', e, POS, token) #tokenSenses, text
def extractSurfaceSemantics(token, parent): global Senses POS = getPartOfSpeech(token, parent) tokenSenses = {} text = token["TEXT"].lower() default = token["TEXT"].upper() if POS in ["N", "V", "ADV", "ADJ"]: try: # Redo as test = foo while not tokenSensesword: try: foo ; except KeyError: foo = next foo tokenSenses = Senses[text] except KeyError: logger.warning("extractSurfaceSemantics : Text not in tagged senses: %s", text) try: # logger.warning('extractSurfaceSemantics : Previously unseen word but in WordNet?: %s', text) # stringified range of possible senses without spaces tokenSenses = {POS: range(1, len(pywordnet.getWord(text, POS).getSenses()) + 1)} except KeyError: try: logger.warning("extractSurfaceSemantics : Inflected version of WordNet word? %s", text) if text.endswith("s"): text = text[:-1] tokenSenses = Senses[text] else: stemmer = PorterStemmer() # Update WordNetStemmer to NLTK 1.4 API stemmer.stem(token) text = token["STEM"] tokenSenses = Senses[text] except KeyError: text = token["TEXT"].lower() try: logger.warning("extractSurfaceSemantics : Misspelling / typo of WordNet word? %s", text) spellchecker = enchant.DictWithPWL("en_US", Lexicon) s = "" for s in spellchecker.suggest(text): if s in Senses: tokenSenses = Senses[s] break if not tokenSenses and spellchecker.suggest(text): s = spellchecker.suggest(text)[0] tokenSenses = {POS: range(1, len(pywordnet.getWord(s, POS).getSenses()) + 1)} if s and Options.Spellcheck: logger.warning("extractSurfaceSemantics : Found spelling correction %s for %s", s, text) text = s # logger.debug('*** extractSurfaceSemantics : Implement spelling correction. *** ') # raise KeyError except KeyError: logger.error("extractSurfaceSemantics : Unknown token: %s", text) return default # Handle experienced typos. if "see" in tokenSenses: ### FIXME adding to dict for typos that are other words text = tokenSenses["see"] try: tokenSenses = Senses[text] except: return default # Handle morphology variants that wordnet understands. elif isinstance(tokenSenses, tuple): text, tokenSenses[POS] = tokenSenses[POS] try: return "_".join([text, POS, ",".join([str(i) for i in tokenSenses[POS]])]) except KeyError: # logger.warning('extractSurfaceSemantics : Expected POS %s for token %s, Got %s, Using %s', # POS, token, tokenSenses.keys(), tokenSenses.keys()[0]) if tokenSenses.keys(): POS = token["POS"] = tokenSenses.keys()[0] return "_".join([text, POS, ",".join([str(i) for i in tokenSenses.values()[0]])]) except Exception, e: logger.error( "extractSurfaceSemantics: %s: Could not find sense %s for token %s", e, POS, token ) # tokenSenses, text
class RouteRunner(object): def __init__(self, Map=None): self.StatusQueue = None self.Map = Map self.Start = None self.Target = None def setRobot(self, Robot): self.robot = Robot self.Map = Robot.pomdp def initializeRoute(self, Mapname, Start, Target): if hasattr(self, 'Start') and Start == self.Start and Target == self.Target: return self.Start = Start self.Target = Target if self.StatusQueue: self.StatusQueue.put(('Start Place', Start)) self.StatusQueue.put(('Destination', Target)) self.robot.setRoute(Start, Target) def planRoute(self): """plan Route from self.Start to self.Target in self.Map.""" pathobject = ShortestPath.ShortestPath(self.Map, self.robot.currentState) (distance, Path) = pathobject.ReturnPath(self.Start, self.Target) #Path is a list of gateway and direction tuples (currentGateway, y) = Path[0] #determine list of commands from Path Route = [] #added to keep the "start and random direction" functionality if self.robot.currentState == None: randomDir = random.randint(0, 3) self.robot.currentState = (currentGateway, randomDir) (temp, mydir) = self.robot.currentState #determine turn & travel commands from list of gateways & directions for (x, desireddir) in Path: while (desireddir != mydir and desireddir != -1): if (mydir > desireddir) and abs(mydir - desireddir) == 1 or ( mydir == 0 and desireddir == 3): Route.append(TurnLeft) mydir -= 1 else: Route.append(TurnRight) mydir += 1 mydir %= 4 if (desireddir != -1): Route.append(TravelFwd) Route.append(DeclareGoal) return Route def execute(self, (Map, Start, Target)): CaughtError = None self.initializeRoute(Map, Start, Target) try: self.executeRoute(self.planRoute()) except (KeyError, LookupError, ValueError), e: #,AttributeError logger.error('%s on %s', e.__class__.__name__, e) CaughtError = e