<gh_stars>1-10


import sqlite3
import json

from jude.utilities import constants


class Database:

    transactions = []
    connections = sqlite3.connect(constants.DATABASE_JUDE)

    def __init__(self):
        self.connection = self.connections.cursor()

    def create_tables(self):

        try:
            self.connection.execute(constants.SQL_DROP_TABLE.format(constants.TABLE_PATTERN))
            self.connection.execute(constants.SQL_DROP_TABLE.format(constants.TABLE_RESPONSE))
        except sqlite3.OperationalError:
            pass

        self.connection.execute(constants.SQL_CREATE_PATTERN)
        self.connection.execute(constants.SQL_CREATE_RESPONSE)

    def fill_tables(self, dataset):
        with open(dataset) as json_data:
            replies = json.load(json_data)

        # loop through each sentence in our intents patterns
        for reply in replies['replies']:
            for pattern in reply['patterns']:
                self.connection.execute(
                    constants.SQL_INSERT_PATTERN.format(reply['tag'], pattern)
                )

            for response in reply['responses']:
                self.connection.execute(
                    constants.SQL_INSERT_RESPONSE.format(reply['tag'], response)
                )

import sqlite3
import json

from jude.utilities import constants


class Database:

    transactions = []
    connections = sqlite3.connect(constants.DATABASE_JUDE)

    def __init__(self):
        self.connection = self.connections.cursor()

    def create_tables(self):

        try:
            self.connection.execute(constants.SQL_DROP_TABLE.format(constants.TABLE_PATTERN))
            self.connection.execute(constants.SQL_DROP_TABLE.format(constants.TABLE_RESPONSE))
        except sqlite3.OperationalError:
            pass

        self.connection.execute(constants.SQL_CREATE_PATTERN)
        self.connection.execute(constants.SQL_CREATE_RESPONSE)

    def fill_tables(self, dataset):
        with open(dataset) as json_data:
            replies = json.load(json_data)

        # loop through each sentence in our intents patterns
        for reply in replies['replies']:
            for pattern in reply['patterns']:
                self.connection.execute(
                    constants.SQL_INSERT_PATTERN.format(reply['tag'], pattern)
                )

            for response in reply['responses']:
                self.connection.execute(
                    constants.SQL_INSERT_RESPONSE.format(reply['tag'], response)
                )
# loop through each sentence in our intents patterns