diff --git a/.DS_Store b/.DS_Store index f35c1a50b..9a874b576 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/.gitignore b/.gitignore index 6f42a17b8..7f61000a2 100644 --- a/.gitignore +++ b/.gitignore @@ -91,7 +91,7 @@ ENV/ # Rope project settings .ropeproject -src/.DS_Store +.DS_Store/ .idea # Pylint output diff --git a/bots/.DS_Store b/bots/.DS_Store index 56581127f..29e32cee3 100644 Binary files a/bots/.DS_Store and b/bots/.DS_Store differ diff --git a/bots/alice2/.DS_Store b/bots/alice2/.DS_Store index 865b25d63..cebe25a59 100644 Binary files a/bots/alice2/.DS_Store and b/bots/alice2/.DS_Store differ diff --git a/bots/rosie_fixed/.DS_Store b/bots/rosie_fixed/.DS_Store new file mode 100644 index 000000000..0b9a44bd2 Binary files /dev/null and b/bots/rosie_fixed/.DS_Store differ diff --git a/bots/rosie_fixed/aiml/default.aiml b/bots/rosie_fixed/aiml/default.aiml index 78a27a25d..75709d72e 100755 --- a/bots/rosie_fixed/aiml/default.aiml +++ b/bots/rosie_fixed/aiml/default.aiml @@ -17263,8 +17263,7 @@
  • What is it to you.
  • I am not sure if I can quantify the answer.
  • -
    - xfind + XFIND @@ -19232,7 +19231,6 @@
  • Try asking another.
  • -
    XFIND @@ -19704,7 +19702,6 @@ as often as possible, do you? -
    @@ -20456,7 +20453,6 @@
  • Doesn't everyone have a
  • . -
    I have
  • a lot of friends on the Internet.
  • @@ -25119,7 +25115,7 @@ SPRINGTIME * - - \ No newline at end of file + diff --git a/src/test/aiml_tests/train_tests/nowaskme.aiml b/src/test/aiml_tests/train_tests/nowaskme.aiml new file mode 100644 index 000000000..2bf1eaccd --- /dev/null +++ b/src/test/aiml_tests/train_tests/nowaskme.aiml @@ -0,0 +1,90 @@ + + + + + + + + + + + + + + + + + + + + + + + + + familiarname + is * + + + + + \ No newline at end of file diff --git a/src/test/aiml_tests/train_tests/test_now_ask_me.py b/src/test/aiml_tests/train_tests/test_now_ask_me.py new file mode 100644 index 000000000..ceda0e52e --- /dev/null +++ b/src/test/aiml_tests/train_tests/test_now_ask_me.py @@ -0,0 +1,26 @@ +import unittest +import os +from test.aiml_tests.client import TestClient +from programy.config import ConfigurationFactory +from programy.config import ClientConfiguration + +class NowAskMeTrainTestClient(TestClient): + + def __init__(self): + TestClient.__init__(self, debug=True) + + def load_configuration(self, arguments): + self.configuration = ClientConfiguration() + ConfigurationFactory.load_configuration_from_file(self.configuration, os.path.dirname(__file__)+"/testconfig.yaml") + +class TrainAIMLTests(unittest.TestCase): + + def setUp(cls): + TrainAIMLTests.test_client = NowAskMeTrainTestClient() + + def test_now_ask_me(self): + TrainAIMLTests.test_client.bot.brain.dump_tree() + response = TrainAIMLTests.test_client.bot.ask_question("test", "daddy is great") + self.assertIsNotNone(response) + #TODO Sort out space in questions + self.assertEqual("Now you can ask me: \" Who IS GREAT \"? and \" What does my DADDY BE \"?", response) diff --git a/src/test/aiml_tests/test_train_aiml.py b/src/test/aiml_tests/train_tests/test_train_aiml.py similarity index 78% rename from src/test/aiml_tests/test_train_aiml.py rename to src/test/aiml_tests/train_tests/test_train_aiml.py index fd0cb0a22..2eb741e81 100644 --- a/src/test/aiml_tests/test_train_aiml.py +++ b/src/test/aiml_tests/train_tests/test_train_aiml.py @@ -7,11 +7,11 @@ class TrainTestClient(TestClient): def __init__(self): - TestClient.__init__(self) + TestClient.__init__(self, debug=True) def load_configuration(self, arguments): self.configuration = ClientConfiguration() - ConfigurationFactory.load_configuration_from_file(self.configuration, os.path.dirname(__file__)+"/test_files/train/testconfig.yaml") + ConfigurationFactory.load_configuration_from_file(self.configuration, os.path.dirname(__file__)+"/testconfig.yaml") class TrainAIMLTests(unittest.TestCase): @@ -19,38 +19,44 @@ def setUp(cls): TrainAIMLTests.test_client = TrainTestClient() def test_train_noun(self): + TrainAIMLTests.test_client.bot.brain.dump_tree() + response = TrainAIMLTests.test_client.bot.ask_question("test", "jessica likes to smoke cigars") self.assertIsNotNone(response) self.assertEqual("Do you smoke cigars too?", response) - TrainAIMLTests.test_client.bot.brain.dump_tree() response = TrainAIMLTests.test_client.bot.ask_question("test", "who likes to smoke cigars") self.assertIsNotNone(response) self.assertEqual("Jessica likes to smoke cigars", response) - TrainAIMLTests.test_client.bot.brain.dump_tree() + response = TrainAIMLTests.test_client.bot.ask_question("test", "who likes to smoke cigars") + self.assertIsNotNone(response) + self.assertEqual("Jessica likes to smoke cigars", response) + response = TrainAIMLTests.test_client.bot.ask_question("test", "what does jessica like") self.assertIsNotNone(response) self.assertEqual("Jessica likes to smoke cigars", response) - TrainAIMLTests.test_client.bot.brain.dump_tree() response = TrainAIMLTests.test_client.bot.ask_question("test", "what does jessica smoke") self.assertIsNotNone(response) self.assertEqual("Jessica likes to smoke cigars", response) - TrainAIMLTests.test_client.bot.brain.dump_tree() response = TrainAIMLTests.test_client.bot.ask_question("test", "who smokes") self.assertIsNotNone(response) self.assertEqual("Jessica likes to smoke cigars", response) def test_train_pronoun(self): + TrainAIMLTests.test_client.bot.brain.dump_tree() + response = TrainAIMLTests.test_client.bot.ask_question("test", "mommy likes to smoke cigars") self.assertIsNotNone(response) - self.assertEqual("Do you smoke cigars too?", response) - TrainAIMLTests.test_client.bot.brain.dump_tree() + #TODO Fix the spacing in quotes + self.assertEqual("Now you can ask me: \" Who LIKES TO SMOKE CIGARS \"? and \" What does my MOMMY LIKE \"?", response) + response = TrainAIMLTests.test_client.bot.ask_question("test", "who likes to smoke cigars") self.assertIsNotNone(response) - self.assertEqual("Mommy likes to smoke cigars", response) \ No newline at end of file + self.assertEqual("Your MOMMY", response) + diff --git a/src/test/aiml_tests/test_files/train/testconfig.yaml b/src/test/aiml_tests/train_tests/testconfig.yaml similarity index 97% rename from src/test/aiml_tests/test_files/train/testconfig.yaml rename to src/test/aiml_tests/train_tests/testconfig.yaml index 0a51a84be..a936316a5 100644 --- a/src/test/aiml_tests/test_files/train/testconfig.yaml +++ b/src/test/aiml_tests/train_tests/testconfig.yaml @@ -9,7 +9,7 @@ brain: files: aiml: - files: /Users/keithsterling/Documents/Development/Python/Projects/AIML/program-y/src/test/aiml_tests/test_files/train + files: /Users/keithsterling/Documents/Development/Python/Projects/AIML/program-y/src/test/aiml_tests/train_tests extension: .aiml directories: false sets: diff --git a/src/test/aiml_tests/underline_star_tests/__init__.py b/src/test/aiml_tests/underline_star_tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/src/test/aiml_tests/test_underline_star_aiml.py b/src/test/aiml_tests/underline_star_tests/test_underline_star_aiml.py similarity index 89% rename from src/test/aiml_tests/test_underline_star_aiml.py rename to src/test/aiml_tests/underline_star_tests/test_underline_star_aiml.py index e1c21c707..66d5830f2 100644 --- a/src/test/aiml_tests/test_underline_star_aiml.py +++ b/src/test/aiml_tests/underline_star_tests/test_underline_star_aiml.py @@ -6,11 +6,11 @@ class BasicTestClient(TestClient): def __init__(self): - TestClient.__init__(self) + TestClient.__init__(self, debug=True) def load_configuration(self, arguments): super(BasicTestClient, self).load_configuration(arguments) - self.configuration.brain_configuration._aiml_files = BrainFileConfiguration(os.path.dirname(__file__)+"/../aiml_tests/test_files/underline_star", ".aiml", False) + self.configuration.brain_configuration._aiml_files = BrainFileConfiguration(os.path.dirname(__file__), ".aiml", False) class UnderlineStarAIMLTests(unittest.TestCase): diff --git a/src/test/aiml_tests/test_files/underline_star/underline_star.aiml b/src/test/aiml_tests/underline_star_tests/underline_star.aiml similarity index 100% rename from src/test/aiml_tests/test_files/underline_star/underline_star.aiml rename to src/test/aiml_tests/underline_star_tests/underline_star.aiml diff --git a/src/test/aiml_tests/underline_tests/__init__.py b/src/test/aiml_tests/underline_tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/src/test/aiml_tests/test_underline_aiml.py b/src/test/aiml_tests/underline_tests/test_underline_aiml.py similarity index 92% rename from src/test/aiml_tests/test_underline_aiml.py rename to src/test/aiml_tests/underline_tests/test_underline_aiml.py index 8f4d383c0..23bbc332d 100644 --- a/src/test/aiml_tests/test_underline_aiml.py +++ b/src/test/aiml_tests/underline_tests/test_underline_aiml.py @@ -6,16 +6,15 @@ class BasicTestClient(TestClient): def __init__(self): - TestClient.__init__(self) + TestClient.__init__(self, debug=True) def load_configuration(self, arguments): super(BasicTestClient, self).load_configuration(arguments) - self.configuration.brain_configuration._aiml_files = BrainFileConfiguration(os.path.dirname(__file__)+"/../aiml_tests/test_files/underline", ".aiml", False) + self.configuration.brain_configuration._aiml_files = BrainFileConfiguration(os.path.dirname(__file__), ".aiml", False) class UnderlineAIMLTests(unittest.TestCase): - @classmethod - def setUpClass(cls): + def setUp(cls): UnderlineAIMLTests.test_client = BasicTestClient() def test_underline_first(self): diff --git a/src/test/aiml_tests/test_files/underline/star.aiml b/src/test/aiml_tests/underline_tests/underline.aiml similarity index 100% rename from src/test/aiml_tests/test_files/underline/star.aiml rename to src/test/aiml_tests/underline_tests/underline.aiml diff --git a/src/test/aiml_tests/util_tests/__init__.py b/src/test/aiml_tests/util_tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/src/test/aiml_tests/test_util.py b/src/test/aiml_tests/util_tests/test_util.py similarity index 54% rename from src/test/aiml_tests/test_util.py rename to src/test/aiml_tests/util_tests/test_util.py index ee8f8b0e8..295bff24a 100644 --- a/src/test/aiml_tests/test_util.py +++ b/src/test/aiml_tests/util_tests/test_util.py @@ -4,21 +4,21 @@ from test.aiml_tests.client import TestClient from programy.config import BrainFileConfiguration -class UtiltiyTestClient(TestClient): +class UtiltyTestClient(TestClient): def __init__(self): TestClient.__init__(self, debug=True) def load_configuration(self, arguments): - super(UtiltiyTestClient, self).load_configuration(arguments) - self.configuration.brain_configuration._aiml_files = BrainFileConfiguration(os.path.dirname(__file__)+"/../aiml_tests/test_files/util", ".aiml", False) + super(UtiltyTestClient, self).load_configuration(arguments) + self.configuration.brain_configuration._aiml_files = BrainFileConfiguration(os.path.dirname(__file__), ".aiml", False) -class UtiltiyAIMLTests(unittest.TestCase): +class UtiltyAIMLTests(unittest.TestCase): def setUp(self): - UtiltiyAIMLTests.test_client = UtiltiyTestClient() + UtiltyAIMLTests.test_client = UtiltyTestClient() def test_util_function(self): - response = UtiltiyAIMLTests.test_client.bot.ask_question("test", "KEITH IS A PROGRAMMER") + response = UtiltyAIMLTests.test_client.bot.ask_question("test", "KEITH IS A PROGRAMMER") self.assertIsNotNone(response) self.assertEqual(response, 'Ok, I will remember KEITH is a PROGRAMMER .') diff --git a/src/test/aiml_tests/test_files/util/util.aiml b/src/test/aiml_tests/util_tests/util.aiml similarity index 91% rename from src/test/aiml_tests/test_files/util/util.aiml rename to src/test/aiml_tests/util_tests/util.aiml index 9516ac31c..e8fd42987 100644 --- a/src/test/aiml_tests/test_files/util/util.aiml +++ b/src/test/aiml_tests/util_tests/util.aiml @@ -84,4 +84,16 @@ For more information see [url] + + ISANAME + name + + + + + + ISANAME * + + + \ No newline at end of file diff --git a/src/test/aiml_tests/vocab_tests/__init__.py b/src/test/aiml_tests/vocab_tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/src/test/aiml_tests/test_files/vocab/info.aiml b/src/test/aiml_tests/vocab_tests/info.aiml similarity index 100% rename from src/test/aiml_tests/test_files/vocab/info.aiml rename to src/test/aiml_tests/vocab_tests/info.aiml diff --git a/src/test/aiml_tests/test_vocab_aiml.py b/src/test/aiml_tests/vocab_tests/test_vocab_aiml.py similarity index 91% rename from src/test/aiml_tests/test_vocab_aiml.py rename to src/test/aiml_tests/vocab_tests/test_vocab_aiml.py index 43cec1148..ee8d27950 100644 --- a/src/test/aiml_tests/test_vocab_aiml.py +++ b/src/test/aiml_tests/vocab_tests/test_vocab_aiml.py @@ -10,7 +10,7 @@ def __init__(self): def load_configuration(self, arguments): super(BasicTestClient, self).load_configuration(arguments) - self.configuration.brain_configuration._aiml_files = BrainFileConfiguration(os.path.dirname(__file__)+"/../aiml_tests/test_files/vocab", ".aiml", False) + self.configuration.brain_configuration._aiml_files = BrainFileConfiguration(os.path.dirname(__file__), ".aiml", False) class VocabularyAIMLTests(unittest.TestCase): diff --git a/src/test/aiml_tests/xplode_tests/__init__.py b/src/test/aiml_tests/xplode_tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/src/test/aiml_tests/test_xplode_aiml.py b/src/test/aiml_tests/xplode_tests/test_xplode_aiml.py similarity index 93% rename from src/test/aiml_tests/test_xplode_aiml.py rename to src/test/aiml_tests/xplode_tests/test_xplode_aiml.py index 9ffeaee84..3b49948d0 100644 --- a/src/test/aiml_tests/test_xplode_aiml.py +++ b/src/test/aiml_tests/xplode_tests/test_xplode_aiml.py @@ -10,7 +10,7 @@ def __init__(self): def load_configuration(self, arguments): super(BasicTestClient, self).load_configuration(arguments) - self.configuration.brain_configuration._aiml_files = BrainFileConfiguration(os.path.dirname(__file__)+"/../aiml_tests/test_files/xplode", ".aiml", False) + self.configuration.brain_configuration._aiml_files = BrainFileConfiguration(os.path.dirname(__file__), ".aiml", False) class XPlodeAIMLTests(unittest.TestCase): diff --git a/src/test/aiml_tests/test_files/xplode/xplode.aiml b/src/test/aiml_tests/xplode_tests/xplode.aiml similarity index 100% rename from src/test/aiml_tests/test_files/xplode/xplode.aiml rename to src/test/aiml_tests/xplode_tests/xplode.aiml diff --git a/src/test/config_tests/__init__.py b/src/test/config_tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/src/test/test_config.py b/src/test/config_tests/test_config_data.py similarity index 60% rename from src/test/test_config.py rename to src/test/config_tests/test_config_data.py index 3ea14c8d1..8588511f6 100644 --- a/src/test/test_config.py +++ b/src/test/config_tests/test_config_data.py @@ -15,315 +15,6 @@ from programy.config import RestClientConfiguration -class ConfigurationFactoryTests(unittest.TestCase): - - def test_guess_format_from_filename(self): - config_format = ConfigurationFactory.guess_format_from_filename("file.yaml") - self.assertEqual(config_format, "yaml") - config_format = ConfigurationFactory.guess_format_from_filename("file.json") - self.assertEqual(config_format, "json") - config_format = ConfigurationFactory.guess_format_from_filename("file.xml") - self.assertEqual(config_format, "xml") - - def test_guess_format_no_extension(self): - with self.assertRaises(Exception): - ConfigurationFactory.guess_format_from_filename("file_yaml") - - def test_get_config_by_name(self): - client_config = ClientConfiguration() - config_type = ConfigurationFactory.get_config_by_name(client_config, "yaml") - self.assertIsNotNone(config_type) - config_type = ConfigurationFactory.get_config_by_name(client_config, "json") - self.assertIsNotNone(config_type) - config_type = ConfigurationFactory.get_config_by_name(client_config, "xml") - self.assertIsNotNone(config_type) - - def test_get_config_by_name_wrong_extension(self): - with self.assertRaises(Exception): - ConfigurationFactory.get_config_by_name("other") - with self.assertRaises(Exception): - ConfigurationFactory.get_config_by_name("") - with self.assertRaises(Exception): - ConfigurationFactory.get_config_by_name(None) - - -class YamlConfigurationFileTests(unittest.TestCase): - - def test_load_from_file(self): - client_config = ClientConfiguration() - yaml = YamlConfigurationFile(client_config) - self.assertIsNotNone(yaml) - yaml.load_from_file(os.path.dirname(__file__)+"/test_yaml.yaml", ",") - self.assertIsNotNone(yaml.yaml_data) - brain = yaml.get_section("brain") - self.assertIsNotNone(brain) - files = yaml.get_section("files", brain) - self.assertIsNotNone(files) - aiml = yaml.get_section("aiml", files) - self.assertIsNotNone(aiml) - - files = yaml.get_section("files", aiml) - self.assertIsNotNone(files) - self.assertEqual(files, "/aiml") - extension = yaml.get_section("extension", aiml) - self.assertIsNotNone(extension) - self.assertEqual(extension, ".aiml") - directories = yaml.get_section("directories", aiml) - self.assertIsNotNone(directories) - self.assertEqual(directories, True) - - def test_load_from_text(self): - client_config = ClientConfiguration() - yaml = YamlConfigurationFile(client_config) - self.assertIsNotNone(yaml) - yaml.load_from_text(""" - brain: - supress_warnings: true - allow_system_aiml: true - allow_learn_aiml: true - allow_learnf_aiml: true - - files: - aiml: - files: /aiml - extension: .aiml - directories: true - sets: - files: /sets - extension: .txt - directories: false - maps: - files: /maps - extension: .txt - directories: true - denormal: denormal.txt - normal: normal.txt - gender: gender.txt - person: person.txt - person2: person2.txt - predicates: predicates.txt - pronouns: pronouns.txt - properties: properties.txt - triples: triples.txt - preprocessors: preprocessors.txt - postprocessors: postprocessors.txt - - services: - REST: - path: programy.utils.services.rest.GenericRESTService - Pannous: - path: programy.utils.services.pannous.PannousService - Pandora: - path: programy.utils.services.pandora.PandoraService - Wikipedia: - path: programy.utils.services.wikipedia.WikipediaService - - bot: - prompt: ">>>" - default_response: Sorry, I don't have an answer for that! - exit_response: So long, and thanks for the fish! - initial_question: Hi, how can I help you? - """, ".") - - self.assertIsNotNone(yaml.yaml_data) - brain = yaml.get_section("brain") - self.assertIsNotNone(brain) - files = yaml.get_section("files", brain) - self.assertIsNotNone(files) - aiml = yaml.get_section("aiml", files) - self.assertIsNotNone(aiml) - - files = yaml.get_section("files", aiml) - self.assertIsNotNone(files) - self.assertEqual(files, "/aiml") - extension = yaml.get_section("extension", aiml) - self.assertIsNotNone(extension) - self.assertEqual(extension, ".aiml") - directories = yaml.get_section("directories", aiml) - self.assertIsNotNone(directories) - self.assertEqual(directories, True) - - -class JsonConfigurationFileTests(unittest.TestCase): - - def test_load_from_file(self): - client_config = ClientConfiguration() - json = JSONConfigurationFile(client_config) - self.assertIsNotNone(json) - json.load_from_file(os.path.dirname(__file__)+"/test_json.json", ",") - self.assertIsNotNone(json.json_data) - brain = json.get_section("brain") - self.assertIsNotNone(brain) - files = json.get_section("files", brain) - self.assertIsNotNone(files) - aiml = json.get_section("aiml", files) - self.assertIsNotNone(aiml) - - files = json.get_section("files", aiml) - self.assertIsNotNone(files) - self.assertEqual(files, "/aiml") - extension = json.get_section("extension", aiml) - self.assertIsNotNone(extension) - self.assertEqual(extension, ".aiml") - directories = json.get_section("directories", aiml) - self.assertIsNotNone(directories) - self.assertEqual(directories, True) - - def test_load_from_text(self): - client_config = ClientConfiguration() - json = JSONConfigurationFile(client_config) - self.assertIsNotNone(json) - json.load_from_text(""" -{ - "brain": { - "supress_warnings": false, - "allow_system_aiml": true, - "allow_learn_aiml": true, - "allow_learnf_aiml": true, - - "files": { - "aiml": { - "files": "/aiml", - "extension": ".aiml", - "directories": true - }, - "sets": { - "files": "/sets", - "extension": ".txt", - "directories": false - }, - "maps": { - "files": "/maps", - "extension": ".txt", - "directories": true - }, - "denormal": "denormal.txt", - "normal": "normal.txt", - "gender": "gender.txt", - "person": "person.txt", - "person2": "person2.txt", - "predicates": "predicates.txt", - "pronouns": "pronouns.txt", - "properties": "properties.txt", - "triples": "triples.txt", - "preprocessors": "preprocessors.txt", - "postprocessors": "postprocessors.txt" - }, - - "services": { - "REST": { - "path": "programy.utils.services.rest.GenericRESTService" - }, - "Pannous": { - "path": "programy.utils.services.pannous.PannousService" - }, - "Pandora": { - "path": "programy.utils.services.pandora.PandoraService" - }, - "Wikipedia": { - "path": "programy.utils.services.wikipedia.WikipediaService" - } - } - }, - "bot": { - "prompt": ">>>", - "default_response": "Sorry, I don't have an answer for that!", - "exit_response": "So long, and thanks for the fish!", - "initial_question": "Hi, how can I help you>" - } -}""", ",") - - -class XMLConfigurationFileTests(unittest.TestCase): - - def test_load_from_file(self): - client_config = ClientConfiguration() - xml = XMLConfigurationFile(client_config) - self.assertIsNotNone(xml) - xml.load_from_file(os.path.dirname(__file__)+"/test_xml.xml", ",") - self.assertIsNotNone(xml.xml_data) - brain = xml.get_section("brain") - self.assertIsNotNone(brain) - files = xml.get_section("files", brain) - self.assertIsNotNone(files) - aiml = xml.get_section("aiml", files) - self.assertIsNotNone(aiml) - - files = xml.get_section("files", aiml) - self.assertIsNotNone(files) - self.assertEqual(files.text, "/aiml") - extension = xml.get_section("extension", aiml) - self.assertIsNotNone(extension) - self.assertEqual(extension.text, ".aiml") - directories = xml.get_section("directories", aiml) - self.assertIsNotNone(directories) - self.assertEqual(directories.text, "True") - - def test_load_from_text(self): - client_config = ClientConfiguration() - xml = XMLConfigurationFile(client_config) - self.assertIsNotNone(xml) - xml.load_from_text(""" - - - True - True - True - True - - - - /aiml - .aiml - True - - - /sets - .txt - False - - - /maps - .txt - True - - denormal.txt - normal.txt - gender.txt - person.txt - person2.txt - predicates.txt - pronouns.txt - properties.txt - triples.txt - preprocessors.txt - postprocessors.txt - - - - programy.utils.services.rest.GenericRESTService - - - programy.utils.services.pannous.PannousService - - - programy.utils.services.pandora.PandoraService - - - programy.utils.services.wikipedia.WikipediaService - - - - - >>> - Sorry, I don't have an answer for that! - So long, and thanks for the fish! - Hi, how can I help you? - - -""", ",") - - class LoadConfigurationDataTests(unittest.TestCase): def test_load_config_data_yaml(self): diff --git a/src/test/config_tests/test_factory.py b/src/test/config_tests/test_factory.py new file mode 100644 index 000000000..3ec0bd763 --- /dev/null +++ b/src/test/config_tests/test_factory.py @@ -0,0 +1,36 @@ +import unittest + +from programy.config import ConfigurationFactory +from programy.config import ClientConfiguration + + +class ConfigurationFactoryTests(unittest.TestCase): + + def test_guess_format_from_filename(self): + config_format = ConfigurationFactory.guess_format_from_filename("file.yaml") + self.assertEqual(config_format, "yaml") + config_format = ConfigurationFactory.guess_format_from_filename("file.json") + self.assertEqual(config_format, "json") + config_format = ConfigurationFactory.guess_format_from_filename("file.xml") + self.assertEqual(config_format, "xml") + + def test_guess_format_no_extension(self): + with self.assertRaises(Exception): + ConfigurationFactory.guess_format_from_filename("file_yaml") + + def test_get_config_by_name(self): + client_config = ClientConfiguration() + config_type = ConfigurationFactory.get_config_by_name(client_config, "yaml") + self.assertIsNotNone(config_type) + config_type = ConfigurationFactory.get_config_by_name(client_config, "json") + self.assertIsNotNone(config_type) + config_type = ConfigurationFactory.get_config_by_name(client_config, "xml") + self.assertIsNotNone(config_type) + + def test_get_config_by_name_wrong_extension(self): + with self.assertRaises(Exception): + ConfigurationFactory.get_config_by_name("other") + with self.assertRaises(Exception): + ConfigurationFactory.get_config_by_name("") + with self.assertRaises(Exception): + ConfigurationFactory.get_config_by_name(None) diff --git a/src/test/test_json.json b/src/test/config_tests/test_json.json similarity index 100% rename from src/test/test_json.json rename to src/test/config_tests/test_json.json diff --git a/src/test/config_tests/test_json.py b/src/test/config_tests/test_json.py new file mode 100644 index 000000000..b756f823f --- /dev/null +++ b/src/test/config_tests/test_json.py @@ -0,0 +1,96 @@ +import unittest +import os + +from programy.config import JSONConfigurationFile +from programy.config import ClientConfiguration + + +class JsonConfigurationFileTests(unittest.TestCase): + + def test_load_from_file(self): + client_config = ClientConfiguration() + json = JSONConfigurationFile(client_config) + self.assertIsNotNone(json) + json.load_from_file(os.path.dirname(__file__)+"/test_json.json", ",") + self.assertIsNotNone(json.json_data) + brain = json.get_section("brain") + self.assertIsNotNone(brain) + files = json.get_section("files", brain) + self.assertIsNotNone(files) + aiml = json.get_section("aiml", files) + self.assertIsNotNone(aiml) + + files = json.get_section("files", aiml) + self.assertIsNotNone(files) + self.assertEqual(files, "/aiml") + extension = json.get_section("extension", aiml) + self.assertIsNotNone(extension) + self.assertEqual(extension, ".aiml") + directories = json.get_section("directories", aiml) + self.assertIsNotNone(directories) + self.assertEqual(directories, True) + + def test_load_from_text(self): + client_config = ClientConfiguration() + json = JSONConfigurationFile(client_config) + self.assertIsNotNone(json) + json.load_from_text(""" +{ + "brain": { + "supress_warnings": false, + "allow_system_aiml": true, + "allow_learn_aiml": true, + "allow_learnf_aiml": true, + + "files": { + "aiml": { + "files": "/aiml", + "extension": ".aiml", + "directories": true + }, + "sets": { + "files": "/sets", + "extension": ".txt", + "directories": false + }, + "maps": { + "files": "/maps", + "extension": ".txt", + "directories": true + }, + "denormal": "denormal.txt", + "normal": "normal.txt", + "gender": "gender.txt", + "person": "person.txt", + "person2": "person2.txt", + "predicates": "predicates.txt", + "pronouns": "pronouns.txt", + "properties": "properties.txt", + "triples": "triples.txt", + "preprocessors": "preprocessors.txt", + "postprocessors": "postprocessors.txt" + }, + + "services": { + "REST": { + "path": "programy.utils.services.rest.GenericRESTService" + }, + "Pannous": { + "path": "programy.utils.services.pannous.PannousService" + }, + "Pandora": { + "path": "programy.utils.services.pandora.PandoraService" + }, + "Wikipedia": { + "path": "programy.utils.services.wikipedia.WikipediaService" + } + } + }, + "bot": { + "prompt": ">>>", + "default_response": "Sorry, I don't have an answer for that!", + "exit_response": "So long, and thanks for the fish!", + "initial_question": "Hi, how can I help you>" + } +}""", ",") + diff --git a/src/test/config_tests/test_xml.py b/src/test/config_tests/test_xml.py new file mode 100644 index 000000000..4457c97dc --- /dev/null +++ b/src/test/config_tests/test_xml.py @@ -0,0 +1,96 @@ +import unittest +import os + +from programy.config import XMLConfigurationFile +from programy.config import ClientConfiguration + + +class XMLConfigurationFileTests(unittest.TestCase): + + def test_load_from_file(self): + client_config = ClientConfiguration() + xml = XMLConfigurationFile(client_config) + self.assertIsNotNone(xml) + xml.load_from_file(os.path.dirname(__file__)+"/test_xml.xml", ",") + self.assertIsNotNone(xml.xml_data) + brain = xml.get_section("brain") + self.assertIsNotNone(brain) + files = xml.get_section("files", brain) + self.assertIsNotNone(files) + aiml = xml.get_section("aiml", files) + self.assertIsNotNone(aiml) + + files = xml.get_section("files", aiml) + self.assertIsNotNone(files) + self.assertEqual(files.text, "/aiml") + extension = xml.get_section("extension", aiml) + self.assertIsNotNone(extension) + self.assertEqual(extension.text, ".aiml") + directories = xml.get_section("directories", aiml) + self.assertIsNotNone(directories) + self.assertEqual(directories.text, "True") + + def test_load_from_text(self): + client_config = ClientConfiguration() + xml = XMLConfigurationFile(client_config) + self.assertIsNotNone(xml) + xml.load_from_text(""" + + + True + True + True + True + + + + /aiml + .aiml + True + + + /sets + .txt + False + + + /maps + .txt + True + + denormal.txt + normal.txt + gender.txt + person.txt + person2.txt + predicates.txt + pronouns.txt + properties.txt + triples.txt + preprocessors.txt + postprocessors.txt + + + + programy.utils.services.rest.GenericRESTService + + + programy.utils.services.pannous.PannousService + + + programy.utils.services.pandora.PandoraService + + + programy.utils.services.wikipedia.WikipediaService + + + + + >>> + Sorry, I don't have an answer for that! + So long, and thanks for the fish! + Hi, how can I help you? + + +""", ",") + diff --git a/src/test/test_xml.xml b/src/test/config_tests/test_xml.xml similarity index 100% rename from src/test/test_xml.xml rename to src/test/config_tests/test_xml.xml diff --git a/src/test/config_tests/test_yaml.py b/src/test/config_tests/test_yaml.py new file mode 100644 index 000000000..c2e324d32 --- /dev/null +++ b/src/test/config_tests/test_yaml.py @@ -0,0 +1,103 @@ +import unittest +import os + +from programy.config import YamlConfigurationFile +from programy.config import ClientConfiguration + + +class YamlConfigurationFileTests(unittest.TestCase): + + def test_load_from_file(self): + client_config = ClientConfiguration() + yaml = YamlConfigurationFile(client_config) + self.assertIsNotNone(yaml) + yaml.load_from_file(os.path.dirname(__file__)+"/test_yaml.yaml", ",") + self.assertIsNotNone(yaml.yaml_data) + brain = yaml.get_section("brain") + self.assertIsNotNone(brain) + files = yaml.get_section("files", brain) + self.assertIsNotNone(files) + aiml = yaml.get_section("aiml", files) + self.assertIsNotNone(aiml) + + files = yaml.get_section("files", aiml) + self.assertIsNotNone(files) + self.assertEqual(files, "/aiml") + extension = yaml.get_section("extension", aiml) + self.assertIsNotNone(extension) + self.assertEqual(extension, ".aiml") + directories = yaml.get_section("directories", aiml) + self.assertIsNotNone(directories) + self.assertEqual(directories, True) + + def test_load_from_text(self): + client_config = ClientConfiguration() + yaml = YamlConfigurationFile(client_config) + self.assertIsNotNone(yaml) + yaml.load_from_text(""" + brain: + supress_warnings: true + allow_system_aiml: true + allow_learn_aiml: true + allow_learnf_aiml: true + + files: + aiml: + files: /aiml + extension: .aiml + directories: true + sets: + files: /sets + extension: .txt + directories: false + maps: + files: /maps + extension: .txt + directories: true + denormal: denormal.txt + normal: normal.txt + gender: gender.txt + person: person.txt + person2: person2.txt + predicates: predicates.txt + pronouns: pronouns.txt + properties: properties.txt + triples: triples.txt + preprocessors: preprocessors.txt + postprocessors: postprocessors.txt + + services: + REST: + path: programy.utils.services.rest.GenericRESTService + Pannous: + path: programy.utils.services.pannous.PannousService + Pandora: + path: programy.utils.services.pandora.PandoraService + Wikipedia: + path: programy.utils.services.wikipedia.WikipediaService + + bot: + prompt: ">>>" + default_response: Sorry, I don't have an answer for that! + exit_response: So long, and thanks for the fish! + initial_question: Hi, how can I help you? + """, ".") + + self.assertIsNotNone(yaml.yaml_data) + brain = yaml.get_section("brain") + self.assertIsNotNone(brain) + files = yaml.get_section("files", brain) + self.assertIsNotNone(files) + aiml = yaml.get_section("aiml", files) + self.assertIsNotNone(aiml) + + files = yaml.get_section("files", aiml) + self.assertIsNotNone(files) + self.assertEqual(files, "/aiml") + extension = yaml.get_section("extension", aiml) + self.assertIsNotNone(extension) + self.assertEqual(extension, ".aiml") + directories = yaml.get_section("directories", aiml) + self.assertIsNotNone(directories) + self.assertEqual(directories, True) + diff --git a/src/test/test_yaml.yaml b/src/test/config_tests/test_yaml.yaml similarity index 100% rename from src/test/test_yaml.yaml rename to src/test/config_tests/test_yaml.yaml diff --git a/src/test/parser/pattern/test_graph.py b/src/test/parser/pattern/test_graph.py index d00e9c83d..7b8e855be 100644 --- a/src/test/parser/pattern/test_graph.py +++ b/src/test/parser/pattern/test_graph.py @@ -1,12 +1,9 @@ -import xml.etree.ElementTree as ET - +from test.parser.pattern.test_nodes.base import PatternTestBaseClass +from programy.dialog import Sentence from programy.parser.pattern.graph import PatternGraph -from programy.parser.pattern.matcher import PatternMatcher from programy.parser.pattern.nodes import * from programy.parser.template.nodes import * -from programy.dialog import Sentence -from test.parser.pattern.base import PatternTestBaseClass -from programy.parser.aiml_parser import AIMLParser + class PatternGraphTests(PatternTestBaseClass): @@ -15,6 +12,41 @@ def test_init_graph(self): self.assertIsNotNone(graph) self.assertIsNotNone(graph.root) + def test_add_pattern_with_whitepsace(self): + graph = PatternGraph() + topic_element = ET.fromstring('*') + that_element = ET.fromstring('*') + template_graph_root = None + + element = ET.fromstring("\nthis\n that\n the other") + graph.add_pattern_to_graph(element, topic_element, that_element, template_graph_root) + + root = graph.root + self.assertIsNotNone(root) + self.assertIsNotNone(root.children) + self.assertEqual(len(root.children), 1) + self.assertIsInstance(root.children[0], PatternWordNode) + + this = root.children[0] + self.assertIsNotNone(this) + self.assertEqual(this.word, "this") + self.assertEqual(len(this.children), 1) + + that = this.children[0] + self.assertIsNotNone(that) + self.assertEqual(that.word, "that") + self.assertEqual(len(that.children), 1) + + the = that.children[0] + self.assertIsNotNone(the) + self.assertEqual(the.word, "the") + self.assertEqual(len(the.children), 1) + + other = the.children[0] + self.assertIsNotNone(other) + self.assertEqual(other.word, "other") + self.assertEqual(len(other.children), 0) + def test_add_pattern_to_graph_basic(self): graph = PatternGraph() topic_element = ET.fromstring('*') @@ -41,12 +73,6 @@ def test_add_pattern_to_graph_basic(self): self.assertIsInstance(graph.root.children[0], PatternWordNode) self.assertEqual(graph.root.children[1].word, "test2") - matcher = PatternMatcher(graph) - self.assertIsNotNone(matcher.match(self.bot, self.clientid, Sentence("test1"), [], Sentence("*"), [], Sentence("*"), [])) - self.assertIsNotNone(matcher.match(self.bot, self.clientid, Sentence("test2"), [], Sentence("*"), [], Sentence("*"), [])) - self.assertIsNone(matcher.match(self.bot, self.clientid, Sentence("test1 test2"), [], Sentence("*"), [], Sentence("*"), [])) - self.assertIsNone(matcher.match(self.bot, self.clientid, Sentence("test2 test1"), [], Sentence("*"), [], Sentence("*"), [])) - def test_add_pattern_to_graph_multi_word(self): graph = PatternGraph() topic_element = ET.fromstring('*') @@ -85,12 +111,6 @@ def test_add_pattern_to_graph_multi_word(self): self.assertIsInstance(graph.root.children[0].children[1].children[0], PatternWordNode) self.assertEqual(graph.root.children[0].children[1].children[0].word, "test4") - matcher = PatternMatcher(graph) - self.assertIsNone(matcher.match(self.bot, self.clientid, Sentence("test1"), [], Sentence("*"), [], Sentence("*"), [])) - self.assertIsNotNone(matcher.match(self.bot, self.clientid, Sentence("test1 test2"), [], Sentence("*"), [], Sentence("*"), [])) - self.assertIsNone(matcher.match(self.bot, self.clientid, Sentence("test1 test3"), [], Sentence("*"), [], Sentence("*"), [])) - self.assertIsNotNone(matcher.match(self.bot, self.clientid, Sentence("test1 test3 test4"), [], Sentence("*"), [], Sentence("*"), [])) - def test_add_pattern_to_graph_basic_set_text(self): graph = PatternGraph() topic_element = ET.fromstring('*') @@ -108,12 +128,6 @@ def test_add_pattern_to_graph_basic_set_text(self): self.assertIsInstance(graph.root.children[0], PatternSetNode) self.assertEqual(graph.root.children[0].word, "SET1") - matcher = PatternMatcher(graph) - self.assertIsNotNone(matcher.match(self.bot, self.clientid, Sentence("VAL1 IS A VALUE"), [], Sentence("*"), [], Sentence("*"), [])) - self.assertIsNotNone(matcher.match(self.bot, self.clientid, Sentence("VAL2 IS A VALUE"), [], Sentence("*"), [], Sentence("*"), [])) - self.assertIsNotNone(matcher.match(self.bot, self.clientid, Sentence("VAL3 IS A VALUE"), [], Sentence("*"), [], Sentence("*"), [])) - self.assertIsNone(matcher.match(self.bot, self.clientid, Sentence("VAL4 IS A VALUE"), [], Sentence("*"), [], Sentence("*"), [])) - def test_add_pattern_to_graph_basic_set_name_attrib(self): graph = PatternGraph() topic_element = ET.fromstring('*') @@ -131,12 +145,6 @@ def test_add_pattern_to_graph_basic_set_name_attrib(self): self.assertIsInstance(graph.root.children[0], PatternSetNode) self.assertEqual(graph.root.children[0].word, "SET1") - matcher = PatternMatcher(graph) - self.assertIsNotNone(matcher.match(self.bot, self.clientid, Sentence("val1"), [], Sentence("*"), [], Sentence("*"), [])) - self.assertIsNotNone(matcher.match(self.bot, self.clientid, Sentence("val2"), [], Sentence("*"), [], Sentence("*"), [])) - self.assertIsNotNone(matcher.match(self.bot, self.clientid, Sentence("val3"), [], Sentence("*"), [], Sentence("*"), [])) - self.assertIsNone(matcher.match(self.bot, self.clientid, Sentence("val4"), [], Sentence("*"), [], Sentence("*"), [])) - def test_add_pattern_to_graph_basic_bot_text(self): graph = PatternGraph() topic_element = ET.fromstring('*') @@ -154,10 +162,6 @@ def test_add_pattern_to_graph_basic_bot_text(self): self.assertIsInstance(graph.root.children[0], PatternBotNode) self.assertEqual(graph.root.children[0].word, "bot1") - matcher = PatternMatcher(graph) - self.assertIsNotNone(matcher.match(self.bot, self.clientid, Sentence("val1"), [], Sentence("*"), [], Sentence("*"), [])) - self.assertIsNone(matcher.match(self.bot, self.clientid, Sentence("val2"), [], Sentence("*"), [], Sentence("*"), [])) - def test_add_pattern_to_graph_basic_bot_name_attrib(self): graph = PatternGraph() topic_element = ET.fromstring('*') @@ -175,10 +179,6 @@ def test_add_pattern_to_graph_basic_bot_name_attrib(self): self.assertIsInstance(graph.root.children[0], PatternBotNode) self.assertEqual(graph.root.children[0].word, "bot1") - matcher = PatternMatcher(graph) - self.assertIsNotNone(matcher.match(self.bot, self.clientid, Sentence("val1"), [], Sentence("*"), [], Sentence("*"), [])) - self.assertIsNone(matcher.match(self.bot, self.clientid, Sentence("val2"), [], Sentence("*"), [], Sentence("*"), [])) - def test_add_pattern_to_graph_word_set_bot(self): graph = PatternGraph() topic_element = ET.fromstring('*') @@ -225,10 +225,6 @@ def test_add_pattern_to_graph_word_set_bot(self): self.assertEqual(graph.root.children[0].children[0].children[0].children[0].children[0].children[0].word, "test6") - matcher = PatternMatcher(graph) - self.assertIsNotNone(matcher.match(self.bot, self.clientid, Sentence("test1 test2 val1 test4 val1 test6"), [], - Sentence("*"), [], Sentence("*"), [])) - def test_add_pattern_to_graph_basic_zero_or_more(self): graph = PatternGraph() topic_element = ET.fromstring('*') @@ -238,12 +234,6 @@ def test_add_pattern_to_graph_basic_zero_or_more(self): element = ET.fromstring('#') graph.add_pattern_to_graph(element, topic_element, that_element, template_graph_root) - matcher = PatternMatcher(graph) - self.assertIsNotNone(matcher.match(self.bot, self.clientid, Sentence(""), [], Sentence("*"), [], Sentence("*"), [])) - self.assertIsNotNone(matcher.match(self.bot, self.clientid, Sentence("test1"), [], Sentence("*"), [], Sentence("*"), [])) - self.assertIsNotNone(matcher.match(self.bot, self.clientid, Sentence("test1 test2"), [], Sentence("*"), [], Sentence("*"), [])) - self.assertIsNotNone(matcher.match(self.bot, self.clientid, Sentence("test1 test2 test3"), [], Sentence("*"), [], Sentence("*"), [])) - def test_add_pattern_to_graph_zero_or_more_front(self): graph = PatternGraph() topic_element = ET.fromstring('*') @@ -253,10 +243,6 @@ def test_add_pattern_to_graph_zero_or_more_front(self): element = ET.fromstring('# XXX') graph.add_pattern_to_graph(element, topic_element, that_element, template_graph_root) - matcher = PatternMatcher(graph) - self.assertIsNone(matcher.match(self.bot, self.clientid, Sentence(""), [], Sentence("*"), [], Sentence("*"), [])) - self.assertIsNotNone(matcher.match(self.bot, self.clientid, Sentence("Hello XXX"), [], Sentence("*"), [], Sentence("*"), [])) - def test_add_pattern_to_graph_zero_or_more_middle(self): graph = PatternGraph() topic_element = ET.fromstring('*') @@ -266,10 +252,6 @@ def test_add_pattern_to_graph_zero_or_more_middle(self): element = ET.fromstring('XXX # YYY') graph.add_pattern_to_graph(element, topic_element, that_element, template_graph_root) - matcher = PatternMatcher(graph) - self.assertIsNone(matcher.match(self.bot, self.clientid, Sentence(""), [], Sentence("*"), [], Sentence("*"), [])) - self.assertIsNotNone(matcher.match(self.bot, self.clientid, Sentence("XXX Hello YYY"), [], Sentence("*"), [], Sentence("*"), [])) - def test_add_pattern_to_graph_zero_or_more_last(self): graph = PatternGraph() topic_element = ET.fromstring('*') @@ -279,10 +261,6 @@ def test_add_pattern_to_graph_zero_or_more_last(self): element = ET.fromstring('XXX #') graph.add_pattern_to_graph(element, topic_element, that_element, template_graph_root) - matcher = PatternMatcher(graph) - self.assertIsNone(matcher.match(self.bot, self.clientid, Sentence(""), [], Sentence("*"), [], Sentence("*"), [])) - self.assertIsNotNone(matcher.match(self.bot, self.clientid, Sentence("XXX Hello"), [], Sentence("*"), [], Sentence("*"), [])) - def test_add_pattern_to_graph_zero_or_more_multiple(self): graph = PatternGraph() topic_element = ET.fromstring('*') @@ -292,10 +270,6 @@ def test_add_pattern_to_graph_zero_or_more_multiple(self): element = ET.fromstring('XXX # YYY # ZZZ') graph.add_pattern_to_graph(element, topic_element, that_element, template_graph_root) - matcher = PatternMatcher(graph) - self.assertIsNone(matcher.match(self.bot, self.clientid, Sentence(""), [], Sentence("*"), [], Sentence("*"), [])) - self.assertIsNotNone(matcher.match(self.bot, self.clientid, Sentence("XXX Hello YYY There ZZZ"), [], Sentence("*"), [], Sentence("*"), [])) - def test_add_pattern_to_graph_basic_zero_or_more_with_patterns(self): graph = PatternGraph() topic_element = ET.fromstring('*') @@ -309,20 +283,6 @@ def test_add_pattern_to_graph_basic_zero_or_more_with_patterns(self): template_graph_root = TemplateWordNode("RESPONSE2") graph.add_pattern_to_graph(element, topic_element, that_element, template_graph_root) - matcher = PatternMatcher(graph) - - self.assertIsNone(matcher.match(self.bot, self.clientid, Sentence(""), [], Sentence("*"), [], Sentence("*"), [])) - - template = matcher.match(self.bot, self.clientid, Sentence("HELLO"), [], Sentence("*"), [], Sentence("*"), []) - self.assertIsNotNone(template) - self.assertIsInstance(template, PatternTemplateNode) - self.assertEqual(template.template.resolve(None, None), "RESPONSE1") - - template = matcher.match(self.bot, self.clientid, Sentence("WELL HI THERE"), [], Sentence("*"), [], Sentence("*"), []) - self.assertIsNotNone(template) - self.assertIsInstance(template, PatternTemplateNode) - self.assertEqual(template.template.resolve(None, None), "RESPONSE2") - def test_add_pattern_to_graph_basic_one_or_more(self): graph = PatternGraph() topic_element = ET.fromstring('*') @@ -342,12 +302,6 @@ def test_add_pattern_to_graph_basic_one_or_more(self): self.assertIsNone(graph.root._0ormore_hash) self.assertIsNone(graph.root._1ormore_underline) - matcher = PatternMatcher(graph) - self.assertIsNone(matcher.match(self.bot, self.clientid, Sentence(""), [], Sentence("*"), [], Sentence("*"), [])) - self.assertIsNotNone(matcher.match(self.bot, self.clientid, Sentence("test1"), [], Sentence("*"), [], Sentence("*"), [])) - self.assertIsNotNone(matcher.match(self.bot, self.clientid, Sentence("test1 test2"), [], Sentence("*"), [], Sentence("*"), [])) - self.assertIsNotNone(matcher.match(self.bot, self.clientid, Sentence("test1 test2 test3"), [], Sentence("*"), [], Sentence("*"), [])) - def test_add_pattern_to_graph_one_or_more_front(self): graph = PatternGraph() topic_element = ET.fromstring('*') @@ -357,11 +311,6 @@ def test_add_pattern_to_graph_one_or_more_front(self): element = ET.fromstring('* XXX') graph.add_pattern_to_graph(element, topic_element, that_element, template_graph_root) - matcher = PatternMatcher(graph) - self.assertIsNone(matcher.match(self.bot, self.clientid, Sentence(""), [], Sentence("*"), [], Sentence("*"), [])) - self.assertIsNotNone(matcher.match(self.bot, self.clientid, Sentence("HELLO XXX"), [], Sentence("*"), [], Sentence("*"), [])) - self.assertIsNotNone(matcher.match(self.bot, self.clientid, Sentence("HELLO THERE XXX"), [], Sentence("*"), [], Sentence("*"), [])) - def test_add_pattern_to_graph_one_or_more_middle(self): graph = PatternGraph() topic_element = ET.fromstring('*') @@ -371,10 +320,6 @@ def test_add_pattern_to_graph_one_or_more_middle(self): element = ET.fromstring('XXX * YYY') graph.add_pattern_to_graph(element, topic_element, that_element, template_graph_root) - matcher = PatternMatcher(graph) - self.assertIsNone(matcher.match(self.bot, self.clientid, Sentence(""), [], Sentence("*"), [], Sentence("*"), [])) - self.assertIsNotNone(matcher.match(self.bot, self.clientid, Sentence("XXX HELLO THERE YYY"), [], Sentence("*"), [], Sentence("*"), [])) - def test_add_pattern_to_graph_one_or_more_last(self): graph = PatternGraph() topic_element = ET.fromstring('*') @@ -384,10 +329,6 @@ def test_add_pattern_to_graph_one_or_more_last(self): element = ET.fromstring('XXX *') graph.add_pattern_to_graph(element, topic_element, that_element, template_graph_root) - matcher = PatternMatcher(graph) - self.assertIsNone(matcher.match(self.bot, self.clientid, Sentence(""), [], Sentence("*"), [], Sentence("*"), [])) - self.assertIsNotNone(matcher.match(self.bot, self.clientid, Sentence("XXX HELLO THERE YYY"), [], Sentence("*"), [], Sentence("*"), [])) - ################################################################################################################## # @@ -405,7 +346,7 @@ def test_add_topic_to_node_star(self): self.assertIsNotNone(base_node.topic) self.assertIsInstance(base_node.topic, PatternTopicNode) - self.assertTrue(base_node.topic.has_children()) + self.assertFalse(base_node.topic.has_children()) self.assertTrue(base_node.topic.has_wildcard()) self.assertIsNotNone(base_node.topic.star) self.assertEqual(base_node.topic.star.wildcard, "*") @@ -475,7 +416,7 @@ def test_add_that_to_node_star(self): self.assertIsNotNone(base_node.that) self.assertIsInstance(base_node.that, PatternThatNode) - self.assertTrue(base_node.that.has_children()) + self.assertFalse(base_node.that.has_children()) self.assertTrue(base_node.that.has_wildcard()) self.assertIsNotNone(base_node.that.star) self.assertEqual(base_node.that.star.wildcard, "*") diff --git a/src/test/parser/pattern/test_matcher.py b/src/test/parser/pattern/test_matcher.py deleted file mode 100644 index c8e0bc73d..000000000 --- a/src/test/parser/pattern/test_matcher.py +++ /dev/null @@ -1,840 +0,0 @@ -import logging -import xml.etree.ElementTree as ET - -from programy.dialog import Sentence -from programy.parser.pattern.nodes import * -from programy.parser.pattern.graph import PatternGraph -from programy.parser.pattern.matcher import PatternMatcher -from programy.parser.template.nodes import TemplateNode - -from test.parser.pattern.base import PatternTestBaseClass - - -class PatternMatcherTests(PatternTestBaseClass): - - @classmethod - def setUpClass(cls): - PatternMatcherTests.bot = None - PatternMatcherTests.clientid = "testid" - - def test_simple_sequence(self): - - graph = PatternGraph() - - pattern_element = ET.fromstring("A B C") - topic_element = ET.fromstring("*") - that_element = ET.fromstring("*") - template_node = TemplateNode () - - graph.add_pattern_to_graph(pattern_element, topic_element, that_element, template_node) - - matcher = PatternMatcher(graph) - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A B"), pattern_stars, - Sentence("*"), topic_stars, - Sentence("*"), that_stars) - self.assertIsNone(result) - self.assertEqual(len(pattern_stars), 0) - self.assertEqual(len(topic_stars), 0) - self.assertEqual(len(that_stars), 0) - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A B C D"), pattern_stars, - Sentence("*"), topic_stars, - Sentence("*"), that_stars) - self.assertIsNone(result) - self.assertEqual(len(pattern_stars), 0) - self.assertEqual(len(topic_stars), 0) - self.assertEqual(len(that_stars), 0) - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A B C"), pattern_stars, - Sentence("*"), topic_stars, - Sentence("*"), that_stars) - self.assertIsNotNone(result) - self.assertIsInstance(result, PatternTemplateNode) - self.assertEqual(len(pattern_stars), 0) - self.assertEqual(len(topic_stars), 0) - self.assertEqual(len(that_stars), 0) - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A B C"), pattern_stars, - Sentence("X X"), topic_stars, - Sentence("*"), that_stars) - self.assertIsNotNone(result) - self.assertIsInstance(result, PatternTemplateNode) - self.assertEqual(len(pattern_stars), 0) - self.assertEqual(len(topic_stars), 1) - self.assertEqual(topic_stars[0], "X X") - self.assertEqual(len(that_stars), 0) - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A B C"), pattern_stars, - Sentence("*"), topic_stars, - Sentence("Y Y"), that_stars) - self.assertIsNotNone(result) - self.assertIsInstance(result, PatternTemplateNode) - self.assertEqual(len(pattern_stars), 0) - self.assertEqual(len(topic_stars), 0) - self.assertEqual(len(that_stars), 1) - self.assertEqual(that_stars[0], "Y Y") - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A B C"), pattern_stars, - Sentence("X X"), topic_stars, - Sentence("Y Y"), that_stars) - self.assertIsNotNone(result) - self.assertIsInstance(result, PatternTemplateNode) - self.assertEqual(len(pattern_stars), 0) - self.assertEqual(len(topic_stars), 1) - self.assertEqual(topic_stars[0], "X X") - self.assertEqual(len(that_stars), 1) - self.assertEqual(that_stars[0], "Y Y") - - def test_simple_star(self): - graph = PatternGraph() - - pattern_element = ET.fromstring("A * C") - topic_element = ET.fromstring("*") - that_element = ET.fromstring("*") - template_node = TemplateNode() - - graph.add_pattern_to_graph(pattern_element, topic_element, that_element, template_node) - - matcher = PatternMatcher(graph) - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A B B B C"), pattern_stars, - Sentence("*"), topic_stars, - Sentence("*"), that_stars) - self.assertIsNotNone(result) - self.assertIsInstance(result, PatternTemplateNode) - self.assertEqual(len(pattern_stars), 1) - self.assertEqual(pattern_stars[0], "B B B") - self.assertEqual(len(topic_stars), 0) - self.assertEqual(len(that_stars), 0) - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A C"), pattern_stars, - Sentence("*"), topic_stars, - Sentence("*"), that_stars) - self.assertIsNone(result) - self.assertEqual(len(pattern_stars), 0) - self.assertEqual(len(topic_stars), 0) - self.assertEqual(len(that_stars), 0) - - def test_simple_star_at_front(self): - graph = PatternGraph() - - pattern_element = ET.fromstring("* B C") - topic_element = ET.fromstring("*") - that_element = ET.fromstring("*") - template_node = TemplateNode() - - graph.add_pattern_to_graph(pattern_element, topic_element, that_element, template_node) - - matcher = PatternMatcher(graph) - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A B C"), pattern_stars, - Sentence("*"), topic_stars, - Sentence("*"), that_stars) - self.assertIsNotNone(result) - self.assertIsInstance(result, PatternTemplateNode) - self.assertEqual(len(pattern_stars), 1) - self.assertEqual('A', pattern_stars[0]) - self.assertEqual(len(topic_stars), 0) - self.assertEqual(len(that_stars), 0) - - def test_simple_star_at_end(self): - graph = PatternGraph() - - pattern_element = ET.fromstring("A B *") - topic_element = ET.fromstring("*") - that_element = ET.fromstring("*") - template_node = TemplateNode() - - graph.add_pattern_to_graph(pattern_element, topic_element, that_element, template_node) - - matcher = PatternMatcher(graph) - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A B C"), pattern_stars, - Sentence("*"), topic_stars, - Sentence("*"), that_stars) - self.assertIsNotNone(result) - self.assertIsInstance(result, PatternTemplateNode) - self.assertEqual(len(pattern_stars), 1) - self.assertEqual('C', pattern_stars[0]) - self.assertEqual(len(topic_stars), 0) - self.assertEqual(len(that_stars), 0) - - def test_simple_multiple_stars(self): - graph = PatternGraph() - - pattern_element = ET.fromstring("A * * D") - topic_element = ET.fromstring("*") - that_element = ET.fromstring("*") - template_node = TemplateNode() - - graph.add_pattern_to_graph(pattern_element, topic_element, that_element, template_node) - - matcher = PatternMatcher(graph) - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A B C C2 D"), pattern_stars, - Sentence("*"), topic_stars, - Sentence("*"), that_stars) - self.assertIsNotNone(result) - self.assertIsInstance(result, PatternTemplateNode) - self.assertEqual(len(pattern_stars), 2) - self.assertEqual('B', pattern_stars[0]) - self.assertEqual('C C2', pattern_stars[1]) - self.assertEqual(len(topic_stars), 0) - self.assertEqual(len(that_stars), 0) - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A B D"), pattern_stars, - Sentence("*"), topic_stars, - Sentence("*"), that_stars) - self.assertIsNone(result) - self.assertEqual(len(pattern_stars), 0) - self.assertEqual(len(topic_stars), 0) - self.assertEqual(len(that_stars), 0) - - def test_multiple_stars_with_topic_and_that(self): - graph = PatternGraph() - - pattern_element = ET.fromstring("A * * D") - topic_element = ET.fromstring("THIS") - that_element = ET.fromstring("OTHER") - template_node = TemplateNode() - - graph.add_pattern_to_graph(pattern_element, topic_element, that_element, template_node) - - matcher = PatternMatcher(graph) - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A B C C2 D"), pattern_stars, - Sentence("THIS"), topic_stars, - Sentence("OTHER"), that_stars) - self.assertIsNotNone(result) - self.assertIsInstance(result, PatternTemplateNode) - self.assertEqual(len(pattern_stars), 2) - self.assertEqual('B', pattern_stars[0]) - self.assertEqual('C C2', pattern_stars[1]) - self.assertEqual(len(topic_stars), 0) - self.assertEqual(len(that_stars), 0) - - def test_multiple_hashes_with_topic_and_that(self): - graph = PatternGraph() - - pattern_element = ET.fromstring("A # # D") - topic_element = ET.fromstring("THIS") - that_element = ET.fromstring("OTHER") - template_node = TemplateNode() - - graph.add_pattern_to_graph(pattern_element, topic_element, that_element, template_node) - - matcher = PatternMatcher(graph) - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A B C C2 D"), pattern_stars, - Sentence("THIS"), topic_stars, - Sentence("OTHER"), that_stars) - self.assertIsNotNone(result) - self.assertIsInstance(result, PatternTemplateNode) - self.assertEqual(len(pattern_stars), 2) - self.assertEqual('B', pattern_stars[0]) - self.assertEqual('C C2', pattern_stars[1]) - self.assertEqual(len(topic_stars), 0) - self.assertEqual(len(that_stars), 0) - - def test_multiple_hashes_with_topic_and_star(self): - graph = PatternGraph() - - pattern_element = ET.fromstring("A # # D") - topic_element = ET.fromstring("THIS") - that_element = ET.fromstring("*") - template_node = TemplateNode() - - graph.add_pattern_to_graph(pattern_element, topic_element, that_element, template_node) - - matcher = PatternMatcher(graph) - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A B C C2 D"), pattern_stars, - Sentence("THIS"), topic_stars, - Sentence("THAT"), that_stars) - self.assertIsNotNone(result) - self.assertIsInstance(result, PatternTemplateNode) - self.assertEqual(len(pattern_stars), 2) - self.assertEqual('B', pattern_stars[0]) - self.assertEqual('C C2', pattern_stars[1]) - self.assertEqual(len(topic_stars), 0) - self.assertEqual(len(that_stars), 1) - self.assertEqual("THAT", that_stars[0]) - - def test_multiple_hashes_with_star_and_that(self): - graph = PatternGraph() - - pattern_element = ET.fromstring("A # # D") - topic_element = ET.fromstring("*") - that_element = ET.fromstring("THAT") - template_node = TemplateNode() - - graph.add_pattern_to_graph(pattern_element, topic_element, that_element, template_node) - - matcher = PatternMatcher(graph) - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A B C C2 D"), pattern_stars, - Sentence("THIS"), topic_stars, - Sentence("THAT"), that_stars) - self.assertIsNotNone(result) - self.assertIsInstance(result, PatternTemplateNode) - self.assertEqual(len(pattern_stars), 2) - self.assertEqual('B', pattern_stars[0]) - self.assertEqual('C C2', pattern_stars[1]) - self.assertEqual(len(topic_stars), 1) - self.assertEqual("THIS", topic_stars[0]) - self.assertEqual(len(that_stars), 0) - - def test_simple_priorty(self): - graph = PatternGraph() - - pattern_element = ET.fromstring("$A B C") - topic_element = ET.fromstring("*") - that_element = ET.fromstring("*") - template_node = TemplateNode() - - graph.add_pattern_to_graph(pattern_element, topic_element, that_element, template_node) - - matcher = PatternMatcher(graph) - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A B C"), pattern_stars, - Sentence("*"), topic_stars, - Sentence("*"), that_stars) - self.assertIsNotNone(result) - self.assertIsInstance(result, PatternTemplateNode) - self.assertEqual(len(pattern_stars), 0) - self.assertEqual(len(topic_stars), 0) - self.assertEqual(len(that_stars), 0) - - def test_mixed_priorty_and_star(self): - graph = PatternGraph() - - pattern_element1 = ET.fromstring("A $B C") - pattern_element2 = ET.fromstring("A * C") - pattern_element3 = ET.fromstring("A D C") - topic_element = ET.fromstring("*") - that_element = ET.fromstring("*") - template_node1 = TemplateNode() - template_node2 = TemplateNode() - template_node3 = TemplateNode() - - graph.add_pattern_to_graph(pattern_element1, topic_element, that_element, template_node1) - graph.add_pattern_to_graph(pattern_element2, topic_element, that_element, template_node2) - graph.add_pattern_to_graph(pattern_element3, topic_element, that_element, template_node3) - - matcher = PatternMatcher(graph) - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A B C"), pattern_stars, - Sentence("*"), topic_stars, - Sentence("*"), that_stars) - self.assertIsNotNone(result) - self.assertIsInstance(result, PatternTemplateNode) - self.assertEqual(result.template, template_node1) - self.assertEqual(len(pattern_stars), 0) - self.assertEqual(len(topic_stars), 0) - self.assertEqual(len(that_stars), 0) - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A F C"), pattern_stars, - Sentence("*"), topic_stars, - Sentence("*"), that_stars) - self.assertIsNotNone(result) - self.assertIsInstance(result, PatternTemplateNode) - self.assertEqual(result.template, template_node2) - self.assertEqual(len(pattern_stars), 1) - self.assertEqual("F", pattern_stars[0]) - self.assertEqual(len(topic_stars), 0) - self.assertEqual(len(that_stars), 0) - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A D C"), pattern_stars, - Sentence("*"), topic_stars, - Sentence("*"), that_stars) - self.assertIsNotNone(result) - self.assertIsInstance(result, PatternTemplateNode) - self.assertEqual(result.template, template_node3) - self.assertEqual(len(pattern_stars), 0) - self.assertEqual(len(topic_stars), 0) - self.assertEqual(len(that_stars), 0) - - def test_simple_hash(self): - graph = PatternGraph() - - pattern_element = ET.fromstring("A # C") - topic_element = ET.fromstring("*") - that_element = ET.fromstring("*") - template_node = TemplateNode() - - graph.add_pattern_to_graph(pattern_element, topic_element, that_element, template_node) - - matcher = PatternMatcher(graph) - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A B C"), pattern_stars, - Sentence("*"), topic_stars, - Sentence("*"), that_stars) - self.assertIsNotNone(result) - self.assertIsInstance(result, PatternTemplateNode) - self.assertEqual(len(pattern_stars), 1) - self.assertEqual("B", pattern_stars[0]) - self.assertEqual(len(topic_stars), 0) - self.assertEqual(len(that_stars), 0) - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A C"), pattern_stars, - Sentence("*"), topic_stars, - Sentence("*"), that_stars) - self.assertIsNotNone(result) - self.assertIsInstance(result, PatternTemplateNode) - self.assertEqual(len(pattern_stars), 0) - self.assertEqual(len(topic_stars), 0) - self.assertEqual(len(topic_stars), 0) - - def test_simple_hash_at_front(self): - graph = PatternGraph() - - pattern_element = ET.fromstring("# B C") - topic_element = ET.fromstring("*") - that_element = ET.fromstring("*") - template_node = TemplateNode() - - graph.add_pattern_to_graph(pattern_element, topic_element, that_element, template_node) - - matcher = PatternMatcher(graph) - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A B C"), pattern_stars, - Sentence("*"), topic_stars, - Sentence("*"), that_stars) - self.assertIsNotNone(result) - self.assertIsInstance(result, PatternTemplateNode) - self.assertEqual(len(pattern_stars), 1) - self.assertEqual('A', pattern_stars[0]) - self.assertEqual(len(topic_stars), 0) - self.assertEqual(len(that_stars), 0) - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("B C"), pattern_stars, - Sentence("*"), topic_stars, - Sentence("*"), that_stars) - self.assertIsNotNone(result) - self.assertIsInstance(result, PatternTemplateNode) - self.assertEqual(len(pattern_stars), 0) - self.assertEqual(len(topic_stars), 0) - self.assertEqual(len(that_stars), 0) - - def test_simple_hash_at_end(self): - graph = PatternGraph() - - pattern_element = ET.fromstring("A B #") - topic_element = ET.fromstring("*") - that_element = ET.fromstring("*") - template_node = TemplateNode() - - graph.add_pattern_to_graph(pattern_element, topic_element, that_element, template_node) - - matcher = PatternMatcher(graph) - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A B C"), pattern_stars, - Sentence("*"), topic_stars, - Sentence("*"), that_stars) - self.assertIsNotNone(result) - self.assertIsInstance(result, PatternTemplateNode) - self.assertEqual(len(pattern_stars), 1) - self.assertEqual('C', pattern_stars[0]) - self.assertEqual(len(topic_stars), 0) - self.assertEqual(len(that_stars), 0) - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A B"), pattern_stars, - Sentence("*"), topic_stars, - Sentence("*"), that_stars) - self.assertIsNotNone(result) - self.assertIsInstance(result, PatternTemplateNode) - self.assertEqual(len(pattern_stars), 0) - self.assertEqual(len(topic_stars), 0) - self.assertEqual(len(that_stars), 0) - - def test_simple_multiple_hashes(self): - graph = PatternGraph() - - pattern_element = ET.fromstring("A # # D") - topic_element = ET.fromstring("*") - that_element = ET.fromstring("*") - template_node = TemplateNode() - - graph.add_pattern_to_graph(pattern_element, topic_element, that_element, template_node) - - matcher = PatternMatcher(graph) - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A B C D"), pattern_stars, - Sentence("*"), topic_stars, - Sentence("*"), that_stars) - self.assertIsNotNone(result) - self.assertIsInstance(result, PatternTemplateNode) - self.assertEqual(len(pattern_stars), 2) - self.assertEqual('B', pattern_stars[0]) - self.assertEqual('C', pattern_stars[1]) - self.assertEqual(len(topic_stars), 0) - self.assertEqual(len(that_stars), 0) - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A B D"), pattern_stars, - Sentence("*"), topic_stars, - Sentence("*"), that_stars) - self.assertIsNotNone(result) - self.assertIsInstance(result, PatternTemplateNode) - self.assertEqual(len(pattern_stars), 1) - self.assertEqual('B', pattern_stars[0]) - self.assertEqual(len(topic_stars), 0) - self.assertEqual(len(that_stars), 0) - - def test_simple_topic_and_that(self): - graph = PatternGraph() - - pattern_element = ET.fromstring("A # D") - topic_element = ET.fromstring("TOPIC1") - that_element = ET.fromstring("THAT2") - template_node = TemplateNode() - - graph.add_pattern_to_graph(pattern_element, topic_element, that_element, template_node) - - matcher = PatternMatcher(graph) - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A B C D"), pattern_stars, - Sentence("TOPIC1"), topic_stars, - Sentence("THAT2"), that_stars) - self.assertIsNotNone(result) - self.assertIsInstance(result, PatternTemplateNode) - self.assertEqual(len(pattern_stars), 1) - self.assertEqual('B C', pattern_stars[0]) - self.assertEqual(len(topic_stars), 0) - self.assertEqual(len(that_stars), 0) - - def test_simple_star_and_that(self): - graph = PatternGraph() - - pattern_element = ET.fromstring("A * D") - topic_element = ET.fromstring("*") - that_element = ET.fromstring("THAT1") - template_node = TemplateNode() - - graph.add_pattern_to_graph(pattern_element, topic_element, that_element, template_node) - - matcher = PatternMatcher(graph) - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A B C D"), pattern_stars, - Sentence("TOPIC1"), topic_stars, - Sentence("THAT1"), that_stars) - self.assertIsNotNone(result) - self.assertEqual(len(pattern_stars), 1) - self.assertEqual('B C', pattern_stars[0]) - self.assertEqual(len(topic_stars), 1) - self.assertEqual('TOPIC1', topic_stars[0]) - self.assertEqual(len(that_stars), 0) - - def test_simple_topic_and_star(self): - graph = PatternGraph() - - pattern_element = ET.fromstring("A * D") - topic_element = ET.fromstring("TOPIC1") - that_element = ET.fromstring("*") - template_node = TemplateNode() - - graph.add_pattern_to_graph(pattern_element, topic_element, that_element, template_node) - - matcher = PatternMatcher(graph) - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A B C D"), pattern_stars, - Sentence("TOPIC1"), topic_stars, - Sentence("THAT1"), that_stars) - self.assertIsNotNone(result) - self.assertEqual(len(pattern_stars), 1) - self.assertEqual('B C', pattern_stars[0]) - self.assertEqual(len(topic_stars), 0) - self.assertEqual(len(that_stars), 1) - self.assertEqual('THAT1', that_stars[0]) - - def test_simple_multi_branch_star(self): - graph = PatternGraph() - - pattern_element1 = ET.fromstring("A * B C") - pattern_element2 = ET.fromstring("A * C D") - topic_element = ET.fromstring("*") - that_element = ET.fromstring("*") - template_node = TemplateNode() - - pattern_stars = [] - topic_stars = [] - that_stars = [] - - graph.add_pattern_to_graph(pattern_element1, topic_element, that_element, template_node) - graph.add_pattern_to_graph(pattern_element2, topic_element, that_element, template_node) - - matcher = PatternMatcher(graph) - - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A X Y B C"), pattern_stars, - Sentence("*"), topic_stars, - Sentence("*"), that_stars) - self.assertIsNotNone(result) - self.assertIsInstance(result, PatternTemplateNode) - self.assertEqual(1, len(pattern_stars)) - self.assertEqual('X Y', pattern_stars[0]) - self.assertEqual(len(topic_stars), 0) - self.assertEqual(len(that_stars), 0) - - def test_multi_star_multi_branch_star(self): - graph = PatternGraph() - - pattern_element1 = ET.fromstring("A * B * C") - pattern_element2 = ET.fromstring("A * C * D") - topic_element = ET.fromstring("*") - that_element = ET.fromstring("*") - template_node = TemplateNode() - - graph.add_pattern_to_graph(pattern_element1, topic_element, that_element, template_node) - graph.add_pattern_to_graph(pattern_element2, topic_element, that_element, template_node) - - matcher = PatternMatcher(graph) - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A X B Y C"), pattern_stars, - Sentence("*"), topic_stars, - Sentence("*"), that_stars) - self.assertIsNotNone(result) - self.assertIsInstance(result, PatternTemplateNode) - self.assertEqual(2, len(pattern_stars)) - self.assertEqual('X', pattern_stars[0]) - self.assertEqual('Y', pattern_stars[1]) - self.assertEqual(len(topic_stars), 0) - self.assertEqual(len(that_stars), 0) - - def test_simple_arrow(self): - graph = PatternGraph() - - pattern_element = ET.fromstring("A ^ C") - topic_element = ET.fromstring("*") - that_element = ET.fromstring("*") - template_node = TemplateNode() - - graph.add_pattern_to_graph(pattern_element, topic_element, that_element, template_node) - - matcher = PatternMatcher(graph) - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A B B B C"), pattern_stars, - Sentence("*"), topic_stars, - Sentence("*"), that_stars) - self.assertIsNotNone(result) - self.assertIsInstance(result, PatternTemplateNode) - self.assertEqual(1, len(pattern_stars)) - self.assertEqual('B B B', pattern_stars[0]) - self.assertEqual(len(topic_stars), 0) - self.assertEqual(len(that_stars), 0) - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A C"), pattern_stars, - Sentence("*"), topic_stars, - Sentence("*"), that_stars) - self.assertIsNotNone(result) - self.assertIsInstance(result, PatternTemplateNode) - self.assertEqual(len(pattern_stars), 0) - self.assertEqual(len(topic_stars), 0) - self.assertEqual(len(that_stars), 0) - - def test_simple_arrow_at_end(self): - graph = PatternGraph() - - pattern_element = ET.fromstring("A B ^") - topic_element = ET.fromstring("*") - that_element = ET.fromstring("*") - template_node = TemplateNode() - - graph.add_pattern_to_graph(pattern_element, topic_element, that_element, template_node) - - matcher = PatternMatcher(graph) - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A B C"), pattern_stars, - Sentence("*"), topic_stars, - Sentence("*"), that_stars) - self.assertIsNotNone(result) - self.assertIsInstance(result, PatternTemplateNode) - self.assertEqual(1, len(pattern_stars)) - self.assertEqual('C', pattern_stars[0]) - self.assertEqual(len(topic_stars), 0) - self.assertEqual(len(that_stars), 0) - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A B"), pattern_stars, - Sentence("*"), topic_stars, - Sentence("*"), that_stars) - self.assertIsNotNone(result) - self.assertIsInstance(result, PatternTemplateNode) - self.assertEqual(len(pattern_stars), 0) - self.assertEqual(len(topic_stars), 0) - self.assertEqual(len(that_stars), 0) - - def test_simple_arrow_and_hash_at_end(self): - graph = PatternGraph() - - pattern_element1 = ET.fromstring("A ^") - pattern_element2 = ET.fromstring("A #") - topic_element = ET.fromstring("*") - that_element = ET.fromstring("*") - template_node1 = TemplateNode() - template_node2 = TemplateNode() - - graph.add_pattern_to_graph(pattern_element1, topic_element, that_element, template_node1) - graph.add_pattern_to_graph(pattern_element2, topic_element, that_element, template_node2) - - matcher = PatternMatcher(graph) - - pattern_stars = [] - topic_stars = [] - that_stars = [] - result = matcher.match(PatternMatcherTests.bot, PatternMatcherTests.clientid, - Sentence("A C"), pattern_stars, - Sentence("*"), topic_stars, - Sentence("*"), that_stars) - self.assertIsNotNone(result) - self.assertIsInstance(result, PatternTemplateNode) - self.assertEqual(result.template, template_node2) - self.assertEqual(1, len(pattern_stars)) - self.assertEqual('C', pattern_stars[0]) - self.assertEqual(len(topic_stars), 0) - self.assertEqual(len(that_stars), 0) - - diff --git a/src/test/parser/pattern/test_matching/__init__.py b/src/test/parser/pattern/test_matching/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/src/test/parser/pattern/test_matching/base.py b/src/test/parser/pattern/test_matching/base.py new file mode 100644 index 000000000..edc126378 --- /dev/null +++ b/src/test/parser/pattern/test_matching/base.py @@ -0,0 +1,42 @@ +import logging +import xml.etree.ElementTree as ET + +from test.parser.pattern.test_nodes.base import PatternTestBaseClass, TestBot +from programy.dialog import Sentence +from programy.parser.pattern.graph import PatternGraph +from programy.parser.template.nodes import TemplateWordNode +from programy.parser.aiml_parser import AIMLParser + +class PatternMatcherBaseClass(PatternTestBaseClass): + + @classmethod + def setUpClass(cls): + logging.getLogger().setLevel(logging.DEBUG) + PatternMatcherBaseClass.bot = TestBot() + PatternMatcherBaseClass.clientid = "matcher_test" + + def setUp(self): + self._dump_graph = True + self.graph = PatternGraph() + self.matcher = AIMLParser() + + def dump_graph(self): + if self._dump_graph is True: + self.graph.dump() + + def add_pattern_to_graph(self, pattern, topic="*", that="*", template="test"): + + pattern_element = ET.fromstring("%s"%(pattern)) + topic_element = ET.fromstring("%s"%(topic)) + that_element = ET.fromstring("%s"%(that)) + template_node = TemplateWordNode(template) + + self.matcher.pattern_parser.add_pattern_to_graph(pattern_element, topic_element, that_element, template_node) + + def match_sentence(self, sentence, topic="*", that="*"): + + return self.matcher.match_sentence( PatternMatcherBaseClass.bot, + PatternMatcherBaseClass.clientid, + Sentence(sentence), + topic, + that) \ No newline at end of file diff --git a/src/test/parser/pattern/test_matching/test_arrow.py b/src/test/parser/pattern/test_matching/test_arrow.py new file mode 100644 index 000000000..62224dddf --- /dev/null +++ b/src/test/parser/pattern/test_matching/test_arrow.py @@ -0,0 +1,120 @@ + +from test.parser.pattern.test_matching.base import PatternMatcherBaseClass + +class PatternMatcherTests(PatternMatcherBaseClass): + + def test_arrow_tree_matching_empty(self): + + self.add_pattern_to_graph(pattern="^", topic="X", that="Y", template="1") + + self.dump_graph() + + context = self.match_sentence("A", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + self.assertEqual("A", context.star(1)) + + def test_arrow_tree_matching_single(self): + + self.add_pattern_to_graph(pattern="^ A", topic="X", that="Y", template="1") + + self.dump_graph() + + context = self.match_sentence("B A", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + self.assertEqual("B", context.star(1)) + + def test_arrow_tree_matching_front(self): + + self.add_pattern_to_graph(pattern="^ C D", topic="X", that="Y", template="1") + + self.dump_graph() + + context = self.match_sentence("A B C D", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + self.assertEqual("A B", context.star(1)) + + context = self.match_sentence("C D", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + self.assertEqual("", context.star(1)) + + def test_arrow_tree_matching_middle(self): + + self.add_pattern_to_graph(pattern="A ^ D", topic="X", that="Y", template="1") + + self.dump_graph() + + context = self.match_sentence("A B C D", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + self.assertEqual("B C", context.star(1)) + + context = self.match_sentence("A D", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + self.assertEqual("", context.star(1)) + + def test_arrow_tree_matching_end(self): + + self.add_pattern_to_graph(pattern="A B ^", topic="X", that="Y", template="1") + + self.dump_graph() + + context = self.match_sentence("A B C D", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + self.assertEqual("C D", context.star(1)) + + context = self.match_sentence("A B", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + self.assertEqual("", context.star(1)) + + def test_arrow_front_and_back(self): + + self.add_pattern_to_graph(pattern="^ F G", topic="*", that="*", template="2") + self.add_pattern_to_graph(pattern="^ A B ^", topic="*", that="*", template="1") + + self.dump_graph() + + context = self.match_sentence("F A B G", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + self.assertEqual("F", context.star(1)) + self.assertEqual("G", context.star(2)) + + def test_arrow_deep_tree_matching(self): + + self.add_pattern_to_graph(pattern="A ^ B ^ C", topic="X", that="Z", template="1") + self.add_pattern_to_graph(pattern="A ^ B ^ C", topic="X", that="Y", template="1") + + self.dump_graph() + + context = self.match_sentence("A X1 X2 X3 X4 B Y1 Y2 Y3 Y4 C", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + self.assertEqual("X1 X2 X3 X4", context.star(1)) + self.assertEqual("Y1 Y2 Y3 Y4", context.star(2)) \ No newline at end of file diff --git a/src/test/parser/pattern/test_matching/test_basic_branches.py b/src/test/parser/pattern/test_matching/test_basic_branches.py new file mode 100644 index 000000000..27bac2e2d --- /dev/null +++ b/src/test/parser/pattern/test_matching/test_basic_branches.py @@ -0,0 +1,37 @@ + +from test.parser.pattern.test_matching.base import PatternMatcherBaseClass + +class PatternMatcherTests(PatternMatcherBaseClass): + + def test_basic_tree_matching_no_wildcards(self): + + self.add_pattern_to_graph(pattern="A B D E F", topic="X", that="Y", template="1") + self.add_pattern_to_graph(pattern="A B D E F", topic="X", that="Z", template="2") + self.add_pattern_to_graph(pattern="A B D G", topic="X", that="Z", template="3") + + self.dump_graph() + + context = self.match_sentence("A B D E F", topic="X", that="Z") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("2", context.template_node().template.word) + + def test_basic_multi_tree_matching_no_wildcards(self): + + self.add_pattern_to_graph(pattern="A B D", topic="X", that="Y", template="1") + self.add_pattern_to_graph(pattern="A B D", topic="X", that="Z", template="2") + self.add_pattern_to_graph(pattern="A B D", topic="X", that="X", template="3") + + self.dump_graph() + + context = self.match_sentence("A B D", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + context = self.match_sentence("A B D", topic="X", that="Z") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("2", context.template_node().template.word) + + diff --git a/src/test/parser/pattern/test_matching/test_basics.py b/src/test/parser/pattern/test_matching/test_basics.py new file mode 100644 index 000000000..e9f8bfb23 --- /dev/null +++ b/src/test/parser/pattern/test_matching/test_basics.py @@ -0,0 +1,30 @@ +import logging +from test.parser.pattern.test_matching.base import PatternMatcherBaseClass + +class PatternMatcherBasicTests(PatternMatcherBaseClass): + + def test_single_word_match(self): + self.add_pattern_to_graph(pattern="A", topic="X", that="Y", template="1") + + self.dump_graph() + + context = self.match_sentence("A", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + def test_single_word_no_match(self): + self.add_pattern_to_graph(pattern="A", topic="X", that="Y", template="1") + + self.dump_graph() + + context = self.match_sentence("B", topic="X", that="Y") + self.assertIsNone(context) + + + + + + + + diff --git a/src/test/parser/pattern/test_matching/test_bot.py b/src/test/parser/pattern/test_matching/test_bot.py new file mode 100644 index 000000000..780758bdd --- /dev/null +++ b/src/test/parser/pattern/test_matching/test_bot.py @@ -0,0 +1,19 @@ + +from test.parser.pattern.test_matching.base import PatternMatcherBaseClass + +class PatternMatcherBotTests(PatternMatcherBaseClass): + + def test_basic_bot_match(self): + + PatternMatcherBaseClass.bot.brain.properties._properties['firstname'] = 'testbot' + + self.add_pattern_to_graph(pattern="firstname", topic="X", that="Y", template="1") + + self.dump_graph() + + context = self.match_sentence("testbot", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + self.assertEqual("testbot", context.star(1)) diff --git a/src/test/parser/pattern/test_matching/test_hash.py b/src/test/parser/pattern/test_matching/test_hash.py new file mode 100644 index 000000000..9bcc6bac1 --- /dev/null +++ b/src/test/parser/pattern/test_matching/test_hash.py @@ -0,0 +1,128 @@ + +from test.parser.pattern.test_matching.base import PatternMatcherBaseClass + +class PatternMatcherTests(PatternMatcherBaseClass): + + def test_hash_tree_matching_empty(self): + + self.add_pattern_to_graph(pattern="#", topic="X", that="Y", template="1") + + self.dump_graph() + + context = self.match_sentence("A", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + self.assertEqual("A", context.star(1)) + + def test_hash_tree_matching_single(self): + + self.add_pattern_to_graph(pattern="# A", topic="X", that="Y", template="1") + + self.dump_graph() + + context = self.match_sentence("B A", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + self.assertEqual("B", context.star(1)) + + context = self.match_sentence("A", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + self.assertEqual("", context.star(1)) + + def test_hash_tree_matching_front(self): + + self.add_pattern_to_graph(pattern="# C D", topic="X", that="Y", template="1") + + self.dump_graph() + + context = self.match_sentence("A B C D", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + self.assertEqual("A B", context.star(1)) + + context = self.match_sentence("C D", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + self.assertEqual("", context.star(1)) + + def test_hash_tree_matching_middle(self): + + self.add_pattern_to_graph(pattern="A # D", topic="X", that="Y", template="1") + + self.dump_graph() + + context = self.match_sentence("A B C D", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + self.assertEqual("B C", context.star(1)) + + context = self.match_sentence("A D", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + self.assertEqual("", context.star(1)) + + def test_hash_tree_matching_end(self): + + self.add_pattern_to_graph(pattern="A B #", topic="X", that="Y", template="1") + + self.dump_graph() + + context = self.match_sentence("A B C D", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + self.assertEqual("C D", context.star(1)) + + context = self.match_sentence("A B", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + self.assertEqual("", context.star(1)) + + def test_hash_front_and_back(self): + + self.add_pattern_to_graph(pattern="# F G", topic="*", that="*", template="2") + self.add_pattern_to_graph(pattern="# A B #", topic="*", that="*", template="1") + + self.dump_graph() + + context = self.match_sentence("F A B G", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + + self.assertEqual("1", context.template_node().template.word) + self.assertEqual("F", context.star(1)) + self.assertEqual("G", context.star(2)) + + def test_arrow_deep_tree_matching(self): + + self.add_pattern_to_graph(pattern="A # B # C", topic="X", that="Z", template="1") + self.add_pattern_to_graph(pattern="A # B # C", topic="X", that="Y", template="1") + + self.dump_graph() + + context = self.match_sentence("A X1 X2 X3 X4 B Y1 Y2 Y3 Y4 C", topic="X", that="Y") + + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + self.assertEqual("X1 X2 X3 X4", context.star(1)) + self.assertEqual("Y1 Y2 Y3 Y4", context.star(2)) \ No newline at end of file diff --git a/src/test/parser/pattern/test_matching/test_priority.py b/src/test/parser/pattern/test_matching/test_priority.py new file mode 100644 index 000000000..03ca2ec0f --- /dev/null +++ b/src/test/parser/pattern/test_matching/test_priority.py @@ -0,0 +1,18 @@ + +from test.parser.pattern.test_matching.base import PatternMatcherBaseClass + +class PatternMatcherTests(PatternMatcherBaseClass): + + def test_priority_tree_matching_no_wildcards(self): + + self.add_pattern_to_graph(pattern="$A B D", topic="X", that="Y", template="1") + self.add_pattern_to_graph(pattern="A B D", topic="X", that="Y", template="2") + + self.dump_graph() + + context = self.match_sentence("A B D", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + diff --git a/src/test/parser/pattern/test_matching/test_set.py b/src/test/parser/pattern/test_matching/test_set.py new file mode 100644 index 000000000..017126619 --- /dev/null +++ b/src/test/parser/pattern/test_matching/test_set.py @@ -0,0 +1,17 @@ + +from test.parser.pattern.test_matching.base import PatternMatcherBaseClass + +class PatternMatcherSetTests(PatternMatcherBaseClass): + + def test_basic_set_match(self): + + self.add_pattern_to_graph(pattern="I AM number YEARS OLD", topic="X", that="Y", template="1") + + self.dump_graph() + + context = self.match_sentence("I AM 49 YEARS OLD", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + self.assertEqual("49", context.star(1)) diff --git a/src/test/parser/pattern/test_matching/test_star.py b/src/test/parser/pattern/test_matching/test_star.py new file mode 100644 index 000000000..02f0b4fb7 --- /dev/null +++ b/src/test/parser/pattern/test_matching/test_star.py @@ -0,0 +1,135 @@ + +from test.parser.pattern.test_matching.base import PatternMatcherBaseClass + +class PatternMatcherTests(PatternMatcherBaseClass): + + def test_star_tree_matching_single(self): + + self.add_pattern_to_graph(pattern="*", topic="X", that="Y", template="1") + + self.dump_graph() + + context = self.match_sentence("A B D", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + self.assertEqual("A B D", context.star(1)) + + def test_star_tree_matching_front(self): + + self.add_pattern_to_graph(pattern="* C D", topic="X", that="Y", template="1") + + self.dump_graph() + + context = self.match_sentence("A B C D", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + self.assertEqual("A B", context.star(1)) + + def test_star_tree_matching_middle(self): + + self.add_pattern_to_graph(pattern="A * D", topic="X", that="Y", template="1") + + self.dump_graph() + + context = self.match_sentence("A B C D", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + self.assertEqual("B C", context.star(1)) + + def test_star_tree_matching_end(self): + + self.add_pattern_to_graph(pattern="A B *", topic="X", that="Y", template="1") + + self.dump_graph() + + context = self.match_sentence("A B C D", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + self.assertEqual("C D", context.star(1)) + + def test_star_tree_matching_multiple(self): + + self.add_pattern_to_graph(pattern="A * * D", topic="X", that="Y", template="1") + + self.dump_graph() + + context = self.match_sentence("A B C D", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + self.assertEqual("B", context.star(1)) + self.assertEqual("C", context.star(2)) + + def test_star_tree_matching_all(self): + + self.add_pattern_to_graph(pattern="A * D", topic="*", that="*", template="1") + + self.dump_graph() + + context = self.match_sentence("A B C D", topic="X Y", that="Z1 Z2") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + self.assertEqual("B C", context.star(1)) + self.assertEqual("X Y", context.topicstar(1)) + self.assertEqual("Z1 Z2", context.thatstar(1)) + + def test_stars_front_and_back(self): + + self.add_pattern_to_graph(pattern="* F G", topic="*", that="*", template="2") + self.add_pattern_to_graph(pattern="* A B *", topic="*", that="*", template="1") + + self.dump_graph() + + context = self.match_sentence("F A B G", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + self.assertEqual("F", context.star(1)) + self.assertEqual("G", context.star(2)) + self.assertEqual("X", context.topicstar(1)) + self.assertEqual("Y", context.thatstar(1)) + + def test_star_deep_tree_matching(self): + + self.add_pattern_to_graph(pattern="A * B * C", topic="X", that="Z", template="1") + self.add_pattern_to_graph(pattern="A * B * C", topic="X", that="Y", template="1") + + self.dump_graph() + + context = self.match_sentence("A X1 X2 X3 X4 B Y1 Y2 Y3 Y4 C", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + self.assertEqual("X1 X2 X3 X4", context.star(1)) + self.assertEqual("Y1 Y2 Y3 Y4", context.star(2)) + + def test_star_with_set_matching(self): + + self.add_pattern_to_graph(pattern="XXLENGTH * XS number", topic="*", that="*", template="1") + self.add_pattern_to_graph(pattern="XXLENGTH * * XS number", topic="*", that="*", template="2") + + self.dump_graph() + + context = self.match_sentence("XXLENGTH X Y XS 1", topic="*", that="*") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("2", context.template_node().template.word) + + context = self.match_sentence("XXLENGTH X XS 1", topic="*", that="XXLENGTH X Y XS 2") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + diff --git a/src/test/parser/pattern/test_matching/test_underline.py b/src/test/parser/pattern/test_matching/test_underline.py new file mode 100644 index 000000000..16db91fe9 --- /dev/null +++ b/src/test/parser/pattern/test_matching/test_underline.py @@ -0,0 +1,97 @@ + +from test.parser.pattern.test_matching.base import PatternMatcherBaseClass + +class PatternMatcherTests(PatternMatcherBaseClass): + + def test_underline_tree_matching_single(self): + + self.add_pattern_to_graph(pattern="_", topic="X", that="Y", template="1") + + self.dump_graph() + + context = self.match_sentence("A B D", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + def test_underline_tree_matching_front(self): + + self.add_pattern_to_graph(pattern="_ C D", topic="X", that="Y", template="1") + + self.dump_graph() + + context = self.match_sentence("A B C D", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + def test_underline_tree_matching_middle(self): + + self.add_pattern_to_graph(pattern="A _ D", topic="X", that="Y", template="1") + + self.dump_graph() + + context = self.match_sentence("A B C D", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + def test_underline_tree_matching_end(self): + + self.add_pattern_to_graph(pattern="A B _", topic="X", that="Y", template="1") + + self.dump_graph() + + context = self.match_sentence("A B C D", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + def test_underline_tree_matching_multiple(self): + + self.add_pattern_to_graph(pattern="A _ _ D", topic="X", that="Y", template="1") + + self.dump_graph() + + context = self.match_sentence("A B C D", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + def test_underline_tree_matching_all(self): + + self.add_pattern_to_graph(pattern="A _ D", topic="_", that="_", template="1") + + self.dump_graph() + + context = self.match_sentence("A B C D", topic="X Y", that="Z") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + def test_underline_front_and_back(self): + + self.add_pattern_to_graph(pattern="_ F G", topic="*", that="*", template="2") + self.add_pattern_to_graph(pattern="_ A B _", topic="*", that="*", template="1") + + self.dump_graph() + + context = self.match_sentence("F A B G", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + def test_underline_deep_tree_matching(self): + + self.add_pattern_to_graph(pattern="A _ B _ C", topic="X", that="Z", template="1") + self.add_pattern_to_graph(pattern="A _ B _ C", topic="X", that="Y", template="1") + + self.dump_graph() + + context = self.match_sentence("A X1 X2 X3 X4 B Y1 Y2 Y3 Y4 C", topic="X", that="Y") + self.assertIsNotNone(context) + self.assertIsNotNone(context.template_node()) + self.assertEqual("1", context.template_node().template.word) + + self.assertEqual("X1 X2 X3 X4", context.star(1)) + self.assertEqual("Y1 Y2 Y3 Y4", context.star(2)) diff --git a/src/test/parser/pattern/test_matching/test_wildcard_branches.py b/src/test/parser/pattern/test_matching/test_wildcard_branches.py new file mode 100644 index 000000000..66bb1ee72 --- /dev/null +++ b/src/test/parser/pattern/test_matching/test_wildcard_branches.py @@ -0,0 +1,5 @@ + +from test.parser.pattern.test_matching.base import PatternMatcherBaseClass + +class PatternMatcherWildcardBranchesTests(PatternMatcherBaseClass): + pass diff --git a/src/test/parser/pattern/test_nodes.py b/src/test/parser/pattern/test_nodes.py deleted file mode 100644 index c62356076..000000000 --- a/src/test/parser/pattern/test_nodes.py +++ /dev/null @@ -1,350 +0,0 @@ -from programy.dialog import Sentence -from programy.parser.pattern.nodes import * -from programy.parser.template.nodes import TemplateNode - -from test.parser.pattern.base import PatternTestBaseClass - -class PatternNodeTests(PatternTestBaseClass): - - def test_init(self): - node = PatternNode() - self.assertIsNotNone(node) - self.assertFalse(node.is_root()) - self.assertFalse(node.is_priority()) - self.assertFalse(node.is_wildcard()) - self.assertFalse(node.is_zero_or_more()) - self.assertFalse(node.is_one_or_more()) - self.assertFalse(node.has_children()) - self.assertIsNotNone(node.children) - - self.assertFalse(node.equivalent(PatternNode())) - self.assertEqual(node.to_string(), "NODE [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)]") - - node.add_child(PatternNode()) - self.assertEqual(len(node.children), 1) - self.assertEqual(node.to_string(), "NODE [P(0)^(0)#(0)C(1)_(0)*(0)To(0)Th(0)Te(0)]") - - def test_add_child(self): - - node = PatternNode() - - priority_word1 = PatternPriorityWordNode("pword") - priority_word2 = PatternPriorityWordNode("pword") - node.add_child(priority_word1) - new_node = node.add_child(priority_word2) - self.assertEqual(new_node, priority_word1) - - arrow_node1 = PatternZeroOrMoreWildCardNode("^") - arrow_node2 = PatternZeroOrMoreWildCardNode("^") - node.add_child(arrow_node1) - new_node = node.add_child(arrow_node2) - self.assertEqual(new_node, arrow_node1) - - -class PatternRootNodeTests(PatternTestBaseClass): - - def test_init(self): - node = PatternRootNode() - self.assertIsNotNone(node) - - self.assertTrue(node.is_root()) - self.assertFalse(node.is_priority()) - self.assertFalse(node.is_wildcard()) - self.assertFalse(node.is_zero_or_more()) - self.assertFalse(node.is_one_or_more()) - self.assertIsNotNone(node.children) - self.assertFalse(node.has_children()) - - self.assertTrue(node.equivalent(PatternRootNode())) - self.assertEqual(node.to_string(), "ROOT [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)]") - - node.add_child(PatternNode()) - self.assertEqual(len(node.children), 1) - self.assertEqual(node.to_string(), "ROOT [P(0)^(0)#(0)C(1)_(0)*(0)To(0)Th(0)Te(0)]") - - def test_multiple_roots(self): - node1 = PatternRootNode() - node2 = PatternRootNode() - - with self.assertRaises(ParserException) as raised: - node1.can_add(node2) - self.assertTrue(str(raised.exception).startswith("Cannot add root node to existing root node")) - - def test_root_added_to_child(self): - node1 = PatternWordNode("test") - node2 = PatternRootNode() - - with self.assertRaises(ParserException) as raised: - node1.can_add(node2) - self.assertTrue(str(raised.exception).startswith("Cannot add root node to child node")) - - -class PatternTopicNodeTests(PatternTestBaseClass): - - def test_init(self): - node = PatternTopicNode() - self.assertIsNotNone(node) - - self.assertFalse(node.is_root()) - self.assertFalse(node.is_priority()) - self.assertFalse(node.is_wildcard()) - self.assertFalse(node.is_zero_or_more()) - self.assertFalse(node.is_one_or_more()) - self.assertIsNotNone(node.children) - self.assertFalse(node.has_children()) - - self.assertTrue(node.equivalent(PatternTopicNode())) - self.assertEqual(node.to_string(), "TOPIC [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)]") - - def test_topic_to_root(self): - node1 = PatternRootNode() - node2 = PatternTopicNode() - - with self.assertRaises(ParserException) as raised: - node1.can_add(node2) - self.assertEqual(str(raised.exception), "Cannot add topic node to root node") - - def test_multiple_topics(self): - node1 = PatternTopicNode() - node2 = PatternTopicNode() - - with self.assertRaises(ParserException) as raised: - node1.can_add(node2) - self.assertEqual(str(raised.exception), "Cannot add topic node to topic node") - - -class PatternThatNodeTests(PatternTestBaseClass): - - def test_init(self): - node = PatternThatNode() - self.assertIsNotNone(node) - - self.assertFalse(node.is_root()) - self.assertFalse(node.is_priority()) - self.assertFalse(node.is_wildcard()) - self.assertFalse(node.is_zero_or_more()) - self.assertFalse(node.is_one_or_more()) - self.assertIsNotNone(node.children) - self.assertFalse(node.has_children()) - - self.assertTrue(node.equivalent(PatternThatNode())) - self.assertEqual(node.to_string(), "THAT [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)]") - - def test_that_to_root(self): - node1 = PatternRootNode() - node2 = PatternThatNode() - - with self.assertRaises(ParserException) as raised: - node1.can_add(node2) - self.assertEqual(str(raised.exception), "Cannot add that node to root node") - - def test_multiple_thats(self): - node1 = PatternThatNode() - node2 = PatternThatNode() - - with self.assertRaises(ParserException) as raised: - node1.can_add(node2) - self.assertEqual(str(raised.exception), "Cannot add that node to that node") - - -class PatternTemplateNodeTests(PatternTestBaseClass): - - def test_init(self): - - node = PatternTemplateNode(TemplateNode()) - self.assertIsNotNone(node) - - self.assertFalse(node.is_root()) - self.assertFalse(node.is_priority()) - self.assertFalse(node.is_wildcard()) - self.assertFalse(node.is_zero_or_more()) - self.assertFalse(node.is_one_or_more()) - self.assertIsNotNone(node.children) - self.assertFalse(node.has_children()) - - self.assertTrue(node.equivalent(PatternTemplateNode(TemplateNode()))) - self.assertEqual(node.to_string(), "PTEMPLATE [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(1)] ") - - def test_template_to_root(self): - node1 = PatternRootNode() - node2 = PatternTemplateNode(TemplateNode()) - - with self.assertRaises(ParserException) as raised: - node1.can_add(node2) - self.assertEqual(str(raised.exception), "Cannot add template node to root node") - - def test_multiple_templates(self): - node1 = PatternTemplateNode(TemplateNode()) - node2 = PatternTemplateNode(TemplateNode()) - - with self.assertRaises(ParserException) as raised: - node1.can_add(node2) - self.assertEqual(str(raised.exception), "Cannot add template node to template node") - - -class PatternWordNodeTests(PatternTestBaseClass): - - def test_init(self): - node = PatternWordNode("test1") - - self.assertFalse(node.is_root()) - self.assertFalse(node.is_priority()) - self.assertFalse(node.is_wildcard()) - self.assertFalse(node.is_zero_or_more()) - self.assertFalse(node.is_one_or_more()) - self.assertIsNotNone(node.children) - self.assertFalse(node.has_children()) - - self.assertTrue(node.equivalent(PatternWordNode("test1"))) - self.assertFalse(node.is_root()) - self.assertEqual(node.to_string(), "WORD [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] word=[test1]") - - self.assertTrue(node.matches(self.bot, self.clientid, "test1")) - - node.add_child(PatternWordNode("test2")) - self.assertEqual(len(node.children), 1) - self.assertEqual(node.to_string(), "WORD [P(0)^(0)#(0)C(1)_(0)*(0)To(0)Th(0)Te(0)] word=[test1]") - - self.assertIsNotNone(node.matches(self.bot, self.clientid, Sentence("test1"))) - -class PatternPriorityWordNodeTests(PatternTestBaseClass): - - def test_init(self): - node = PatternPriorityWordNode("test1") - self.assertIsNotNone(node) - - self.assertFalse(node.is_root()) - self.assertTrue(node.is_priority()) - self.assertFalse(node.is_wildcard()) - self.assertFalse(node.is_zero_or_more()) - self.assertFalse(node.is_one_or_more()) - self.assertIsNotNone(node.children) - self.assertFalse(node.has_children()) - - self.assertTrue(node.equivalent(PatternPriorityWordNode("test1"))) - self.assertFalse(node.is_root()) - self.assertEqual(node.to_string(), "PWORD [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] word=[test1]") - - self.assertTrue(node.matches(self.bot, self.clientid, "test1")) - - node.add_child(PatternWordNode("test2")) - self.assertEqual(len(node.children), 1) - self.assertEqual(node.to_string(), "PWORD [P(0)^(0)#(0)C(1)_(0)*(0)To(0)Th(0)Te(0)] word=[test1]") - - self.assertTrue(node.matches(self.bot, self.clientid, "test1")) - -class PatternSetNodeTests(PatternTestBaseClass): - - def test_init(self): - node = PatternSetNode("test1") - self.assertIsNotNone(node) - - self.assertFalse(node.is_root()) - self.assertFalse(node.is_priority()) - self.assertFalse(node.is_wildcard()) - self.assertFalse(node.is_zero_or_more()) - self.assertFalse(node.is_one_or_more()) - self.assertIsNotNone(node.children) - self.assertFalse(node.has_children()) - - self.assertTrue(node.equivalent(PatternSetNode("test1"))) - self.assertFalse(node.is_root()) - self.assertEqual(node.to_string(), "SET [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] name=[TEST1]") - - self.bot.brain.sets._sets["TEST1"] = ["val1", "val2", "val3"] - - self.assertTrue(node.matches(self.bot, self.clientid, "val1")) - self.assertTrue(node.matches(self.bot, self.clientid, "val2")) - self.assertFalse(node.matches(self.bot, self.clientid, "val4")) - -class PatternBotNodeTests(PatternTestBaseClass): - - def test_init(self): - node = PatternBotNode("test1") - self.assertIsNotNone(node) - - self.assertFalse(node.is_root()) - self.assertFalse(node.is_priority()) - self.assertFalse(node.is_wildcard()) - self.assertFalse(node.is_zero_or_more()) - self.assertFalse(node.is_one_or_more()) - self.assertIsNotNone(node.children) - self.assertFalse(node.has_children()) - - self.assertTrue(node.equivalent(PatternBotNode("test1"))) - self.assertFalse(node.is_root()) - self.assertEqual(node.to_string(), "BOT [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] property=[test1]") - - self.bot.brain.properties._properties["test1"] = "val1" - - self.assertTrue(node.matches(self.bot, self.clientid, "val1")) - self.assertFalse(node.matches(self.bot, self.clientid, "val4")) - -class PatternZeroOrMoreWildCardNodeTests(PatternTestBaseClass): - - def test_invalid_wildcard(self): - with self.assertRaises(ParserException) as raised: - node = PatternZeroOrMoreWildCardNode("X") - self.assertIsNone(node) - - def test_init(self): - node = PatternZeroOrMoreWildCardNode("#") - - self.assertFalse(node.is_root()) - self.assertFalse(node.is_priority()) - self.assertTrue(node.is_wildcard()) - self.assertTrue(node.is_zero_or_more()) - self.assertFalse(node.is_one_or_more()) - self.assertIsNotNone(node.children) - self.assertFalse(node.has_children()) - - self.assertEqual(node.wildcard, "#") - - self.assertTrue(node.equivalent(PatternZeroOrMoreWildCardNode("#"))) - self.assertFalse(node.is_root()) - self.assertEqual(node.to_string(), "ZEROORMORE [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] wildcard=[#]") - - node = PatternZeroOrMoreWildCardNode("^") - self.assertIsNotNone(node) - - self.assertEqual(node.wildcard, "^") - - self.assertTrue(node.equivalent(PatternZeroOrMoreWildCardNode("^"))) - self.assertFalse(node.is_root()) - self.assertEqual(node.to_string(), "ZEROORMORE [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] wildcard=[^]") - -class PatternOneOrMoreWildCardNodeTests(PatternTestBaseClass): - - def test_invalid_wildcard(self): - with self.assertRaises(ParserException) as raised: - node = PatternOneOrMoreWildCardNode("X") - self.assertIsNone(node) - - def test_init(self): - node = PatternOneOrMoreWildCardNode("*") - self.assertIsNotNone(node) - - self.assertFalse(node.is_root()) - self.assertFalse(node.is_priority()) - self.assertTrue(node.is_wildcard()) - self.assertFalse(node.is_zero_or_more()) - self.assertTrue(node.is_one_or_more()) - - self.assertIsNotNone(node.children) - self.assertFalse(node.has_children()) - - self.assertEqual(node.wildcard, "*") - - self.assertTrue(node.equivalent(PatternOneOrMoreWildCardNode("*"))) - self.assertFalse(node.is_root()) - self.assertEqual(node.to_string(), "ONEORMORE [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] wildcard=[*]") - - node = PatternOneOrMoreWildCardNode("_") - self.assertIsNotNone(node) - - self.assertEqual(node.wildcard, "_") - - self.assertTrue(node.equivalent(PatternOneOrMoreWildCardNode("_"))) - self.assertFalse(node.is_root()) - self.assertEqual(node.to_string(), "ONEORMORE [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] wildcard=[_]") - diff --git a/src/test/parser/pattern/test_nodes/__init__.py b/src/test/parser/pattern/test_nodes/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/src/test/parser/pattern/base.py b/src/test/parser/pattern/test_nodes/base.py similarity index 100% rename from src/test/parser/pattern/base.py rename to src/test/parser/pattern/test_nodes/base.py diff --git a/src/test/parser/pattern/test_nodes/test_bot.py b/src/test/parser/pattern/test_nodes/test_bot.py new file mode 100644 index 000000000..01b6c548a --- /dev/null +++ b/src/test/parser/pattern/test_nodes/test_bot.py @@ -0,0 +1,21 @@ +from test.parser.pattern.test_nodes.base import PatternTestBaseClass +from programy.parser.pattern.nodes import * + +class PatternBotNodeTests(PatternTestBaseClass): + + def test_init(self): + node = PatternBotNode("test1") + self.assertIsNotNone(node) + + self.assertFalse(node.is_root()) + self.assertFalse(node.is_priority()) + self.assertFalse(node.is_wildcard()) + self.assertFalse(node.is_zero_or_more()) + self.assertFalse(node.is_one_or_more()) + self.assertIsNotNone(node.children) + self.assertFalse(node.has_children()) + + self.assertTrue(node.equivalent(PatternBotNode("test1"))) + self.assertFalse(node.is_root()) + self.assertEqual(node.to_string(), "BOT [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] property=[test1]") + diff --git a/src/test/parser/pattern/test_nodes/test_node.py b/src/test/parser/pattern/test_nodes/test_node.py new file mode 100644 index 000000000..b2564ad99 --- /dev/null +++ b/src/test/parser/pattern/test_nodes/test_node.py @@ -0,0 +1,40 @@ +from test.parser.pattern.test_nodes.base import PatternTestBaseClass +from programy.parser.pattern.nodes import * + +class PatternNodeTests(PatternTestBaseClass): + + def test_init(self): + node = PatternNode() + self.assertIsNotNone(node) + self.assertFalse(node.is_root()) + self.assertFalse(node.is_priority()) + self.assertFalse(node.is_wildcard()) + self.assertFalse(node.is_zero_or_more()) + self.assertFalse(node.is_one_or_more()) + self.assertFalse(node.has_children()) + self.assertIsNotNone(node.children) + + self.assertFalse(node.equivalent(PatternNode())) + self.assertEqual(node.to_string(), "NODE [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)]") + + node.add_child(PatternNode()) + self.assertEqual(len(node.children), 1) + self.assertEqual(node.to_string(), "NODE [P(0)^(0)#(0)C(1)_(0)*(0)To(0)Th(0)Te(0)]") + + def test_add_child(self): + + node = PatternNode() + + priority_word1 = PatternPriorityWordNode("pword") + priority_word2 = PatternPriorityWordNode("pword") + node.add_child(priority_word1) + new_node = node.add_child(priority_word2) + self.assertEqual(new_node, priority_word1) + + arrow_node1 = PatternZeroOrMoreWildCardNode("^") + arrow_node2 = PatternZeroOrMoreWildCardNode("^") + node.add_child(arrow_node1) + new_node = node.add_child(arrow_node2) + self.assertEqual(new_node, arrow_node1) + + diff --git a/src/test/parser/pattern/test_nodes/test_one_or_more.py b/src/test/parser/pattern/test_nodes/test_one_or_more.py new file mode 100644 index 000000000..018fb3494 --- /dev/null +++ b/src/test/parser/pattern/test_nodes/test_one_or_more.py @@ -0,0 +1,38 @@ +from test.parser.pattern.test_nodes.base import PatternTestBaseClass +from programy.parser.pattern.nodes import * + +class PatternOneOrMoreWildCardNodeTests(PatternTestBaseClass): + + def test_invalid_wildcard(self): + with self.assertRaises(ParserException) as raised: + node = PatternOneOrMoreWildCardNode("X") + self.assertIsNone(node) + + def test_init(self): + node = PatternOneOrMoreWildCardNode("*") + self.assertIsNotNone(node) + + self.assertFalse(node.is_root()) + self.assertFalse(node.is_priority()) + self.assertTrue(node.is_wildcard()) + self.assertFalse(node.is_zero_or_more()) + self.assertTrue(node.is_one_or_more()) + + self.assertIsNotNone(node.children) + self.assertFalse(node.has_children()) + + self.assertEqual(node.wildcard, "*") + + self.assertTrue(node.equivalent(PatternOneOrMoreWildCardNode("*"))) + self.assertFalse(node.is_root()) + self.assertEqual(node.to_string(), "ONEORMORE [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] wildcard=[*]") + + node = PatternOneOrMoreWildCardNode("_") + self.assertIsNotNone(node) + + self.assertEqual(node.wildcard, "_") + + self.assertTrue(node.equivalent(PatternOneOrMoreWildCardNode("_"))) + self.assertFalse(node.is_root()) + self.assertEqual(node.to_string(), "ONEORMORE [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] wildcard=[_]") + diff --git a/src/test/parser/pattern/test_nodes/test_priority.py b/src/test/parser/pattern/test_nodes/test_priority.py new file mode 100644 index 000000000..e48165ecf --- /dev/null +++ b/src/test/parser/pattern/test_nodes/test_priority.py @@ -0,0 +1,25 @@ +from test.parser.pattern.test_nodes.base import PatternTestBaseClass +from programy.parser.pattern.nodes import * + + +class PatternPriorityWordNodeTests(PatternTestBaseClass): + + def test_init(self): + node = PatternPriorityWordNode("test1") + self.assertIsNotNone(node) + + self.assertFalse(node.is_root()) + self.assertTrue(node.is_priority()) + self.assertFalse(node.is_wildcard()) + self.assertFalse(node.is_zero_or_more()) + self.assertFalse(node.is_one_or_more()) + self.assertIsNotNone(node.children) + self.assertFalse(node.has_children()) + + self.assertTrue(node.equivalent(PatternPriorityWordNode("test1"))) + self.assertFalse(node.is_root()) + self.assertEqual(node.to_string(), "PWORD [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] word=[test1]") + + node.add_child(PatternWordNode("test2")) + self.assertEqual(len(node.children), 1) + self.assertEqual(node.to_string(), "PWORD [P(0)^(0)#(0)C(1)_(0)*(0)To(0)Th(0)Te(0)] word=[test1]") diff --git a/src/test/parser/pattern/test_nodes/test_root.py b/src/test/parser/pattern/test_nodes/test_root.py new file mode 100644 index 000000000..364514011 --- /dev/null +++ b/src/test/parser/pattern/test_nodes/test_root.py @@ -0,0 +1,41 @@ +from test.parser.pattern.test_nodes.base import PatternTestBaseClass +from programy.parser.pattern.nodes import * + +class PatternRootNodeTests(PatternTestBaseClass): + + def test_init(self): + node = PatternRootNode() + self.assertIsNotNone(node) + + self.assertTrue(node.is_root()) + self.assertFalse(node.is_priority()) + self.assertFalse(node.is_wildcard()) + self.assertFalse(node.is_zero_or_more()) + self.assertFalse(node.is_one_or_more()) + self.assertIsNotNone(node.children) + self.assertFalse(node.has_children()) + + self.assertTrue(node.equivalent(PatternRootNode())) + self.assertEqual(node.to_string(), "ROOT [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)]") + + node.add_child(PatternNode()) + self.assertEqual(len(node.children), 1) + self.assertEqual(node.to_string(), "ROOT [P(0)^(0)#(0)C(1)_(0)*(0)To(0)Th(0)Te(0)]") + + def test_multiple_roots(self): + node1 = PatternRootNode() + node2 = PatternRootNode() + + with self.assertRaises(ParserException) as raised: + node1.can_add(node2) + self.assertTrue(str(raised.exception).startswith("Cannot add root node to existing root node")) + + def test_root_added_to_child(self): + node1 = PatternWordNode("test") + node2 = PatternRootNode() + + with self.assertRaises(ParserException) as raised: + node1.can_add(node2) + self.assertTrue(str(raised.exception).startswith("Cannot add root node to child node")) + + diff --git a/src/test/parser/pattern/test_nodes/test_set.py b/src/test/parser/pattern/test_nodes/test_set.py new file mode 100644 index 000000000..2d2ff7b1b --- /dev/null +++ b/src/test/parser/pattern/test_nodes/test_set.py @@ -0,0 +1,21 @@ +from test.parser.pattern.test_nodes.base import PatternTestBaseClass +from programy.parser.pattern.nodes import * + +class PatternSetNodeTests(PatternTestBaseClass): + + def test_init(self): + node = PatternSetNode("test1") + self.assertIsNotNone(node) + + self.assertFalse(node.is_root()) + self.assertFalse(node.is_priority()) + self.assertFalse(node.is_wildcard()) + self.assertFalse(node.is_zero_or_more()) + self.assertFalse(node.is_one_or_more()) + self.assertIsNotNone(node.children) + self.assertFalse(node.has_children()) + + self.assertTrue(node.equivalent(PatternSetNode("test1"))) + self.assertFalse(node.is_root()) + self.assertEqual(node.to_string(), "SET [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] name=[TEST1]") + diff --git a/src/test/parser/pattern/test_nodes/test_template.py b/src/test/parser/pattern/test_nodes/test_template.py new file mode 100644 index 000000000..ff299811a --- /dev/null +++ b/src/test/parser/pattern/test_nodes/test_template.py @@ -0,0 +1,40 @@ +from test.parser.pattern.test_nodes.base import PatternTestBaseClass +from programy.parser.pattern.nodes import * +from programy.parser.template.nodes import TemplateNode + + +class PatternTemplateNodeTests(PatternTestBaseClass): + + def test_init(self): + + node = PatternTemplateNode(TemplateNode()) + self.assertIsNotNone(node) + + self.assertFalse(node.is_root()) + self.assertFalse(node.is_priority()) + self.assertFalse(node.is_wildcard()) + self.assertFalse(node.is_zero_or_more()) + self.assertFalse(node.is_one_or_more()) + self.assertIsNotNone(node.children) + self.assertFalse(node.has_children()) + + self.assertTrue(node.equivalent(PatternTemplateNode(TemplateNode()))) + self.assertEqual(node.to_string(), "PTEMPLATE [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(1)] ") + + def test_template_to_root(self): + node1 = PatternRootNode() + node2 = PatternTemplateNode(TemplateNode()) + + with self.assertRaises(ParserException) as raised: + node1.can_add(node2) + self.assertEqual(str(raised.exception), "Cannot add template node to root node") + + def test_multiple_templates(self): + node1 = PatternTemplateNode(TemplateNode()) + node2 = PatternTemplateNode(TemplateNode()) + + with self.assertRaises(ParserException) as raised: + node1.can_add(node2) + self.assertEqual(str(raised.exception), "Cannot add template node to template node") + + diff --git a/src/test/parser/pattern/test_nodes/test_that.py b/src/test/parser/pattern/test_nodes/test_that.py new file mode 100644 index 000000000..905190d2e --- /dev/null +++ b/src/test/parser/pattern/test_nodes/test_that.py @@ -0,0 +1,36 @@ +from test.parser.pattern.test_nodes.base import PatternTestBaseClass +from programy.parser.pattern.nodes import * + + +class PatternThatNodeTests(PatternTestBaseClass): + + def test_init(self): + node = PatternThatNode() + self.assertIsNotNone(node) + + self.assertFalse(node.is_root()) + self.assertFalse(node.is_priority()) + self.assertFalse(node.is_wildcard()) + self.assertFalse(node.is_zero_or_more()) + self.assertFalse(node.is_one_or_more()) + self.assertIsNotNone(node.children) + self.assertFalse(node.has_children()) + + self.assertTrue(node.equivalent(PatternThatNode())) + self.assertEqual(node.to_string(), "THAT [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)]") + + def test_that_to_root(self): + node1 = PatternRootNode() + node2 = PatternThatNode() + + with self.assertRaises(ParserException) as raised: + node1.can_add(node2) + self.assertEqual(str(raised.exception), "Cannot add that node to root node") + + def test_multiple_thats(self): + node1 = PatternThatNode() + node2 = PatternThatNode() + + with self.assertRaises(ParserException) as raised: + node1.can_add(node2) + self.assertEqual(str(raised.exception), "Cannot add that node to that node") diff --git a/src/test/parser/pattern/test_nodes/test_topic.py b/src/test/parser/pattern/test_nodes/test_topic.py new file mode 100644 index 000000000..0306460ac --- /dev/null +++ b/src/test/parser/pattern/test_nodes/test_topic.py @@ -0,0 +1,34 @@ +from test.parser.pattern.test_nodes.base import PatternTestBaseClass +from programy.parser.pattern.nodes import * + +class PatternTopicNodeTests(PatternTestBaseClass): + def test_init(self): + node = PatternTopicNode() + self.assertIsNotNone(node) + + self.assertFalse(node.is_root()) + self.assertFalse(node.is_priority()) + self.assertFalse(node.is_wildcard()) + self.assertFalse(node.is_zero_or_more()) + self.assertFalse(node.is_one_or_more()) + self.assertIsNotNone(node.children) + self.assertFalse(node.has_children()) + + self.assertTrue(node.equivalent(PatternTopicNode())) + self.assertEqual(node.to_string(), "TOPIC [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)]") + + def test_topic_to_root(self): + node1 = PatternRootNode() + node2 = PatternTopicNode() + + with self.assertRaises(ParserException) as raised: + node1.can_add(node2) + self.assertEqual(str(raised.exception), "Cannot add topic node to root node") + + def test_multiple_topics(self): + node1 = PatternTopicNode() + node2 = PatternTopicNode() + + with self.assertRaises(ParserException) as raised: + node1.can_add(node2) + self.assertEqual(str(raised.exception), "Cannot add topic node to topic node") diff --git a/src/test/parser/pattern/test_nodes/test_word.py b/src/test/parser/pattern/test_nodes/test_word.py new file mode 100644 index 000000000..01d4c498b --- /dev/null +++ b/src/test/parser/pattern/test_nodes/test_word.py @@ -0,0 +1,25 @@ +from test.parser.pattern.test_nodes.base import PatternTestBaseClass +from programy.dialog import Sentence +from programy.parser.pattern.nodes import * + + +class PatternWordNodeTests(PatternTestBaseClass): + + def test_init(self): + node = PatternWordNode("test1") + + self.assertFalse(node.is_root()) + self.assertFalse(node.is_priority()) + self.assertFalse(node.is_wildcard()) + self.assertFalse(node.is_zero_or_more()) + self.assertFalse(node.is_one_or_more()) + self.assertIsNotNone(node.children) + self.assertFalse(node.has_children()) + + self.assertTrue(node.equivalent(PatternWordNode("test1"))) + self.assertFalse(node.is_root()) + self.assertEqual(node.to_string(), "WORD [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] word=[test1]") + + node.add_child(PatternWordNode("test2")) + self.assertEqual(len(node.children), 1) + self.assertEqual(node.to_string(), "WORD [P(0)^(0)#(0)C(1)_(0)*(0)To(0)Th(0)Te(0)] word=[test1]") diff --git a/src/test/parser/pattern/test_nodes/test_zero_or_more.py b/src/test/parser/pattern/test_nodes/test_zero_or_more.py new file mode 100644 index 000000000..5fd721516 --- /dev/null +++ b/src/test/parser/pattern/test_nodes/test_zero_or_more.py @@ -0,0 +1,37 @@ +from test.parser.pattern.test_nodes.base import PatternTestBaseClass +from programy.parser.pattern.nodes import * + + +class PatternZeroOrMoreWildCardNodeTests(PatternTestBaseClass): + + def test_invalid_wildcard(self): + with self.assertRaises(ParserException) as raised: + node = PatternZeroOrMoreWildCardNode("X") + self.assertIsNone(node) + + def test_init(self): + node = PatternZeroOrMoreWildCardNode("#") + + self.assertFalse(node.is_root()) + self.assertFalse(node.is_priority()) + self.assertTrue(node.is_wildcard()) + self.assertTrue(node.is_zero_or_more()) + self.assertFalse(node.is_one_or_more()) + self.assertIsNotNone(node.children) + self.assertFalse(node.has_children()) + + self.assertEqual(node.wildcard, "#") + + self.assertTrue(node.equivalent(PatternZeroOrMoreWildCardNode("#"))) + self.assertFalse(node.is_root()) + self.assertEqual(node.to_string(), "ZEROORMORE [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] wildcard=[#]") + + node = PatternZeroOrMoreWildCardNode("^") + self.assertIsNotNone(node) + + self.assertEqual(node.wildcard, "^") + + self.assertTrue(node.equivalent(PatternZeroOrMoreWildCardNode("^"))) + self.assertFalse(node.is_root()) + self.assertEqual(node.to_string(), "ZEROORMORE [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] wildcard=[^]") + diff --git a/src/test/parser/template/graph/test_basics.py b/src/test/parser/template/graph/test_basics.py index f728d2d5b..2d7a2c4e3 100644 --- a/src/test/parser/template/graph/test_basics.py +++ b/src/test/parser/template/graph/test_basics.py @@ -1,34 +1,10 @@ import unittest import xml.etree.ElementTree as ET -from programy.bot import Bot -from programy.brain import Brain -from programy.config import ClientConfiguration, BrainConfiguration -from programy.dialog import Question, Sentence -from programy.parser.template.graph import TemplateGraph from programy.parser.template.nodes import * +from test.parser.template.graph.test_graph_client import TemplateGraphTestClient - -class TemplateGraphBasicTests(unittest.TestCase): - - def setUp(self): - self.parser = TemplateGraph() - self.assertIsNotNone(self.parser) - - self.test_brain = None - self.test_sentence = Sentence("test sentence") - self.test_sentence._stars = ['one', 'two', 'three', 'four', 'five', 'six'] - self.test_sentence._thatstars = ["*"] - self.test_sentence._topicstars = ["*"] - - test_config = ClientConfiguration() - - self.test_bot = Bot(Brain(BrainConfiguration()), config=test_config.bot_configuration) - self.test_clientid = "testid" - - conversation = self.test_bot.get_conversation(self.test_clientid) - question = Question.create_from_sentence(self.test_sentence) - conversation._questions.append(question) +class TemplateGraphBasicTests(TemplateGraphTestClient): def test_template_no_content(self): template = ET.fromstring(""" diff --git a/src/test/parser/template/graph/test_bot.py b/src/test/parser/template/graph/test_bot.py index 6c83a44e7..7f34bef3d 100644 --- a/src/test/parser/template/graph/test_bot.py +++ b/src/test/parser/template/graph/test_bot.py @@ -1,34 +1,9 @@ import unittest -import xml.etree.ElementTree as ET - -from programy.bot import Bot -from programy.brain import Brain -from programy.config import ClientConfiguration, BrainConfiguration -from programy.dialog import Question, Sentence -from programy.parser.template.graph import TemplateGraph from programy.parser.template.nodes import * +from test.parser.template.graph.test_graph_client import TemplateGraphTestClient -class TemplateGraphBotTests(unittest.TestCase): - - def setUp(self): - self.parser = TemplateGraph() - self.assertIsNotNone(self.parser) - - self.test_brain = None - self.test_sentence = Sentence("test sentence") - self.test_sentence._stars = ['one', 'two', 'three', 'four', 'five', 'six'] - self.test_sentence._thatstars = ["*"] - self.test_sentence._topicstars = ["*"] - - test_config = ClientConfiguration() - - self.test_bot = Bot(Brain(BrainConfiguration()), config=test_config.bot_configuration) - self.test_clientid = "testid" - - conversation = self.test_bot.get_conversation(self.test_clientid) - question = Question.create_from_sentence(self.test_sentence) - conversation._questions.append(question) +class TemplateGraphBotTests(TemplateGraphTestClient): def test_bot_name_as_attrib(self): template = ET.fromstring(""" diff --git a/src/test/parser/template/graph/test_condition.py b/src/test/parser/template/graph/test_condition.py index c3975af33..083785dbf 100644 --- a/src/test/parser/template/graph/test_condition.py +++ b/src/test/parser/template/graph/test_condition.py @@ -1,34 +1,10 @@ import unittest -import xml.etree.ElementTree as ET -from programy.bot import Bot -from programy.brain import Brain -from programy.config import ClientConfiguration, BrainConfiguration -from programy.dialog import Question, Sentence -from programy.parser.template.graph import TemplateGraph from programy.parser.template.nodes import * +from test.parser.template.graph.test_graph_client import TemplateGraphTestClient -class TemplateGraphConditionTests(unittest.TestCase): - - def setUp(self): - self.parser = TemplateGraph() - self.assertIsNotNone(self.parser) - - self.test_brain = None - self.test_sentence = Sentence("test sentence") - self.test_sentence._stars = ['one', 'two', 'three', 'four', 'five', 'six'] - self.test_sentence._thatstars = ["*"] - self.test_sentence._topicstars = ["*"] - - test_config = ClientConfiguration() - - self.test_bot = Bot(Brain(BrainConfiguration()), config=test_config.bot_configuration) - self.test_clientid = "testid" - - conversation = self.test_bot.get_conversation(self.test_clientid) - question = Question.create_from_sentence(self.test_sentence) - conversation._questions.append(question) +class TemplateGraphConditionTests(TemplateGraphTestClient): def test_condition_template_no_attribs(self): template = ET.fromstring(""" diff --git a/src/test/parser/template/graph/test_datetime.py b/src/test/parser/template/graph/test_datetime.py index 33636f682..e29702a86 100644 --- a/src/test/parser/template/graph/test_datetime.py +++ b/src/test/parser/template/graph/test_datetime.py @@ -1,34 +1,9 @@ import unittest -import xml.etree.ElementTree as ET - -from programy.bot import Bot -from programy.brain import Brain -from programy.config import ClientConfiguration, BrainConfiguration -from programy.dialog import Question, Sentence -from programy.parser.template.graph import TemplateGraph from programy.parser.template.nodes import * +from test.parser.template.graph.test_graph_client import TemplateGraphTestClient -class TemplateGraphDateTimeTests(unittest.TestCase): - - def setUp(self): - self.parser = TemplateGraph() - self.assertIsNotNone(self.parser) - - self.test_brain = None - self.test_sentence = Sentence("test sentence") - self.test_sentence._stars = ['one', 'two', 'three', 'four', 'five', 'six'] - self.test_sentence._thatstars = ["*"] - self.test_sentence._topicstars = ["*"] - - test_config = ClientConfiguration() - - self.test_bot = Bot(Brain(BrainConfiguration()), config=test_config.bot_configuration) - self.test_clientid = "testid" - - conversation = self.test_bot.get_conversation(self.test_clientid) - question = Question.create_from_sentence(self.test_sentence) - conversation._questions.append(question) +class TemplateGraphDateTimeTests(TemplateGraphTestClient): def test_date_format_as_attrib(self): template = ET.fromstring(""" diff --git a/src/test/parser/template/graph/test_extension.py b/src/test/parser/template/graph/test_extension.py index 57e8c0a37..ddf20a228 100644 --- a/src/test/parser/template/graph/test_extension.py +++ b/src/test/parser/template/graph/test_extension.py @@ -1,12 +1,7 @@ import unittest -import xml.etree.ElementTree as ET - -from programy.bot import Bot -from programy.brain import Brain -from programy.config import ClientConfiguration, BrainConfiguration -from programy.dialog import Question, Sentence -from programy.parser.template.graph import TemplateGraph from programy.parser.template.nodes import * +from test.parser.template.graph.test_graph_client import TemplateGraphTestClient + class TestExtension(object): @@ -14,26 +9,8 @@ def execute(self, data): print(data) return "executed" -class TemplateGraphBotTests(unittest.TestCase): - - def setUp(self): - self.parser = TemplateGraph() - self.assertIsNotNone(self.parser) - - self.test_brain = None - self.test_sentence = Sentence("test sentence") - self.test_sentence._stars = ['one', 'two', 'three', 'four', 'five', 'six'] - self.test_sentence._thatstars = ["*"] - self.test_sentence._topicstars = ["*"] - - test_config = ClientConfiguration() - - self.test_bot = Bot(Brain(BrainConfiguration()), config=test_config.bot_configuration) - self.test_clientid = "testid" - conversation = self.test_bot.get_conversation(self.test_clientid) - question = Question.create_from_sentence(self.test_sentence) - conversation._questions.append(question) +class TemplateGraphBotTests(TemplateGraphTestClient): def test_extension_as_attrib(self): template = ET.fromstring(""" diff --git a/src/test/parser/template/graph/test_get.py b/src/test/parser/template/graph/test_get.py index 63401237b..a0925b593 100644 --- a/src/test/parser/template/graph/test_get.py +++ b/src/test/parser/template/graph/test_get.py @@ -1,34 +1,9 @@ import unittest -import xml.etree.ElementTree as ET - -from programy.bot import Bot -from programy.brain import Brain -from programy.config import ClientConfiguration, BrainConfiguration -from programy.dialog import Question, Sentence -from programy.parser.template.graph import TemplateGraph from programy.parser.template.nodes import * +from test.parser.template.graph.test_graph_client import TemplateGraphTestClient -class TemplateGraphGetTests(unittest.TestCase): - - def setUp(self): - self.parser = TemplateGraph() - self.assertIsNotNone(self.parser) - - self.test_brain = None - self.test_sentence = Sentence("test sentence") - self.test_sentence._stars = ['one', 'two', 'three', 'four', 'five', 'six'] - self.test_sentence._thatstars = ["*"] - self.test_sentence._topicstars = ["*"] - - test_config = ClientConfiguration() - - self.test_bot = Bot(Brain(BrainConfiguration()), config=test_config.bot_configuration) - self.test_clientid = "testid" - - conversation = self.test_bot.get_conversation(self.test_clientid) - question = Question.create_from_sentence(self.test_sentence) - conversation._questions.append(question) +class TemplateGraphGetTests(TemplateGraphTestClient): def test_get_template_predicate_as_attrib(self): template = ET.fromstring(""" diff --git a/src/test/parser/template/graph/test_graph_client.py b/src/test/parser/template/graph/test_graph_client.py new file mode 100644 index 000000000..9787edbea --- /dev/null +++ b/src/test/parser/template/graph/test_graph_client.py @@ -0,0 +1,41 @@ +import unittest +import xml.etree.ElementTree as ET + +from programy.bot import Bot +from programy.brain import Brain +from programy.config import ClientConfiguration, BrainConfiguration +from programy.dialog import Question, Sentence +from programy.parser.template.graph import TemplateGraph +from programy.parser.pattern.matcher import MatchContext, Match +from programy.parser.pattern.nodes import PatternOneOrMoreWildCardNode +from programy.parser.aiml_parser import AIMLParser + +class TemplateGraphTestClient(unittest.TestCase): + + def setUp(self): + self.parser = TemplateGraph(AIMLParser()) + self.assertIsNotNone(self.parser) + + self.test_brain = None + self.test_sentence = Sentence("test sentence") + + test_node = PatternOneOrMoreWildCardNode("*") + + self.test_sentence._matched_context = MatchContext() + self.test_sentence._matched_context._matched_nodes = [Match(Match.WORD, test_node, 'one'), + Match(Match.WORD, test_node, 'two'), + Match(Match.WORD, test_node, 'three'), + Match(Match.WORD, test_node, 'four'), + Match(Match.WORD, test_node, 'five'), + Match(Match.WORD, test_node, 'six'), + Match(Match.TOPIC, test_node, '*'), + Match(Match.THAT, test_node, '*')] + + test_config = ClientConfiguration() + + self.test_bot = Bot(Brain(BrainConfiguration()), config=test_config.bot_configuration) + self.test_clientid = "testid" + + conversation = self.test_bot.get_conversation(self.test_clientid) + question = Question.create_from_sentence(self.test_sentence) + conversation._questions.append(question) diff --git a/src/test/parser/template/graph/test_learn.py b/src/test/parser/template/graph/test_learn.py index 3dbbec835..357a5f193 100644 --- a/src/test/parser/template/graph/test_learn.py +++ b/src/test/parser/template/graph/test_learn.py @@ -1,37 +1,13 @@ import unittest -import xml.etree.ElementTree as ET -from programy.bot import Bot -from programy.brain import Brain -from programy.config import ClientConfiguration, BrainConfiguration -from programy.dialog import Question, Sentence -from programy.parser.template.graph import TemplateGraph from programy.parser.template.nodes import * -from programy.parser.aiml_parser import AIMLParser from programy.config import BrainFileConfiguration +from test.parser.template.graph.test_graph_client import TemplateGraphTestClient -class TemplateGraphLearnTests(unittest.TestCase): - def setUp(self): - self.parser = TemplateGraph(AIMLParser()) - self.assertIsNotNone(self.parser) +class TemplateGraphLearnTests(TemplateGraphTestClient): - self.test_brain = None - self.test_sentence = Sentence("test sentence") - self.test_sentence._stars = ['one', 'two', 'three', 'four', 'five', 'six'] - self.test_sentence._thatstars = ["*"] - self.test_sentence._topicstars = ["*"] - - test_config = ClientConfiguration() - - self.test_bot = Bot(Brain(BrainConfiguration()), config=test_config.bot_configuration) - self.test_clientid = "testid" - - conversation = self.test_bot.get_conversation(self.test_clientid) - question = Question.create_from_sentence(self.test_sentence) - conversation._questions.append(question) - - def test_eval_simple(self): + def test_eval_simple(self): template = ET.fromstring("""