from codecs import open as copen
from os import listdir
from re import match, sub, findall
from structures import getStopwords
from utility import loadStruct
from xml.dom.minidom import parse


def convertFormat(f_lda, f_proj, slda=True):
    """ Format to either SLDA or LLDA.

    LLDA: [l_1] w_1 ... w_n
    SLDA: [l_1 ... l_k|w_1 ... w_n] ... [l_1 ... l_k|w_1 ... w_n]
    """
    print "Converting to SLDA format"

    formal = 0
    informal = 0
    neutral = 0
    double = 0
    nn_lost = 0
    on_lost = 0
    no_lost = 0
    total = 0

    with copen(f_lda, "w", encoding="utf-8") as lda_f:
        for f in listdir(f_proj):
            with copen(f_proj + f, encoding="utf-8") as proj_f:
                for line in proj_f:
                    m = match(".*>(.+)</.*", line)

                    if m:
                        clean = m.group(1).strip().split(" ")

                        if len(clean) > 1:
                            sentence = " ".join(clean)

                            if "f=\"0\" i=\"0\"" in line:
                                if not "you" in clean:
                                    neutral += 1
                                    total += 1
                                    if slda:
                                        lda_f.write("[2|%s]." % sentence)
                                    else:
                                        lda_f.write("[2] %s\n" % sentence)
                                else:
                                    nn_lost += 1
                            elif "f=\"1\" i=\"0\"" in line:
                                if not "you" in clean:
                                    on_lost += 1
                                else:
                                    formal += 1
                                    total += 1
                                    if slda:
                                        lda_f.write("[1|%s]." % sentence)
                                    else:
                                        lda_f.write("[1] %s\n" % sentence)
                            elif "f=\"0\" i=\"1\"" in line:
                                if not "you" in clean:
                                    no_lost += 1
                                else:
                                    informal += 1
                                    total += 1
                                    if slda:
                                        lda_f.write("[0|%s]." % sentence)
                                    else:
                                        lda_f.write("[0] %s\n" % sentence)
                            elif "f=\"1\" i=\"1\"" in line:
                                double += 0
                if slda:
                    lda_f.write("\n")
    print "Formal: %d" % formal
    print "Informal: %d" % informal
    print "Neutral: %d" % neutral
    print "Formal + Informal: %d" % double
    print "You in Neutral: %d" % nn_lost
    print "You not in Formal: %d" % on_lost
    print "You not in Informal: %d" % no_lost
    print "Total sentences %d" % total


def convertGutenberg(f_root, f_type, f_gproj, slda=True, filter=True):
    """ Converts the Gutenberg corps to SLDA or LLDA format. """
    stopwords = getStopwords()
    formal = 0
    informal = 0
    neutral = 0
    double = 0
    nn_lost = 0
    on_lost = 0
    no_lost = 0
    total = 0
    form = "SLDA"
    f_ttype = f_type

    if not slda:
        form = "LLDA"

    if f_type == "test":
        f_ttype = "gold"

    print "Converting Gutenberg corpus to %s format for %sing" % (form, f_type)

    with copen(f_root + f_ttype, "w", encoding="utf-8") as lda_f:
        for f in listdir(f_gproj + f_type):
            with copen(f_gproj + f_type + "/" + f, encoding="utf-8") as proj_f:
                for line in proj_f:
                    m = match(".*>(.+)</.*", line)

                    if m:
                        sentence = m.group(1).strip().split(" ")
                        clean = []

                        for word in sentence:
                            word = word.lower()

                            if filter:
                                if word not in stopwords and len(word) > 1 and word.isalpha():
                                    clean.append(word)
                            else:
                                if word.isalpha():
                                    clean.append(word)

                        if len(clean) > 1:
                            sentence = " ".join(clean)

                            if "f=\"0\" i=\"0\"" in line:
                                if not "you" in clean:
                                    neutral += 1
                                    total += 1
                                    if slda:
                                        lda_f.write("[2|%s]." % sentence)
                                    else:
                                        lda_f.write("[2] %s\n" % sentence)
                                else:
                                    nn_lost += 1
                            elif "f=\"1\" i=\"0\"" in line:
                                if not "you" in clean:
                                    on_lost += 1
                                else:
                                    formal += 1
                                    total += 1
                                    if slda:
                                        lda_f.write("[1|%s]." % sentence)
                                    else:
                                        lda_f.write("[1] %s\n" % sentence)