def setUp(self): type_vars =\ {'main_var': {'name': 'Product Name', 'codename': 'productname'}, 'cat_vars': {'name': ['Brand', 'Category'], 'codename': ['brand', 'category']}, 'label_var': {'name': 'Subscription Plan'}} responses_formatter =\ {'main_var': (lambda l: ''.join(['\n * '+s for s in l]), 'query_productnames'), 'cat_vars': {'Brand': joiner_brands, 'Category': joiner_categories}, 'label_var': (lambda l, p: ''.join(['\n * '+r+': '+s+"€" for r, s in zip(l, p)]), 'query_productnames'), 'join_cats': (category_joiner, 'query_catnames') } parameter_formatter =\ {'label': price_q_keywords} ## Test the constructor datafile = os.path.join(os.path.abspath(__file__), '../../datasets/products.csv') datafile = os.path.abspath(datafile) # data = DataBaseAPI(pd.read_csv(datafile, index_col=0), type_vars, # responses_formatter) self.data = DataBaseAPI(datafile, type_vars, responses_formatter, parameter_formatter) ## Get keywords for queries keywords_cat = [] for e in self.data.categories.keys(): keywords_cat += self.data.categories[e] keywords_main =\ list(self.data.data[self.data.type_vars['main_var']['name']]) self.keywords_cat = keywords_cat self.keywords_main = keywords_main ## Message self.message =\ {'query': {'query_idxs': [], 'query_names': [], 'query_result': {}, 'query_pars': {'label': True}}, 'answer_names': []} self.create_pre = lambda x: { 'query_idxs': x, 'query_result': { 'query_type': True } } ## Files to test package_path =\ os.path.dirname(os.path.dirname(os.path.abspath(__file__))) self.example_db_yaml =\ os.path.join(os.path.dirname(package_path), 'examples/example_dbparser/db_connection.yml')
def __init__(self, profile_user=None, logging_file=None, databases=None): self.profile_user = ProfileUser(profile_user) self.messagesDB = [] self.queriesDB = [] self.databases = {} if databases is None: pass elif type(databases) == dict: self.databases.update(databases) for d, v in self.databases.items(): if isinstance(v, dict): self.databases[d] = DataBaseAPI.from_parameters(v) # assert(isinstance(self.databases[d], DataBaseAPI)) else: ## It should be DataBaseAPI object self.databases = {'db': databases}
def create_handler_ui_test(configuration_file, db_configuration_file): ## Parser parameters parameters = parse_configuration_file(configuration_file) db_pars = parse_configuration_file_dbapi(db_configuration_file) ## Testing state machine testing_pars = create_testing_mode_parameters(parameters) if not isinstance(testing_pars, dict): testing_pars = testing_pars[0] statemachine = ConversationStateMachine.from_parameters(testing_pars) ## DB setting db_api = DataBaseAPI(**db_pars) handler_db = HandlerConvesationDB(databases=db_api) ## Handler tester_handler = TerminalUIHandler(handler_db, statemachine) return tester_handler
def __init__(self, configuration_file, db_configuration_file): ## Parser parameters parameters = parse_configuration_file(configuration_file) db_pars = parse_configuration_file_dbapi(db_configuration_file) ## Obtain description states_list_table, path_states, xstates, transitions =\ create_tables(parameters) treestates, graphs_statemachines, complete_network =\ create_graphs(states_list_table, path_states, xstates, transitions) self.complete_network = complete_network ## Testing state machine testing_pars = create_testing_mode_parameters(parameters) if not isinstance(testing_pars, dict): testing_pars = testing_pars[0] statemachine = ConversationStateMachine.from_parameters(testing_pars) ## DB setting db_api = DataBaseAPI(**db_pars) handler_db = HandlerConvesationDB(databases=db_api) super().__init__(handler_db, statemachine) ## Functions def ifstate(pathname): aux = states_list_table[states_list_table['Pathname'] == pathname] aux = list(aux['IfStateMachine'])[0] return not aux def get_sonstate(path): if ifstate(path): return path path = path_builder_states(xstates[path]['StartState'], path) if not ifstate(path): return get_sonstate(path) return path ## Initial setting initials = states_list_table[states_list_table['level'] == 0] initial_state = get_sonstate(list(initials['Pathname'])[0]) self.current_state = initial_state[:]
class Test_DataBaseAPI(unittest.TestCase): """Testing and standarization of the interface of DBAPI. """ def setUp(self): type_vars =\ {'main_var': {'name': 'Product Name', 'codename': 'productname'}, 'cat_vars': {'name': ['Brand', 'Category'], 'codename': ['brand', 'category']}, 'label_var': {'name': 'Subscription Plan'}} responses_formatter =\ {'main_var': (lambda l: ''.join(['\n * '+s for s in l]), 'query_productnames'), 'cat_vars': {'Brand': joiner_brands, 'Category': joiner_categories}, 'label_var': (lambda l, p: ''.join(['\n * '+r+': '+s+"€" for r, s in zip(l, p)]), 'query_productnames'), 'join_cats': (category_joiner, 'query_catnames') } parameter_formatter =\ {'label': price_q_keywords} ## Test the constructor datafile = os.path.join(os.path.abspath(__file__), '../../datasets/products.csv') datafile = os.path.abspath(datafile) # data = DataBaseAPI(pd.read_csv(datafile, index_col=0), type_vars, # responses_formatter) self.data = DataBaseAPI(datafile, type_vars, responses_formatter, parameter_formatter) ## Get keywords for queries keywords_cat = [] for e in self.data.categories.keys(): keywords_cat += self.data.categories[e] keywords_main =\ list(self.data.data[self.data.type_vars['main_var']['name']]) self.keywords_cat = keywords_cat self.keywords_main = keywords_main ## Message self.message =\ {'query': {'query_idxs': [], 'query_names': [], 'query_result': {}, 'query_pars': {'label': True}}, 'answer_names': []} self.create_pre = lambda x: { 'query_idxs': x, 'query_result': { 'query_type': True } } ## Files to test package_path =\ os.path.dirname(os.path.dirname(os.path.abspath(__file__))) self.example_db_yaml =\ os.path.join(os.path.dirname(package_path), 'examples/example_dbparser/db_connection.yml') def test_instance_from_parameters(self): parameters = parse_configuration_file_dbapi(self.example_db_yaml) DataBaseAPI.from_parameters(parameters) def test_query(self): ## Query for categories ids_cat, pars = self.data.query(self.keywords_cat) ids_cat, pars =\ self.data.query(self.keywords_cat, pre=self.create_pre(ids_cat)) ## Query main ids_main, pars = self.data.query(self.keywords_main) ids_main, pars =\ self.data.query(self.keywords_main, pre=self.create_pre(ids_main)) ## Cross queries ids, pars =\ self.data.query(self.keywords_cat, pre=self.create_pre(ids_main)) ids, pars =\ self.data.query(self.keywords_main, pre=self.create_pre(ids_cat)) ## Get label labels = self.data.get_label(ids) def test_complete_query(self): q_cat = self.data.get_query_info(self.keywords_cat) q_main = self.data.get_query_info(self.keywords_main) q_info = self.data.get_query_info(self.keywords_cat, pre=q_cat['query']) q_info = self.data.get_query_info(self.keywords_main, pre=q_cat['query']) q_info = self.data.get_query_info(self.keywords_cat, pre=q_main['query']) q_info = self.data.get_query_info(self.keywords_main, pre=q_cat['query'], label=True) q_info = self.data.get_query_info(self.keywords_cat, pre=q_main['query'], label=True) q_info = self.data.get_query_info(self.keywords_cat, pre=q_info['query']) q_info = self.data.get_query_info(self.keywords_main, pre=q_main['query']) q_info = self.data.get_query_info(self.keywords_main, pre=q_main['query']) def test_get_message_reflection(self): self.data.get_reflection_query(self.message)
def test_instance_from_parameters(self): parameters = parse_configuration_file_dbapi(self.example_db_yaml) DataBaseAPI.from_parameters(parameters)
conversa = ConversationStateMachine('Conversation', conv_states, 'Hello', end_states) ########################################################################### ## DB information type_vars =\ {'main_var': {'name': 'Product Name', 'codename': 'productname'}, 'cat_vars': {'name': ['Brand', 'Category'], 'codename': ['brand', 'category']}, 'label_var': {'name': 'Subscription Plan'}} responses_formatter =\ {'main_var': (lambda l: ''.join(['\n'+' '*8+'* '+s for s in l]), 'query_productnames'), 'cat_vars': {'Brand': joiner_brands, 'Category': joiner_categories}, 'label_var': (lambda l, p: ''.join(['\n'+' '*8+'* '+r+': '+s+"€/month" for r, s in zip(l, p)]), 'query_productnames'), 'join_cats': (category_joiner, 'query_catnames') } parameter_formatter =\ {'label': price_q_keywords} db_api = DataBaseAPI(datapath, type_vars, responses_formatter, parameter_formatter) handler_db = HandlerConvesationDB(databases=db_api) ## Running a conversation handler_ui = TerminalUIHandler(handler_db, conversa) handler_ui.run()