Пример #1
0
def api():
    # Check for request type, content type
    if request.method == 'GET':
        return make_response({'error': 'Method Not Allowed'}, 405)
    if request.content_type != 'application/json':
        return make_response({'error': 'Unsupported Media Type'}, 415)

    else:
        # Check for request body in correct format
        try:
            txtInput = request.json['text']
        except:
            return make_response({'error': 'Bad Request'}, 400)

        # Create API response
        scores = SentimentIntensityAnalyzer().polarity_scores(txtInput)
        sentiment = getSentiment(scores.get('compound'))
        if sentiment == 0:
            sentiment = 'ERROR'
        wordCount = getWordCount(txtInput)
        mostFrequent = getMostFrequent(txtInput, 5)
        return jsonify(sentiment=sentiment,
                        compound=scores.get('compound'),
                        pos=scores.get('pos'),
                        neu=scores.get('neu'),
                        neg=scores.get('neg'),
                        wordCount=wordCount,
                        mostFrequent=mostFrequent,
                        )
Пример #2
0
    def on_data(
            self,
            data):  # pulling the text out of the JSON file each tweet comes in
        json_load = json.loads(data)
        try:
            texts = json_load['text']
            coded = texts.encode('utf-8')
            s = str(coded)
            ss = SentimentIntensityAnalyzer().polarity_scores(s)
            datetime = json.loads(data)[
                'created_at']  # print the datetime of the tweet
            text = s[1:]  # print the text of the tweet
            compound = ss.get('compound', '0')
            negative = ss.get('neg', '0')
            positive = ss.get('pos', '0')
            neutral = ss.get(
                'neu', '0')  # print the VADER sentiment scores for the tweet
            user_id = json.loads(data)['user'].get('id', 'NA')
            user_name = json.loads(data)['user'].get('name', 'NA')
            user_screen_name = json.loads(data)['user'].get(
                'screen_name', 'NA')
            location = json.loads(data)['user'].get('location', 'NA')
            num_of_followers = json.loads(data)['user'].get(
                'followers_count', 'NA')
            time_zone = json.loads(data)['user'].get('time_zone', 'NA')
            cur.execute('insert into twitter_stream_netflix (datetime_created, user_id, user_name, user_screen_name,\
             location, num_of_followers, time_zone, tweet_text, vader_compound, vader_negative,\
            vader_positive, vader_neutral) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'                                                                                       , datetime, user_id, user_name,\
                        user_screen_name, location, num_of_followers, time_zone, text, compound, negative, positive, neutral)
            conn.commit()  # insert values into SQL database
        except:
            cur.execute('insert into twitter_stream_netflix (datetime_created, user_id, user_name, user_screen_name,\
                 location, num_of_followers, time_zone, tweet_text, vader_compound, vader_negative,\
                vader_positive, vader_neutral) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'                                                                                           , 'NA', 'NA', 'NA', \
                        'NA', 'NA', 'NA', 'NA', 'NA', 0, 0, 0, 0)
            conn.commit()  # accounting for blank values
            err = sys.exc_info()[2]
            print(err)
            print('YEEEEEEEEEEEEH BWOI SORTED MATE')

            def on_error(self, status):
                print(status)

            def on_error(self, status_code):
                if status_code == 420:
                    # returning False in on_data disconnects the stream
                    return False
Пример #3
0
def service():
    form = serviceForm()

    descriptions = {
        'neg': 'Proportion of the text that has negative sentiment',
        'neu': 'Proportion of the text that is neutral',
        'pos': 'Proportion of the text that has positive sentiment',
        'compound': 'A normalized, weighted composite score for the text overall. Ranges \
                    between -1 (most negative) to 1 (most positive)',
        'sentiment': 'Overall sentiment, based on compound score. Below -0.05 is negative', 
        'word_count': 'Word Count', 
        'most_frequent': 'Most frequently used words and number of appearances in the text'
    }
    displayResults = False
    txtInput = ''
    scores = {
        'neg': '-',
        'neu': '-',
        'pos': '-',
        'compound': '-' 
    }
    sentiment = '-'
    wordCount = '-'
    mostFrequent = '-'

    if request.method == 'POST':
        displayResults = True
        txtInput = form.inputText.data
        scores = SentimentIntensityAnalyzer().polarity_scores(txtInput)
        sentiment = getSentiment(scores.get('compound'))
        if sentiment == 0:
            sentiment = 'ERROR'
        wordCount = getWordCount(txtInput)
        mostFrequent = getMostFrequent(txtInput, 5)

    return render_template(
        'service.html',
        nav=nav,
        serviceUrl=serviceUrl,
        form=form,
        displayResults=displayResults,
        txtInput=txtInput,
        scores=scores,
        sentiment=sentiment,
        wordCount=wordCount,
        mostFrequent=mostFrequent,
        descriptions=descriptions
    )
Пример #4
0
def print_sample_sentiments(samples):
    """*Processes sentiment analysis for a list of samples and prints the results*
    
    samples (list): A list of strings to process
    """

    sample_sentiments = []
    counter = 0

    for sample in samples:
        score = SentimentIntensityAnalyzer().polarity_scores(sample)
        counter += 1
        sample_sentiments.append([
            counter,
            score.get('neg'),
            score.get('neu'),
            score.get('pos'),
            score.get('compound'), sample
        ])
        print(counter, score.get('neg'), score.get('neu'), score.get('pos'),
              score.get('compound'), sample)
Пример #5
0
def analyze():
    """
    This function will analyze the top 5 best reviews for sentiment. The imdb library already lists the reviews according to their filter::"Best". So we do not have to write a function for that. Also, instead of treating these as individual strings, we combine them into one large string. This allows for us to use a single natural language processing to determine the overall sentiment of the reviews.
    """
    search()
    value = 0
    analyizetext = ""
    for element in range(0, 5):
        analyizetext = analyizetext + imdb._get_reviews_data(
            identi)[value]['text']
        value += 1
    score = SentimentIntensityAnalyzer().polarity_scores(analyizetext)
    if score.get("pos") > score.get("neg"):
        print("The top 5 reviews for the movie:", title,
              ". Has been generally positive.")
    elif score.get("pos") < score.get("neg"):
        print("The top 5 reviews for the movie:", title,
              ". Has been generally negative.")
    elif score.get("pos") == score.get("neg"):
        print("The top 5 reviews for the movie:", title,
              ". Has been basically neutral.")
    againfun()
Пример #6
0
# Top 10 Most Frequent Words in the Wiki Page
print(
    f'Top 10 Most Frequent Words and its Frequency in Wiki page \'{page_1.title}\' are:'
)
word_freq.top_10_most_freq(word_freq.clean(page_1.content))

# Naturalization
import nltk
from nltk.sentiment.vader import SentimentIntensityAnalyzer

score_1 = SentimentIntensityAnalyzer().polarity_scores(
    page_1.content)  # AI Emotions
print(score_1)

neu_1 = score_1.get('neu')


def is_neutral(neu):
    """
    Identify if the inputed neutrality measurement is greater or equal to .80
    """
    if neu >= 0.80:
        return True
    else:
        return False


if is_neutral(neu_1) is True:
    print(f'The {page_1.title} Wikipedia Page is Neutral')
else: