Esempio n. 1
0
def sentimental():
    # creating object of TwitterClient Class
    hashtag = request.query['hashtag']
    api = TwitterClient()
    # calling function to get tweets

    people = {}

    tweets = api.get_tweets(query='#' + hashtag, count=10)

    # picking positive tweets from tweets
    positive_tweets = [
        tweet for tweet in tweets if tweet['sentiment'] == 'positive'
    ]
    # percentage of positive tweets
    positive = 100 * len(positive_tweets) / len(tweets)
    # picking negative tweets from tweets
    negative_tweets = [
        tweet for tweet in tweets if tweet['sentiment'] == 'negative'
    ]
    # percentage of negative tweets
    negative = 100 * len(negative_tweets) / len(tweets)
    # percentage of neutral tweets
    neutral = 100 * (len(tweets) - len(negative_tweets) -
                     len(positive_tweets)) / len(tweets)
    data = {
        "parsed_tweets": len(tweets),
        "positive": positive,
        "negative": negative,
        "neutral": neutral,
        "tweets": tweets,
    }
    response.content_type = 'application/json'
    return json.dumps(data)
def search():
    api = TwitterClient()
    q = request.args.get('q')

    # calling function to get tweets
    tweets = api.get_tweets(query=q, count=200)

    # picking positive tweets from tweets
    ptweets = [tweet for tweet in tweets if tweet['sentiment'] == 'positive']

    positive = round(100 * len(ptweets) / len(tweets), 4)

    # picking negative tweets from tweets
    ntweets = [tweet for tweet in tweets if tweet['sentiment'] == 'negative']

    negative = round(100 * len(ntweets) / len(tweets), 4)

    # percentage of neutral tweets

    neutral = round(
        100 * (len(tweets) - len(ntweets) - len(ptweets)) / len(tweets), 4)

    return render_template('index.html',
                           has_results=True,
                           positive=positive,
                           negative=negative,
                           neutral=neutral,
                           q=q,
                           ptweets=random.sample(ptweets, 3),
                           ntweets=random.sample(ntweets, 3))