ECDC / csv /convert_csv.py
qanastek's picture
Update
a9d8f74
raw
history blame
1.67 kB
import re
import pandas as pd
from tqdm import tqdm
from bs4 import BeautifulSoup
# https://wt-public.emm4u.eu/Resources/ECDC-TM/2012_10_Terms-of-Use_ECDC-TM.pdf
INPUT_TMX = "ECDC.tmx"
print("⌛ Load TMX...")
tree = BeautifulSoup(open(INPUT_TMX,"r"), features="lxml")
print("⚗️ Parse content")
sentences = []
for tu in tqdm(tree.findAll("tu")):
langs = {}
for tuv in tu.findAll("tuv"):
text = re.sub("\s+", " ", tuv.seg.text.replace("\n"," "))
langs[tuv["xml:lang"].lower()] = text
sentences.append(langs)
content = []
print("⚙️ Convert to CSV")
# For each sentences
for idx, sentence in tqdm(enumerate(sentences)):
# For each language
for lang in sentence:
# Pass english
if lang == "en" or len(sentence[lang].replace(" ","")) <= 0:
continue
# Add row
content.append({
'key': "doc_" + str(idx),
'lang': "en-" + lang,
'source_text': sentence["en"],
'target_text': sentence[lang]
})
df = pd.DataFrame({
'key': [],
'lang': [],
'source_text': [],
'target_text': []
})
df = df.append(content)
# Sort by language pair
df = df.sort_values(by=['lang'], ascending=True)
print("💾 Save as CSV and GZ")
df.to_csv("ECDC.csv", index=False)
df.to_csv("ECDC.csv.gz", index=False, compression="gzip")
uniq_elements = list(set(df['lang'].unique()))
l = open("langs.txt","w")
l.write("\n".join(uniq_elements))
l.close()
uniq_elements = df.groupby('lang').count()
l = open("stats.md","w")
l.write(str(uniq_elements))
l.close()