import os
from subprocess import Popen

from marmot.util.force_align import Aligner

def train_alignments(src_train, tg_train, align_model='align_model'):
    cdec = os.environ['CDEC_HOME']
    if cdec == '':
        sys.stderr.write('No CDEC_HOME variable found. Please install cdec and/or set the variable\n')
        return ''
    if src_train == '' or tg_train == '':
        sys.stderr.write('No parallel corpus for training\n')
        return ''
    # join source and target files
    joint_name = os.path.basename(src_train)+'_'+os.path.basename(tg_train)
    src_tg_file=open(joint_name, 'w')
    get_corp = Popen([cdec+'/corpus/paste-files.pl', src_train, tg_train], stdout=src_tg_file)
    get_corp.wait()
    src_tg_file.close()

    src_tg_clean = open(joint_name+'.clean', 'w')
    clean_corp = Popen([cdec+'/corpus/filter-length.pl', joint_name], stdout=src_tg_clean)
    clean_corp.wait()
    src_tg_clean.close()

    # train the alignment model
    fwd_align = open(align_model+'.fwd_align', 'w')
    rev_align = open(align_model+'.rev_align', 'w')
    fwd_err = open(align_model+'.fwd_err', 'w')
    rev_err = open(align_model+'.rev_err', 'w')

    fwd = Popen([cdec+'/word-aligner/fast_align', '-i'+joint_name+'.clean', '-d', '-v', '-o', '-palign_model.fwd_params'], stdout=fwd_align, stderr=fwd_err )
    rev = Popen([cdec+'/word-aligner/fast_align', '-i'+joint_name+'.clean', '-r', '-d', '-v', '-o', '-palign_model.rev_params'], stdout=rev_align, stderr=rev_err)
    fwd.wait()
    rev.wait()

    fwd_align.close()
    rev_align.close()
    fwd_err.close()
    rev_err.close()

    return os.path.dirname(align_model+'.fwd_align')+align_model


def align_sentence(src_line, tg_line, align_model):
    # TODO: there is an error here if one or both fields are missing -- we cannot align a sentence without both src_line and tg_line
    # throw an error prompting the user to specify another dataset or context creator
    # if not src_line or not tg_line:


    cur_alignments = [[] for i in range(len(tg_line))]

    aligner = Aligner(align_model+'.fwd_params',align_model+'.fwd_err',align_model+'.rev_params',align_model+'.rev_err')
    align_str = aligner.align(' '.join(src_line)+u' ||| '+' '.join(tg_line))
    # parse the return value from the aligner
    for pair in align_str.split():
        pair = pair.split('-')
        cur_alignments[int(pair[1])].append( int(pair[0]) )
    aligner.close()

    return cur_alignments

@@ -3,16 +3,27 @@
 port os%0A
+import sys%0A
 from sub
@@ -89,16 +89,17 @@
 ligner%0A%0A
+%0A
 def trai
@@ -131,16 +131,25 @@
 g_train,
+ tmp_dir,
  align_m
@@ -517,16 +517,26 @@
 t_name =
+ tmp_dir +
  os.path
@@ -602,17 +602,19 @@
 _tg_file
-=
+ = 
 open(joi
@@ -952,24 +952,75 @@
 an.close()%0A%0A
+    align_model_full = tmp_dir + '/' + align_model%0A
     # train 
@@ -1063,32 +1063,37 @@
 open(align_model
+_full
 +'.fwd_align', '
@@ -1124,24 +1124,29 @@
 (align_model
+_full
 +'.rev_align
@@ -1175,32 +1175,37 @@
 open(align_model
+_full
 +'.fwd_err', 'w'
@@ -1228,32 +1228,37 @@
 open(align_model
+_full
 +'.rev_err', 'w'
@@ -1349,32 +1349,34 @@
  '-v', '-o', '-p
+'+
 align_model.fwd_
@@ -1370,16 +1370,23 @@
 gn_model
+_full+'
 .fwd_par
@@ -1424,17 +1424,16 @@
 =fwd_err
- 
 )%0A    re
@@ -1529,16 +1529,18 @@
 -o', '-p
+'+
 align_mo
@@ -1542,16 +1542,23 @@
 gn_model
+_full+'
 .rev_par
@@ -1718,74 +1718,8 @@
 ()%0A%0A
-    return os.path.dirname(align_model+'.fwd_align')+align_model%0A%0A
 %0Adef
@@ -2025,17 +2025,16 @@
 _line:%0A%0A
-%0A
     cur_
@@ -2121,32 +2121,33 @@
 l+'.fwd_params',
+ 
 align_model+'.fw
@@ -2153,16 +2153,17 @@
 wd_err',
+ 
 align_mo
@@ -2180,16 +2180,17 @@
 params',
+ 
 align_mo
@@ -2438,17 +2438,16 @@
 .append(
- 
 int(pair
@@ -2450,17 +2450,16 @@
 pair%5B0%5D)
- 
 )%0A    al

save align model to temporary dir