本文整理汇总了Python中interpreter.Interpreter.setContext方法的典型用法代码示例。如果您正苦于以下问题:Python Interpreter.setContext方法的具体用法?Python Interpreter.setContext怎么用?Python Interpreter.setContext使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类interpreter.Interpreter
的用法示例。
在下文中一共展示了Interpreter.setContext方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from interpreter import Interpreter [as 别名]
# 或者: from interpreter.Interpreter import setContext [as 别名]
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)