File size: 1,919 Bytes
6a5e475
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
import os
import pandas as pd
from tqdm import tqdm
from tabulate import tabulate
from translate.storage.tmx import tmxfile

# http://docs.translatehouse.org/projects/translate-toolkit/en/latest/api/storage.html

INPUT_DIR = "corpus/"
OUTPUT_PATH = "csv_corpus/"
os.makedirs(OUTPUT_PATH, exist_ok=True)

x = []

# For each language file
for file_name in tqdm(os.listdir(INPUT_DIR)):

    if ".tmx" not in file_name:
        continue

    # Documents counter
    cpt = 0

    # Lenght of the sentences
    source_lens = []
    target_lens = []

    print("file_name : ", file_name)

    # Get the languages
    LANG_PAIR = file_name.split(".")[0]
    L1, L2 = LANG_PAIR.split("-")

    data = []

    df = pd.DataFrame(data={
        'id': [],
        'lang': [],
        'source_text': [],
        'target_text': []
    })

    # Read the file
    with open(INPUT_DIR + file_name, 'rb') as fin:

        # For each sentence
        for node in tmxfile(fin, L1, L2).unit_iter():

            # Add the sentence pair
            data.append({
                'id': node.getid(),
                'lang': LANG_PAIR,
                'source_text': node.source,
                'target_text': node.target
            })

            cpt += 1
            source_lens.append(len(node.source.split(" ")))
            target_lens.append(len(node.target.split(" ")))

    x.append([
        L2,
        cpt,
        int(sum(source_lens) / len(source_lens)),
        int(sum(target_lens) / len(target_lens))
    ])

    # Add to the data frame
    df = df.append(data)

    # Convert to CSV
    df.to_csv(OUTPUT_PATH + LANG_PAIR + ".csv", index=False)

log_file = open("stats.md","w")
log_file.write(str(
    tabulate(x, headers=['Lang', '# Docs', 'Avg. # Source Tokens', 'Avg. # Target Tokens'], tablefmt='orgtbl')
).replace("+","|"))
log_file.close()