示例#1
0
    def test_load(self):
        storage_factory = StorageFactory()

        file_store_config = FileStorageConfiguration()
        file_store_config._gender_storage = FileStoreConfiguration(
            file=os.path.dirname(__file__) + os.sep + "test_files" + os.sep +
            "gender.txt",
            fileformat="text",
            extension="txt",
            encoding="utf-8",
            delete_on_start=False)

        storage_engine = FileStorageEngine(file_store_config)

        storage_factory._storage_engines[
            StorageFactory.GENDER] = storage_engine
        storage_factory._store_to_engine_map[
            StorageFactory.GENDER] = storage_engine

        collection = GenderCollection()
        self.assertIsNotNone(collection)

        self.assertTrue(collection.load(storage_factory))

        self.assertEqual(collection.gender(" WITH HIM "), [
            re.compile('(^WITH HIM | WITH HIM | WITH HIM$)', re.IGNORECASE),
            ' WITH HER '
        ])
        self.assertEqual(collection.genderise_string("This is with him "),
                         "This is with her")
示例#2
0
    def __init__(self, configuration: BrainConfiguration):
        self._configuration = configuration
        self._aiml_parser = AIMLParser(self)

        self._denormal_collection = DenormalCollection()
        self._normal_collection = NormalCollection()
        self._gender_collection = GenderCollection()
        self._person_collection = PersonCollection()
        self._person2_collection = PersonCollection()
        self._rdf_collection = RDFCollection()
        self._sets_collection = SetCollection()
        self._maps_collection = MapCollection()
        self._properties_collection = PropertiesCollection()

        self._preprocessors = ProcessorLoader()
        self._postprocessors = ProcessorLoader()

        self._authentication = None
        self._authorisation = None

        self._default_oob = None
        self._oob = {}

        self._regex_templates = {}

        self._dynamics_collection = DynamicsCollection()

        self.load(self._configuration)
示例#3
0
    def test_reload_jp(self):
        storage_factory = StorageFactory()
        tokenizer = TokenizerJP()

        file_store_config = FileStorageConfiguration()
        file_store_config._gender_storage = FileStoreConfiguration(
            file=os.path.dirname(__file__) + os.sep + "test_files" + os.sep +
            "gender_jp.txt",
            format="text",
            extension="txt",
            encoding="utf-8",
            delete_on_start=False)

        storage_engine = FileStorageEngine(file_store_config)

        storage_factory._storage_engines[
            StorageFactory.GENDER] = storage_engine
        storage_factory._store_to_engine_map[
            StorageFactory.GENDER] = storage_engine

        collection = GenderCollection()
        self.assertIsNotNone(collection)

        collection.load(storage_factory)

        self.assertEqual(collection.gender("彼"), '彼女')
        self.assertEqual(collection.genderise_string(tokenizer, "彼が来た"),
                         "彼女が来た")

        collection.reload(storage_factory)

        self.assertEqual(collection.gender("彼"), '彼女')
        self.assertEqual(collection.genderise_string(tokenizer, "彼が来た"),
                         "彼女が来た")
    def assert_upload_from_text(self, store):

        store.empty()

        store.upload_from_text(
            None, """
                                " with him "," with her "
                                " with her "," with him "
                                " to him "," to her "
                                " to her "," to him "
                                " on him "," on her "
                                " on her "," on him "
                                " in him "," in her "
                                " in her "," in him "
                                " for him "," for her "
                                " for her "," for him "
                                " he "," she "
                                " his "," her "
                                " him "," her "
                                " her "," his "
                                " she "," he "
                                """)

        collection = GenderCollection()
        store.load(collection)

        self.assertEqual(collection.gender(" WITH HIM "), [
            re.compile('(^WITH HIM | WITH HIM | WITH HIM$)', re.IGNORECASE),
            ' WITH HER '
        ])
        self.assertEqual(collection.genderise_string("This is with him "),
                         "This is with her")
