Create update_dataset.py
Browse files- update_dataset.py +50 -0
update_dataset.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datasets import load_dataset
|
2 |
+
import random
|
3 |
+
|
4 |
+
dataset = load_dataset("Jean-Baptiste/wikiner_fr")
|
5 |
+
|
6 |
+
|
7 |
+
# Remove duplicated rows in the dataset #####
|
8 |
+
|
9 |
+
|
10 |
+
# Remove duplicates in each set
|
11 |
+
def remove_duplicates(examples: dict[str, list]) -> list[bool]:
|
12 |
+
seen_sentences = set()
|
13 |
+
res = []
|
14 |
+
for example_tokens in examples['tokens']:
|
15 |
+
sentence = tuple(example_tokens)
|
16 |
+
if sentence not in seen_sentences:
|
17 |
+
res.append(True)
|
18 |
+
seen_sentences.add(sentence)
|
19 |
+
else:
|
20 |
+
res.append(False)
|
21 |
+
print(f"Removed {len(examples['tokens']) - sum(res)} duplicates")
|
22 |
+
return res
|
23 |
+
|
24 |
+
|
25 |
+
dataset = dataset.filter(remove_duplicates, batched=True, batch_size=None)
|
26 |
+
|
27 |
+
# Remove the duplicates in the train set present in the test set (leakage)
|
28 |
+
test_sentences = set(tuple(w) for w in dataset['test']['tokens'])
|
29 |
+
dataset['train'] = dataset['train'].filter(
|
30 |
+
lambda examples: [s not in test_sentences for s in [tuple(w) for w in examples['tokens']]],
|
31 |
+
batched=True,
|
32 |
+
batch_size=None
|
33 |
+
)
|
34 |
+
|
35 |
+
|
36 |
+
# Decapitalize words randomly #####
|
37 |
+
|
38 |
+
def decapitalize_tokens(example, probability=0.2):
|
39 |
+
for i, token in enumerate(example['tokens']):
|
40 |
+
if token.istitle() and \
|
41 |
+
i != 0 and \
|
42 |
+
random.random() < probability and \
|
43 |
+
example['ner_tags'][i] != 0:
|
44 |
+
example['tokens'][i] = token.lower()
|
45 |
+
return example
|
46 |
+
|
47 |
+
|
48 |
+
dataset_with_mixed_caps = dataset.map(decapitalize_tokens)
|
49 |
+
|
50 |
+
dataset_with_mixed_caps.push_to_hub("wikiner_fr_mixed_caps")
|