#!/usr/bin/env python

"""
A simple Python wrapper for the stanford IE binary that makes it easier to use it
on UNIX/Windows systems.
Note: The script does some minimal sanity checking of the input, but don't
    expect it to cover all cases. After all, it is a just a wrapper.
Example:
    > echo "Barack Obama was born in Hawaii." > text.txt
    > python main.py -f text.txt
    > python main.py -f text.txt,text2.txt (for batch mode).
    Should display
    1.000: (Barack Obama; was; born)
    1.000: (Barack Obama; was born in; Hawaii)
Authors:    Philippe Remy       <github: philipperemy>
Version:    2016-07-08
"""

# Copyright (c) 2016, Philippe Remy <github: philipperemy>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

from __future__ import print_function

import os
from argparse import ArgumentParser
from subprocess import Popen
from sys import argv
from sys import stderr

#biblioteca usada para encontrar o value maximo dentro das keys de um dicionario
import operator
import main
import subprocess
#abrir arquivo de texto e dar um split passando quebra de linha (\n) como parâmetro,
#resultando em uma lista já dividida

JAVA_BIN_PATH = 'java'
DOT_BIN_PATH = 'dot'
STANFORD_IE_FOLDER = 'stanford-openie'

tmp_folder = '/tmp/openie/'
if not os.path.exists(tmp_folder):
    os.makedirs(tmp_folder)


def arg_parse():
    arg_p = ArgumentParser('Stanford IE Python Wrapper')
    arg_p.add_argument('-f', '--filename', type=str, default=None)
    arg_p.add_argument('-v', '--verbose', action='store_true')
    arg_p.add_argument('-g', '--generate_graph', action='store_true')
    return arg_p


def debug_print(log, verbose):
    if verbose:
        print(log)


def process_entity_relations(entity_relations_str, verbose=True):
    # format is ollie.
    entity_relations = list()
    for s in entity_relations_str:
        entity_relations.append(s[s.find("(") + 1:s.find(")")].split(';'))
    return entity_relations


def generate_graphviz_graph(entity_relations, verbose=True):
    """digraph G {
    # a -> b [ label="a to b" ];
    # b -> c [ label="another label"];
    }"""
    graph = list()
    graph.append('digraph {')
    for er in entity_relations:
        graph.append('"{}" -> "{}" [ label="{}" ];'.format(er[0], er[2], er[1]))
    graph.append('}')

    out_dot = tmp_folder + 'out.dot'
    with open(out_dot, 'w') as output_file:
        output_file.writelines(graph)

    out_png = tmp_folder + 'out.png'
    command = '{} -Tpng {} -o {}'.format(DOT_BIN_PATH, out_dot, out_png)
    debug_print('Executing command = {}'.format(command), verbose)
    dot_process = Popen(command, stdout=stderr, shell=True)
    dot_process.wait()
    assert not dot_process.returncode, 'ERROR: Call to dot exited with a non-zero code status.'
    print('Wrote graph to {} and {}'.format(out_dot, out_png))


def stanford_ie(input_filename, verbose=True, generate_graphviz=False):
    out = tmp_folder + 'out.txt'
    input_filename = input_filename.replace(',', ' ')

    new_filename = ''
    for filename in input_filename.split():
        if filename.startswith('/'):  # absolute path.
            new_filename += '{} '.format(filename)
        else:
            new_filename += '../{} '.format(filename)

    absolute_path_to_script = os.path.dirname(os.path.realpath(__file__)) + '/'
    command = 'cd {};'.format(absolute_path_to_script)
    command += 'cd {}; {} -mx4g -cp "stanford-openie.jar:stanford-openie-models.jar:lib/*" ' \
               'edu.stanford.nlp.naturalli.OpenIE {} -format ollie > {}'. \
        format(STANFORD_IE_FOLDER, JAVA_BIN_PATH, new_filename, out)

    if verbose:
        debug_print('Executing command = {}'.format(command), verbose)
        java_process = Popen(command, stdout=stderr, shell=True)
    else:
        java_process = Popen(command, stdout=stderr, stderr=open(os.devnull, 'w'), shell=True)
    java_process.wait()
    assert not java_process.returncode, 'ERROR: Call to stanford_ie exited with a non-zero code status.'

    with open(out, 'r') as output_file:
        results_str = output_file.readlines()
    os.remove(out)

    results = process_entity_relations(results_str, verbose)
    if generate_graphviz:
        generate_graphviz_graph(results, verbose)

    return results



f = open('artigos.txt','r')
arquivo = f.read()
arquivoSemiPronto = arquivo.split('\n')
#filtrar a lista, removendo todos os itens nulos ('')
arquivoLista = list(filter(None, arquivoSemiPronto))
#loop pra iterar sobre os artigos e tirar a primeira linha com informação desnecessária (se a primeira linha possuir '[', é retirada)
counter=0
for x in arquivoLista:
    if '[' in x:
        arquivoLista.remove(x)
        continue
    else:
        continue

#loop onde se o indice 2 dos 5 primeiros itens da lista atual for "Comments", é printado com comments e retirado esses mesmo 5 primeiros da lista para printar
#o próximo doc. caso não seja "Comments", o doc é printado sem comments e apenas os 4 primeiros são retirados, até a lista esvaziar e quebrar o laço.
fileCounter = 0
while len(arquivoLista) != 0:
    fileCounter = fileCounter + 1
    #filename = 'samples.txt'
    filename = str('data/' + str(fileCounter) + '.txt')
    txt = open(filename,'w+')
    if 'Comments' in (arquivoLista[2]):
        if fileCounter > 4 and fileCounter < 8:
            print('Title: ' + arquivoLista[0] + '\n' + 'Autores: ' + arquivoLista[1] + '\n' + arquivoLista[2] + '\n' + arquivoLista[3] + '\n' + 'Abstract: ' + arquivoLista[4] )
            txt.write(arquivoLista[4])
            txt.flush()
            os.fsync(txt)
            entities_relations = stanford_ie(filename, False, False)
            for (i, relation) in enumerate(entities_relations):
                print('Relation' + str(i) + ': ' + str(relation))
        arquivoLista.pop(0)
        arquivoLista.pop(0)
        arquivoLista.pop(0)
        arquivoLista.pop(0)
        arquivoLista.pop(0)
        continue
    if 'Comments' not in (arquivoLista[2]):
        if fileCounter > 4 and fileCounter < 8:
            print('Title: ' + arquivoLista[0] + '\n' + 'Autores: ' + arquivoLista[1] + '\n' + arquivoLista[2] + '\n' + 'Abstract: ' + arquivoLista[3])
            txt.write(arquivoLista[3])
            txt.flush()
            os.fsync(txt)
            entities_relations = stanford_ie(filename, False, False)
            for (i, relation) in enumerate(entities_relations):
                print('Relation' + str(i) + ': ' + str(relation))
        arquivoLista.pop(0)
        arquivoLista.pop(0)
        arquivoLista.pop(0)
        arquivoLista.pop(0)
        continue
