lang-model/naive-nlu/tree_nlu/tests/gac_100.py

738 lines
22 KiB
Python
Raw Normal View History

import logging
2017-05-23 20:16:27 +00:00
from ..knowledge_base import KnowledgeBase
2017-05-24 20:37:44 +00:00
from ..utils.visuals import show_progbar
2017-05-23 20:16:27 +00:00
def _assert(args):
assert(args)
def _assert_msg(args, msg):
assert(args, msg)
2017-05-23 20:16:27 +00:00
examples = [
('full_example',
{
"text": "is icecream cold?",
"affirmation": "icecream is cold",
"parsed": ("question",
("exists-property-with-value", 'icecream', 'cold')),
"answer": True,
"after_execution": [(
lambda knowledge: _assert('cold' in knowledge.knowledge['icecream']['property'])
),],
}),
('full_example',
{
"text": "is earth a planet?",
"affirmation": "earth is a planet",
"parsed": ("question",
("pertenence-to-group", 'earth', 'planet')),
"answer": True,
"after_execution": [(
lambda knowledge: _assert('planet' in knowledge.knowledge['earth']['groups'])
),],
}),
('full_example',
{
"text": "Is green a color?",
"affirmation": "green is a color",
"parsed": ("question",
("pertenence-to-group", 'green', 'color')),
"answer": True,
"after_execution": [(
lambda knowledge: _assert('color' in knowledge.knowledge['green']['groups'])
),],
}),
('full_example',
{
"text": "do airplanes fly?",
"affirmation": "airplanes fly",
"parsed": ("question",
("has-capacity", 'plane', 'fly')),
"answer": True,
"after_execution": [(
lambda knowledge: _assert('fly' in knowledge.knowledge['plane']['capacities'])
),],
}),
2017-05-24 18:13:42 +00:00
('full_example',
{
"text": "Is it hot during the summer?",
"affirmation": "it is hot during summer",
2017-05-24 18:13:42 +00:00
"parsed": ("question",
("implies", 'summer', 'hot')),
"answer": True,
"after_execution": [(
lambda knowledge: _assert('hot' in knowledge.knowledge['summer']['implications'])
),],
2017-05-24 18:13:42 +00:00
}),
2017-05-24 18:42:15 +00:00
('full_example',
{
"text": "is chile in south america ?",
"affirmation": "chile is in south america",
"parsed": ("question",
("property-has-value", 'chile', 'location', 'south america')),
"answer": True,
2017-05-24 20:17:57 +00:00
"after_execution": [(
lambda knowledge: _assert('south america' in knowledge.knowledge['chile']['location'])
),],
}),
('full_example',
{
"text": "Was Socrates a man?",
"affirmation": "Socrates was a man",
"parsed": ("question",
("pertenence-to-group", 'socrates', 'man')),
"answer": True,
"after_execution": [(
lambda knowledge: _assert('man' in knowledge.knowledge['socrates']['groups'])
),],
2017-05-24 18:42:15 +00:00
}),
('full_example',{
"text": "Computers use electricity?",
"affirmation": "Computers use electricity",
"parsed": ("question",
('perform-verb-over-object', 'computers', 'use', 'electricity')),
"answer": True,
"after_execution": [(
lambda knowledge: print("->", knowledge.knowledge['computers'])
),],
}),
2017-05-23 20:16:27 +00:00
# {
# "text": "The dominant language in france is french?",
# "affirmation": "The dominant language in france is french?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "was abraham lincoln once president of the united states?",
# "affirmation": "was abraham lincoln once president of the united states?",
# "parsed": (),
# "answer": None,
# },
('text_example',
{
"question": "is milk white?",
"affirmation": "milk is white",
"answer": True,
}),
2017-05-23 20:16:27 +00:00
# {
# "text": "do people have emotions?",
# "affirmation": "do people have emotions?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "do objects appear smaller as they move away from you?",
# "affirmation": "do objects appear smaller as they move away from you?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Does the human species have a male and female gender?",
# "affirmation": "Does the human species have a male and female gender?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Is a mountain mostly made of rock?",
# "affirmation": "Is a mountain mostly made of rock?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "is sun microsystems a computer company?",
# "affirmation": "is sun microsystems a computer company?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Do you see with your eyes and smell with your nose?",
# "affirmation": "Do you see with your eyes and smell with your nose?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Is smoking bad for your health?",
# "affirmation": "Is smoking bad for your health?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Does a dog have four legs?",
# "affirmation": "Does a dog have four legs?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Do mammals have hearts?",
# "affirmation": "Do mammals have hearts?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "is the Earth a planet?",
# "affirmation": "is the Earth a planet?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Is water a liquid?",
# "affirmation": "Is water a liquid?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Is Bugs Bunny a cartoon character?",
# "affirmation": "Is Bugs Bunny a cartoon character?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Do Humans communicate by Telephone?",
# "affirmation": "Do Humans communicate by Telephone?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "is beer a drink ?",
# "affirmation": "is beer a drink ?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "are there 12 months in a year?",
# "affirmation": "are there 12 months in a year?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "does the sun hurt your eyes when you look at it?",
# "affirmation": "does the sun hurt your eyes when you look at it?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Do most cars have doors?",
# "affirmation": "Do most cars have doors?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "is orange both a fruit and a colour?",
# "affirmation": "is orange both a fruit and a colour?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Is water a necessity?",
# "affirmation": "Is water a necessity?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Do CDs have better quality sound than Cassettes?",
# "affirmation": "Do CDs have better quality sound than Cassettes?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "do animals die?",
# "affirmation": "do animals die?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Is the arctic cold?",
# "affirmation": "Is the arctic cold?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Do people have 2 eyes?",
# "affirmation": "Do people have 2 eyes?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "does a person have a brain?",
# "affirmation": "does a person have a brain?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Is the rain wet?",
# "affirmation": "Is the rain wet?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Is division a mathematical operation?",
# "affirmation": "Is division a mathematical operation?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "is 400 greater than 399?",
# "affirmation": "is 400 greater than 399?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "is magenta a color?",
# "affirmation": "is magenta a color?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Are books educational?",
# "affirmation": "Are books educational?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Was the Great Wall of China built by humans?",
# "affirmation": "Was the Great Wall of China built by humans?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Are pianos musical instruments?",
# "affirmation": "Are pianos musical instruments?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Has Bill Clinton been President of the United States?",
# "affirmation": "Has Bill Clinton been President of the United States?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Is a whale a mammal?",
# "affirmation": "Is a whale a mammal?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Are lemons yellow?",
# "affirmation": "Are lemons yellow?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Is the South Pole cold?",
# "affirmation": "Is the South Pole cold?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Is Africa warm?",
# "affirmation": "Is Africa warm?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Is Antarctica cold?",
# "affirmation": "Is Antarctica cold?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Is rock is generally harder than wood?",
# "affirmation": "Is rock is generally harder than wood?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Do dogs chase cats?",
# "affirmation": "Do dogs chase cats?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "can humans die from cold temperatures?",
# "affirmation": "can humans die from cold temperatures?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "do people enjoy conversation?",
# "affirmation": "do people enjoy conversation?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Is Bill Clinton the President of the United States?",
# "affirmation": "Is Bill Clinton the President of the United States?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Are books a good source of information?",
# "affirmation": "Are books a good source of information?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "are friends different than enemies?",
# "affirmation": "are friends different than enemies?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "are people alive?",
# "affirmation": "are people alive?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Do triangles have 3 sides?",
# "affirmation": "Do triangles have 3 sides?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Is Ice cream cold?",
# "affirmation": "Is Ice cream cold?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Are all sides of a square the same length?",
# "affirmation": "Are all sides of a square the same length?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Do all people eat food?",
# "affirmation": "Do all people eat food?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "do dentists repair teeth?",
# "affirmation": "do dentists repair teeth?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Is America bigger than Japan?",
# "affirmation": "Is America bigger than Japan?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Do all triangles have three sides?",
# "affirmation": "Do all triangles have three sides?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "A grocery store sales food?",
# "affirmation": "A grocery store sales food?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Does a sunburn cause pain?",
# "affirmation": "Does a sunburn cause pain?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Is a computer an invention?",
# "affirmation": "Is a computer an invention?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "have humans visited the moon?",
# "affirmation": "have humans visited the moon?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Are there people in India?",
# "affirmation": "Are there people in India?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Was Einstein a genius?",
# "affirmation": "Was Einstein a genius?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Are we on the planet earth?",
# "affirmation": "Are we on the planet earth?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "do people comb their hair in the morning?",
# "affirmation": "do people comb their hair in the morning?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Does it hurt to lose a friend?",
# "affirmation": "Does it hurt to lose a friend?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Are there people on the earth?",
# "affirmation": "Are there people on the earth?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Was George Washington a president of the United States of America?",
# "affirmation": "Was George Washington a president of the United States of America?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Does an ocean have salt water in it?",
# "affirmation": "Does an ocean have salt water in it?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Is night darker than day?",
# "affirmation": "Is night darker than day?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Does a triangle have three sides?",
# "affirmation": "Does a triangle have three sides?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Are peaches fruit?",
# "affirmation": "Are peaches fruit?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Do people urinate?",
# "affirmation": "Do people urinate?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Is Germany located in Europe?",
# "affirmation": "Is Germany located in Europe?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Do mirrors reflect light?",
# "affirmation": "Do mirrors reflect light?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Are people born naked?",
# "affirmation": "Are people born naked?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Is it hot near the equator?",
# "affirmation": "Is it hot near the equator?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "is paper made from trees?",
# "affirmation": "is paper made from trees?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Can a female have children?",
# "affirmation": "Can a female have children?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Are people born every day?",
# "affirmation": "Are people born every day?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Are shoes worn on the feet?",
# "affirmation": "Are shoes worn on the feet?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "does it get wet when it rains?",
# "affirmation": "does it get wet when it rains?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Are there plants and insects in the rainforest which have no names?",
# "affirmation": "Are there plants and insects in the rainforest which have no names?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Do people eat pigs?",
# "affirmation": "Do people eat pigs?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Do businessmen wear ties?",
# "affirmation": "Do businessmen wear ties?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Is New York in the United States?",
# "affirmation": "Is New York in the United States?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Are humans more intelligent than ants?",
# "affirmation": "Are humans more intelligent than ants?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Are ravens black?",
# "affirmation": "Are ravens black?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Are there rats on ships?",
# "affirmation": "Are there rats on ships?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "are lions animals?",
# "affirmation": "are lions animals?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "6 is greater than 5?",
# "affirmation": "6 is greater than 5?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Is water made of hydrogen and oxygen?",
# "affirmation": "Is water made of hydrogen and oxygen?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "is the sky blue on a clear day?",
# "affirmation": "is the sky blue on a clear day?",
# "parsed": (),
# "answer": None,
# },
# {
# "text": "Do most people work during the day?",
# "affirmation": "Do most people work during the day?",
# "parsed": (),
# "answer": None,
# },
]
base_knowledge = {
'icecream': {
2017-05-23 21:29:34 +00:00
"groups": {'noun', 'object', 'comestible', 'sweet'},
2017-05-23 20:16:27 +00:00
},
2017-05-24 18:13:42 +00:00
'hot': {
"groups": {'property', 'temperature'},
},
'summer': {
"groups": {'epoch'},
},
'planet': {
2017-05-23 21:29:34 +00:00
"groups": {'noun', 'group'},
},
'white': {
"groups": {'noun', 'color', 'concept', 'property'},
},
'green': {
2017-05-23 21:29:34 +00:00
"groups": {'noun', 'color', 'concept'},
},
'milk': {
"groups": {'noun'},
},
'fly': {
2017-05-23 21:29:34 +00:00
"groups": {'verb'},
},
'computers': {
"groups": {'object'},
},
'use': {
"groups": {'verb'},
},
'electricity': {
"groups": {'power'},
},
2017-05-23 20:16:27 +00:00
}
def main():
knowledge = KnowledgeBase(
knowledge=base_knowledge,
)
2017-05-24 20:37:44 +00:00
total = len(examples)
for i, (example_type, data) in enumerate(examples):
if example_type == 'full_example':
affirmation = {
'text': data['affirmation'],
'parsed': data['parsed'][1],
}
question = data
2017-05-24 20:37:44 +00:00
show_progbar(i, total, data['affirmation'])
differences = knowledge.train([affirmation])
2017-05-24 20:37:44 +00:00
show_progbar(i, total, data['text'])
differences = knowledge.train([question])
print(differences())
result, _, _ = knowledge.process(data['text'])
if "after_execution" in data:
for f in data["after_execution"]:
f(knowledge)
if result != data['answer']:
raise AssertionError('{} is not {}'.format(result, data['answer']))
elif example_type == 'text_example':
2017-05-24 20:37:44 +00:00
show_progbar(i, total, data['affirmation'])
affirmation = data['affirmation']
logging.debug("Processing affirmation: {}".format(affirmation))
_, _, _ = knowledge.process(affirmation)
2017-05-24 20:37:44 +00:00
show_progbar(i, total, data['question'])
question = data['question']
logging.debug("Processing question : {}".format(question))
result, _, _ = knowledge.process(question)
2017-05-23 20:16:27 +00:00
if result != data['answer']:
raise AssertionError('{} is not {}'.format(result, data['answer']))
2017-05-23 20:16:27 +00:00
else:
raise NotImplementedError('Example type: {}'.format(example_type))
2017-05-24 20:37:44 +00:00
print("\r\x1b[K", end='')