File size: 2,564 Bytes
6514cc3 df907da 6514cc3 0388815 6514cc3 381bc18 6514cc3 0388815 381bc18 6514cc3 aac2c7b 381bc18 aac2c7b 6514cc3 |
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 80 81 82 83 84 85 86 87 88 89 90 91 |
import pandas as pd
import xmltodict
from sklearn.model_selection import train_test_split
import glob
import sys
import os
filelist = glob.glob('translate_dataset/*.tsv')
data = pd.DataFrame()
for tsvfile in filelist:
tmp = pd.read_csv(tsvfile, sep='\t')
tmp.columns=['source','target']
tmp['rev_source'] = tmp['target']
tmp['rev_target'] = tmp['source']
path = tsvfile.split("/")
source = path[1][0:3]
target = path[1][3:6]
prefix = f"{source}_{target}: "
tmp['source'] = prefix + tmp['source']
rev_prefix = f"{target}_{source}: "
tmp['rev_source'] = rev_prefix + tmp['rev_source']
data = pd.concat([data,tmp])
#Shuffle
data = data.sample(frac=1).reset_index(drop=True)
# Add both directions
original = data[['source','target']]
reverse = data[['rev_source','rev_target']]
reverse.columns=['source','target']
data = pd.concat([original,reverse])
#remove the source prefix
data['source'] = data['source'].str[4:]
data = data.sample(frac=1).reset_index(drop=True)
#Clean before splitting
data['source'] = data['source'].str.replace('\t',' ')
data['source'] = data['source'].str.replace('\n',' ')
data['target'] = data['target'].str.replace('\t',' ')
data['target'] = data['target'].str.replace('\n',' ')
# Train - test - dev
train, test = train_test_split(data, test_size=0.2)
test, dev = train_test_split(test, test_size=0.5)
# Write the datasets to disk
test.to_csv('tsv_all_target/test.tsv', index=False, header=False, sep='\t')
dev.to_csv('tsv_all_target/dev.tsv', index=False, header=False, sep='\t')
print("Finished writint dev and test. Now we are adding some extra language detection and English translation")
# Add the language detection to the training dataset
filelist = glob.glob('langid/langid_datafiles/*train.tsv')
for tsvfile in filelist:
tmp = pd.read_csv(tsvfile, sep='\t')
tmp.columns=['source','target']
train = pd.concat([train,tmp])
# Add extra English training set - Currently we do not add this to the test/dev-collection
tmp = pd.read_csv('eng_nob/train.tsv', sep='\t')
tmp.columns=['source','target']
train = pd.concat([train,tmp])
#Final shuffle
train = train.sample(frac=1).reset_index(drop=True)
#Clean before splitting
train['source'] = train['source'].str.replace('\t',' ')
train['source'] = train['source'].str.replace('\n',' ')
train['target'] = train['target'].str.replace('\t',' ')
train['target'] = train['target'].str.replace('\n',' ')
train.to_csv('tsv_all_target/train.tsv', index=False, header=False, sep='\t')
print("Finished")
|