#!/usr/bin/env python3
# pylint: disable=C0103
"""This module does nothing ...."""
import math
from document.utils import printProgressBar
from nltk.collocations import BigramCollocationFinder
from nltk.metrics import BigramAssocMeasures
from mlcore import TextService
import progressbar
import pyorient

orientdb = pyorient.OrientDB("localhost", 2424)
orientdb.connect("root", "root")
orientdb.db_open('datascience', "root", "root")

text_service = TextService()


def bag_of_bigrams_words(words, n=100, score_fn=BigramAssocMeasures.chi_sq):
    bigram_finder = BigramCollocationFinder.from_words(words)
    bigrams = bigram_finder.nbest(score_fn, n)
    return dict([(bigram, True) for bigram in bigrams])


def save_bigrams_as_many(bigrams):
    default_cluster = (orientdb.command("SELECT FROM ( SELECT expand( classes ) FROM metadata:schema ) WHERE name = 'Bigrams'")[0]).oRecordData['defaultClusterId']
    #orientdb.command("delete from Bigrams")
    for key in bigrams.keys():
        w = dict(id=key, qtd=bigrams.get(key))
        # orientdb.record_create(default_cluster, w)
        print (w)

if __name__ == "__main__":
    print("bigramming..")
    result = orientdb.query("select * from Documento LIMIT 100")
    current = 0
    total = len(result)
    bar = progressbar.ProgressBar(max_value=total)
    all_texts = ""
    for each in result:
        current += 1
        bar.update(current)
        all_texts = all_texts + each.texto_arquivo
    bar.finish()
    # save_bigrams_as_many(bigra
    bigrams = bag_of_bigrams_words(text_service.tokenize(all_texts))
    save_bigrams_as_many(bigrams)
