Exemple #1
0
    def on_data(self, data):
        try:
            all_data = json.loads(data)
            all_tweets = all_data["text"]

            all_tweets = preprocess(all_tweets)

            punctuation = list(string.punctuation)

            stop_words = set(stopwords.words('english') + punctuation + ['RT'])

            tweet = [
                tweets for tweets in all_tweets if tweets not in stop_words
            ]

            tweet = ' '.join(str(e) for e in tweet)

            sentiment_value, confidence = s.sentiment(tweet)

            print(tweet, sentiment_value, confidence)

            if confidence * 100 >= 80:
                output = open("twitter-out.txt", "a")
                output.write(tweet)
                output.write("\t")
                output.write(sentiment_value)
                output.write('\n')
                output.close()

            return True
        except BaseException as e:
            print("Error on_data: %s" % str(e))
            return True
        return False
Exemple #2
0
def submit():
    text = request.form['text']
    query = text.lower()
    tweets = g.user.twitter_request(
        'https://api.twitter.com/1.1/search/tweets.json?q={}'.format(query))
    tweet_texts = [{
        'tweet': tweet['text'],
        'label': 'neutral'
    } for tweet in tweets['statuses']]

    for tweet in tweet_texts:
        tweet['tweet'] = '"""' + tweet[
            'tweet'] + '"""'  # this is needed to handle multi-line tweets
        tweet['label'] = sentiment(tweet['tweet'])

    return render_template('search.html', content=tweet_texts)