-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClean_author2doc.py
92 lines (68 loc) · 3.19 KB
/
Clean_author2doc.py
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
"""
Time: Sat Oct 22
@author: Jiannan, Tin
"""
import json
import re
import string
from collections import defaultdict
import itertools
# Remove special characters
def remove_punctuation(text):
text = ''.join([c for c in text if c not in string.punctuation])
return text
def clean_author2doc(file_uncleaned):
# input: uncleaned author2doc json file
# output: cleaned author2doc json file
# open JSON file
f = open(file_uncleaned)
# load json file, a dictionary
data = json.load(f)
# for key_token in data.keys():
data_keys = list(data.keys())
data_keys_cleaned = [remove_punctuation(i) for i in data_keys]
# data_keys_cleaned_unique = [*set(data_keys_cleaned)]
# create a dictionary of frequency
dict_cleaned = defaultdict(list)
for i,item in enumerate(data_keys_cleaned):
dict_cleaned[item].extend([i])
# create a dictionary of final output
json_cleaned_target = defaultdict(list)
for keys in dict_cleaned.keys():
json_cleaned_target[keys] = list(itertools.chain.from_iterable([data.get(list(data)[i]) for i in dict_cleaned[keys]]))
# further adjustments
# drop keys that don't make sense
drop_keys = ['', 'us','Mitwirkung','gerichts','unter']
for key in drop_keys:
json_cleaned_target.pop(key, None)
# extend the right keys
json_cleaned_target['Broß'].extend(json_cleaned_target['Bro'])
json_cleaned_target['DiFabio'].extend(json_cleaned_target['Dixa0Fabio'])
json_cleaned_target['Gerhardt'].extend(json_cleaned_target['Gerhard'])
json_cleaned_target['Hassemer'].extend(json_cleaned_target['Hasseme'])
json_cleaned_target['Henschel'].extend(json_cleaned_target['Hentschel'])
json_cleaned_target['Hermanns'].extend(json_cleaned_target['Herrmanns'])
json_cleaned_target['Hömig'].extend(json_cleaned_target['Hmig'])
json_cleaned_target['HohmannDennhard'].extend(json_cleaned_target['HohmannDennhardt'])
json_cleaned_target['Jäger'].extend(json_cleaned_target['Jaeger'])
json_cleaned_target['KessalWulf'].extend(json_cleaned_target['KessalWulff'])
json_cleaned_target['Kühling'].extend(json_cleaned_target['Khling'])
json_cleaned_target['LübbeWolff'].extend(json_cleaned_target['LübbeWoff'])
json_cleaned_target['Voßkuhle'].extend(json_cleaned_target['Vosskuhle'])
json_cleaned_target['Wallrabenstein'].extend(json_cleaned_target['Wallrabensein'])
drop_keys_alt = ['Bro','Gerhard','Hasseme','Hentschel','Herrmanns','Hmig','HohmannDennhardt', \
'Jaeger','KessalWulff','Khling','LübbeWoff','Vosskuhle','Wallrabensein','Dixa0Fabio']
for key in drop_keys_alt:
json_cleaned_target.pop(key, None)
# rename one key
json_cleaned_target['Leusser'] = json_cleaned_target.pop('Leuser', None)
#Delete any key with None value
for key, value in json_cleaned_target.copy().items():
if value is None:
del json_cleaned_target[key]
print(sorted(json_cleaned_target))
return json_cleaned_target
if __name__ == "__main__":
json_cleaned = clean_author2doc("author2doc_bverfg230107.json")
with open('clean_author2doc_bverfg230107.json', 'w') as f:
json.dump(json_cleaned, f)