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 build_pqd(number_of_objects): """ Pqd is the probability that an the answer will be yes to a keyword asked about an object where the keyword shows up X number of times in the descriptions Summed over all objects where a keyword shows up X number of times """ log.info('Building Pqd') probabilityD = [0, 0, 0, 0, 0, 0, 0] denominator = [0, 0, 0, 0, 0, 0, 0] all_tags = tags.get_all() for objectID in range(1, number_of_objects + 1): log.info(" Object %d", objectID) for tag in range(0, 289): # T is a based on a tag and an object description. T is how many times a tag is used in an object's description. It can be 0-6 db.cursor.execute( 'SELECT * FROM Descriptions WHERE description like "%' + all_tags[tag] + '%" AND objectID = ' + str(objectID)) T = len(db.cursor.fetchall()) # count is the number of times someone answered yes to a tag/object pair db.cursor.execute('SELECT * FROM QuestionAnswers WHERE tag = "' + all_tags[tag] + '" AND object = ' + str(objectID) + ' AND answer = TRUE') count = len(db.cursor.fetchall()) # D is the total number of times a tag/object pair has been asked (yes's and no's) db.cursor.execute('SELECT * FROM QuestionAnswers WHERE tag = "' + all_tags[tag] + '" AND object = ' + str(objectID)) D = len(db.cursor.fetchall()) # For the T value based on th specific tag/object pair, update the probability of all tag/object pairs with the same T value probabilityD[T] += count denominator[T] += D for freq in range(0, 7): # This puts the sum of the yes answers and the total answers into the row that corresponds with the T value db.cursor.execute( 'INSERT INTO Pqd (t_value, yes_answers, total_answers) VALUES (%s, %s, %s)', (freq, probabilityD[freq], denominator[freq])) db.connection.commit()
def build_pqd(): """ Pqd is the probability that an the answer will be yes to a keyword asked about an object where the keyword shows up X number of times in the descriptions Summed over all objects where a keyword shows up X number of times """ log.info('Building Pqd') probabilityD = [0,0,0,0,0,0,0] denominator = [0,0,0,0,0,0,0] all_tags = tags.get_all() for objectID in range(1,18): log.info(" Object %d", objectID) for tag in range(0, 289): db.cursor.execute('SELECT * FROM Descriptions WHERE description like "%' + all_tags[tag] + '%" AND objectID = ' + str(objectID)) T = len(db.cursor.fetchall()) #T is a based on a tag and an object description. T is how many times a tag is used in an object's description. It can be 0-6 db.cursor.execute('SELECT * FROM QuestionAnswers WHERE tag = "' + all_tags[tag] + '" AND object = ' + str(objectID) + ' AND answer = TRUE') count = len(db.cursor.fetchall()) #count is the number of times someone answered yes to a tag/object pair db.cursor.execute('SELECT * FROM QuestionAnswers WHERE tag = "' + all_tags[tag] + '" AND object = ' + str(objectID)) D = len(db.cursor.fetchall()) #D is the total number of times a tag/object pair has been asked (yesses and nos) probabilityD[T] = probabilityD[T] + count denominator[T] = denominator[T] + D #For the T value based on the specific tag/object pair, update the probability of all tag/object pairs with the same T value for freq in range(0,7): #This puts the sum of the yes answers and the total answers into the row that corresponds with the T value db.cursor.execute('INSERT INTO Pqd (t_value, yes_answers, total_answers) VALUES (%s, %s, %s)', (freq, probabilityD[freq], denominator[freq])) db.connection.commit() print probabilityD[freq]