示例#5
0
    def test_collection(self):
        collection = GenderCollection()
        self.assertIsNotNone(collection)

        count = collection.load_from_text("""
                " with him "," with her "
                " with her "," with him "
                " to him "," to her "
                " to her "," to him "
                " on him "," on her "
                " on her "," on him "
                " in him "," in her "
                " in her "," in him "
                " for him "," for her "
                " for her "," for him "
                " he "," she "
                " his "," her "
                " him "," her "
                " her "," his "
                " she "," he "
        """)
        self.assertEqual(count, 15)

        self.assertEqual(collection.genderise_string("This is with him "),
                         "This is with her")

        #self.assertEqual("(^with him | with him | with him$)", collection.gender(" with him "))
        self.assertIsNone(collection.gender("unknown"))
示例#6
0
文件: brain.py 项目: rkc007/program-y
    def __init__(self, bot, configuration: BrainConfiguration):
        self._bot = bot
        self._configuration = configuration

        self._tokenizer = self.load_tokenizer()

        self._aiml_parser = self.load_aiml_parser()

        self._denormal_collection = DenormalCollection()
        self._normal_collection = NormalCollection()
        self._gender_collection = GenderCollection()
        self._person_collection = PersonCollection()
        self._person2_collection = PersonCollection()
        self._rdf_collection = RDFCollection()
        self._sets_collection = SetCollection()
        self._maps_collection = MapCollection()
        self._properties_collection = PropertiesCollection()
        self._variables_collection = PropertiesCollection()

        self._preprocessors = ProcessorLoader()
        self._postprocessors = ProcessorLoader()

        self._authentication = None
        self._authorisation = None

        self._default_oob = None
        self._oob = {}

        self._regex_templates = {}

        self._dynamics_collection = DynamicsCollection()

        self.load(self.configuration)

        self.dump_brain_tree()
示例#7
0
    def test_collection_duplicate(self):
        collection = GenderCollection()
        self.assertIsNotNone(collection)

        collection.add_to_lookup("with him", 'with her')
        collection.add_to_lookup("with him", 'with you')

        self.assertEqual(collection.gender("WITH HIM"), 'with her')
示例#8
0
    def test_collection_duplicate_jp(self):
        collection = GenderCollection()
        self.assertIsNotNone(collection)

        collection.add_to_lookup("彼", '彼女')
        collection.add_to_lookup("彼", '彼氏')

        self.assertEqual(collection.gender("彼"), '彼女')
示例#9
0
    def test_collection_operations(self):
        collection = GenderCollection()
        self.assertIsNotNone(collection)

        collection.add_to_lookup(" WITH HIM ", [re.compile('(^WITH HIM | WITH HIM | WITH HIM$)', re.IGNORECASE), ' WITH HER '])

        self.assertTrue(collection.has_key(" WITH HIM "))
        self.assertEqual([re.compile('(^WITH HIM | WITH HIM | WITH HIM$)', re.IGNORECASE), ' WITH HER '], collection.value(" WITH HIM "))

        self.assertEqual(collection.gender(" WITH HIM "), [re.compile('(^WITH HIM | WITH HIM | WITH HIM$)', re.IGNORECASE), ' WITH HER '])
        self.assertEqual(collection.genderise_string("This is with him "), "This is with her")
示例#10
0
    def assert_upload_csv_file(self, store, filename):

        store.empty()

        store.upload_from_file(filename, format=Store.CSV_FORMAT)

        collection = GenderCollection()
        store.load(collection)

        self.assertEqual(collection.gender(" WITH HIM "), [re.compile('(^WITH HIM | WITH HIM | WITH HIM$)', re.IGNORECASE), ' WITH HER '])
        self.assertEqual(collection.genderise_string("This is with him "), "This is with her")
示例#11
0
    def assert_upload_from_text_file(self, store):

        store.empty()

        store.upload_from_file(os.path.dirname(__file__) + os.sep + "data" + os.sep + "lookups" + os.sep + "text" + os.sep + "gender.txt")

        collection = GenderCollection()
        store.load(collection)

        self.assertEqual(collection.gender(" WITH HIM "), [re.compile('(^WITH HIM | WITH HIM | WITH HIM$)', re.IGNORECASE), ' WITH HER '])
        self.assertEqual(collection.genderise_string("This is with him "), "This is with her")
