File size: 3,209 Bytes
d36f0b0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
import json
from copy import deepcopy
def remove_blank_value_for_conv(conversations):
new_conversations = deepcopy(conversations)
for conv in conversations:
if len(conv['value']) == 0:
new_conversations.remove(conv)
return new_conversations
def remove_blank_value(data):
for item in data:
item['conversations'] = remove_blank_value_for_conv(item['conversations'])
return data
def remove_conv_with_one_person(data):
new_data = deepcopy(data)
for item in data:
if len(set([conv['from'] for conv in item['conversations']])) <= 1:
new_data.remove(item)
return new_data
def compute_similarity(conv1, conv2):
conv1_list = [item['from'] + item['value'] for item in conv1]
conv2_list = [item['from'] + item['value'] for item in conv2]
if len(conv1_list) < len(conv2_list):
count = 0
for conv in conv1_list:
if conv in conv2_list:
count += 1
return count / len(conv1_list)
else:
count = 0
for conv in conv2_list:
if conv in conv1_list:
count += 1
return count / len(conv2_list)
def remove_repeated_value(data):
visited_item = []
new_data = deepcopy(data)
for item1 in data:
need_remove = []
item1_deleted = False
for item2 in visited_item:
sim = compute_similarity(item1['conversations'], item2['conversations'])
if sim > 0.8:
# print("item1", item1, "item2", item2, sep='\n')
if len(item1['conversations']) < len(item2['conversations']):
new_data.remove(item1)
item1_deleted = True
break
else:
new_data.remove(item2)
need_remove.append(item2)
for item in need_remove:
visited_item.remove(item)
if not item1_deleted:
visited_item.append(item1)
return new_data
def remove_diags_without_target_role_and_input_role(data):
new_data = deepcopy(data)
for item in data:
roles = [c['from'] for c in item['conversations']]
if item['input_role'] not in roles or item['target_role'] not in roles:
new_data.remove(item)
continue
last_response_is_input_role = False
for i in range(len(item['conversations'])-1, -1, -1):
if item['conversations'][i]['from'] == item['target_role']:
continue
else:
if item['conversations'][i]['from'] == item['input_role']:
last_response_is_input_role = True
break
if not last_response_is_input_role:
new_data.remove(item)
print(item)
return new_data
def clean_diag(diags):
diags = remove_repeated_value(diags)
diags = remove_blank_value(diags)
diags = remove_conv_with_one_person(diags)
return diags
def clean_diag_with_repeated(diags):
diags = remove_blank_value(diags)
diags = remove_conv_with_one_person(diags)
return diags |