| import re
|
| import pandas as pd
|
| from tqdm import tqdm
|
| from bs4 import BeautifulSoup
|
|
|
|
|
|
|
| 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 idx, sentence in tqdm(enumerate(sentences)):
|
|
|
|
|
| for lang in sentence:
|
|
|
|
|
| if lang == "en" or len(sentence[lang].replace(" ","")) <= 0:
|
| continue
|
|
|
|
|
| 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)
|
|
|
|
|
| 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()
|
|
|