예제 #1
0
class ModelGenerator:
    """
    The class responsible for training the data and generating a model for a given classifier
    """
    def __init__(self, classifier):
        self.model = None
        self.scaler = None
        self.db_helper = DatabaseHelper()
        self.classifier = classifier

    def train(self):
        """
        Trains on data with an ML Classification Algorithm
        Creates a model and a scaler to be used for prediction
        Stores the model and the scaler in the database
        """
        x, y = self.get_data()
        if x and y:
            self.model, self.scaler = self.classifier.create_model(x, y)

    def get_data(self):
        """Returns the features(x) and the labels(y) to train on"""
        y_train = []
        x_train = []
        try:
            training_data = self.db_helper.check_and_fetch(False)
            rel_path = os.path.dirname(os.path.realpath(__file__))
            headers_file = os.path.join(rel_path, 'headers.csv')
            print headers_file
            header_string = open(headers_file)
            headers = header_string.read().split(',')
            for datum in training_data:
                x_train_row = []
                y_train.append(float(datum['label']))
                for header in headers[1:]:
                    x_train_row.append(float(datum[header]))
                x_train.append(x_train_row)
        except Exception:
            print Exception
        return x_train, y_train

    def store_model(self):
        """Stores the model in the database"""
        model = pickle.dumps(self.model)
        scaler = pickle.dumps(self.scaler)
        self.db_helper.put_model(model, scaler)
예제 #2
0
def __get_all_doable_sentence_card_vocab(learned_words):
    # Get literally all of the card info in the sentences deck
    query = "select n.flds "
    query += "from cards c "
    query += "join notes n on c.nid = n.id "
    query += "where c.did = 1547537208241 "
    query += "order by n.sfld"
    query_results = DatabaseHelper.execute(query)
    return TextParser.for_each_get_doable_ids(query_results, learned_words)
예제 #3
0
def __get_unstudied_sentence_card_ids():
    # Get IDs of sentence cards that are new and suspended (haven't studied)
    query = "select n.sfld "
    query += "from cards c "
    query += "join notes n on c.nid = n.id "
    query += "where c.type = 0 "
    query += "and c.queue = -1 "
    query += "and c.did = 1547537208241 "
    query += "order by n.sfld"
    return TextParser.for_each_trim_to_first(DatabaseHelper.execute(query))
예제 #4
0
def get_model():
    """
    Retrieves the global model and sends it back over the HTTP response
    The POST body should contain the user_model_time.
    eg:
    {"time":20161213-010905;}
    :return:
    """
    user_time = time.strptime(request.form['time'], "%Y%m%d-%H%M%S")
    db_helper = DatabaseHelper()
    latest_global_model = db_helper.get_latest_model()
    latest_global_scaler = db_helper.get_latest_scaler()
    for model, scaler in zip(latest_global_model, latest_global_scaler):
        model_time = time.strptime(model['time'], "%Y%m%d-%H%M%S")
        if model_time > user_time:
            ret_val = {"time": model["time"], "model": model["model"], "scaler": scaler["scaler"]}
            return jsonify(**ret_val)
        else:
            return "NO"
예제 #5
0
def __update_database_unsuspend_doable_cards(final_id_lst):
    """
    Gets all of the Sort ID's for the cards that contain
    only the vocabulary that you have learned. Then it
    generates a query which will update all of the cards
    that you need to learn by un-suspending them and setting
    them to 'New'. The query is as follows:
    :type final_id_lst: str
    """
    query = "update cards "
    query += "set queue = 0, type = 0 "
    query += "where nid in  "
    query += "( "
    query += "select id  "
    query += "from notes  "
    query += f"where sfld in ({final_id_lst}) "
    query += ") "
    query += "and did = 1547537208241"
    DatabaseHelper.execute(query)
    print(f"The following sentence cards were unsuspended:\n{final_id_lst}")
예제 #6
0
class ReadFile:
    filename = ""
    db_helper = DatabaseHelper()

    def __init__(self):
        self.filename = "data/rc_1LukeDriving.csv"

    def parse_csv(self):
        with open(self.filename) as f:
            for row in csv.DictReader(f):
                #print(row)
                self.db_helper.write_to_database(json.dumps(row))
예제 #7
0
def get_learned_rtk_kanji():
    """
    This method will return all of the words that you have learned from the
    RTK kanji deck as a list, using the below query.
    """
    query = "select n.flds "
    query += "from cards c "
    query += "join notes n on c.nid = n.id "
    query += "where c.type in (1,2) "
    query += "and c.did = 1554632049429"
    vocab = DatabaseHelper.execute(query)
    return TextParser.kanji_deck_only_main_vocab_word(vocab)
