Add prep code
Browse files- .gitignore +3 -0
- data_prep/original_v0/download.sh +17 -0
- data_prep/original_v0/extract.py +99 -0
- data_prep/original_v1/extract.py +98 -0
.gitignore
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.tar
|
| 2 |
+
data_prep/original_v0/data/
|
| 3 |
+
data_prep/original_v1/data/
|
data_prep/original_v0/download.sh
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
wget https://object.pouta.csc.fi/Tatoeba-Challenge-devtest/dev.tar
|
| 2 |
+
wget https://object.pouta.csc.fi/Tatoeba-Challenge-devtest/test.tar
|
| 3 |
+
|
| 4 |
+
# extract
|
| 5 |
+
tar -xvf dev.tar
|
| 6 |
+
tar -xvf test.tar
|
| 7 |
+
|
| 8 |
+
mv data/release/test/v2023-09-26 data/release/test/latest
|
| 9 |
+
mv data/release/dev/v2023-09-26 data/release/dev/latest
|
| 10 |
+
|
| 11 |
+
for file in data/release/test/latest/*.gz; do
|
| 12 |
+
gunzip $file
|
| 13 |
+
done
|
| 14 |
+
|
| 15 |
+
for file in data/release/dev/latest/*.gz; do
|
| 16 |
+
gunzip $file
|
| 17 |
+
done
|
data_prep/original_v0/extract.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from glob import glob
|
| 2 |
+
from datasets import Dataset
|
| 3 |
+
|
| 4 |
+
langs = [
|
| 5 |
+
"asm",
|
| 6 |
+
"awa",
|
| 7 |
+
# "bal",
|
| 8 |
+
"ben",
|
| 9 |
+
"bho",
|
| 10 |
+
# "bod",
|
| 11 |
+
"brx",
|
| 12 |
+
# "div",
|
| 13 |
+
"guj",
|
| 14 |
+
# "hif",
|
| 15 |
+
"hin",
|
| 16 |
+
"kan",
|
| 17 |
+
"kha",
|
| 18 |
+
"kok",
|
| 19 |
+
"lah",
|
| 20 |
+
"mai",
|
| 21 |
+
"mal",
|
| 22 |
+
"mar",
|
| 23 |
+
"mni",
|
| 24 |
+
"nep",
|
| 25 |
+
"nst",
|
| 26 |
+
"ori",
|
| 27 |
+
"pan",
|
| 28 |
+
"pli",
|
| 29 |
+
# "rhg",
|
| 30 |
+
"san",
|
| 31 |
+
"sat",
|
| 32 |
+
# "sin",
|
| 33 |
+
"snd",
|
| 34 |
+
"tam",
|
| 35 |
+
"tel",
|
| 36 |
+
"urd",
|
| 37 |
+
]
|
| 38 |
+
|
| 39 |
+
HF_REPO = "sarvamai/tatoeba-indic"
|
| 40 |
+
|
| 41 |
+
dev_dir = "data/release/dev/latest"
|
| 42 |
+
|
| 43 |
+
for lang in langs:
|
| 44 |
+
files = glob(f"{dev_dir}/*.eng-{lang}.txt") + glob(f"{dev_dir}/*.{lang}-eng.txt")
|
| 45 |
+
|
| 46 |
+
if not files:
|
| 47 |
+
print(f"No dev files found for lang: {lang}")
|
| 48 |
+
continue
|
| 49 |
+
|
| 50 |
+
assert len(files) == 1
|
| 51 |
+
|
| 52 |
+
src_lines, tgt_lines = [], []
|
| 53 |
+
|
| 54 |
+
with open(files[0], "r") as f:
|
| 55 |
+
for line in f:
|
| 56 |
+
src_lang, tgt_lang, src, tgt = line.strip().split("\t")
|
| 57 |
+
if src_lang != "eng":
|
| 58 |
+
# Swap src and tgt
|
| 59 |
+
src_lang, tgt_lang = tgt_lang, src_lang
|
| 60 |
+
src, tgt = tgt, src
|
| 61 |
+
|
| 62 |
+
if not src.strip() or not tgt.strip():
|
| 63 |
+
continue
|
| 64 |
+
|
| 65 |
+
src_lines.append(src.strip())
|
| 66 |
+
tgt_lines.append(tgt.strip())
|
| 67 |
+
|
| 68 |
+
dataset = Dataset.from_dict({"src": src_lines, "tgt": tgt_lines})
|
| 69 |
+
dataset.push_to_hub(HF_REPO, lang, split="dev")
|
| 70 |
+
|
| 71 |
+
test_dir = "data/release/test/latest"
|
| 72 |
+
|
| 73 |
+
for lang in langs:
|
| 74 |
+
files = glob(f"{test_dir}/*.eng-{lang}.txt") + glob(f"{test_dir}/*.{lang}-eng.txt")
|
| 75 |
+
|
| 76 |
+
if not files:
|
| 77 |
+
print(f"No test files found for lang: {lang}")
|
| 78 |
+
continue
|
| 79 |
+
|
| 80 |
+
assert len(files) == 1
|
| 81 |
+
|
| 82 |
+
src_lines, tgt_lines = [], []
|
| 83 |
+
|
| 84 |
+
with open(files[0], "r") as f:
|
| 85 |
+
for line in f:
|
| 86 |
+
src_lang, tgt_lang, src, tgt = line.strip().split("\t")
|
| 87 |
+
if src_lang != "eng":
|
| 88 |
+
# Swap src and tgt
|
| 89 |
+
src_lang, tgt_lang = tgt_lang, src_lang
|
| 90 |
+
src, tgt = tgt, src
|
| 91 |
+
|
| 92 |
+
if not src.strip() or not tgt.strip():
|
| 93 |
+
continue
|
| 94 |
+
|
| 95 |
+
src_lines.append(src.strip())
|
| 96 |
+
tgt_lines.append(tgt.strip())
|
| 97 |
+
|
| 98 |
+
dataset = Dataset.from_dict({"src": src_lines, "tgt": tgt_lines})
|
| 99 |
+
dataset.push_to_hub(HF_REPO, lang, split="test")
|
data_prep/original_v1/extract.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
from glob import glob
|
| 4 |
+
import time
|
| 5 |
+
|
| 6 |
+
from datasets import Dataset
|
| 7 |
+
|
| 8 |
+
langs = [
|
| 9 |
+
"asm",
|
| 10 |
+
"awa",
|
| 11 |
+
# "bal",
|
| 12 |
+
"ben",
|
| 13 |
+
"bho",
|
| 14 |
+
# "bod",
|
| 15 |
+
"brx",
|
| 16 |
+
# "div",
|
| 17 |
+
"guj",
|
| 18 |
+
# "hif",
|
| 19 |
+
"hin",
|
| 20 |
+
"kan",
|
| 21 |
+
"kha",
|
| 22 |
+
"kok",
|
| 23 |
+
# "lah",
|
| 24 |
+
"mai",
|
| 25 |
+
"mal",
|
| 26 |
+
"mar",
|
| 27 |
+
"mni",
|
| 28 |
+
"nep",
|
| 29 |
+
# "nst",
|
| 30 |
+
"ori",
|
| 31 |
+
"pan",
|
| 32 |
+
"pli",
|
| 33 |
+
# "rhg",
|
| 34 |
+
"san",
|
| 35 |
+
"sat",
|
| 36 |
+
# "sin",
|
| 37 |
+
"snd",
|
| 38 |
+
"tam",
|
| 39 |
+
"tel",
|
| 40 |
+
"urd",
|
| 41 |
+
]
|
| 42 |
+
|
| 43 |
+
HF_REPO = "sarvamai/tatoeba-indic"
|
| 44 |
+
BASE_URL = "https://object.pouta.csc.fi/Tatoeba-Challenge-v2023-09-26/{direction}.tar"
|
| 45 |
+
|
| 46 |
+
for lang in langs:
|
| 47 |
+
direction = f"eng-{lang}"
|
| 48 |
+
url1 = BASE_URL.format(direction=direction)
|
| 49 |
+
# Check if the file exists
|
| 50 |
+
response = requests.head(url1)
|
| 51 |
+
if response.status_code == 200:
|
| 52 |
+
print(f"File exists: {url1}")
|
| 53 |
+
download_url = url1
|
| 54 |
+
else:
|
| 55 |
+
direction = f"{lang}-eng"
|
| 56 |
+
url2 = BASE_URL.format(direction=direction)
|
| 57 |
+
response = requests.head(url2)
|
| 58 |
+
if response.status_code == 200:
|
| 59 |
+
print(f"File exists: {url2}")
|
| 60 |
+
download_url = url2
|
| 61 |
+
else:
|
| 62 |
+
raise ValueError(f"File does not exist: {url1} or {url2}")
|
| 63 |
+
|
| 64 |
+
file_name = download_url.split("/")[-1]
|
| 65 |
+
if not os.path.exists(file_name):
|
| 66 |
+
# Download the file
|
| 67 |
+
os.system(f"wget {download_url}")
|
| 68 |
+
|
| 69 |
+
# Extract the file
|
| 70 |
+
os.system(f"tar -xvf {file_name}")
|
| 71 |
+
|
| 72 |
+
src_lang, tgt_lang = direction.split("-")
|
| 73 |
+
|
| 74 |
+
sets = ["test", "dev"]
|
| 75 |
+
|
| 76 |
+
for split in sets:
|
| 77 |
+
src_files = glob(f"data/release/**/{direction}/{split}.src")
|
| 78 |
+
if not src_files:
|
| 79 |
+
print(f"No {split} src files found for {direction}")
|
| 80 |
+
continue
|
| 81 |
+
|
| 82 |
+
tgt_files = glob(f"data/release/**/{direction}/{split}.trg")
|
| 83 |
+
|
| 84 |
+
src_lines, tgt_lines = [], []
|
| 85 |
+
for src_line in open(src_files[0]):
|
| 86 |
+
src_lines.append(src_line.strip())
|
| 87 |
+
|
| 88 |
+
for tgt_line in open(tgt_files[0]):
|
| 89 |
+
tgt_lines.append(tgt_line.strip())
|
| 90 |
+
|
| 91 |
+
assert len(src_lines) == len(tgt_lines), f"Lengths of {split} src and tgt files are not equal for {direction}"
|
| 92 |
+
|
| 93 |
+
if src_lang != "eng":
|
| 94 |
+
# Swap src and tgt
|
| 95 |
+
src_lines, tgt_lines = tgt_lines, src_lines
|
| 96 |
+
|
| 97 |
+
dataset = Dataset.from_dict({"src": src_lines, "tgt": tgt_lines})
|
| 98 |
+
dataset.push_to_hub(HF_REPO, lang, split=split, commit_message=f"Add {split} split for {lang} language"); time.sleep(2)
|