示例#12
0
    def test_collection_operations(self):
        collection = GenderCollection()
        self.assertIsNotNone(collection)

        collection.add_to_lookup("with him", 'with her')

        self.assertTrue(collection.has_keyVal("WITH HIM"))
        self.assertEqual('with her', collection.value("WITH HIM"))

        self.assertEqual(collection.gender("WITH HIM"), 'with her')
        self.assertEqual(collection.genderise_string(None, "This is with him"),
                         "This is with her")
示例#13
0
    def test_collection_invalid(self):
        collection = GenderCollection()
        self.assertIsNotNone(collection)

        collection.add_to_lookup("with him", 'with her')

        self.assertFalse(collection.has_keyVal("WITH YOU"))
        self.assertIsNone(collection.value("WITH YOU"))

        self.assertIsNone(collection.gender("WITH YOU"))
        self.assertEqual(collection.genderise_string(None, "This is with you"),
                         "This is with you")
示例#14
0
    def test_collection_invalid_JP(self):
        collection = GenderCollection()
        self.assertIsNotNone(collection)

        collection.add_to_lookup("彼", '彼女')

        self.assertFalse(collection.has_keyVal("彼氏"))
        self.assertIsNone(collection.value("彼氏"))

        tokenizer = TokenizerJP()
        self.assertIsNone(collection.gender("彼氏"))
        self.assertEqual(collection.genderise_string(tokenizer, "彼氏が来た"),
                         "彼氏が来た")
示例#15
0
    def test_collection_operations_JP(self):
        collection = GenderCollection()
        self.assertIsNotNone(collection)

        collection.add_to_lookup("彼", '彼女')
        tokenizer = TokenizerJP()

        self.assertTrue(collection.has_keyVal("彼"))
        self.assertEqual('彼女', collection.value("彼"))

        self.assertEqual(collection.gender("彼"), '彼女')
        self.assertEqual(collection.genderise_string(tokenizer, "彼が来た"),
                         "彼女が来た")
示例#16
0
    def test_load_from_file(self):
        config = FileStorageConfiguration()
        config._gender_storage = FileStoreConfiguration(file=os.path.dirname(__file__) + os.sep + "data" + os.sep + "lookups" + os.sep + "text" + os.sep + "gender.txt", fileformat="text", encoding="utf-8", delete_on_start=False)
        engine = FileStorageEngine(config)
        engine.initialise()
        store = FileGenderStore(engine)

        gender_collection = GenderCollection()
        
        store.load(gender_collection)

        self.assertEqual(gender_collection.gender(" WITH HIM "), [re.compile('(^WITH HIM | WITH HIM | WITH HIM$)', re.IGNORECASE), ' WITH HER '])
        self.assertEqual(gender_collection.genderise_string("This is with him "), "This is with her")
示例#17
0
    def __init__(self, bot, configuration: BrainConfiguration):

        assert bot is not None
        assert configuration is not None

        self._questions = 0

        self._bot = bot
        self._configuration = configuration

        self._pattern_factory = None
        self._template_factory = None

        self._binaries = BinariesManager(configuration.binaries)
        self._braintree = BraintreeManager(configuration.braintree)

        self._tokenizer = Tokenizer.load_tokenizer(configuration.tokenizer)

        self._denormal_collection = DenormalCollection()
        self._normal_collection = NormalCollection()
        self._gender_collection = GenderCollection()
        self._person_collection = PersonCollection()
        self._person2_collection = Person2Collection()

        self._rdf_collection = RDFCollection()
        self._sets_collection = SetCollection()
        self._maps_collection = MapCollection()

        self._properties_collection = PropertiesCollection()
        self._default_variables_collection = DefaultVariablesCollection()
        self._regex_templates = RegexTemplatesCollection()
        self._dynamics_collection = DynamicsCollection()

        self._preprocessors = PreProcessorCollection()
        self._postprocessors = PostProcessorCollection()
        self._postquestionprocessors = PostQuestionProcessorCollection()

        self._services = ServiceHandler()

        self._oobhandler = OOBHandler()

        self._security = SecurityManager(configuration.security)

        self._aiml_parser = self.load_aiml_parser()

        self.load(self.configuration)
