def train_with_random(dict_nouns): _best_result = 0; _best_params = Parameters.get_current_params() for i in range(0,1000): # random_all_feature Parameters.random_params_values() # calculate_result -> current best (precision, recall, accuracy) = SimilarityWordnetOxford.similarityWords(dict_nouns) if accuracy > _best_result: _best_result = accuracy _best_params = Parameters.get_current_params() (best_result_loop, best_params_loop) = train_from_base(dict_nouns) if best_result_loop>= _best_result: _best_result = best_result_loop _best_params = best_params_loop Parameters.set_params_from_arr(_best_params) WriteParametersAndResult.append_params_and_result_to_file(_best_params)
def train_from_base(dict_nouns): # run this feature in range -> choice best result lower = -1 previous_random = -1; best_result_loop = 0 best_params_loop = Parameters.get_current_params() while lower < 7: chosen_feature = random.randint(0,6) while chosen_feature == previous_random: chosen_feature = random.randint(0,6) previous_random = chosen_feature best_result = 0 best_params = [] if lower >= 0: chosen_feature = lower if chosen_feature == 0: (best_result, best_params) = jaccard_weight(dict_nouns) elif chosen_feature == 1: (best_result, best_params) = choice_1_COL_RANGE_FIRST(dict_nouns) elif chosen_feature == 2: (best_result, best_params) = choice_N_N_RANGE_FIRST(dict_nouns) elif chosen_feature == 3: (best_result, best_params) = feature_wn(dict_nouns) elif chosen_feature == 4: (best_result, best_params) = feature_dict(dict_nouns) elif chosen_feature == 5: (best_result, best_params) = feature_POS(dict_nouns) else: (best_result, best_params) = nbest_similarity(dict_nouns) # compare with _best if best_result >= best_result_loop: best_result_loop = best_result best_params_loop = best_params lower = -1 else: lower += 1 Parameters.set_params_from_arr(best_params_loop) return (best_result_loop, best_params_loop)