class Bottlenose: def __init__(self, bootstrapVocabulary=False): Concept(bootstrapVocabulary=bootstrapVocabulary) self._contexts = [Context()] self._context = self._contexts[0] self._translator = Translator() self._interpreter = Interpreter(self._context) def tell(self, input): JSON = self._translator.visit(grammar.parse(input)) return self.tellJSON(JSON) def tellJSON(self, JSON): results = self._interpreter.interpret(JSON) self._context.ponderRecentMentions() if isinstance(results, set) or isinstance(results, list): objects = list() for result in results: objects.append(BottlenoseObject(result, self._context)) return objects elif not results: return None else: return [BottlenoseObject(results, self._context)] def ask(self, subject, clause=None): query = "?" + subject if clause: query += "(" + clause + ")" return self.tell(query) def context(self): return self._context def listContexts(self): return self._contexts def setContext(self, index): if index >= 0 and index < len(self._contexts): self._context = self._contexts[index] self._interpreter.setContext(self._contexts[index]) def loadFile(self, filePath, onlyBeliefs=False, onlyStatements=False): file = open(filePath, 'r') for line in file: line = line.rstrip("\n") JSON = self._translator.visit(grammar.parse(line)) if 'statement' in JSON: if not onlyBeliefs: self.tellJSON(JSON) else: if not onlyStatements: self.tellJSON(JSON) def loadDirectory(self, dirPath): filePaths = [] for root, dirnames, filenames in os.walk(dirPath): for filename in fnmatch.filter(filenames, '*.bottle'): filePaths.append(os.path.join(root, filename)) for filePath in filePaths: self.loadFile(filePath, onlyBeliefs=True) for filePath in filePaths: self.loadFile(filePath, onlyStatements=True)