示例#18
0
    def __init__(self, configuration: BrainConfiguration):
        self._configuration = configuration
        self._aiml_parser = AIMLParser()

        self._denormal_collection = DenormalCollection()
        self._normal_collection = NormalCollection()
        self._gender_collection = GenderCollection()
        self._person_collection = PersonCollection()
        self._person2_collection = PersonCollection()
        self._predicates_collection = PredicatesCollection()
        self._pronouns_collection = PronounsCollection()
        self._triples_collection = TriplesCollection()
        self._sets_collection = SetCollection()
        self._maps_collection = MapCollection()
        self._properties_collection = PropertiesCollection()

        self._preprocessors = ProcessorLoader()
        self._postprocessors = ProcessorLoader()

        self.load(self._configuration)
示例#19
0
    def test_load_with_exception(self):
        storage_factory = StorageFactory()

        file_store_config = FileStorageConfiguration()
        file_store_config._gender_storage = FileStoreConfiguration(
            file=os.path.dirname(__file__) + os.sep + "test_files" + os.sep +
            "gender.txt",
            fileformat="text",
            extension="txt",
            encoding="utf-8",
            delete_on_start=False)

        storage_engine = FileStorageEngine(file_store_config)

        storage_factory._storage_engines[
            StorageFactory.GENDER] = storage_engine
        storage_factory._store_to_engine_map[
            StorageFactory.GENDER] = storage_engine

        collection = GenderCollection()
        self.assertIsNotNone(collection)

        self.assertFalse(collection.load(storage_factory))
示例#20
0
 def test_initialise_collection(self):
     collection = GenderCollection()
     self.assertIsNotNone(collection)
示例#21
0
    def __init__(self, bot, configuration: BrainConfiguration):

        assert (bot is not None)
        assert (configuration is not None)

        self._bot = bot
        self._configuration = configuration

        self._binaries = BinariesManager(configuration.binaries)
        self._braintree = BraintreeManager(configuration.braintree)
        self._tokenizer = Tokenizer.load_tokenizer(configuration)

        if configuration.debugfiles.save_errors_collection is True:
            errors_dict = {}
        else:
            errors_dict = None

        self._denormal_collection = DenormalCollection(errors_dict)
        self._normal_collection = NormalCollection(errors_dict)
        self._gender_collection = GenderCollection(errors_dict)
        self._person_collection = PersonCollection(errors_dict)
        self._person2_collection = Person2Collection(errors_dict)
        self._rdf_collection = RDFCollection(errors_dict)
        self._sets_collection = SetCollection(errors_dict)
        self._maps_collection = MapCollection(errors_dict)

        self._properties_collection = PropertiesCollection(errors_dict)
        self._default_variables_collection = DefaultVariablesCollection(
            errors_dict)
        self._botnames_collection = BotNamesCollection(errors_dict)

        self._preprocessors = PreProcessorCollection(errors_dict)
        self._postprocessors = PostProcessorCollection(errors_dict)

        self._pattern_factory = None
        self._template_factory = None

        self._security = SecurityManager(configuration.security)

        self._oobhandler = OOBHandler(configuration.oob)

        self._regex_templates = RegexTemplatesCollection(errors_dict)

        self._dynamics_collection = DynamicsCollection()

        self._aiml_parser = self.load_aiml_parser()

        self._nlu_collection = NluCollection(bot.client, configuration.nlu,
                                             errors_dict)
        self._nlu = NluRequest.load_nlu(configuration.nlu)
        self._nlu_utterance = None

        self.load(self.configuration)

        if configuration.debugfiles.save_errors_collection is True:
            storage_factory = self.bot.client.storage_factory
            if storage_factory.entity_storage_engine_available(
                    StorageFactory.ERRORS_COLLECTION) is True:
                errors_collection_engine = storage_factory.entity_storage_engine(
                    StorageFactory.ERRORS_COLLECTION)
                errors_collection_store = errors_collection_engine.errors_collection_store(
                )
                errors_collection_store.save_errors_collection(errors_dict)