def fill_commands(self, commands): """Fills in underspecified fields based on current knowledge""" for c in commands: if isinstance(c, Command): if self.last_object: if c.theme and is_pronoun(c.theme.name): c.theme.name = self.last_object.name if c.patient and is_pronoun(c.patient.name): c.patient.name = self.last_object.name if c.condition and c.condition.theme\ and is_pronoun(c.condition.theme.name): c.condition.theme.name = self.last_object.name if self.last_location: if c.location and c.location.name == 'there': c.location.name = self.last_location.name for f in self.facts: if isinstance(f, MapFact): if c.destination and not c.source: # Fill in source if c.theme: result = f.query_map(None, c.theme) if len(result) > 0: c.source = result[0] if not c.location and not c.destination \ and not c.source: # Fill in location if c.theme: result = f.query_map(None, c.theme) if len(result) > 0: c.location = result[0]
def fill_commands(self, commands): for c in commands: if isinstance(c, Command): if self.last_object: if c.theme and is_pronoun(c.theme.name): c.theme.name = self.last_object.name if c.patient and is_pronoun(c.patient.name): c.patient.name = self.last_object.name if c.condition and c.condition.theme and is_pronoun(c.condition.theme.name): c.condition.theme.name = self.last_object.name if self.last_location: if c.location and c.location.name == 'there': c.location.name = self.last_location.name for f in self.facts: if isinstance(f, MapFact): if c.destination and not c.source: # Fill in source if c.theme: result = f.query_map(None, c.theme) if len(result) > 0: c.source = result[0] if not c.location and not c.destination and not c.source: # Fill in location if c.theme: result = f.query_map(None, c.theme) if len(result) > 0: c.location = result[0]
class Entity(object): """Parent class of entities""" TYPES = ['Object', 'Location'] TYPE_ID = -1 # Subclasses should override TYPE_ID def __init__(self, name=None, description=None): self.name = name self.quantifier = Quantifier() # Using mutable object as default argument causes # it to be aliased across instances self.description = description if description is not None else [] def merge(self, other): """Merge this entity with another entity""" if other.name is not None: self.name = other.name self.quantifier.merge(other.quantifier) self.description.extend(other.description) def readable(self, case=True): # case: true for subject false for object if self.name == '*': return 'I' if case else 'me' elif is_pronoun(self.name): return self.name else: return '%s %s' % (self.quantifier.readable(), self.name)
def readable(self, case=True): # case: true for subject false for object if self.name == '*': return 'I' if case else 'me' elif is_pronoun(self.name): return self.name else: return '%s %s' % (self.quantifier.readable(), self.name)
def process_semantic_structures(self, semantic_structures, source=None): response = '' for structure in semantic_structures: if isinstance(structure, Assertion): self.assimilate(structure, source) response = 'Got it. %s' % structure.readable() elif isinstance(structure, Query): response = self.query(structure) # Assertions, Commands have themes and locations that may be referenced later if isinstance(structure, Assertion) or isinstance(structure, Command): # Only replace resolved object (theme has priority over condition) if structure.theme and not is_pronoun(structure.theme.name): self.last_object = structure.theme elif structure.condition and structure.condition.theme and not is_pronoun(structure.condition.theme.name): self.last_object = structure.condition.theme # Only replace resolved locations (theme has priority over condition) if structure.location and structure.location.name != 'there': self.last_location = structure.location elif structure.condition and isinstance(structure.condition, Assertion) and structure.condition.location and structure.condition.location.name != 'there': self.last_location = structure.condition.location return response
def process_semantic_structures(self, semantic_structures, source=None): """Processes semantic structures and returns a response if given a query""" response = '' for structure in semantic_structures: if isinstance(structure, Assertion): self.assimilate(structure, source) response = 'Got it. %s' % structure.readable() elif isinstance(structure, Query): response = self.query(structure) # Assertions, Commands have themes and locations that # may be referenced later if isinstance(structure, Command): # Only replace resolved object # (theme has priority over condition) if structure.theme and not is_pronoun(structure.theme.name): self.last_object = structure.theme elif structure.condition and structure.condition.theme \ and not is_pronoun(structure.condition.theme.name): self.last_object = structure.condition.theme # Only replace resolved locations # (theme has priority over condition) if structure.location and structure.location.name != 'there': self.last_location = structure.location elif structure.condition and \ isinstance(structure.condition, Assertion) and \ structure.condition.location and \ structure.condition.location.name != 'there': self.last_location = structure.condition.location elif isinstance(structure, Assertion): if structure.theme and not is_pronoun(structure.theme.name): self.last_object = structure.theme if structure.location and structure.location.name != 'there': self.last_location = structure.location return response