diff --git a/naive-nlu/tree_nlu/test.py b/naive-nlu/tree_nlu/test.py index d97c2f2..c7e1a6e 100644 --- a/naive-nlu/tree_nlu/test.py +++ b/naive-nlu/tree_nlu/test.py @@ -1,10 +1,12 @@ import logging from .tests import basic +from .tests import gac_100 logging.getLogger().setLevel(logging.WARNING) tests = ( ("basic", basic), + ("gac 100", gac_100), ) def main(): @@ -12,11 +14,11 @@ def main(): try: test_module.main() print(" ✓ {}".format(test_name)) - except AssertionError: - print(" ✗ {}".format(test_name)) + except AssertionError as ae: + print(" ✗ {}: {}".format(test_name, ae.args[0])) except Exception as e: print(" ! {} {}".format(test_name, e)) - + raise if __name__ == '__main__': main() diff --git a/naive-nlu/tree_nlu/tests/basic.py b/naive-nlu/tree_nlu/tests/basic.py index eb03ad7..ba09ce2 100644 --- a/naive-nlu/tree_nlu/tests/basic.py +++ b/naive-nlu/tree_nlu/tests/basic.py @@ -111,7 +111,8 @@ def test_assumption(expectedResponse, knowledge, query): end_result = result.getter() if is_modifiable_property(result) else result logging.info("\x1b[0;3{}mResult: {}\x1b[0m".format("1" if end_result != expectedResponse else "2", end_result)) - assert(end_result == expectedResponse) + if end_result != expectedResponse: + raise AssertionError('{} is not {}'.format(end_result, expectedResponse)) def main(): knowledge = KnowledgeBase( diff --git a/naive-nlu/tree_nlu/tests/gac_100.py b/naive-nlu/tree_nlu/tests/gac_100.py new file mode 100644 index 0000000..77d7139 --- /dev/null +++ b/naive-nlu/tree_nlu/tests/gac_100.py @@ -0,0 +1,637 @@ +from ..knowledge_base import KnowledgeBase + +examples = [ + { + "text": "is icecream cold?", + "affirmation": "icecream is cold", + "parsed": ("question", ("exists-property-with-value", 'icecream', 'cold')), + "answer": True, + }, + # { + # "text": "is earth a planet?", + # "affirmation": "is earth a planet?", + # "parsed": (), + # "answer": None, + # }, + # { + # "text": "Is green a color?", + # "affirmation": "Is green a color?", + # "parsed": (), + # "answer": None, + # }, + # { + # "text": "do airplanes fly?", + # "affirmation": "do airplanes fly?", + # "parsed": (), + # "answer": None, + # }, + # { + # "text": "Is it hot during the summer?", + # "affirmation": "Is it hot during the summer?", + # "parsed": (), + # "answer": None, + # }, + # { + # "text": "is chile in south america ?", + # "affirmation": "is chile in south america ?", + # "parsed": (), + # "answer": None, + # }, + # { + # "text": "Was Socrates a man?", + # "affirmation": "Was Socrates a man?", + # "parsed": (), + # "answer": None, + # }, + # { + # "text": "Computers use electricity?", + # "affirmation": "Computers use electricity?", + # "parsed": (), + # "answer": None, + # }, + # { + # "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": "Is milk white?", + # "affirmation": "Is milk white?", + # "parsed": (), + # "answer": None, + # }, + # { + # "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': { + "groups": set(['noun', 'object', 'comestible', 'sweet']), + }, + "cold": { + "groups": set(['property', 'temperature']), + "as_property": "temperature", + } +} + +def main(): + knowledge = KnowledgeBase( + knowledge=base_knowledge, + ) + + affirmations = [ + { + 'text': x['affirmation'], + 'parsed': x['parsed'][1], + } + for x in examples + ] + questions = examples + + differences = knowledge.train(affirmations) + differences = knowledge.train(questions) + + for example in examples: + result, _, _ = knowledge.process(example['text']) + + if result != example['answer']: + raise AssertionError('{} is not {}'.format(result, example['answer']))