# -*- coding: utf-8 -*-

"""Tool for exploring Lost data.

Assume data can be found at '/people/pecheux/data/train10/'

Data sets created are the following:

- en: list of english sentences
- fr: list of french sentences
- ali: list of symetric alignments
- ali_sents: list of aligned sentences.
      An aligned sentence the list of tuples for that sentence.
- all_tpl_sent: dictionary 
      {key:tuple, value:set of sentences with that tuple in}

Function provided:

- print_sent
- print_sent_horizontal
---

Nicolas Pécheux
Thursday Jun 15, 2012
"""

__version__ = 0.1


import collections


data_path = '/people/pecheux/data/train10/'

direction = 'en2fr'

# Load data

# en: list of english sentences
with open(data_path + 'en') as en_file:
    en = [line.strip() for line in en_file]

# fr: list of french sentences
with open(data_path + 'fr') as fr_file:
    fr = [line.strip() for line in fr_file]

# ali: list of symetric alignments
with open(data_path + direction + '.ali') as ali_file:
    ali = [line.strip() for line in ali_file]

# ali_sents: list of aligned sentences.
#    An aligned sentence the list of tuples for that sentence.
with open(data_path + direction + '.fst') as fsts_file:
    fsts = fsts_file.read().split('EOS\n')[:-1]
# !!! Fix: Not efficient and not nice in fact
ali_sents = []
for fst in fsts:
    fst = fst.split('\n')[1:-3]
    tmp_sent = []
    for line in fst:
        tmp = [elt.split('|')[0] for elt in line.split()[2:4]]
        tmp_sent += [tmp]
    ali_sents += [tmp_sent]

# all_tpl_sent: dictionary 
#      {key:tuple, value:set of sentences with that tuple in}
all_tpl_sent = {}
for i, sent in enumerate(ali_sents):
    for src, trg in sent:
        if (src, trg) not in all_tpl_sent:
            all_tpl_sent[(src, trg)] = set()
        all_tpl_sent[(src, trg)].add(i)

def print_sent(n):
    # !!! Fix: Note very sure this is elegant...
    # !!! ToDo: Align nicely with format (howTo center ?)
    sent = ali_sents[n]
    en_sent, fr_sent = zip(*sent)
    for e in en_sent:
        print e,
    print '\n'
    for f in fr_sent:
        print f,
    print

def print_sent_horizontal(n):
    sent = ali_sents[n]
    n = max([len(t[0]) for t in sent]);
    for t in sent:
        print '{:{width}}\t{}'.format(t[0], t[1], width=n)
