Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_class_relationship_is_one_way(self):
nlu = NLU()
nlu.integrate("C(Mary, person)")
nlu.integrate("C(Joe, person)")
nlu.integrate("C(kid, person)")
logger.info("State: \n%s\n" % graph(nlu.working_memory))
gen = InheritFromParentClass()
concepts = list(gen.gen(nlu.question_store))
logger.info("Generated concepts: %r" % concepts)
self.assertEqual(len(concepts), 0)
def test_class_deduction(self):
nlu = NLU()
nlu.integrate("C(Bp,city)")
nlu.integrate("C(city,place)")
q, a, c = nlu.ask("C(Bp, place)")
logger.info("Answer to question %s: %s" % (q, nlu.create_answer(q, a)))
self.assertIsNotNone(a)
def test_implication_works_after_parent_statement(self):
nlu = NLU()
nlu.integrate("F(Joe,tired)")
nlu.integrate("IM(F(Joe,tired),F(Joe,slow))")
nlu.integrate("IM(F(Joe,slow),F(Joe,angry))")
q, a, c = nlu.ask("F(Joe,angry)")
self.assertIsNotNone(c)
self.assertEqual(c.probability, 1.0)
def test_integrate_defines_probability(self):
nlu = NLU()
nlu.integrate("IM(F(Joe,tired),F(Joe,slow))")
q, a, c = nlu.ask("F(Joe,tired)")
self.assertEqual(c.probability, .5)
nlu.integrate("F(Joe,tired)")
q, a, c = nlu.ask("F(Joe,tired)")
self.assertIsNotNone(c)
self.assertEqual(c.probability, 1.0)
def test_class_other_relations(self):
nlu = NLU()
nlu.integrate("F(bird,fly)")
nlu.integrate("C(penguin,bird)")
nlu.integrate("F(penguin,swim)")
q, a, c = nlu.ask("F(penguin, fly)")
logger.info("State:\n%s\n" % graph(nlu.question_store))
logger.info("Answer to question %s: %s" % (q, nlu.create_answer(q, a)))
self.assertIsNotNone(a)
def test_implication_with_class(self):
nlu = NLU()
nlu.integrate("IM(F(person,tired),F(person,slow))")
nlu.integrate("IM(F(person,slow),F(person,angry))")
nlu.integrate("C(Joe,person)")
q, a, c = nlu.ask("F(Joe,angry)")
logger.info("Answer to question %s: %s p=%r" % (q, nlu.create_answer(q, a), c.probability))
nlu.integrate("F(Joe,tired)")
logger.info("State:\n%s\n" % graph(nlu.working_memory))
q, a, c = nlu.ask("F(Joe,angry)")
logger.info("State:\n%s\n" % graph(nlu.question_store))
logger.info("Answer to question %s: %s p=%r" % (q, nlu.create_answer(q, a), c.probability))
self.assertIsNotNone(c)
self.assertEqual(c.probability, 1.0)
def test_implication_transfers_probability(self):
nlu = NLU()
nlu.integrate("IM(F(Joe,tired),F(Joe,slow))")
nlu.integrate("IM(F(Joe,slow),F(Joe,angry))")
nlu.integrate("F(Joe,tired)")
q, a, c = nlu.ask("F(Joe,angry)")
self.assertIsNotNone(c)
self.assertEqual(c.probability, 1.0)
def create_answer(question: Concept, mapping: Mapping[Concept, Concept]) -> Concept:
if mapping is None:
return None
# simple
if question.is_simple():
if question in mapping:
return mapping[question]
return question
# compound
ap = [] # type: list[Concept]
for p in question.parents:
ap.append(NLU.create_answer(p, mapping))
return Concept(question.name, question.relation, ap)
command = input("> ") # type: str
if command == 'help':
print("Input Mentalase statements or questions one line per expression, or")
print("input free text statements or questions (multiple statements per line, questions separately), or")
print("one of these commands:")
print(" working_memory (wm) : graph description of working memory state")
print(" question_store (qs) : graph description of last question store")
print(" reset : reset session")
print(" log [level] : set log level")
print(" help : this help")
print(" exit : exit")
elif command == 'exit':
break
elif command == 'reset':
nlu = NLU()
print("Session reset")
elif command == 'working_memory' or command == 'wm':
print("\n%s\n" % graph(nlu.working_memory))
elif command == 'question_store' or command == 'qs':
print("\n%s\n" % graph(nlu.question_store))
elif command.startswith("log "):
level = command[4:].upper()
print("Setting log level to [%s]" % level)
try:
logging.root.setLevel(level)
except ValueError:
print("Unknown value. Valid values: %s" % ", ".join(logging._nameToLevel))
elif len(command) == 0:
pass
else:
if "?" in command:
answer = self.create_answer(parsed, match)
print("Question: %s" % parsed)
direct = str(answer)
p = ''
if matched_concept is not None:
p = '(p=%r)' % matched_concept.probability
print("Answer: %s %s" % (direct, p))
if matched_concept is not None:
full = str(matched_concept)
if direct != full:
print("Full answer: %s" % full)
if __name__ == '__main__':
nlu = NLU()
nlu.integrate("C(Budapest, city)")
nlu.integrate("C(New York, city)")
nlu.integrate("C(Tokyo, city)")
nlu.integrate("C(city, location)")
nlu.integrate("F(A(she,arrive),T(yesterday),R(from,Budapest),R(to,Vienna))", "She arrived y from Bp to V")
nlu.integrate("F(A(she,stay),R(at,Hilton),R(on,Ring))", "She stayed at the Hilton")
print(graph(nlu.working_memory, words=True))
nlu.ask_question("?")
nlu.ask_question("F(A(she,arrive), T(?))")
nlu.ask_question("A(she,?)")
nlu.ask_question("F(A(?,stay),R(at, Hilton))")
nlu.ask_question("F(A(she,?),T(yesterday))")