import nltk
import json

from merriam_webster import APP_KEY, BASE_URL
from requests import get
from flask_restful import Resource
from flask import request,jsonify
from nltk.tokenize import word_tokenize
from sacremoses import MosesDetokenizer
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer

class Essayist(Resource):

    def post(self):
        '''
        Entry point for the API request to retrieve the analysis for some text and
        the desired sentiment of the text .
        '''

        analysis = self.get_essay_analysis(request.form['essay_text'], request.form['desired_sentiment'])

        return analysis

    def get_essay_analysis(self, essay_text, desired_sentiment):
        '''
        Get the essay analysis using the VADER Sentiment Analysis package and Merriam-Webster Thesaurus API. 
        VADER is a pre-trained machine learning model the derives the sentiment (positive , neutral negative) 
        from text. Sentiment is scored on a scale from -100 to 100 where -100 is extremely negative, 0 is neutral, 
        and 100 is exremetly positive.
        '''

        # Set a reference to the analyzer
        analyzer = SentimentIntensityAnalyzer()

        # Set a reference to the detokenizer
        detokenizer = MosesDetokenizer(lang='en')

        # Declare analysis dictionary
        analysis = {
            "overall_score": None,
            "text": None,
            "tagged_words": {},
        }

        # Declare the total compound score
        total_compound_score = 0;

        # Split essay text into array of sentences
        sentences = nltk.tokenize.sent_tokenize(essay_text)

        for sentence_idx, sentence in enumerate(sentences):

            # Tokenize the sentence
            tokenized_sentence = word_tokenize(sentence)

            for word_idx, word in enumerate(tokenized_sentence):
                compound_score = analyzer.polarity_scores(word)['compound']

                # Find words that do not match desired sentiment. e.g.
                # Desired sentiment = 'positive', tag words like 'terrible' or 'awful'
                # Desired sentiment = 'negative', tag words like 'great', 'wonderful'
                if (desired_sentiment == 'positive' and compound_score <= -0.1) or (desired_sentiment == 'negative' and compound_score >= 0.1):
                    antonymns = self.get_antonyms(word)

                    analysis['tagged_words'][word] = antonymns

                    tokenized_sentence[word_idx] = f'<span class="opposite-tone-word">{word}</span>'

                # Find words that do match desired sentiment. e.g.
                # Desired sentiment = 'positive', tag words like 'happy' or 'great'
                # Desired sentiment = 'negative', tag words like 'sad', 'awful'
                if (desired_sentiment == 'positive' and compound_score >= 0.1) or (desired_sentiment == 'negative' and compound_score <= -0.1):
                    synonyms = self.get_synonyms(word)

                    analysis['tagged_words'][word] = synonyms

                    tokenized_sentence[word_idx] = f'<span class="match-tone-word">{word}</span>'

            # Detokentize the sentence and replace it since it now contains tagged words
            sentences[sentence_idx] = detokenizer.detokenize(tokenized_sentence)

            # Add the sentence compund sentiment score the total score
            total_compound_score += analyzer.polarity_scores(sentence)['compound']

        # Calculate the overall compound score
        analysis['overall_score'] = round(( total_compound_score / len(sentences) ) * 100)

        # Detokenize the text
        analysis['text'] = detokenizer.detokenize(sentences)

        return analysis
        

    def get_antonyms(self, word):
        '''
        Get the antonyms for a given word from the Merriam-Webster Thesaurus API
        and add them to the analysis
        '''
        url  = f'{merriam_webster.BASE_URL}/{word}?key={merriam_webster.APP_KEY}'
        json = get(url).json()

        # Get the antonyms from the returned json
        antonymns = json[0]['meta']['ants'] if len(json) > 0 else []

        flattened_antonyms = [item for sublist in antonymns for item in sublist];

        return flattened_antonyms

    def get_synonyms(self, word):
        '''
        Get the antonyms for a given word from the Merriam-Webster Thesaurus API
        and add them to the analysis
        '''
        url  = f'{merriam_webster.BASE_URL}/{word}?key={merriam_webster.APP_KEY}'
        json = get(url).json()

        # Get the antonyms from the returned json
        synonyms = json[0]['meta']['syns'] if len(json) > 0 else []

        flattened_synonyms = [item for sublist in synonyms for item in sublist];

        return flattened_synonyms




