def find_winner(sentences, obj_a, obj_b, aspects): """ Finds the winner of two objects for given sentences and aspects. Returns a dictionary containing all the information of the comparison (winner, sentences for each object, score for each object and more). sentences: Dictionary dictionary containing sentences obj_a: Argument the first competing object obj_b: Argument the second competing object aspects: List list of Aspects """ if len(sentences) > 0: max_sentscore = max(sentence.ES_score for sentence in sentences) for sentence in sentences: comp_result = what_is_better(sentence.text, obj_a, obj_b) sentence.set_confidence(comp_result['marker_cnt']) if comp_result['winner'] == obj_a: # objectA won the sentence add_points(find_aspects(sentence.text, aspects), obj_a, sentence, max_sentscore, score_function) else: # objectB won the sentence add_points(find_aspects(sentence.text, aspects), obj_b, sentence, max_sentscore, score_function) obj_a.sentences = prepare_sentence_list(obj_a.sentences) obj_b.sentences = prepare_sentence_list(obj_b.sentences) return build_final_dict(obj_a, obj_b, sentences)
def count_confindences(prepared_sentences, classification_results, aspects): perfect = 0 excellent = 0 good = 0 medium = 0 ok = 0 for index, row in prepared_sentences.iterrows(): if len(find_aspects(row['sentence'], aspects)) == 0: continue label = classification_results['max'][index] if label != 'NONE': confidence = classification_results[label][index] if confidence > 0.8: perfect += 1 elif confidence > 0.7: excellent += 1 elif confidence > 0.6: good += 1 elif confidence > 0.5: medium += 1 else: ok += 1 excellent += perfect good += excellent medium += good ok += medium return (excellent, good, medium, ok)
def evaluate(sentences, prepared_sentences, classification_results, obj_a, obj_b, aspects): if len(sentences) > 0: max_sentscore = max(sentence.ES_score for sentence in sentences) counts = count_confindences(prepared_sentences, classification_results, aspects) threshold_sentences = find_threshold(counts, 5) threshold_score = find_threshold(counts, 3) for index, row in prepared_sentences.iterrows(): label = classification_results['max'][index] if label == 'NONE': continue classification_confidence = classification_results[label][index] sentence_text = row['sentence'] for s in sentences: if s.text == sentence_text: sentence = s break sentences.remove(sentence) sentence.set_confidence(classification_confidence.item()) contained_aspects = find_aspects(sentence.text, aspects) if (label == 'BETTER' and row['object_a'] == obj_a.name) or ( label == 'WORSE' and row['object_b'] == obj_a.name): add_points(contained_aspects, obj_a, sentence, max_sentscore, score_function, threshold_sentences, threshold_score) else: add_points(contained_aspects, obj_b, sentence, max_sentscore, score_function, threshold_sentences, threshold_score) with open('../config.json') as json_data_file: config = json.load(json_data_file) if config['use_heuristics']: for aspect in aspects: negation_dissolve_heuristic(obj_a, obj_b, aspect.name, aspects, threshold_score) negation_dissolve_heuristic(obj_b, obj_a, aspect.name, aspects, threshold_score) obj_a.sentences = prepare_sentence_list(obj_a.sentences) obj_b.sentences = prepare_sentence_list(obj_b.sentences) return build_final_dict(obj_a, obj_b, sentences)
def move_assignment(sentences_to_move, from_object, to_object, aspect, aspects, threshold_score): points_to_move = 0 points_for_multiple = 0 print('For', '\'' + aspect + '\'', 'there were moved', len(sentences_to_move), 'sentences from', from_object.name, 'to', to_object.name, '.') print('----') for sentence in sentences_to_move: points = ( sentence.CAM_score ) / 10 if sentence.confidence < threshold_score else sentence.CAM_score if len(find_aspects(sentence.text, aspects)) > 1: points_for_multiple = points_for_multiple + points else: points_to_move = points_to_move + points print('-' + re.sub(' +', ' ', re.sub('[^a-zA-Z0-9 ]', ' ', sentence.text))) from_object.sentences = [ sentence for sentence in from_object.sentences if sentence not in sentences_to_move ] from_object.points[aspect] = from_object.points[aspect] - points_to_move from_object.totalPoints = from_object.totalPoints - \ (points_to_move + points_for_multiple) to_object.sentences.extend(sentences_to_move) to_object.add_points(aspect, points_to_move) if points_for_multiple > 0: from_object.points['multiple'] = from_object.points['multiple'] - \ points_for_multiple to_object.add_points('multiple', points_for_multiple) print((points_to_move / (to_object.totalPoints + from_object.totalPoints)) * 100, '% moved (total)') print((points_to_move / (to_object.points[aspect] + from_object.points[aspect])) * 100, '% moved for', aspect) print('----')