def jsonToSQLJob(): print(">>>>>>>>>>Initiating Persistence to SQLLite>>>>>>>>>>") conn = create_connection(get_database_path()) drop_table(conn, get_drop_query('D')) create_table(conn, get_create_query('D')) drop_table(conn, get_drop_query('C')) create_table(conn, get_create_query('C')) with open('participation.json') as json_file: data = json.load(json_file) i = 1 for p in data['all']: insert_table( conn, "INSERT INTO participation values(" + str(i) + "," + str(p) + ");") i = i + 1 with open('punch_card.json') as json_file: data = json.load(json_file) for p in data: insert_table( conn, "INSERT INTO punch_card values(" + str(p[0]) + "," + str(p[1]) + "," + str(p[2]) + ");") print("<<<<<<<<<<<<<<<<<<Persistence to SQLLite Complete<<<<<<<<<<<<<")
def SolutionToDbJob(type, solutionA, solutionB): conn = create_connection(get_database_path()) if (type == "findDayOfMaximumCommit"): insert_table( conn, "INSERT INTO day_with_max_commit values(" + str(solutionA) + ",'" + str(solutionB) + "');") if (type == "findTheWeekWithMaxCommit"): insert_table( conn, "INSERT INTO week_with_max_commit values(" + str(solutionA) + ");")
def domain_decision(parameter_list): global stopword_set corrected_parameter_list = [] for parameter in parameter_list: b = check_in_vocabulary(parameter) log("check_in_vocabulary = " + str(b)) if not b: words = jieba_cut_vocabulary(parameter) corrected_parameter_list.extend(words) else: corrected_parameter_list.append(parameter) topics_cost = get_topic_cost_by_words(corrected_parameter_list) selected_topics = select_topic(topics_cost) global parameter_similar_list result_parameter_order_list = [] #排序parameter相似度----- for topic in selected_topics: index = topic_list_en.index(topic) log(topic + ', index = ' + str(index)) object = {'topic': topic} parameter_list = [] for i in range(len(parameter_similar_list)): word = '' obj = () for key in parameter_similar_list[i].keys(): word = key obj = (word, parameter_similar_list[i][word][index]) #log('word = '+word+', score = '+str(parameter_similar_list[i][word][index])) parameter_list.append(obj) parameter_list = sorted(parameter_list, key=lambda x: x[1], reverse=True) parameter_array = [] for tuple in parameter_list: parameter_array.append(tuple[0]) object['parameter'] = parameter_array result_parameter_order_list.append(object) log('parameter_list = ' + str(result_parameter_order_list)) #排序parameter相似度-----end # [{'topic':'music', 'parameter':['五月天','溫柔']}] log("選中 ****** " + str(selected_topics) + " ******") return [selected_topics, topics_cost, result_parameter_order_list]
def draw(): surface.fill((0, 0, 0)) particle_system.draw(surface) player1.draw(surface) for asteroid in asteroids: asteroid.draw(surface) ######### # surf_level = fonts[16].render("Level: "+str(level), True, (255,255,255)) # surface.blit(surf_level,(10,10)) # # surf_lives = fonts[16].render("Lives: "+str(max([player1.lives,0])), True, (255,255,255)) # surface.blit(surf_lives,(10,25)) # # surf_score = fonts[16].render("Score: "+str(player1.score), True, (255,255,255)) # surface.blit(surf_score,(screen_size[0]-surf_score.get_width()-10,10)) # # surf_highscore = fonts[16].render("High Score: "+str(hs), True, (255,255,255)) # surface.blit(surf_highscore,(screen_size[0]-surf_highscore.get_width()-10,25)) if player1.lives >= 0: surf_remain = fonts[16].render( "Asteroids Left: " + str(len(asteroids)), True, (255, 255, 255)) surface.blit(surf_remain, (10, screen_size[1] - surf_remain.get_height() - 10)) if level_text_brightness > 0.0: col = rndint(255.0 * level_text_brightness) surf_level = fonts[32].render("Level " + str(level), True, (col, col, col), (0, 0, 0)) pos = [(screen_size[0] / 2.0) - (surf_level.get_width() / 2.0), (screen_size[1] / 2.0) - (surf_level.get_height() / 2.0)] surface.blit(surf_level, pos, special_flags=BLEND_MAX) else: surf_level = fonts[32].render("F2 Starts New Game", True, (255, 255, 255), (0, 0, 0)) pos = [(screen_size[0] / 2.0) - (surf_level.get_width() / 2.0), (screen_size[1] / 2.0) - (surf_level.get_height() / 2.0)] surface.blit(surf_level, pos, special_flags=BLEND_MAX) image_data = pygame.surfarray.array3d(pygame.display.get_surface()) pygame.display.flip() return image_data
def domain_decision_detail(list): result = '' log('domain_decision_detail = ' + str(len(list))) detail_msg = '' domain_result = domain_decision(list) if len(domain_result) == 0: return "key word not in vocabulary" result = result + str(list) + '<br>' for i in range(TOPIC_SIZE): detail_msg = detail_msg + topic_list[i] + ':' + str( domain_result[1][i]) + '<br>' result = result + str(domain_result[0]) + '<br>' + detail_msg log('result = ' + str(result)) return json.dumps(result, ensure_ascii=False)
def get_topic_cost_by_words(words): segment_cost_list = [] result = [] words_in_vocabulary = [] global parameter_similar_list parameter_similar_list = [] for word in words: topic_cost_by_word = get_topic_cost(word) if len(topic_cost_by_word) == TOPIC_SIZE: words_in_vocabulary.append(word) segment_cost_list.append(topic_cost_by_word) for i in range(len(words_in_vocabulary)): object = {} object[words_in_vocabulary[i]] = segment_cost_list[i] parameter_similar_list.append(object) log('[ ' + str(words_in_vocabulary[i]) + ' ] cost list') msg = '' for j in range(TOPIC_SIZE): msg = msg + topic_list[j] + ':' + str( segment_cost_list[i][j]) + " | " log('>>> ' + msg) if len(segment_cost_list) == 0: return [] #[[ 0.11703352075249501, .....],[0.5000000000000002, .......],[0.5000000000000002, ......]] # >>> [ 0.11703352075249501, 0.5000000000000002, .......,] log(str(words)) log(' === weighting handling ===') for i in range(TOPIC_SIZE): cost = 0 words_total_len = 0 for j in range(len(segment_cost_list)): cost = cost + segment_cost_list[j][i] * len(words[j]) #TODO words_total_len = words_total_len + len(words[j]) if words_total_len > 0: cost = cost / words_total_len result.append(cost) log(topic_list[i] + ' : ' + str(cost)) return result
def compute_accuracy(training_sentences, is_show): total = 0 match_conuter = 0 selected_topic_counter = 0 for i in range(len(training_sentences)): total = total + len(training_sentences[i]) for sentence in training_sentences[i]: sentence_seg = sentence.split(",") topic_list = demo_domain.domain_decision(sentence_seg)[0] selected_topic_counter = selected_topic_counter + len(topic_list) # log(str(sentence_seg)+' = '+str(topic_list), is_show) if i==0 and demo_domain.topic_list_en[0] in topic_list: match_conuter = match_conuter + 1 elif i==1 and demo_domain.topic_list_en[1] in topic_list: match_conuter = match_conuter + 1 elif i==2 and demo_domain.topic_list_en[2] in topic_list: match_conuter = match_conuter + 1 elif i==3 and demo_domain.topic_list_en[3] in topic_list: match_conuter = match_conuter + 1 elif i==4 and demo_domain.topic_list_en[4] in topic_list: match_conuter = match_conuter + 1 elif i==5 and demo_domain.topic_list_en[5] in topic_list: match_conuter = match_conuter + 1 elif i==6 and demo_domain.topic_list_en[6] in topic_list: match_conuter = match_conuter + 1 elif i==7 and demo_domain.topic_list_en[7] in topic_list: match_conuter = match_conuter + 1 elif i==8 and demo_domain.topic_list_en[8] in topic_list: match_conuter = match_conuter + 1 else: log('not match sentence = '+str(sentence_seg)+' = '+str(topic_list), is_show) cover_rate = round(match_conuter*100/total,2) if selected_topic_counter == 0: accuracy_rate = 0 else: accuracy_rate = round(match_conuter*100/selected_topic_counter,2) log('Cover rate = '+str(cover_rate)+'%', is_show) log('Accuracy rate = '+str(accuracy_rate)+'%', is_show) return cover_rate, accuracy_rate
def start_training(training_sentences): #init demo_domain.offset = 0.01 learning = 0.002 max_integrating_rate = [0,0] max_cover_rate = [0,0] max_accuracy_rate = [0,0] final_offset ={} lower_bound = 2*1/demo_domain.TOPIC_SIZE*100 while(True): rate = compute_accuracy(training_sentences, False) if integrating_cal(rate[0], rate[1]) > integrating_cal(max_integrating_rate[0], max_integrating_rate[1]): max_integrating_rate = rate final_offset['integrating'] = demo_domain.offset print('[update] integrating offset = '+str(final_offset['integrating'])+', CoverRate = '+str(max_integrating_rate[0])+'%, AccuracyRate = '+str(max_integrating_rate[1])+'%') if rate[0] > max_cover_rate[0]: max_cover_rate = rate final_offset['cover'] = demo_domain.offset print('[update] cover offset = '+str(final_offset['cover'])+', CoverRate = '+str(max_cover_rate[0])+'%, AccuracyRate = '+str(max_cover_rate[1])+'%') if rate[1] > max_accuracy_rate[1]: max_accuracy_rate = rate final_offset['accuracy'] = demo_domain.offset print('[update] accuracy offset = '+str(final_offset['accuracy'])+', CoverRate = '+str(max_accuracy_rate[0])+'%, AccuracyRate = '+str(max_accuracy_rate[1])+'%') if rate[0] == 100 and rate[1] < lower_bound: break demo_domain.offset = round(demo_domain.offset + learning, 3) print('='*80) print('[final] integrating offset = '+str(final_offset['integrating'])+', CoverRate = '+str(max_integrating_rate[0])+'%, AccuracyRate = '+str(max_integrating_rate[1])+'%') print('[final] cover offset = '+str(final_offset['cover'])+', CoverRate = '+str(max_cover_rate[0])+'%, AccuracyRate = '+str(max_cover_rate[1])+'%') print('[final] accuracy offset = '+str(final_offset['accuracy'])+', CoverRate = '+str(max_accuracy_rate[0])+'%, AccuracyRate = '+str(max_accuracy_rate[1])+'%') demo_domain.offset = final_offset['integrating'] compute_accuracy(training_sentences, True)
def get_topic_cost(key_word): topic_cost_list = [] global topic_list for i in range(len(topic_list)): max_cost = -1 feature_cost_list = [] #選出最大feature當作topic cost for topic in topic_feature_list[i]: try: res = model.similarity(key_word, topic) if (res > max_cost): max_cost = res feature_cost = {} feature_cost[topic] = res feature_cost_list.append(feature_cost) except Exception as e: log('get_topic_cost : ' + repr(e)) return [] log(key_word + ' >>> feature list : ' + str(feature_cost_list)) # topic_cost_object['domain'] = feature_cost_list topic_cost_list.append(max_cost) log(' --------------------------------------------- ') return topic_cost_list
try: model.most_similar(word, topn=1) return True except Exception as e: return False def log(str): if is_log_showing: print(str) if __name__ == '__main__': init_model() while True: try: query = input() q_list = query.split() if len(q_list) == 1: result = domain_decision(q_list[0]) domain_decision(q_list[0]) print("相似詞前 100 排序") res = model.most_similar(q_list[0], topn=100) for item in res: print(item[0] + "," + str(item[1])) except Exception as e: print(repr(e))
def write_hs(): f = open("hs.txt", "wb") f.write(str(hs).encode()) f.close()