a0ee523aa84e663b972f0759ac4e1e44632a31d4
<mask token>


class Essayist(Resource):
    <mask token>
    <mask token>
    <mask token>

    def get_synonyms(self, word):
        """
        Get the antonyms for a given word from the Merriam-Webster Thesaurus API
        and add them to the analysis
        """
        url = (
            f'{merriam_webster.BASE_URL}/{word}?key={merriam_webster.APP_KEY}')
        json = get(url).json()
        synonyms = json[0]['meta']['syns'] if len(json) > 0 else []
        flattened_synonyms = [item for sublist in synonyms for item in sublist]
        return flattened_synonyms

<mask token>


class Essayist(Resource):
    <mask token>
    <mask token>

    def get_antonyms(self, word):
        """
        Get the antonyms for a given word from the Merriam-Webster Thesaurus API
        and add them to the analysis
        """
        url = (
            f'{merriam_webster.BASE_URL}/{word}?key={merriam_webster.APP_KEY}')
        json = get(url).json()
        antonymns = json[0]['meta']['ants'] if len(json) > 0 else []
        flattened_antonyms = [item for sublist in antonymns for item in sublist
            ]
        return flattened_antonyms

    def get_synonyms(self, word):
        """
        Get the antonyms for a given word from the Merriam-Webster Thesaurus API
        and add them to the analysis
        """
        url = (
            f'{merriam_webster.BASE_URL}/{word}?key={merriam_webster.APP_KEY}')
        json = get(url).json()
        synonyms = json[0]['meta']['syns'] if len(json) > 0 else []
        flattened_synonyms = [item for sublist in synonyms for item in sublist]
        return flattened_synonyms

<mask token>


class Essayist(Resource):

    def post(self):
        """
        Entry point for the API request to retrieve the analysis for some text and
        the desired sentiment of the text .
        """
        analysis = self.get_essay_analysis(request.form['essay_text'],
            request.form['desired_sentiment'])
        return analysis

    def get_essay_analysis(self, essay_text, desired_sentiment):
        """
        Get the essay analysis using the VADER Sentiment Analysis package and Merriam-Webster Thesaurus API. 
        VADER is a pre-trained machine learning model the derives the sentiment (positive , neutral negative) 
        from text. Sentiment is scored on a scale from -100 to 100 where -100 is extremely negative, 0 is neutral, 
        and 100 is exremetly positive.
        """
        analyzer = SentimentIntensityAnalyzer()
        detokenizer = MosesDetokenizer(lang='en')
        analysis = {'overall_score': None, 'text': None, 'tagged_words': {}}
        total_compound_score = 0
        sentences = nltk.tokenize.sent_tokenize(essay_text)
        for sentence_idx, sentence in enumerate(sentences):
            tokenized_sentence = word_tokenize(sentence)
            for word_idx, word in enumerate(tokenized_sentence):
                compound_score = analyzer.polarity_scores(word)['compound']
                if (desired_sentiment == 'positive' and compound_score <= -
                    0.1 or desired_sentiment == 'negative' and 
                    compound_score >= 0.1):
                    antonymns = self.get_antonyms(word)
                    analysis['tagged_words'][word] = antonymns
                    tokenized_sentence[word_idx
                        ] = f'<span class="opposite-tone-word">{word}</span>'
                if (desired_sentiment == 'positive' and compound_score >= 
                    0.1 or desired_sentiment == 'negative' and 
                    compound_score <= -0.1):
                    synonyms = self.get_synonyms(word)
                    analysis['tagged_words'][word] = synonyms
                    tokenized_sentence[word_idx
                        ] = f'<span class="match-tone-word">{word}</span>'
            sentences[sentence_idx] = detokenizer.detokenize(tokenized_sentence
                )
            total_compound_score += analyzer.polarity_scores(sentence)[
                'compound']
        analysis['overall_score'] = round(total_compound_score / len(
            sentences) * 100)
        analysis['text'] = detokenizer.detokenize(sentences)
        return analysis

    def get_antonyms(self, word):
        """
        Get the antonyms for a given word from the Merriam-Webster Thesaurus API
        and add them to the analysis
        """
        url = (
            f'{merriam_webster.BASE_URL}/{word}?key={merriam_webster.APP_KEY}')
        json = get(url).json()
        antonymns = json[0]['meta']['ants'] if len(json) > 0 else []
        flattened_antonyms = [item for sublist in antonymns for item in sublist
            ]
        return flattened_antonyms

    def get_synonyms(self, word):
        """
        Get the antonyms for a given word from the Merriam-Webster Thesaurus API
        and add them to the analysis
        """
        url = (
            f'{merriam_webster.BASE_URL}/{word}?key={merriam_webster.APP_KEY}')
        json = get(url).json()
        synonyms = json[0]['meta']['syns'] if len(json) > 0 else []
        flattened_synonyms = [item for sublist in synonyms for item in sublist]
        return flattened_synonyms

