Example #1
0
    def on_data(self, data):
	global i
	global big_d
	if (i > 1000):
		exit()
        f = open('file.txt', 'a+')
        j = json.loads(data)
        s = j['text'].encode('utf-8')
        f.write(s)
        print('\n' + s)
        f.truncate()
	simple = {}
        j['score'], j['magnitude'] = sentiment_analysis.analyze('file.txt')
	simple['score']=j['score']  
	simple['timestamp_ms']=j['timestamp_ms']
	f = open('file.txt', 'r+')
	tweet_id = 'tweet' + str(i+1)
	big_d[tweet_id] = simple
	os.remove('result.json')
	result = open('result.json', 'a+')
	json.dump(big_d, result)
	i += 1
        f.close()
	#os.system("gsutil cp result.json gs://tweet_bucket/")
        return True
Example #2
0
def get_tweets(query, start_date, end_date, get_sentiment=True):
    tweet_criteria = got.manager.TweetCriteria().setQuerySearch(
        query).setSince(start_date).setUntil(end_date).setTopTweets(
            True).setMaxTweets(10)

    overall_sentiment_score = 0.0

    for tweet in got.manager.TweetManager.getTweets(tweet_criteria):
        try:
            sentiment = sentiment_analysis.analyze(tweet.text)[0]
            category = content_classification.classify(tweet.text)

            if category == "News/Politics" or category == "/Finance/Investing":
                sentiment.score *= 1.5

            overall_sentiment_score += sentiment.score
            print(f"{tweet.text}: {category} {sentiment.score}\n")
        except Exception as e:
            print(f"Exception: {e}")
            pass

    if get_sentiment:
        return overall_sentiment_score
    else:
        return got.manager.TweetManager.getTweets(tweet_criteria)
Example #3
0
def analyze_url(url):
    article = Article(url)
    article.download()
    article.parse()

    result = sentiment_analysis.analyze(article.text)

    return result[0].score
Example #4
0
def index():
    choice = request.form['option']
    # First Long-term Analysis
    twitter_sentiment_analysis.get_tweets(choice, 50)
    result1 = sentiment_analysis.analyze("tweet.text")

    # Second Short-term analysis
    twitter_sentiment_analysis.get_tweets(choice, 10)
    result2 = sentiment_analysis.analyze("tweet.text")

    # Overall Score
    overall_reliability = overall_scorer.isReliable(result1, result2)
    overall_score = overall_scorer.scorer(result1, result2)
    return render_template('result.html',
                           choice=choice,
                           overall_reliability=overall_reliability,
                           overall_score=overall_score)
Example #5
0
def index():
  if request.method == "POST":
    req = request.form
    text = req.get('text')

    if text:
      analysis = analyze(text)
      return render_template('index.html', analysis=analysis, text=text)
      
    else:
      feedback = "Text to Analyze cannot be empty. Please fill it out."
      return render_template('index.html', feedback=feedback)

  return render_template('index.html')
Example #6
0
def get_tweets(screen_name):
    # Twitter only allows access to a users most recent 3240 tweets with this method

    # authorize twitter, initialize tweepy
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_secret)
    api = tweepy.API(auth)

    # initialize a list to hold all the tweepy Tweets
    alltweets = []

    # make initial request for most recent tweets (200 is the maximum allowed count)
    new_tweets = api.user_timeline(screen_name=screen_name, count=20)

    # save most recent tweets
    alltweets.extend(new_tweets)

    # save the id of the oldest tweet less one
    oldest = alltweets[-1].id - 1

    # keep grabbing tweets until there are no tweets left to grab
    while len(new_tweets) > 0:


        # all subsiquent requests use the max_id param to prevent duplicates
        new_tweets = api.user_timeline(screen_name=screen_name, count=200, max_id=oldest)

        # save most recent tweets
        alltweets.extend(new_tweets)

        # update the id of the oldest tweet less one
        oldest = alltweets[-1].id - 1


    # transform the tweepy tweets into a 2D array that will populate the csv
    outtweets = [tweet.text.encode("utf-8") for tweet in alltweets]

    # write the csv
    print(outtweets)
    sad_score_list = analyze(outtweets)
    return sad_score_list

    pass
def test_neg(resource, capsys):
    analyze(resource('neg.txt'))
    out, err = capsys.readouterr()
    score = float(re.search('score of (.+?) with', out).group(1))
    magnitude = float(re.search('magnitude of (.+?)', out).group(1))
    assert score * magnitude < 0
def test_neutral(capsys):
    analyze(os.path.join(RESOURCES, "neutral.txt"))
    out, err = capsys.readouterr()
    magnitude = float(re.search("magnitude of (.+?)", out).group(1))
    assert magnitude <= 2.0
def test_mixed(capsys):
    analyze(os.path.join(RESOURCES, "mixed.txt"))
    out, err = capsys.readouterr()
    score = float(re.search("score of (.+?) with", out).group(1))
    assert score <= 0.3
    assert score >= -0.3
def test_neg(capsys):
    analyze(os.path.join(RESOURCES, "neg.txt"))
    out, err = capsys.readouterr()
    score = float(re.search("score of (.+?) with", out).group(1))
    magnitude = float(re.search("magnitude of (.+?)", out).group(1))
    assert score * magnitude < 0
def test_mixed(capsys):
    analyze(os.path.join(RESOURCES, 'mixed.txt'))
    out, err = capsys.readouterr()
    score = float(re.search('score of (.+?) with', out).group(1))
    assert score <= 0.3
    assert score >= -0.3
def test_neg(capsys):
    analyze(os.path.join(RESOURCES, 'neg.txt'))
    out, err = capsys.readouterr()
    score = float(re.search('score of (.+?) with', out).group(1))
    magnitude = float(re.search('magnitude of (.+?)', out).group(1))
    assert score * magnitude < 0
def test_pos(capsys):
    analyze(os.path.join(RESOURCES, 'pos.txt'))
    out, err = capsys.readouterr()
    score = float(re.search('score of (.+?) with', out).group(1))
    magnitude = float(re.search('magnitude of (.+?)', out).group(1))
    assert score * magnitude > 0
def test_mixed(resource, capsys):
    analyze(resource('mixed.txt'))
    out, err = capsys.readouterr()
    score = float(re.search('score of (.+?) with', out).group(1))
    assert score <= 0.3
    assert score >= -0.3
def test_neutral(capsys):
    analyze(os.path.join(RESOURCES, 'neutral.txt'))
    out, err = capsys.readouterr()
    magnitude = float(re.search('magnitude of (.+?)', out).group(1))
    assert magnitude <= 2.0
def test_neutral(resource, capsys):
    analyze(resource('neutral.txt'))
    out, err = capsys.readouterr()
    magnitude = float(re.search('magnitude of (.+?)', out).group(1))
    assert magnitude <= 2.0
Example #17
0
def test_analyze():
    assert not sa.analyze(filename)