예제 #8
0
class MobileHelper:
    def __init__(self):
        self.db = DatabaseHelper()
        self.recipes_end_point = 'https://spoonacular-recipe-food-nutrition-v1.p.mashape.com/recipes/findByIngredients'
        self.analyzed_recipe_end_point = 'https://spoonacular-recipe-food-nutrition-v1.p.mashape.com/recipes/'

    def on_request(self, ch, method, props, body):
        response = []  # initialize response passed back to the bridge
        print('Message Received', body.decode('utf-8'))
        message = json.loads(body.decode('utf-8'))
        if 'type' not in message:
            response.append({
                'status':
                'BAD',
                'error':
                'Invalid syntax. Message must contain a type and data key.'
            })
        elif message['type'] == 'recipes':
            response = self.get_recipes(20, False)
        elif message['type'] == 'remove':
            if 'data' in message:
                for item in message['data']:
                    self.db.remove_item(str(item['name']),
                                        int(item['quantity']))
                response = self.db.get_all_items()
                # call bluetooth function
                self.bluetoothSend(response)
            return
        elif message['type'] == 'add':
            if 'data' in message:
                for item in message['data']:
                    self.db.add_item(str(item['name']), int(item['quantity']))
                response = self.db.get_all_items()
                # call bluetooth function
                self.bluetoothSend(response)
        elif message['type'] == 'price':
            response = self.get_price(message['data'])
        elif message['type'] == 'pantry':
            response = self.db.get_all_items()
            print(response)
        elif message['type'] == 'recipe':
            if 'data' in message:
                response = self.get_recipe(message['data'][0]['id'])

        ch.basic_publish(exchange='',
                         routing_key=str(props.reply_to),
                         properties=pika.BasicProperties(
                             correlation_id=props.correlation_id),
                         body=(json.dumps(response)))

    def get_recipes(self, number, fill_ingredients):
        pantry = self.db.get_all_items()
        pantry_str = ''
        for item in pantry:
            pantry_str += str(item['name']) + ','
        print(pantry_str)
        payload = {
            'fillIngredients': bool(fill_ingredients),
            'ingredients': pantry_str,
            'limitLicense': False,
            'number': int(number),
            'ranking': 2
        }
        response = requests.get(
            self.recipes_end_point,
            headers={
                "X-Mashape-Key":
                "DpzHfS6foEmshErpzeQcagXsunIip1DIy2Gjsn65PREQIIVopo",
                "Accept": "application/json"
            },
            params=payload)
        print(response.url)
        print(json.dumps(response.json(), indent=4, sort_keys=True))
        return response.json()

    def get_recipe(self, id):
        url = self.analyzed_recipe_end_point + str(
            id) + '/analyzedInstructions'
        payload = {'stepBreakdown': True}
        response = requests.get(
            url,
            headers={
                "X-Mashape-Key":
                "DpzHfS6foEmshErpzeQcagXsunIip1DIy2Gjsn65PREQIIVopo",
                "Accept": "application/json"
            },
            params=payload)
        print(response.url)
        print(json.dumps(response.json(), indent=4, sort_keys=True))
        steps = []
        if len(response.json()) == 0:
            return ['No steps found.']
        for s in response.json()[0]['steps']:
            steps.append(s['step'])
        print(steps)
        return steps

    def get_price(self, name):
        prices = []
        for n in name:
            print(n)
            response = requests.get(
                'http://api.walmartlabs.com/v1/search?apiKey=bsgcpte3pz8wxqaspmnjrs5n&query={'
                + n + '}&format=json')
            for it in response.json()['items']:
                if 'salePrice' in it:
                    prices.append(it['salePrice'])
                    break
        print(prices)
        return prices

    def bluetoothSend(self, items):
        sock = BluetoothSocket(RFCOMM)
        port = 1
        try:
            sock.connect(('B8:27:EB:F5:49:CC', port))
            for item in items:
                sock.send(json.dumps(item))
            sock.send('{"end": 0}')
            sock.close()
        except btcommon.BluetoothError as error:
            time.sleep(1)
            self.bluetoothSend(items)
예제 #9
0
 def __init__(self):
     self.db = DatabaseHelper()
     self.recipes_end_point = 'https://spoonacular-recipe-food-nutrition-v1.p.mashape.com/recipes/findByIngredients'
     self.analyzed_recipe_end_point = 'https://spoonacular-recipe-food-nutrition-v1.p.mashape.com/recipes/'
예제 #10
0
 def __init__(self, classifier):
     self.model = None
     self.scaler = None
     self.db_helper = DatabaseHelper()
     self.classifier = classifier