from flask import Flask
from flask import session
from flask import Response
from flask import request
from flask import redirect, url_for
from flask import render_template

from api.core import app, db
from api.models.user import User
from api.models.page import Page
from gensim import corpora, models, similarities

import urllib
import json
import datetime


@app.route('/nlp/sentiment')
def nlp_sentiment():
    params = {
        'apikey':     '2ccd6f653c1e4253b6ac5ee0dadb284bde58331e',
        'text':       'I hate this tv show. It became really booring.',
        'outputMode': 'json'
    }
    params = urllib.urlencode(params)

    sentiment = json.loads(
        urllib.urlopen('http://access.alchemyapi.com/calls/text/TextGetTextSentiment?%s' % params).read()
    )

    return Response(json.dumps({
        'accounts': sentiment
    }), mimetype='application/json')


@app.route('/nlp/rank_keywords')
def npl_rank_keywords():
    params = {
        'apikey':     '2ccd6f653c1e4253b6ac5ee0dadb284bde58331e',
        'text':       'I hate this tv show. It became really booring.',
        'outputMode': 'json',
        'sentiment':  1
    }
    params = urllib.urlencode(params)

    ranked = json.loads(
        urllib.urlopen('http://access.alchemyapi.com/calls/text/TextGetRankedKeywords?%s' % params).read()
    )

    return Response(json.dumps({
        'accounts': ranked
    }), mimetype='application/json')

def match_similar(inputs, questions):

    # remove common words and tokenize
    stoplist = set('for of the and to in by from on with as'.split())
    texts = [[word for word in question.lower().split() if word not in stoplist] for question in questions]

    print texts

    # Create dictionary
    dictionary = corpora.Dictionary(texts)

    print dictionary

    # Define corpus
    corpus = [dictionary.doc2bow(text) for text in texts]

    print corpus

    # Define LSI space
    lsi = models.LsiModel(corpus, id2word=dictionary, num_topics=2)

    # Get similarity of teh doc vs documents
    vector = dictionary.doc2bow(inputs.lower().split())
    vector_lsi = lsi[vector]

    index = similarities.MatrixSimilarity(lsi[corpus])
    sims = index[vector_lsi]
    print sims
    return sims

def match_group(inputs, groups, min_threshold):

    group_questions = []
    for key, group in groups.items():
        group_questions.append(group['content'])
    
    group_similarity = sorted(enumerate(match_similar(inputs, group_questions)), key=lambda item: -item[1])

    print group_similarity

    for group_id, similarity in group_similarity:
        if similarity > min_threshold:
            return groups[str(group_id)]

    # Create new group
    print('New Group created');
    

@app.route('/nlp/similar')
def nlp_similar():
    min_threshold = 0.4;

    groups = {
        '1': {
            'id': 1,
            'content': 'Cars are awesome television',
        },
        '2': {
            'id': 2,
            'content': 'Will the show continue',
        }
    }

    questions = { 
        '1': {
            'id': 1,
            'question': "Human machine interface for lab abc computer applications",
            'similarity': 0.9,
            'group_id': 1
        },
        '2': {
            'id': 2,
            'question': "A survey of user opinion of computer system response time",
            'similarity': 9.1,
            'group_id': 1
        },
        '3': {
            'id': 3,
            'question': "The EPS user interface management system",
            'similarity': 0.6,
            'group_id': 2
        },
        '4': {
            'id': 4,
            'question': "System and human system engineering testing of EPS",
            'similarity': 7.7,
            'group_id': 2
        },
        '5': {
            'id': 5,
            'question': "Relation of user perceived response time to error measurement",
            'similarity': 8.1,
            'group_id': 0
        },
        '6': {
            'id': 6,
            'question': "The generation of random binary unordered trees",
            'similarity': 7.2,
            'group_id': 1
        },
        '7': {
            'id': 7,
            'question': "The intersection graph of paths in trees",
            'similarity': 1.6,
            'group_id': 2
        },
        '8': {
            'id': 8,
            'question': "Graph minors IV Widths of trees and well quasi ordering",
            'similarity': 2.3,
            'group_id': 0
        },
        '9': {
            'id': 6,
            'question': "Graph minors A survey",
            'similarity': 3.1,
            'group_id': 1
        }
    }

    inputs = "I like watching television"
    sims = match_group(inputs, groups, min_threshold)

    return Response(json.dumps(sims), mimetype='application/json')

@@ -2869,35 +2869,48 @@
 ': '
-Cars are awesome television
+The EPS user interface management system
 ',%0A 
@@ -2983,30 +2983,65 @@
 ': '
-Will the show continue
+Human machine interface for lab abc computer applications
 ',%0A 
@@ -4715,31 +4715,31 @@
  = %22
-I like watching televis
+Human computer interact
 ion%22

Update NLP thingie