Linhz/ViMNer
1
1def format_predictions(words, predictions):2 '''3 Chuyển đổi danh sách từ và dự đoán sang định dạng (word, label)4 '''5 formatted = []6 for word, label in zip(words, predictions):7 formatted.append((word, label))8 return formatted9 10def process_predictions(predictions):11 '''12 Tách các từ có dấu gạch dưới thành các từ riêng biệt với cùng nhãn13 '''14 formatted = []15 for word, label in predictions:16 if '_' in word:17 formatted.append((word.replace('_', ' '), label))18 else:19 formatted.append((word, label))20 return formatted21 22 23def combine_entities(predictions):24 combined = []25 temp_entity = []26 temp_label = None27 28 for word, label in predictions:29 if label.startswith('B-'):30 if temp_entity:31 combined.append((' '.join(temp_entity), temp_label))32 temp_entity = []33 temp_entity.append(word)34 temp_label = label35 elif label.startswith('I-') and temp_label and label[2:] == temp_label[2:]:36 temp_entity.append(word)37 else:38 if temp_entity:39 combined.append((' '.join(temp_entity), temp_label))40 temp_entity = []41 temp_label = None42 combined.append((word, label))43 44 if temp_entity:45 combined.append((' '.join(temp_entity), temp_label))46 47 return combined48 49 50 51 52def remove_B_prefix(entities):53 modified_entities = []54 for word, label in entities:55 if label.startswith('B-'):56 label = label[2:] # Loại bỏ phần 'B-' khỏi nhãn57 modified_entities.append((word, label))58 return modified_entities59 60 61def combine_i_tags(tokens_labels):62 combined = []63 current_combination = []64 current_label = None65 66 for token, label in tokens_labels:67 if label.startswith('I-'):68 label = label[2:] # Remove the 'I-' prefix69 if current_label is None:70 current_label = label71 current_combination.append(token)72 elif current_label == label:73 current_combination.append(token)74 else:75 combined.append((' '.join(current_combination), current_label))76 current_combination = [token]77 current_label = label78 else:79 if current_combination:80 combined.append((' '.join(current_combination), current_label))81 current_combination = []82 current_label = None83 combined.append((token, label))84 85 if current_combination:86 combined.append((' '.join(current_combination), current_label))87 88 return combined89 90# tokens_labels = [('Dân', 'O'), ('trí', 'O'), ('Chức', 'O'), ('vô', 'O'), ('địch', 'O'), ('Euro 2008', 'EVENT-SPORT'), ('đầy', 'O'), ('thuyết', 'O'), ('phục', 'O'), ('của', 'O'), ('Tây Ban Nha', 'LOCATION'), ('trên', 'O'), ('đất', 'O'), ('Áo', 'LOCATION'), ('và', 'O'), ('Thụy Sĩ', 'PERSON'), ('đã', 'O'), ('mở', 'O'), ('ra', 'O'), ('kỷ', 'O'), ('nguyên', 'O'), ('vinh', 'O'), ('quanh', 'O'), ('của', 'O'), ('La', 'ORGANIZATION'), ('Furia', 'I-ORGANIZATION-SPORTS'), ('Roja', 'I-ORGANIZATION-SPORTS'), (',', 'O'), ('với', 'O'), ('lối', 'O'), ('chơi', 'O'), ('tiqui', 'O'), ('taka', 'O'), ('đầy', 'O'), ('biến', 'O'), ('ảo', 'O'), ('.', 'O'), ('Trong', 'O'), ('quá', 'O'), ('khứ', 'O'), (',', 'O'), ('Tây Ban Nha', 'LOCATION'), ('nổi', 'O'), ('tiếng', 'O'), ('với', 'O'), ('biệt', 'O'), ('danh', 'O'), ('Vua', 'O'), ('vòng', 'O'), ('loại', 'O'), ('.', 'O'), ('Họ', 'O'), ('thường', 'O'), ('thi', 'O'), ('đấu', 'O'), ('rất', 'O'), ('tốt', 'O'), ('ở', 'O'), ('vòng', 'O'), ('loại', 'O'), ('nhưng', 'O'), ('lại', 'O'), ('chưa', 'O'), ('bao', 'O'), ('giờ', 'O'), ('chứng', 'O'), ('minh', 'O'), ('được', 'O'), ('sức', 'O'), ('mạnh', 'O'), ('ở', 'O'), ('vòng', 'O'), ('chung', 'O'), ('kết', 'O'), ('giải', 'O'), ('đấu', 'O'), ('lớn', 'O'), ('.', 'O'), ('Lần', 'O'), ('duy', 'O'), ('nhất', 'O'), ('họ', 'O'), ('lên', 'O'), ('ngôi', 'O'), ('là', 'O'), ('ở', 'O'), ('kỳ', 'O'), ('Euro', 'EVENT-SPORT'), ('1964', 'O'), ('.', 'O')]91 92# combined_tokens_labels = combine_i_tags(tokens_labels)93# print(combined_tokens_labels)94 95 96 