import nltk
import json
from merriam_webster import APP_KEY, BASE_URL
from requests import get
from flask_restful import Resource
from flask import request, jsonify
from nltk.tokenize import word_tokenize
from sacremoses import MosesDetokenizer
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer


class Essayist(Resource):

    def post(self):
        """
        Entry point for the API request to retrieve the analysis for some text and
        the desired sentiment of the text .
        """
        analysis = self.get_essay_analysis(request.form['essay_text'],
            request.form['desired_sentiment'])
        return analysis

    def get_essay_analysis(self, essay_text, desired_sentiment):
        """
        Get the essay analysis using the VADER Sentiment Analysis package and Merriam-Webster Thesaurus API. 
        VADER is a pre-trained machine learning model the derives the sentiment (positive , neutral negative) 
        from text. Sentiment is scored on a scale from -100 to 100 where -100 is extremely negative, 0 is neutral, 
        and 100 is exremetly positive.
        """
        analyzer = SentimentIntensityAnalyzer()
        detokenizer = MosesDetokenizer(lang='en')
        analysis = {'overall_score': None, 'text': None, 'tagged_words': {}}
        total_compound_score = 0
        sentences = nltk.tokenize.sent_tokenize(essay_text)
        for sentence_idx, sentence in enumerate(sentences):
            tokenized_sentence = word_tokenize(sentence)
            for word_idx, word in enumerate(tokenized_sentence):
                compound_score = analyzer.polarity_scores(word)['compound']
                if (desired_sentiment == 'positive' and compound_score <= -
                    0.1 or desired_sentiment == 'negative' and 
                    compound_score >= 0.1):
                    antonymns = self.get_antonyms(word)
                    analysis['tagged_words'][word] = antonymns
                    tokenized_sentence[word_idx
                        ] = f'<span class="opposite-tone-word">{word}</span>'
                if (desired_sentiment == 'positive' and compound_score >= 
                    0.1 or desired_sentiment == 'negative' and 
                    compound_score <= -0.1):
                    synonyms = self.get_synonyms(word)
                    analysis['tagged_words'][word] = synonyms
                    tokenized_sentence[word_idx
                        ] = f'<span class="match-tone-word">{word}</span>'
            sentences[sentence_idx] = detokenizer.detokenize(tokenized_sentence
                )
            total_compound_score += analyzer.polarity_scores(sentence)[
                'compound']
        analysis['overall_score'] = round(total_compound_score / len(
            sentences) * 100)
        analysis['text'] = detokenizer.detokenize(sentences)
        return analysis

    def get_antonyms(self, word):
        """
        Get the antonyms for a given word from the Merriam-Webster Thesaurus API
        and add them to the analysis
        """
        url = (
            f'{merriam_webster.BASE_URL}/{word}?key={merriam_webster.APP_KEY}')
        json = get(url).json()
        antonymns = json[0]['meta']['ants'] if len(json) > 0 else []
        flattened_antonyms = [item for sublist in antonymns for item in sublist
            ]
        return flattened_antonyms

    def get_synonyms(self, word):
        """
        Get the antonyms for a given word from the Merriam-Webster Thesaurus API
        and add them to the analysis
        """
        url = (
            f'{merriam_webster.BASE_URL}/{word}?key={merriam_webster.APP_KEY}')
        json = get(url).json()
        synonyms = json[0]['meta']['syns'] if len(json) > 0 else []
        flattened_synonyms = [item for sublist in synonyms for item in sublist]
        return flattened_synonyms

import nltk
import json

