forked from nlpaueb/greek-bert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnormalize_data.py
67 lines (58 loc) · 2.17 KB
/
normalize_data.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
import glob
from multiprocessing import Pool
import unicodedata
filenames = glob.glob('/home/chalkidis/greek_corpora/common_crawl_shards/*')
filenames += glob.glob('/home/chalkidis/greek_corpora/europarl_shards/*')
filenames += glob.glob('/home/chalkidis/greek_corpora/wikipedia_shards/*')
def _is_punctuation(char):
"""Checks whether `chars` is a punctuation character."""
cp = ord(char)
# We treat all non-letter/number ASCII as punctuation.
# Characters such as "^", "$", and "`" are not in the Unicode
# Punctuation class but we treat them as punctuation anyways, for
# consistency.
if ((cp >= 33 and cp <= 47) or (cp >= 58 and cp <= 64) or
(cp >= 91 and cp <= 96) or (cp >= 123 and cp <= 126)):
return True
cat = unicodedata.category(char)
if cat.startswith("P"):
return True
return False
def _run_split_on_punc(text):
"""Splits punctuation on a piece of text."""
chars = list(text)
i = 0
start_new_word = True
output = []
while i < len(chars):
char = chars[i]
if _is_punctuation(char):
output.append([char])
start_new_word = True
else:
if start_new_word:
output.append([])
start_new_word = False
output[-1].append(char)
i += 1
return ["".join(x) for x in output]
def strip_accents_and_lowercase(s):
return ''.join(c for c in unicodedata.normalize('NFD', s)
if unicodedata.category(c) != 'Mn').lower()
def normalize(filename):
output_file = open(filename.replace('greek_corpora', 'greek_corpora_norm'), 'w', encoding='utf8')
with open(filename, encoding='utf8') as file:
for line in file.readlines():
tokens = line.lower().split()
splited_tokens = []
for token in tokens:
splited_tokens.extend(_run_split_on_punc(token))
line = ' '.join(splited_tokens)
line = strip_accents_and_lowercase(line)
if line.endswith('\n'):
output_file.write(line)
else:
output_file.write(line+'\n')
output_file.close()
with Pool(processes=10) as pool:
pool.map(normalize, filenames)