#!/bin/bash

s=sl
t=hr
f=Case


# NORMALIZATION

# Normalize the treebank
./normalize.pl $s < $s-ud-train.conllu > $s-ud-train.norm.conllu


# TRANSLATION

# Train source tagger
udpipe --train --tokenizer=none --parser=none $s.tagger.udpipe $s-ud-train.norm.conllu

# Word-align the treebank
./monogreedy_align.sh $s $t > monogr_$s-$t-tag.aligned

# Create the translation table
./trtable_src2tgt_feats.py < monogr_$s-$t-tag.aligned trtable_$s-$t.pickle

# Translate the treebank
./translate_conll_src2tgt_feats.py < $s-ud-train.norm.conllu trtable_$s-$t.pickle > $s-ud-train.norm.trans-$t.conllu


# EMBEDDINGS

# Tokenize
treex Read::Sentences lines_per_doc=10000 W2A::Tokenize A2W::ConcatenateTokens Write::Sentences < OpenSubtitles2016.$s-$t.$t > OpenSubtitles2016.$s-$t.$t.tok

# Pre-train word2vec
word2vec -train OpenSubtitles2016.$s-$t.$t.tok -output OpenSubtitles2016.$s-$t.$t.vectors -cbow 0 -size 50 -window 10 -negative 5 -hs 0 -sample 1e-1 -threads 4 -binary 0 -iter 15 -min-count 5


# MORPHO FEATS

# Simplify morpho feats
./feats2FEAT.py $f < $s-ud-train.norm.trans-$t.conllu > $s-ud-train.norm.trans-$t.$f.conllu


# PARSER

# Train the parser (and tagger, so that the parser is trained on predicted tags)
udpipe --train --tokenizer=none \
    --tagger='use_lemma=0;provide_lemma=0;use_xpostag=0;provide_xpostag=0' \
    --parser='embedding_form_file=OpenSubtitles2016.'$s-$t.$t'.vectors' \
    $s.norm.trans-$t.$f.udpipe $s-ud-train.norm.trans-$t.$f.conllu

# Eval on dev
cat $t-ud-predPoS-dev.conllu | \
    ./feats2FEAT.py $f | \
    udpipe --accuracy --parse $s.norm.trans-$t.$f.udpipe

