RaiBP commited on
Commit
bd26805
·
verified ·
1 Parent(s): 963a04b

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +62 -0
README.md CHANGED
@@ -1,3 +1,65 @@
1
  ---
2
  license: mit
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: mit
3
  ---
4
+ ## Counting translation instances
5
+ In order to count translation instances containing English paired with German, French, Spanish, Portuguese, Italian or Dutch, you can use:
6
+ ```python
7
+ from datasets import load_dataset
8
+ import json
9
+ from tqdm import tqdm
10
+
11
+ # Specify the dataset name
12
+ dataset_name = "RaiBP/openwebtext2-first-30-chunks-lang-detect-raw-output"
13
+
14
+ # Load the dataset
15
+ translation_dataset = load_dataset(dataset_name, data_dir="translation")
16
+
17
+ dataset = translation_dataset["train"]
18
+ n_examples = len(dataset)
19
+ total_instances = 0
20
+ counts_dict = {"de": 0, "fr": 0, "es": 0, "pt": 0, "it": 0, "nl": 0}
21
+ others_count = 0
22
+ instances = {}
23
+ for document in tqdm(dataset, total=n_examples):
24
+ embedded_label = document["embedded_label"]
25
+ primary_label = document["primary_label"]
26
+ document_id = document["document_index"]
27
+ instance_id = document["instance_index"]
28
+ id = f"{document_id}-{instance_id}"
29
+ if id not in instances.keys():
30
+ instances[id] = [f"{embedded_label}-{primary_label}"]
31
+ else:
32
+ instances[id].append(f"{embedded_label}-{primary_label}")
33
+
34
+ for id, labels in instances.items():
35
+ state = 0
36
+ found_langs = []
37
+ for langs in labels:
38
+ lang_pair = langs.split("-")
39
+ if "en" in lang_pair:
40
+ non_english = lang_pair[0] if lang_pair[1] == "en" else lang_pair[1]
41
+ if non_english in counts_dict.keys():
42
+ state = 1 # found a translation with English and a language in the counts_dict
43
+ found_langs.append(non_english)
44
+ elif state != 1:
45
+ state = 2 # found a translation with English and a language not in the counts_dict
46
+ elif state != 1:
47
+ state = 2 # found a translation without English
48
+ if state == 1:
49
+ majority_lang = max(set(found_langs), key=found_langs.count)
50
+ counts_dict[majority_lang] += 1
51
+ elif state == 2:
52
+ others_count += 1
53
+ else:
54
+ print("Error: state is 0")
55
+
56
+ # Specify the file path where you want to save the JSON file
57
+ file_path = "translation_counts.json"
58
+ counts_dict["others"] = others_count
59
+
60
+ # Save the dictionary as a JSON file
61
+ with open(file_path, "w") as json_file:
62
+ json.dump(
63
+ counts_dict, json_file, indent=2
64
+ ) # indent argument is optional, but it makes the file more human-readable
65
+ ```