|
| 1 | +# Run with: python -m augmented_data.location |
| 2 | + |
| 3 | +import nltk |
| 4 | +import spacy |
| 5 | +import random |
| 6 | +from .utils import replace_multiple, normalize_text_to_mms |
| 7 | + |
| 8 | + |
| 9 | +# nltk.download('punkt') |
| 10 | +# nltk.download('averaged_perceptron_tagger') |
| 11 | +# nltk.download('maxent_ne_chunker') |
| 12 | +# nltk.download('words') |
| 13 | + |
| 14 | +nlp = spacy.load('de_core_news_sm') |
| 15 | + |
| 16 | + |
| 17 | +def replace_location_entities(dataset_text, dataset_mms): |
| 18 | + location_names = set() |
| 19 | + dataset_text_with_metadata = {} |
| 20 | + |
| 21 | + excludes = {'Zuges', 'Alternativen', 'D', 'RE 77','A.', 'D.','Umsteigen','Weiteres', |
| 22 | + 'Notbremse', 'Reservierungen','IC 2313','Sonderzug','Rhein'} |
| 23 | + |
| 24 | + |
| 25 | + for folder_name, file_content in dataset_text.items(): |
| 26 | + file_with_metadata = [] |
| 27 | + for (start_time, end_time, sentence, number) in file_content: |
| 28 | + sentences_to_analyze = sentence.strip().translate({ord(i): None for i in "„“"}) |
| 29 | + # sentences_to_analyze = sentence.strip().replace("„", "").replace("“", "") |
| 30 | + doc = nlp(sentences_to_analyze) |
| 31 | + entities = [(ent.text, ent.label_) for ent in doc.ents] |
| 32 | + # print(entities) |
| 33 | + # print(folder_name, sentence) |
| 34 | + |
| 35 | + for ent in doc.ents: |
| 36 | + if ent.label_ == 'LOC' and ent.text not in excludes: |
| 37 | + # Check if "Hauptbahnhof" is present in the entity text |
| 38 | + location = ent.text |
| 39 | + if 'Hauptbahnhof' in location: |
| 40 | + location = location.replace('Hauptbahnhof', '').strip() # Remove "Hauptbahnhof" and strip extra spaces |
| 41 | + location_names.add(location) |
| 42 | + # print('location_names are', location_names) |
| 43 | + file_with_metadata.append((start_time, end_time, sentence, number, entities)) |
| 44 | + |
| 45 | + dataset_text_with_metadata[folder_name] = file_with_metadata |
| 46 | + # print("file_with_metadata", dataset_text_with_metadata) |
| 47 | + |
| 48 | + # print(location_names) # Finding all the locations |
| 49 | + |
| 50 | + result_text = {} |
| 51 | + result_mms = {} |
| 52 | + |
| 53 | + for folder_name, file_content in dataset_text_with_metadata.items(): |
| 54 | + location_counts = {} |
| 55 | + for line_number, (start_time, end_time, sentence, number, entities) in enumerate(file_content): |
| 56 | + for (text, label) in entities: |
| 57 | + if label == 'LOC' and text not in excludes: |
| 58 | + if 'Hauptbahnhof' in text: |
| 59 | + text = text.replace('Hauptbahnhof', '').strip() |
| 60 | + location_counts[text] = location_counts.get(text, 0) + 1 #counting the number of times same location appears in a file |
| 61 | + # print(f'WARNING: location {text} in file {folder_name} appears multiple times') |
| 62 | + |
| 63 | + mapping = {} |
| 64 | + for location, count in location_counts.items(): |
| 65 | + assert len(location_names) > 1, f'ERROR: only one location found' |
| 66 | + while True: |
| 67 | + new_location = random.choice(tuple(location_names)) |
| 68 | + if new_location != location: |
| 69 | + break |
| 70 | + mapping[location] = new_location |
| 71 | + |
| 72 | + |
| 73 | + new_text_data = [] |
| 74 | + for (start_time, end_time, sentence, number, entities) in file_content: |
| 75 | + sentence, _ = replace_multiple(sentence, mapping) |
| 76 | + new_text_data.append((start_time, end_time, sentence, number)) |
| 77 | + result_text[folder_name] = new_text_data |
| 78 | + |
| 79 | + replaced_counts = {} |
| 80 | + new_mms_data = [] |
| 81 | + for row in dataset_mms[folder_name]: |
| 82 | + mapping_mms = dict((normalize_text_to_mms(k), normalize_text_to_mms(v)) for k, v in mapping.items()) |
| 83 | + new_row = row.copy() |
| 84 | + word = row['maingloss'] |
| 85 | + if word in mapping_mms: |
| 86 | + new_row['maingloss'] = mapping_mms[word] |
| 87 | + replaced_counts[word] = replaced_counts.get(word, 0) + 1 |
| 88 | + new_mms_data.append(new_row) |
| 89 | + result_mms[folder_name] = new_mms_data |
| 90 | + |
| 91 | + for location, count in location_counts.items(): |
| 92 | + location_mms = normalize_text_to_mms(location) |
| 93 | + replaced_count = replaced_counts.get(location_mms, 0) |
| 94 | + if replaced_count != count: |
| 95 | + print(f'WARNING: replaced_count in file {folder_name} should be {count} but was {replaced_count}, trying to replace {location_mms}') |
| 96 | + |
| 97 | + return (result_text, result_mms) |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + from .dataset import * |
| 103 | + dataset_text = read_dataset_text() |
| 104 | + dataset_mms = read_dataset_mms() |
| 105 | + |
| 106 | + result_text, result_mms = replace_location_entities(dataset_text, dataset_mms) |
| 107 | + write_dataset_text(dataset_text, result_text, main_folder = 'modified/location/text') |
| 108 | + write_dataset_mms(dataset_mms, result_mms, main_folder = 'modified/location/mms') |
0 commit comments