def retag_files(self): mp3_files = self.folders.retrieve_mp3_files(self.SOURCE_PATH) for mp3_file in mp3_files: complete_file_path = self.folders.join(self.SOURCE_PATH, mp3_file.name) mp3 = self.tags.open_mp3_file(complete_file_path) tags = self.tags.read_tags(mp3) if len(tags) != 2 or not tags.get('song', False): tags = self.tags.get_artist_and_song_from_file_name(mp3_file) tags = self.utils.capitalize(tags) source_file_path = self.folders.join( self.SOURCE_PATH, mp3_file.name, ) new_path = self.folders.copy_file( source_file_path, self.DESTINATION_PATH, '{}.mp3'.format(tags.get('song', mp3_file.name)), ) new_mp3_file = self.tags.open_mp3_file(new_path) self.tags.write_artist_and_song_tags(new_mp3_file, tags)
def get_t(object_id, question_id, number_of_objects): """ Returns the number of an object's descriptions that contain a specific tag """ global _descriptions if not _descriptions: _descriptions = [{} for _ in range(number_of_objects)] all_tags = tags.get_all() db.cursor.execute( 'SELECT description, objectID, descNum FROM Descriptions') for row in db.cursor.fetchall(): for tag in all_tags: if tag in row[0]: if not tag in _descriptions[row[1] - 1]: _descriptions[row[1] - 1][tag] = 1 else: _descriptions[row[1] - 1][tag] += 1 tag = tags.get(question_id) object_id = int(object_id) o = _descriptions[object_id - 1] if not tag in o: return 0 return o[tag]
def get_t(object_id, question_id, number_of_objects): """ Returns the number of an object's descriptions that contain a specific tag """ global _descriptions if not _descriptions: _descriptions = [{} for _ in range(number_of_objects)] all_tags = tags.get_all() db.cursor.execute('SELECT description, objectID, descNum FROM Descriptions') for row in db.cursor.fetchall(): for tag in all_tags: if tag in row[0]: if not tag in _descriptions[row[1]-1]: _descriptions[row[1]-1][tag] = 1 else: _descriptions[row[1]-1][tag] += 1 tag = tags.get(question_id) object_id = int(object_id) o = _descriptions[object_id-1] if not tag in o: return 0 return o[tag]
def get_t(object_id, question_id): """ Returns the number of descriptions that an object has that contains a specific tag """ tag = tags.get(question_id) db.cursor.execute('SELECT COUNT(*) \ FROM Descriptions \ WHERE description LIKE %s \ AND objectID = %s', ('%{0}%'.format(tag), str(object_id))) return db.cursor.fetchone()[0]
def ask(question_id, object, game, answer_data, answers, pO, Pi, p_tags, objects): """ Ask a question """ # Takes best question and updates all object probabilies based on the answer probabilityD = get_tval() question_tag = tags.get(question_id) #answer = raw_input("Does it have " + tags[question_id-1] + "? (yes/no) ") #answer = answer.lower() answer = answer_data[object.id-1][question_id-1] #print game_folder, object.id,objectlist[object.id-1][0],'qt->'+question_tag+' ' ,'ans->'+answer for objectID in range(0, 17): T = get_t(objectID+1, question_id) N = objects[objectID][question_id][0] D = objects[objectID][question_id][1] if answer == 'yes': answers.append(True) K = probabilityD[T] + (N + 1)/(D + 2.0) if Pi[0][question_id-1] == -1: multiplier = K / 2 else: multiplier = (K + Pi[objectID][question_id-1]) / 3 else: answers.append(False) K = (1 - probabilityD[T]) + (D - N + 1)/(D + 2.0) if Pi[0][question_id-1] == -1: multiplier = K / 2 else: multiplier = (K + 1 - Pi[objectID][question_id-1]) / 3 pO[objectID] = pO[objectID] * multiplier # Normalize the probabilities so that all object probabilities will sum to 1 pO = pO / np.sum(pO) # Save the qustions to each answer and the updated probabilities with open("example.txt", "a") as myfile: myfile.write(question_tag + " -> " + answer+ " \n") myfile.write(str(pO) + "\n") return pO, answers
def decode(self, filename): ext = filename.split('.')[-1].lower() result = { 'wav': os.path.join(self.tmpdir, os.path.splitext(filename)[0]) + u'.wav', 'lossless': False, 'tags': tags.get(filename), } if ext == 'flac': self.pipe([[ 'flac', '-sd', '-o', result['wav'], filename ]]) result['lossless'] = True result['target'] = 'flac' elif ext == 'mp3': self.pipe([[ 'lame', '--decode', '--quiet', filename, result['wav'] ]]) result['target'] = 'mp3_dl' else: return None for tag in sorted(result['tags']): logger.info('Tag %s: %s' % (tag, '; '.join(result['tags'][tag]))) return result
def _record_results(self, game, game_answers, game_questions, guess, result, number_of_objects): """ Puts results into the DB as well as writing them to file for examination """ # TODO: make sure this can be used for new objects and when playing with the human's choice of object log.info('Recording object results to the database') for i in range(0, len(game_questions)): T = questions.get_t(self.id, game_questions[i], number_of_objects) if game_answers[i] == True: db.cursor.execute("SELECT yes_answers FROM Pqd where t_value = %s", (T,)) yes_count = db.cursor.fetchone()[0] #print yes_count, 'yes' db.cursor.execute("UPDATE Pqd SET yes_answers = %s WHERE t_value = %s", (yes_count + 1, T)) db.cursor.execute("SELECT total_answers FROM Pqd where t_value = %s", (T,)) total_count = db.cursor.fetchone()[0] #print total_count db.cursor.execute("UPDATE Pqd SET total_answers = %s WHERE t_value = %s", (total_count + 1, T)) db.cursor.execute("INSERT INTO answers (oid, qid, answer) VALUES (%s, %s, %s)", (str(self.id), game_questions[i], game_answers[i])) db.connection.commit() if result == 0: result = 'lose' else: result = 'win' # TODO: clean up all the text files because this is kind of ridiculous with open("game.txt", "a") as myfile: myfile.write(str(game.id)+','+ str(self.id) +','+ str(guess.name)+"," + str(self.name) + "," + str(len(game_questions)) + "," + result + "\n") myfile.close() with open("answers.txt", "a") as answerfile: answerfile.write("\n" + str(game.id) + " " + str(self.id) + " " + result + "\n") for i in range(0, len(game_questions)): answerfile.write(tags.get(game_questions[i]) + " -> " + str(game_answers[i]) + "\n") answerfile.close()
def _record_results(self, game, game_answers, game_questions, guess, result): """ Puts results into the DB as well as writing them to file for examination """ log.info('Recording object results') for i in range(0, len(game_questions)): T = questions.get_t(self.id, game_questions[i]) if game_answers[i] == True: db.cursor.execute("SELECT yes_answers FROM Pqd where t_value = %s", (T,)) yes_count = db.cursor.fetchone()[0] #print yes_count, 'yes' db.cursor.execute("UPDATE Pqd SET yes_answers = %s WHERE t_value = %s", (yes_count + 1, T)) db.cursor.execute("SELECT total_answers FROM Pqd where t_value = %s", (T,)) total_count = db.cursor.fetchone()[0] #print total_count db.cursor.execute("UPDATE Pqd SET total_answers = %s WHERE t_value = %s", (total_count + 1, T)) db.cursor.execute("INSERT INTO answers (oid, qid, answer) VALUES (%s, %s, %s)", (str(self.id), game_questions[i], game_answers[i])) db.connection.commit() if result == 0: result = 'lose' else: result = 'win' with open("game.txt", "a") as myfile: myfile.write(str(game.id)+','+ str(self.id) +','+ str(guess.name)+"," + str(len(game_questions)) + "," + result + "\n") myfile.close() with open("answers.txt", "a") as answerfile: answerfile.write("\n" + str(game.id) + " " + str(self.id) + " " + result + "\n") for i in range(0, len(game_questions)): answerfile.write(tags.get(game_questions[i]) + " -> " + str(game_answers[i]) + "\n") answerfile.close()
def ask(question_id, object_we_play, game, answers, pO, Pi, objects, number_of_objects, answer_data): """ Asks a question and updates probabilities based on the answer """ # gets probability that an answer will be yes for each of the 7 possible t-values of question/answer combos probabilityD = get_tval() # gets the correct question for the given tag # This is where we'll update the code to include newly-generated questions; right now the code just pulls pre-generated questions from a database. question_tag = tags.get(question_id) questions = tags.get_questions() question = questions[question_id - 1] # in not simulated game, get answer from user if config.args.notsimulated: answer = interface.ask(question + " ") # in simulated game, get answer from answer bank in database else: answer = answer_data[game.id - 1][object_we_play.id - 1][question_id - 1] print question, answer answers.append(answer) multipliers = [] for objectID in range(17): # for all known objects # count the number of that object's descriptions that contain the tag used to formulate that question T = get_t(objectID + 1, question_id, number_of_objects) # number of yes answers to this question/object combo yes_answers = objects[objectID][question_id - 1][0] # total number of answers given for this question/object combo total_answers = objects[objectID][question_id - 1][1] # TODO: see if Dr. Nielsen has any suggestions for improving the use of the # image models since they reduce the accuracy at the moment if answer: K = probabilityD[T] + (yes_answers + 1) / (total_answers + 2.0) # if Pi is -1 then it means we're skipping the image models for that tag if Pi[0][question_id - 1] == -1: multiplier = K / 2 multipliers.append(multiplier) else: multiplier = (K + Pi[objectID][question_id - 1]) / 3 multipliers.append(multiplier) else: K = (1 - probabilityD[T]) + (total_answers - yes_answers + 1) / (total_answers + 2.0) if Pi[0][question_id - 1] == -1: multiplier = K / 2 else: multiplier = (K + 1 - Pi[objectID][question_id - 1]) / 3 pO[objectID] *= multiplier for objectID in range( 17, number_of_objects ): # TODO: for all new objects/unknown tags for a new object # this assigns a probability to unknown objects based on the idea that # an object is about as likely as the rest of the objects to have the quality # of a certain tag - i.e. many objects are balls, but not many objects are polka dotted # TODO: make more sophisticated - some tags are opposites, so if we know an opposite then we # can take that into consideration when finding the multiplier multiplier = np.mean(multipliers) pO[objectID] *= multiplier # Normalize the probabilities so that all object probabilities will sum to 1 pO /= np.sum(pO) # Save the questions to each answer and the updated probabilities with open("example.txt", "a") as myfile: myfile.write(str(question_tag) + " -> " + str(answer) + " \n") myfile.write(str(pO) + "\n") return pO, answers
def ask(question_id, object_we_play, game, answers, pO, Pi, objects, number_of_objects, answer_data): """ Asks a question and updates probabilities based on the answer """ # gets probability that an answer will be yes for each of the 7 possible t-values of question/answer combos probabilityD = get_tval() # gets the correct question for the given tag question_tag = tags.get(question_id) questions = tags.get_questions() question = questions[question_id - 1] # in not simulated game, get answer from user if config.args.notsimulated: answer = interface.ask(question + " ") # in simulated game, get answer from answer bank in database else: answer = answer_data[game.id-1][object_we_play.id-1][question_id-1] print question, answer answers.append(answer) multipliers = [] for objectID in range(17): # for all known objects # count the number of that object's descriptions that contain the tag used to formulate that question T = get_t(objectID+1, question_id, number_of_objects) # number of yes answers to this question/object combo yes_answers = objects[objectID][question_id-1][0] # total number of answers given for this question/object combo total_answers = objects[objectID][question_id-1][1] # TODO: see if Dr. Nielsen has any suggestions for improving the use of the # image models since they reduce the accuracy at the moment if answer: K = probabilityD[T] + (yes_answers + 1)/(total_answers + 2.0) # if Pi is -1 then it means we're skipping the image models for that tag if Pi[0][question_id-1] == -1: multiplier = K / 2 multipliers.append(multiplier) else: multiplier = (K + Pi[objectID][question_id-1]) / 3 multipliers.append(multiplier) else: K = (1 - probabilityD[T]) + (total_answers - yes_answers + 1)/(total_answers + 2.0) if Pi[0][question_id-1] == -1: multiplier = K / 2 else: multiplier = (K + 1 - Pi[objectID][question_id-1]) / 3 pO[objectID] *= multiplier for objectID in range(17,number_of_objects): # TODO: for all new objects/unknown tags for a new object # this assigns a probability to unknown objects based on the idea that # an object is about as likely as the rest of the objects to have the quality # of a certain tag - i.e. many objects are balls, but not many objects are polka dotted # TODO: make more sophisticated - some tags are opposites, so if we know an opposite then we # can take that into consideration when finding the multiplier multiplier = np.mean(multipliers) pO[objectID] *= multiplier # Normalize the probabilities so that all object probabilities will sum to 1 pO /= np.sum(pO) # Save the questions to each answer and the updated probabilities with open("example.txt", "a") as myfile: myfile.write(str(question_tag) + " -> " + str(answer) + " \n") myfile.write(str(pO) + "\n") return pO, answers