from merriam_webster import APP_KEY, BASE_URL
from requests import get
from flask_restful import Resource
from flask import request,jsonify
from nltk.tokenize import word_tokenize
from sacremoses import MosesDetokenizer
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer

class Essayist(Resource):

    def post(self):
        '''
        Entry point for the API request to retrieve the analysis for some text and
        the desired sentiment of the text .
        '''

        analysis = self.get_essay_analysis(request.form['essay_text'], request.form['desired_sentiment'])

        return analysis

    def get_essay_analysis(self, essay_text, desired_sentiment):
        '''
        Get the essay analysis using the VADER Sentiment Analysis package and Merriam-Webster Thesaurus API. 
        VADER is a pre-trained machine learning model the derives the sentiment (positive , neutral negative) 
        from text. Sentiment is scored on a scale from -100 to 100 where -100 is extremely negative, 0 is neutral, 
        and 100 is exremetly positive.
        '''

        # Set a reference to the analyzer
        analyzer = SentimentIntensityAnalyzer()

        # Set a reference to the detokenizer
        detokenizer = MosesDetokenizer(lang='en')

        # Declare analysis dictionary
        analysis = {
            "overall_score": None,
            "text": None,
            "tagged_words": {},
        }

        # Declare the total compound score
        total_compound_score = 0;

        # Split essay text into array of sentences
        sentences = nltk.tokenize.sent_tokenize(essay_text)

        for sentence_idx, sentence in enumerate(sentences):

            # Tokenize the sentence
            tokenized_sentence = word_tokenize(sentence)

            for word_idx, word in enumerate(tokenized_sentence):
                compound_score = analyzer.polarity_scores(word)['compound']

                # Find words that do not match desired sentiment. e.g.
                # Desired sentiment = 'positive', tag words like 'terrible' or 'awful'
                # Desired sentiment = 'negative', tag words like 'great', 'wonderful'
                if (desired_sentiment == 'positive' and compound_score <= -0.1) or (desired_sentiment == 'negative' and compound_score >= 0.1):
                    antonymns = self.get_antonyms(word)

                    analysis['tagged_words'][word] = antonymns

                    tokenized_sentence[word_idx] = f'<span class="opposite-tone-word">{word}</span>'

                # Find words that do match desired sentiment. e.g.
                # Desired sentiment = 'positive', tag words like 'happy' or 'great'
                # Desired sentiment = 'negative', tag words like 'sad', 'awful'
                if (desired_sentiment == 'positive' and compound_score >= 0.1) or (desired_sentiment == 'negative' and compound_score <= -0.1):
                    synonyms = self.get_synonyms(word)

                    analysis['tagged_words'][word] = synonyms

                    tokenized_sentence[word_idx] = f'<span class="match-tone-word">{word}</span>'

            # Detokentize the sentence and replace it since it now contains tagged words
            sentences[sentence_idx] = detokenizer.detokenize(tokenized_sentence)

            # Add the sentence compund sentiment score the total score
            total_compound_score += analyzer.polarity_scores(sentence)['compound']

        # Calculate the overall compound score
        analysis['overall_score'] = round(( total_compound_score / len(sentences) ) * 100)

        # Detokenize the text
        analysis['text'] = detokenizer.detokenize(sentences)

        return analysis
        

    def get_antonyms(self, word):
        '''
        Get the antonyms for a given word from the Merriam-Webster Thesaurus API
        and add them to the analysis
        '''
        url  = f'{merriam_webster.BASE_URL}/{word}?key={merriam_webster.APP_KEY}'
        json = get(url).json()

        # Get the antonyms from the returned json
        antonymns = json[0]['meta']['ants'] if len(json) > 0 else []

        flattened_antonyms = [item for sublist in antonymns for item in sublist];

        return flattened_antonyms

    def get_synonyms(self, word):
        '''
        Get the antonyms for a given word from the Merriam-Webster Thesaurus API
        and add them to the analysis
        '''
        url  = f'{merriam_webster.BASE_URL}/{word}?key={merriam_webster.APP_KEY}'
        json = get(url).json()

        # Get the antonyms from the returned json
        synonyms = json[0]['meta']['syns'] if len(json) > 0 else []

        flattened_synonyms = [item for sublist in synonyms for item in sublist];

        return flattened_synonyms



