#!/usr/bin/env python
# coding: utf-8

# In[ ]:


import os
import time
import re
import itertools
from collections import Counter
from nltk.stem import WordNetLemmatizer
from nltk.corpus import wordnet
from nltk.corpus.reader.verbnet import VerbnetCorpusReader

lemmatizer = WordNetLemmatizer()

prep = ['off', 'on', 'to', 'with', 'from', 'for', 'up', 'in', 'back', 'through', 'down', 'into', 'out']

def common_elements(list1, list2):
    return [token for token in list1 if token in list2]

#def remove_prep(a_list):
#    for token in a_list:
#        if token in prep:
#            try:
#                a_list.remove(token)
#            except ValueError:
#                pass
#    return a_list


def add_underscore(a_list):
    #a_list = remove_prep(a_list)
    if len(a_list) == 1:
        token = lemmatizer.lemmatize(a_list[0], wordnet.VERB)
        return token
    else:
        new_list = [lemmatizer.lemmatize(item, wordnet.VERB) for item in a_list]
        token = '_'.join(new_list)
        if token.endswith('_'):
            token = token[:-1]
        return token


def add_gap(a_list):
    #a_list = remove_prep(a_list)
    if len(a_list) == 1:
        return a_list[0]
    else:
        word = ' '.join(a_list)
        if word.endswith(' '):
            word = word[:-1]
        return word


def return_keywords(premise, hypothesis):
    """return key_verbs and verbs
    key_verbs : verbs in form for verbnet and wordnet
    verbs : verbs in form same as in premise and hypothesis
    """
    key_verbs, verbs = [], []
    p_split = (re.sub(r'[,.!?]','', premise)).split(' ')
    h_split = (re.sub(r'[,.!?]','', hypothesis)).split(' ')
    count = Counter(p_split+h_split)
    keywords = [item[0] for item in count.items() if item[1] == 1]

    key_verbs.append(add_underscore(common_elements(keywords, p_split)))
    key_verbs.append(add_underscore(common_elements(keywords, h_split)))

    verbs.append(add_gap(common_elements(keywords, p_split)))
    verbs.append(add_gap(common_elements(keywords, h_split)))

    return key_verbs, verbs



def return_verbnet():
    verbnet = {}
    for subdir, dirs, files in os.walk('verbnet3.3/'):
        for file in files:
            filename = file
            verbname = filename.split('-')[0]
            synonyms = VerbnetCorpusReader('verbnet3.3/', filename).lemmas()
            verbnet[verbname] = synonyms
    return verbnet


def return_synonyms(verb):
    """return a list of synonyms of the verb in wordnet"""
    a_list, synonyms = [], []
    for syn in wordnet.synsets(verb, pos=wordnet.VERB):
        a_list += syn.lemma_names()
    return list(set(a_list))


def return_hypernyms(verb):
    """return a list of hypernyms of the verb in wordnet"""
    a_list = []
    for syn in wordnet.synsets(verb, pos=wordnet.VERB):
        for hyp in syn.hypernyms():
            a_list += hyp.lemma_names()
    return list(set(a_list))


def create_kbsent(verbnet, key_verbs, verbs):
    key1 = [key for key, value in verbnet.items() if key_verbs[0] in value]
    key2 = [key for key, value in verbnet.items() if key_verbs[1] in value]
    verbs1 = list(set(itertools.chain.from_iterable(map(lambda x: verbnet[x], key1))))
    verbs2 = list(set(itertools.chain.from_iterable(map(lambda x: verbnet[x], key2))))

    if len(key1)==0 or len(key2)==0:#if verb is not found in verbnet
        return ''
    else:
        if len(common_elements(verbs1, verbs2)) >= 1:
            return key_verbs[0] + ' and ' + key_verbs[1] + ' are synonyms'
        else:
            return key_verbs[0] + ' and ' + key_verbs[1] + ' are not synonyms'



def create_kbsent_mix(verbnet, key_verbs, verbs):
    key1 = [key for key, value in verbnet.items() if key_verbs[0] in value]
    key2 = [key for key, value in verbnet.items() if key_verbs[1] in value]
    verbs1 = list(set(itertools.chain.from_iterable(map(lambda x: verbnet[x], key1))))
    verbs2 = list(set(itertools.chain.from_iterable(map(lambda x: verbnet[x], key2))))
    
    syn1 = return_synonyms(key_verbs[0])
    syn2 = return_synonyms(key_verbs[1])

    hyp1 = return_hypernyms(key_verbs[0])
    hyp2 = return_hypernyms(key_verbs[1])

    combined1 = verbs1 + syn1
    combined2 = verbs2 + syn2

    if len(common_elements(combined1, combined2)) >= 1 or len(common_elements(hyp1, hyp2)) >= 1:
        return re.sub('_',' ',key_verbs[0]) + ' and ' + re.sub('_',' ',key_verbs[1])  + ' are synonymous'
    else:
        return re.sub('_',' ',key_verbs[0]) + ' and ' + re.sub('_',' ',key_verbs[1]) + ' are not synonymous'




