aapot commited on
Commit
05759f3
1 Parent(s): 0205cf8

Add datasets

Browse files
Files changed (5) hide show
  1. .gitattributes +1 -0
  2. clean_data.py +58 -0
  3. clean_funcs.py +179 -0
  4. train.csv +3 -0
  5. valid.csv +3 -0
.gitattributes CHANGED
@@ -25,3 +25,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
25
  *.zip filter=lfs diff=lfs merge=lfs -text
26
  *.zstandard filter=lfs diff=lfs merge=lfs -text
27
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
25
  *.zip filter=lfs diff=lfs merge=lfs -text
26
  *.zstandard filter=lfs diff=lfs merge=lfs -text
27
  *tfevents* filter=lfs diff=lfs merge=lfs -text
28
+ *.csv filter=lfs diff=lfs merge=lfs -text
clean_data.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datasets
2
+ from fastcore.utils import compose
3
+ from clean_funcs import *
4
+
5
+ fi_mc4 = datasets.load_dataset("mc4", "fi")
6
+ print(fi_mc4)
7
+
8
+ data_preprocessing_funcs = compose(*[fix_html, remove_control_char, remove_remaining_control_chars, remove_unicode_symbols,
9
+ standardise_punc, remove_news_tags, replace_urls, replace_usernames, remove_duplicate_words_punctuation, remove_multi_space])
10
+ data_stats_funcs = compose(*[count_alphabet, count_numbers, count_upper, count_str_len,
11
+ predict_lang, calculate_alphabet_ratio, calculate_number_ratio, calculate_upper_ratio])
12
+
13
+ min_alphabet_ratio = 0.75
14
+ max_upper_ratio = 0.10
15
+ max_number_ratio = 0.05
16
+ min_pred_lang_percentage = 0.95
17
+
18
+ # TRAIN SPLIT
19
+ num_rows = fi_mc4["train"].num_rows
20
+ print(f"Original dataset train rows {num_rows}")
21
+ fi_mc4["train"] = fi_mc4["train"].map(
22
+ data_preprocessing_funcs, num_proc=64, batched=True, writer_batch_size=100000)
23
+
24
+ fi_train_only_longer = fi_mc4["train"].filter(
25
+ lambda example: len(example['text'].split()) >= 20, num_proc=64)
26
+ num_rows = fi_train_only_longer.num_rows
27
+ print(f"Only longer texts dataset train rows {num_rows}")
28
+
29
+ fi_train_only_longer = fi_train_only_longer.map(
30
+ data_stats_funcs, num_proc=64, batched=False, writer_batch_size=100000)
31
+
32
+ fi_train_cleaned = fi_train_only_longer.filter(lambda example: example['alphabet_ratio'] > min_alphabet_ratio and example['upper_ratio'] < max_upper_ratio and example[
33
+ 'number_ratio'] < max_number_ratio and example['predicted_lang'] == '__label__fi' and example['predicted_lang_percentage'] > min_pred_lang_percentage, num_proc=64)
34
+ num_rows = fi_train_cleaned.num_rows
35
+ print(f"Final cleaned dataset train rows {num_rows}")
36
+
37
+ # VAL SPLIT
38
+ num_rows = fi_mc4["validation"].num_rows
39
+ print(f"Original dataset val rows {num_rows}")
40
+ fi_mc4["validation"] = fi_mc4["validation"].map(
41
+ data_preprocessing_funcs, num_proc=64, batched=True)
42
+
43
+ fi_val_only_longer = fi_mc4["validation"].filter(
44
+ lambda example: len(example['text'].split()) >= 20, num_proc=64)
45
+ num_rows = fi_val_only_longer.num_rows
46
+ print(f"Only longer texts dataset val rows {num_rows}")
47
+
48
+ fi_val_only_longer = fi_val_only_longer.map(
49
+ data_stats_funcs, num_proc=64, batched=False)
50
+
51
+ fi_val_cleaned = fi_val_only_longer.filter(lambda example: example['alphabet_ratio'] > min_alphabet_ratio and example['upper_ratio'] < max_upper_ratio and example['number_ratio']
52
+ < max_number_ratio and example['predicted_lang'] == '__label__fi' and example['predicted_lang_percentage'] > min_pred_lang_percentage, num_proc=64)
53
+ num_rows = fi_val_cleaned.num_rows
54
+ print(f"Final cleaned dataset val rows {num_rows}")
55
+
56
+ # SAVE TO DISK
57
+ fi_train_cleaned.to_csv("train.csv", num_proc=64)
58
+ fi_val_cleaned.to_csv("valid.csv", num_proc=64)
clean_funcs.py ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastcore.basics import listify
2
+ import unicodedata
3
+ import unidecode
4
+ from string import punctuation
5
+ import html
6
+ from itertools import groupby
7
+ import fasttext
8
+ import re
9
+
10
+ control_char_regex = re.compile(r'[\r\n\t]+')
11
+ url_regex = re.compile(
12
+ r'((http|https)\:\/\/)?[a-zA-Z0-9\.\/\?\:@\-_=#]+\.([a-zA-Z]){2,6}([a-zA-Z0-9\.\&\/\?\:@\-_=#])*')
13
+ username_regex = re.compile(r'(^|[^@\w])@(\w{1,15})\b')
14
+
15
+ FASTTEXT_MODEL_PATH = 'lid.176.bin'
16
+ fasttext_model = fasttext.load_model(FASTTEXT_MODEL_PATH)
17
+
18
+
19
+ def fix_html(example):
20
+ "From fastai: 'Fix messy things we've seen in documents'"
21
+ tmp_ls = []
22
+ for e in listify(example['text']):
23
+ e = e.replace('#39;', "'").replace('amp;', '&').replace('#146;', "'").replace('nbsp;', ' ').replace(
24
+ '#36;', '$').replace('\\n', "\n").replace('quot;', "'").replace('<br />', "\n").replace(
25
+ '\\"', '"').replace('<unk>', ' ').replace(' @.@ ', '.').replace(' @-@ ', '-').replace('...', ' …')
26
+ tmp_ls.append(html.unescape(e))
27
+
28
+ example['text'] = tmp_ls
29
+ return example
30
+
31
+
32
+ def remove_control_char(example):
33
+ tmp_ls = []
34
+ for e in listify(example['text']):
35
+ tmp_ls.append(re.sub(control_char_regex, '.', e))
36
+
37
+ example['text'] = tmp_ls
38
+ return example
39
+
40
+
41
+ def remove_remaining_control_chars(example):
42
+ tmp_ls = []
43
+ for e in listify(example['text']):
44
+ tmp_ls.append(
45
+ ''.join(ch for ch in e if unicodedata.category(ch)[0] != 'C'))
46
+
47
+ example['text'] = tmp_ls
48
+ return example
49
+
50
+
51
+ def remove_unicode_symbols(example):
52
+ tmp_ls = []
53
+ for e in listify(example['text']):
54
+ tmp_ls.append(
55
+ ''.join(ch for ch in e if unicodedata.category(ch)[0] != 'So'))
56
+
57
+ example['text'] = tmp_ls
58
+ return example
59
+
60
+
61
+ def standardise_punc(example):
62
+ transl_table = dict([(ord(x), ord(y))
63
+ for x, y in zip(u"‘’´“”–-", u"'''\"\"--")])
64
+ tmp_ls = []
65
+ for e in listify(example['text']):
66
+ e = e.translate(transl_table)
67
+ e = re.sub(r"[^a-zA-Z0-9ÖÄÅöäå .,'%&€$=*@+;<>/()!?%:-]", " ", e)
68
+ tmp_ls.append(e)
69
+
70
+ example['text'] = tmp_ls
71
+ return example
72
+
73
+
74
+ def remove_news_tags(example):
75
+ tmp_ls = []
76
+ for e in listify(example['text']):
77
+ e = re.sub(r"(<[A-Z].+?>)|(</[A-Z].+?>)", "", e)
78
+ tmp_ls.append(e)
79
+
80
+ example['text'] = tmp_ls
81
+ return example
82
+
83
+
84
+ def replace_urls(example):
85
+ filler, tmp_ls = '', []
86
+ for e in listify(example['text']):
87
+ e = re.sub(r"(<a.+?>)|(</a>)|(<ref.+?>)", "", e)
88
+ e = re.sub(url_regex, filler, e)
89
+ tmp_ls.append(e)
90
+
91
+ example['text'] = tmp_ls
92
+ return example
93
+
94
+
95
+ def replace_usernames(example):
96
+ filler, tmp_ls = '', []
97
+ for e in listify(example['text']):
98
+ occ = e.count('@')
99
+ for _ in range(occ):
100
+ e = e.replace('@<user>', f'{filler}')
101
+ # replace other user handles by filler
102
+ e = re.sub(username_regex, filler, e)
103
+ # add spaces between, and remove double spaces again
104
+ e = e.replace(filler, f' {filler} ')
105
+ e = ' '.join(e.split())
106
+ tmp_ls.append(e)
107
+
108
+ example['text'] = tmp_ls
109
+ return example
110
+
111
+
112
+ def remove_duplicate_words_punctuation(example):
113
+ tmp_ls = []
114
+ for e in listify(example['text']):
115
+ e = re.sub(r'\b(\w+)( \1\b)+', r'\1', e)
116
+ punc = set(punctuation)
117
+ newtext = []
118
+ for k, g in groupby(e):
119
+ if k in punc:
120
+ newtext.append(k)
121
+ else:
122
+ newtext.extend(g)
123
+ e = ''.join(newtext)
124
+ tmp_ls.append(e)
125
+
126
+ example['text'] = tmp_ls
127
+ return example
128
+
129
+
130
+ def remove_multi_space(example):
131
+ tmp_ls = []
132
+ for e in listify(example['text']):
133
+ tmp_ls.append(' '.join(e.split()))
134
+
135
+ example['text'] = tmp_ls
136
+ return example
137
+
138
+
139
+ def count_alphabet(batch):
140
+ batch['alphabet_len'] = len(re.findall(r'[äÄöÖåÅa-zA-Z]', batch['text']))
141
+ return batch
142
+
143
+
144
+ def count_numbers(batch):
145
+ batch['number_len'] = len(re.findall(r'[0-9]', batch['text']))
146
+ return batch
147
+
148
+
149
+ def count_upper(batch):
150
+ batch['upper_len'] = len(re.findall(r'[ÄÖÅA-Z]', batch['text']))
151
+ return batch
152
+
153
+
154
+ def count_str_len(batch):
155
+ batch['total_len'] = len(batch['text'])
156
+ return batch
157
+
158
+
159
+ def predict_lang(batch):
160
+ pred = fasttext_model.predict(batch['text'])
161
+ batch['predicted_lang'] = pred[0][0]
162
+ batch['predicted_lang_percentage'] = float(pred[1][0])
163
+ return batch
164
+
165
+
166
+ def calculate_alphabet_ratio(batch):
167
+ batch['alphabet_ratio'] = int(
168
+ batch['alphabet_len']) / int(batch['total_len'])
169
+ return batch
170
+
171
+
172
+ def calculate_number_ratio(batch):
173
+ batch['number_ratio'] = int(batch['number_len']) / int(batch['total_len'])
174
+ return batch
175
+
176
+
177
+ def calculate_upper_ratio(batch):
178
+ batch['upper_ratio'] = int(batch['upper_len']) / int(batch['total_len'])
179
+ return batch
train.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:fc9f4e2d48ba5fdd16e8636c17f775bcab1284958705b7cbb097005d1fa8f579
3
+ size 66554165333
valid.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ff43367cf849f61f749cad72307c8bbdb67b46f553c004f35ad07ca683a83a9a
3
+ size 65904851