示例#1
0
def search():

    # validate screen_name
    screen_name = request.args.get("screen_name", "")
    if not screen_name:
        return redirect(url_for("index"))
    
    # get screen_name's tweets
    tweets = helpers.get_user_timeline(screen_name, 100)

    # Check if tweets array contains None
    if tweets is None:
        sys.exit("Error: No tweets was returned!")
    
    # Absolute paths to lists 
    positives = os.path.join(sys.path[0], "positive-words.txt")
    negatives = os.path.join(sys.path[0], "negative-words.txt")
    
    # Initialize an Analyze object
    analyzer = Analyzer(positives, negatives)
    
    # Initialize sentiment analysis counts for chart values
    positive, negative, neutral = 0.0, 0.0, 0.0
    
    # Iterate through tweets 
    for tweet in tweets:
        
        # Return score analysis for tweet
        score = analyzer.analyze(tweet)
        
        # Increment respective sentiment analysis counts
        if score > 0.0:
            positive += 1
        elif score < 0.0:
            negative += 1
        else:
            neutral += 1
        
    # Set sentiment analysis counts to percentages
    num_tweets = positive + negative + neutral

    positive = positive / num_tweets
    negative = negative / num_tweets
    neutral = neutral / num_tweets

    # generate chart
    chart = helpers.chart(positive, negative, neutral)

    # render results
    return render_template("search.html", chart=chart, screen_name=screen_name)
示例#2
0
def search():

    # validate screen_name
    screen_name = request.args.get("screen_name", "")
    if not screen_name:
        return redirect(url_for("index"))

    # get screen_name's tweets
    tweets = helpers.get_user_timeline(screen_name)

    # TODO
    positive, negative, neutral = 0.0, 0.0, 100.0

    # generate chart
    chart = helpers.chart(positive, negative, neutral)

    # render results
    return render_template("search.html", chart=chart, screen_name=screen_name)
示例#3
0
def search():

    # validate screen_name
    screen_name = request.args.get("screen_name", "")
    if not screen_name:
        return redirect(url_for("index"))

    # get screen_name's tweets
    tweets = helpers.get_user_timeline(screen_name, 50)

    # TODO
    positive, negative, neutral = 0.0, 0.0, 0.0

    # absolute paths to lists
    positives = os.path.join(sys.path[0], "positive-words.txt")
    negatives = os.path.join(sys.path[0], "negative-words.txt")

    # instantiate analyzer
    analyzer = Analyzer(positives, negatives)
    final_Score = 0
    # analyze word
    for text in tweets:
        score = analyzer.analyze(text)
        if score > 0.0:
            final_Score += 1
            positive += 1
        elif score < 0.0:
            final_Score -= 1
            negative += 1
        else:
            neutral += 1
    print(final_Score)

    file = open("tweet.csv", "a", newline='')
    writer = csv.writer(file)
    writer.writerow((request.args.get("screen_name", ""), final_Score,
                     positive, negative, neutral, tweets))
    file.close()

    # generate chart
    chart = helpers.chart(positive, negative, neutral)

    # render results
    return render_template("search.html", chart=chart, screen_name=screen_name)
示例#4
0
def search():

    # validate screen_name
    screen_name = request.args.get("screen_name", "")
    if not screen_name:
        return redirect(url_for("index"))

    # get screen_name's tweets
    tweets = helpers.get_user_timeline(screen_name)

    # TODO
    
    # absolute paths to lists
    positives = os.path.join(sys.path[0], "positive-words.txt")
    negatives = os.path.join(sys.path[0], "negative-words.txt")

    # instantiate analyzer
    analyzer = Analyzer(positives, negatives)
    
    positive, negative, neutral = 0.0, 0.0, 0.0
    
    for tweet in tweets:
        score = analyzer.analyze(tweet)
        if score > 0.0:
            positive += 1
        elif score < 0.0:
            negative += 1
        else:
            neutral += 1
    
    sentiments = positive + negative + neutral
    
    # Percentage 
    positive = (positive /  sentiments) * 100
    negative = (negative /  sentiments) * 100
    neutral = (neutral /  sentiments) * 100 
    
    #print("Positive: " + str(positive) + " Negative: " + str(negative) + " Neutral: " + str(neutral))

    # generate chart
    chart = helpers.chart(positive, negative, neutral)

    # render results
    return render_template("search.html", chart=chart, screen_name=screen_name)
示例#5
0
def search():

    # validate screen_name
    screen_name = request.args.get("screen_name", "").lstrip("@")
    if not screen_name:
        return redirect(url_for("index"))

    # get screen_name's tweets
    tweets = helpers.get_user_timeline(screen_name)
    if not screen_name:
        return redirect(url_for("index"))
    if not tweets:
        return redirect(url_for("index"))
    else:
        positives = os.path.join(sys.path[0], "positive-words.txt")
        negatives = os.path.join(sys.path[0], "negative-words.txt")
        
        #queries Twitter’s API for a user’s most recent 100 tweets
        from helpers import get_user_timeline 

        tweetlist = helpers.get_user_timeline(screen_name,100)
        #analyzes the sentiment of each of those tweets
        # instantiate analyzer
      
        positive = 0
        negative = 0
        neutral = 0
        
        tw_analyzer = Analyzer(positives, negatives)
        # analyze word
        for tweet in tweetlist:
            score =  tw_analyzer.analyze(tweet)
            if score > 0.0:
                positive = positive +1
            elif score < 0.0:
                 negative =  negative +1
            else:
                neutral = neutral +1
    
        # generate chart
        chart = helpers.chart(positive, negative, neutral)
    
        # render results
        return render_template("search.html", chart=chart, screen_name=screen_name)
def search():

    # validate screen_name
    screen_name = request.args.get("screen_name", "")
    if not screen_name:
        return redirect(url_for("index"))

    # get screen_name's tweets
    tweets = helpers.get_user_timeline(screen_name, count=100)
    if not tweets:
        return redirect(url_for("index"))

    # TODO
    analyzer = Analyzer('positive-words.txt', 'negative-words.txt')

    # analyze word
    positive = 0
    negative = 0
    neutral = 0
    total = 0

    for tweet in tweets:

        score = analyzer.analyze(tweet)
        if score > 0.0:
            positive += 1
            total += 1
        elif score < 0.0:
            negative += 1
            total += 1
        else:
            neutral += 1
            total += 1
    positive = positive / total
    negative = negative / total
    neutral = neutral / total
    print(positive, 'pos', negative, 'neg', neutral, 'neut')

    # generate chart
    chart = helpers.chart(positive, negative, neutral)

    # render results
    return render_template("search.html", chart=chart, screen_name=screen_name)
示例#7
0
def search():

    # validate screen_name
    screen_name = request.args.get("screen_name", "")
    if not screen_name:
        return redirect(url_for("index"))

    # absolute paths to lists
    positives = os.path.join(sys.path[0], "positive-words.txt")
    negatives = os.path.join(sys.path[0], "negative-words.txt")

    # instantiate analyzer
    analyzer = Analyzer(positives, negatives)

    name = screen_name.strip('@')

    # get screen_name's tweets
    tweets = helpers.get_user_timeline(screen_name, 100)
    if tweets == None:
        return redirect(url_for("index"))

    # declare count variables
    count, positive_count, negative_count, neutral_count = 0, 0, 0, 0

    # get single tweets and count them
    for tweet in tweets:
        count += 1
        score = analyzer.analyze(tweet)
        if score > 0.0:
            positive_count += 1
        elif score < 0.0:
            negative_count += 1
        else:
            neutral_count += 1

    # get 100 percent
    positive, negative, neutral = positive_count / count * 100, negative_count / count * 100, neutral_count / count * 100

    # generate chart
    chart = helpers.chart(positive, negative, neutral)

    # render results
    return render_template("search.html", chart=chart, screen_name=name)
示例#8
0
def search():

    # validate screen_name
    screen_name = request.args.get("screen_name", "").lstrip("@")
    if not screen_name:
        return redirect(url_for("index"))

    # get screen_name's tweets
    tweets = helpers.get_user_timeline(screen_name)

    # TODO
    if tweets == None:
        return redirect(url_for("index"))
    
    tknzr = TweetTokenizer()
    
    # absolute paths to lists
    positives = os.path.join(sys.path[0], "positive-words.txt")
    negatives = os.path.join(sys.path[0], "negative-words.txt")
    # instantiate analyzer
    analyzer = Analyzer(positives, negatives)
    
    positive, negative, neutral = 0.0, 0.0, 0.0
    
    for tweet in tweets:
        print(tknzr.tokenize(tweet))
        # analyze tweet
        score = analyzer.analyze(tweet)
        if score > 0.0:
            # positive = (positive / len(tweets)) * 100
            positive = positive + 1
            print("positive: {}".format(positive))
        elif score < 0.0:
            negative = negative + 1
            print("negative: {}".format(negative))
        else:
            neutral = neutral + 1
            
    # generate chart
    chart = helpers.chart(positive, negative, neutral)

    # render results
    return render_template("search.html", chart=chart, screen_name=screen_name)
示例#9
0
def search():

    # validate screen_name
    screen_name = request.args.get("screen_name", "").lstrip("@")
    if not screen_name:
        return redirect(url_for("index"))

    # get screen_name's tweets
    tweets = helpers.get_user_timeline(screen_name, count=N_TWEETS)

    # TODO
    # color print
    positive, negative, neutral = get_scores(tweets)

    # generate chart
    chart = helpers.chart(positive, negative, neutral)

    # render results
    return render_template("search.html", chart=chart, screen_name=screen_name)
示例#10
0
def search():

    # validate screen_name
    screen_name = request.args.get("screen_name", "").lstrip("@")
    if not screen_name:
        return redirect(url_for("index"))

    # get screen_name's tweets
    tweets = helpers.get_user_timeline(screen_name, count=100)

    Analyzer(positives="positive-words.txt", negatives="negative-words.txt")

    positive = 0
    negative = 0
    neutral = 0

    for tweet in tweets:
        words = tweet.lower().split(" ")
        score = 0
        for word in words:
            if word.lower() in Analyzer.positive_words:
                score += 1
            elif word.lower() in Analyzer.negative_words:
                score -= 1
            else:
                score = score
                if score > 0.0:
                    positive += 1
                elif score < 0.0:
                    negative += 1
                else:
                    neutral += 1

    total = (positive + negative + neutral)
    posititve = (positive / total) * 100
    negative = (negative / total) * 100
    neutral = (neutral / total) * 100

    # generate chart
    chart = helpers.chart(positive, negative, neutral)

    # render results
    return render_template("search.html", chart=chart, screen_name=screen_name)
示例#11
0
def search():
    # validate screen_name
    screen_name = request.args.get("screen_name", "")
    if not screen_name:
        return redirect(url_for("index"))

    number_of_tweets = 100

    # get screen_name's tweets
    tweets = helpers.get_user_timeline(screen_name, number_of_tweets)

    # get stats from tweets
    positive, negative, neutral = helpers.get_tweets_stats(tweets)

    # generate chart
    chart = helpers.chart(positive, negative, neutral)

    # render results
    return render_template("search.html", chart=chart, screen_name=screen_name)
示例#12
0
def search():

    # validate screen_name
    screen_name = request.args.get("screen_name", "").lstrip("@")
    if not screen_name:
        return redirect(url_for("index"))

    positives = os.path.join(sys.path[0], "positive-words.txt")
    negatives = os.path.join(sys.path[0], "negative-words.txt")

    positive, negative, neutral = 0.0, 0.0, 100.0

    analyzer = Analyzer(positives, negatives)
    # get screen_name's tweets
    tweets = helpers.get_user_timeline(screen_name)
    #if tweets != None:
    for i in tweets:
        score = analyzer.analyze(i)
        if score > 0.0:
            positive += 1.0
            neutral - 1.0
        elif score < 0.0:
            negative += 1.0
            neutral - 1.0
    #else:
    #   return redirect(url_for("index")

    #positive, negative, neutral = 0.0, 0.0, 100.0

    #if score > 0.0:
    #   positive += 1.0
    #  neutral - 1.0
    #elif score < 0.0:
    #   negative -= 1.0
    #  neutral - 1.0
    print(positive)

    # generate chart
    chart = helpers.chart(positive, negative, neutral)

    # render results
    return render_template("search.html", chart=chart, screen_name=screen_name)
def search():

    #A LOT OF CODE DUPLICATION!
    # absolute paths to positive and negative lists.
    positives = os.path.join(sys.path[0], "positive-words.txt")
    negatives = os.path.join(sys.path[0], "negative-words.txt")

    # instantiate analyzer
    analyzer = Analyzer(positives, negatives)

    # validate screen_name
    screen_name = request.args.get("screen_name", "").lstrip("@")
    if not screen_name:
        return redirect(url_for("index"))

    # get screen_name's tweets
    tweets = helpers.get_user_timeline(screen_name, count=99)
    if tweets is None:
        return redirect(url_for("index"))

    positive = 0
    negative = 0
    neutral = 0
    for tweet in tweets:
        score = analyzer.analyze(tweet)
        totalScore = score
        if score > 0:
            positive += 1
        elif score < 0:
            negative += 1
        else:
            neutral += 1
        totalScore += 1

    positive, negative, neutral = int((positive / totalScore) * 100), int(
        (negative / totalScore) * 100), int((neutral / totalScore) * 100)

    # generate chart
    chart = helpers.chart(positive, negative, neutral)

    # render results
    return render_template("search.html", chart=chart, screen_name=screen_name)
示例#14
0
def search():

    # validate screen_name
    screen_name = request.args.get("screen_name", "")
    if not screen_name:
        return redirect(url_for("index"))

    # get screen_name's tweets
    tweets = helpers.get_user_timeline(screen_name)

    # TODO
    if tweets == None:
        print("We couldn't get the tweets from " + screen_name)
        sys.exit()

    # absolute paths to lists
    positives = os.path.join(sys.path[0], "positive-words.txt")
    negatives = os.path.join(sys.path[0], "negative-words.txt")

    # instantiate analyzer
    analyzer = Analyzer(positives, negatives)

    positive, negative, neutral = 0.0, 0.0, 0.0
    score = 0
    for tweet in tweets:
        # analyze word
        score = analyzer.analyze(tweet)

        if score > 0.0:
            positive = positive + 1
        elif score < 0.0:
            negative = negative + 1
        else:
            neutral = neutral + 1
    total = positive + negative + neutral
    positive, negative, neutral = positive / total, negative / total, neutral / total

    # generate chart
    chart = helpers.chart(positive, negative, neutral)

    # render results
    return render_template("search.html", chart=chart, screen_name=screen_name)
示例#15
0
def search():

    # validate screen_name
    screen_name = request.args.get("screen_name", "")
    if not screen_name:
        return redirect(url_for("index"))

    # get screen_name's tweets
    tweets = helpers.get_user_timeline(screen_name, 100)
    if tweets is None:
        return redirect(url_for("index"))

     # absolute paths to lists
    positives = os.path.join(sys.path[0], "positive-words.txt")
    negatives = os.path.join(sys.path[0], "negative-words.txt")
    
    # instantiate analyzer
    analyzer = Analyzer(positives, negatives)
    
    sump = 0
    sumn = 0
    sumg = 0
    for tweet in tweets:
        # analyze word
        score = analyzer.analyze(tweet)
        if score > 0.0:
            sump +=1
        elif score < 0.0:
            sumn += 1
        else:
            sumg += 1
    sumt = sump + sumn + sumg
    rp = (sump/sumt)*100
    rn = (sumn/sumt)*100
    rg = (sumg/sumt)*100
    positive, negative, neutral = rp, rn, rg

    # generate chart
    chart = helpers.chart(positive, negative, neutral)

    # render results
    return render_template("search.html", chart=chart, screen_name=screen_name)
示例#16
0
def search():

    # validate screen_name
    screen_name = request.args.get("screen_name", "").lstrip("@")
    if not screen_name:
        return redirect(url_for("index"))

    # get screen_name's tweets
    tweets = helpers.get_user_timeline(screen_name)

    # init analyzer
    positives = os.path.join(sys.path[0], "positive-words.txt")
    negatives = os.path.join(sys.path[0], "negative-words.txt")

    # instantiate analyzer
    analyzer = Analyzer(positives, negatives)

    tokenize = TweetTokenizer().tokenize

    positive, negative, neutral = 0.0, 0.0, 0.0

    user_timeline = get_user_timeline(screen_name, 100)

    if user_timeline is None:
        exit("Could not find user, please try a different @ name!")

    for tweet in user_timeline:
        score = 0
        for word in tokenize(tweet):
            score += analyzer.analyze(word)
        if score > 0:
            positive += 1
        elif score < 0:
            negative += 1
        else:
            neutral += 1

    # generate chart
    chart = helpers.chart(positive, negative, neutral)

    # render results
    return render_template("search.html", chart=chart, screen_name=screen_name)
示例#17
0
def search():

    # validate screen_name
    screen_name = request.args.get("screen_name", "")
    if not screen_name:
        return redirect(url_for("index"))

    # get screen_name's tweets
    tweets = helpers.get_user_timeline(screen_name)
    if tweets == None:
        return redirect(url_for("index"))

    # absolute paths to lists
    positives = os.path.join(sys.path[0], "positive-words.txt")
    negatives = os.path.join(sys.path[0], "negative-words.txt")

    # instantiate analyzer
    analyzer = Analyzer(positives, negatives)

    # analyze tweets
    positive = 0
    negative = 0
    neutral = 0
    score = 0
    for tweet in tweets:

        score = analyzer.analyze(tweet)

        if score > 0.0:
            positive += 1
        elif score < 0.0:
            negative += 1
        else:
            neutral += 1

        score = 0

    # generate chart
    chart = helpers.chart(positive, negative, neutral)

    # render results
    return render_template("search.html", chart=chart, screen_name=screen_name)
示例#18
0
def search():

    # validate screen_name
    screen_name = request.args.get("screen_name", "").lstrip("@")
    if not screen_name:
        return redirect(url_for("index"))

    # get screen_name's tweets
    userTweets = helpers.get_user_timeline(screen_name)
    if userTweets == None:
        return redirect(url_for("index"))

    positive, negative, neutral = 0.0, 0.0, 0.0

    # absolute paths to lists
    positives = os.path.join(sys.path[0], "positive-words.txt")
    negatives = os.path.join(sys.path[0], "negative-words.txt")

    # instantiate analyzer
    analyzer = Analyzer(positives, negatives)
    # iterate through the first 50 of the user's tweets

    # determine how many tweets the user has
    numTweets = len(userTweets)
    if numTweets > 100:
        numTweets = 100

    for i in range(numTweets):
        # analyze tweet
        score = analyzer.analyze(userTweets[i])
        if score > 0.0:
            positive = positive + 1.0
        elif score < 0.0:
            negative = negative + 1.0
        else:
            neutral = neutral + 1.0

    # generate chart
    chart = helpers.chart(positive, negative, neutral)

    # render results
    return render_template("search.html", chart=chart, screen_name=screen_name)
示例#19
0
def search():

    # validate screen_name
    screen_name = request.args.get("screen_name", "")
    if not screen_name:
        return redirect(url_for("index"))

    # get screen_name's tweets
    tweets = helpers.get_user_timeline(screen_name, 100)

    # TODO

    # absolute paths to lists
    positives = os.path.join(sys.path[0], "positive-words.txt")
    negatives = os.path.join(sys.path[0], "negative-words.txt")

    # instantiate analyzer
    analyzer = Analyzer(positives, negatives)

    # return to index if screen_name doesn't exist
    if tweets == None:
        return redirect(url_for("index"))

    # create positive, negative and neutral counter
    # these variables are for chart plotting
    positive, negative, neutral = 0, 0, 0

    # analyze each tweet
    for tweet in tweets:
        score = analyzer.analyze(tweet)
        if score > 0.0:
            positive += 1
        elif score < 0.0:
            negative += 1
        else:
            neutral += 1

    # generate chart
    chart = helpers.chart(positive, negative, neutral)

    # render results
    return render_template("search.html", chart=chart, screen_name=screen_name)
示例#20
0
def search():

    # validate screen_name
    screen_name = request.args.get("screen_name", "").lstrip("@")
    if not screen_name:
        return redirect(url_for("index"))

    # get screen_name's tweets
    tweets = helpers.get_user_timeline(screen_name, 100)

    #if 100 tweets dont exist
    if tweets == None:
        tweets = helpers.get_user_timeline(screen_name, 100)

    if tweets == None:
        return redirect(url_for("index"))

    # set all counts to 0.0
    positive, negative, neutral = 0.0, 0.0, 0.0

    #list paths
    negatives = os.path.join(sys.path[0], "negative-words.txt")
    positives = os.path.join(sys.path[0], "positive-words.txt")

    #analyzer constructor
    analyzer = Analyzer(positives, negatives)

    #iterate over tweets classifying them
    for tweet in tweets:
        score = analyzer.analyze(tweet)
        if score > 0.0:
            positive = positive + 1
        elif score < 0.0:
            negative = negative + 1
        else:
            neutral = neutral + 1

    # generate chart
    chart = helpers.chart(positive, negative, neutral)

    # render results
    return render_template("search.html", chart=chart, screen_name=screen_name)
示例#21
0
def search():

    # validate screen_name
    screen_name = request.args.get("screen_name", "")
    if not screen_name:
        return redirect(url_for("index"))

    # get screen_name's tweets
    tweets = helpers.get_user_timeline(screen_name, 100)

    # handle get_user_timeline errors
    if tweets == None:
        return redirect(url_for("index"))

    # absolute paths to lists
    positives = os.path.join(sys.path[0], "positive-words.txt")
    negatives = os.path.join(sys.path[0], "negative-words.txt")

    # instantiate analyzer
    analyzer = Analyzer(positives, negatives)

    # counts for sentiment categories
    pos_count, neg_count, neut_count = 0, 0, 0

    # score and assign sentiment category to each tweet
    for tweet in tweets:
        score = analyzer.analyze(tweet)
        if score > 0.0:
            pos_count += 1
        elif score < 0.0:
            neg_count += 1
        else:
            neut_count += 1

    whole = pos_count + neg_count + neut_count
    positive, negative, neutral = (pos_count / whole), (neg_count / whole), (neut_count / whole)

    # generate chart
    chart = helpers.chart(positive, negative, neutral)

    # render results
    return render_template("search.html", chart=chart, screen_name=screen_name)
示例#22
0
def search():

    # validate screen_name
    screen_name = request.args.get("screen_name", "")
    screen_name = screen_name.strip('@')
    if not screen_name:
        return redirect(url_for("index"))

    # get screen_name's tweets
    tweets = helpers.get_user_timeline(screen_name)
        
    # absolute path to list
    positive = os.path.join(sys.path[0], "positive-word.txt")
    negative = os.path.join(sys.path[0], "negative-word.txt")
    
    # instantiate analyzer
    analyzer = Analyzer(positive, negative)
    
    pos, neg, neutr = 0.0, 0.0, 100.0
    
    if tweets==None:
        pos = 1
        neg = 1
        neutr = 1
    else:        
        # calculations
        for tweet in tweets:
            score = analyzer.analyze(tweet)
            
            if score>0.0:
                pos = pos + 1
            elif score<0.0:
                neg = neg + 1
            else:
                neutr = neutr + 1
            
    # generate chart
    chart = helpers.chart(pos, neg, neutr)

    # render results
    return render_template("search.html", chart=chart, screen_name=screen_name)
def search():

    # validate screen_name
    screen_name = request.args.get("screen_name", "").lstrip("@")
    if not screen_name:
        return redirect(url_for("index"))

    positives = os.path.join(sys.path[0], "positive-words.txt")
    negatives = os.path.join(sys.path[0], "negative-words.txt")

    analyzer = Analyzer(positives, negatives)

    # get screen_name's tweets
    tweets = helpers.get_user_timeline(screen_name, 100)

    if not tweets:
        return redirect(url_for("index"))

    positive = 0
    negative = 0
    neutral = 0

    for tweet in tweets:
        score = analyzer.analyze(tweet)

        if score > 0.0:
            positive += 1
        elif score < 0.0:
            negative += 1
        else:
            neutral += 1
        score = 0

    # # TODO
    # positive, negative, neutral = 0.0, 0.0, 100.0

    # generate chart
    chart = helpers.chart(positive, negative, neutral)

    # render results
    return render_template("search.html", chart=chart, screen_name=screen_name)
示例#24
0
def search():

    # validate screen_name
    screen_name = request.args.get("screen_name", "")
    if not screen_name:
        return redirect(url_for("index"))

    # get screen_name's tweets
    tweets = helpers.get_user_timeline(screen_name, 200)

    # absolute paths to lists
    positives = "static/SentiWS_v18c_Positive.txt"
    negatives = "static/SentiWS_v18c_Negative.txt"
    poENG = "static/positive-words.txt"
    neENG = "static/negative-words.txt"

    # instantiate analyzer
    analyzer = Analyzer(positives, negatives, poENG, neENG)

    # variables for counting
    positive = 0
    negative = 0

    # analyzing words and adding int to matching variable
    for tweet in tweets:

        tweet['score'] = analyzer.analyze(tweet['tweet'])

        if tweet['score'] > 0.0:
            positive += tweet['score']
        elif tweet['score'] < 0.0:
            negative -= tweet['score']

    # generate chart
    chart = helpers.chart(positive, negative)

    # render results
    return render_template("search.html",
                           chart=chart,
                           screen_name=screen_name,
                           tweets=tweets)
示例#25
0
def search():

    # validate screen_name
    screen_name = request.args.get("screen_name", "")
    if not screen_name:
        return redirect(url_for("index"))

    # queries for user’s most recent 100 tweets
    tweets = helpers.get_user_timeline(screen_name, 100)

    # redirect to index if no tweets are found
    if tweets == None:
        return redirect(url_for("index"))

    # absolute paths to lists
    positives = os.path.join(sys.path[0], "positive-words.txt")
    negatives = os.path.join(sys.path[0], "negative-words.txt")

    # initialize analyzer
    analyzer = Analyzer(positives, negatives)

    # keep track of positive, negative and neutral tweets
    positive = 0
    negative = 0
    neutral = 0

    for tweet in tweets:
        score = analyzer.analyze(tweet)

        if score > 0.0:
            positive += 1
        elif score < 0.0:
            negative += 1
        else:
            neutral += 1

    # generate chart
    chart = helpers.chart(positive, negative, neutral)

    # render results
    return render_template("search.html", chart=chart, screen_name=screen_name)
示例#26
0
def search():
    """Searches screen name and returns chart."""

    # validate screen_name
    screen_name = request.args.get("screen_name", "").strip('@')
    if not screen_name:
        return redirect(url_for("index"))

    # get screen_name's tweets
    tweets = helpers.get_user_timeline(screen_name)
    if tweets == None:
        return redirect(url_for("index"))

    # initialize scores (%) for chart
    positive, negative, neutral = 0.0, 0.0, 100.0

    # absolute paths to lists
    positives = os.path.join(sys.path[0], "positive-words.txt")
    negatives = os.path.join(sys.path[0], "negative-words.txt")

    # instantiate analyzer
    analyzer = Analyzer(positives, negatives)

    # get 100 tweets from user
    tweets = helpers.get_user_timeline(screen_name, 100)

    # calculate total score for chart
    for tweet in tweets:
        score = analyzer.analyze(tweet)
        if score > 0.0:
            positive += 1
            neutral -= 1
        elif score < 0.0:
            negative += 1
            neutral -= 1

    # generate chart
    chart = helpers.chart(positive, negative, neutral)

    # render results
    return render_template("search.html", chart=chart, screen_name=screen_name)
示例#27
0
def search():

    # validate screen_name
    screen_name = request.args.get("screen_name", "")
    if not screen_name:
        return redirect(url_for("index"))

    # path to positive and negative word dictionary
    positives = os.path.join(sys.path[0], "positive-words.txt")
    negatives = os.path.join(sys.path[0], "negative-words.txt")

    # get screen_name's tweets
    tweets = helpers.get_user_timeline(screen_name)
    if tweets == None:
        return render_template("index.html")

    #initialize analyzer
    analyzer = Analyzer(positives, negatives)

    cpositive, cnegative, cneutral = 0.0, 0.0, 0.0
    for tweet in tweets:
        #give a score to every tweet
        score = analyzer.analyze(tweet)
        if score > 0.0:
           cpositive += 1
        elif score < 0.0:
            cnegative += 1
        else:
            cneutral +=1

    total = cpositive + cnegative +cneutral
    if total == 0:
        return render_template("index.html")

    positive, negative, neutral = cpositive/total, cnegative/total, cneutral/total

    # generate chart
    chart = helpers.chart(positive, negative, neutral)

    # render results
    return render_template("search.html", chart=chart, screen_name=screen_name)
示例#28
0
def search():

    # validate screen_name
    screen_name = request.args.get("screen_name", "")
    if not screen_name:
        return redirect(url_for("index"))

    # get screen_name's tweets
    tweets = helpers.get_user_timeline(screen_name, 100)

    positives = os.path.join(sys.path[0], "positive-words.txt")
    negatives = os.path.join(sys.path[0], "negative-words.txt")

    if tweets == None:
        sys.exit("Usage: incorect @screen_name")

    posit, negat, neut = 0.0, 0.0, 0.0

    for tweet in tweets:

        # instantiate analyzer
        analyzer = Analyzer(positives, negatives)
        score = analyzer.analyze(tweet)

        if score > 0.0:
            posit += 1.0
        elif score < 0.0:
            negat += 1.0
        else:
            neut += 1.0

    one_percent  = (posit + negat + neut) / 100
    positive = one_percent * posit
    negative = one_percent * negat
    neutral = one_percent * neut

    # generate chart
    chart = helpers.chart(positive, negative, neutral)

    # render results
    return render_template("search.html", chart=chart, screen_name=screen_name)
示例#29
0
def search():
    screen_name = request.args.get("screen_name", "").lstrip("@")
    if not screen_name:
        return redirect(url_for("index"))
    positives = os.path.join(sys.path[0], "positive-words.txt")
    negatives = os.path.join(sys.path[0], "negative-words.txt")
    analyzer = Analyzer(positives, negatives)
    tweets = helpers.get_user_timeline(screen_name, 100)
    if tweets == None:
        return redirect(url_for("index"))
    positive, negative, neutral = 0, 0, 0
    for tweet in tweets:
        count = analyzer.analyze(tweet)
        if count > 0.0:
            positive += 1
        elif count < 0.0:
            negative += 1
        else:
            neutral += 1
    chart = helpers.chart(positive, negative, neutral)
    return render_template("search.html", chart=chart, screen_name=screen_name)
示例#30
0
def search():

    # validate screen_name
    screen_name = request.args.get("screen_name", "").lstrip("@")
    if not screen_name:
        return redirect(url_for("index"))

    # get screen_name's tweets
    tweets = helpers.get_user_timeline(screen_name, count=100)

    positives = os.path.join(sys.path[0], "positive-words.txt")
    negatives = os.path.join(sys.path[0], "negative-words.txt")
    analyzer = Analyzer(positives, negatives)

    total_tweets = len(tweets)

    positive = 0.0
    negative = 0.0
    neutral = total_tweets

    for tweet in tweets:
        score = analyzer.analyze(tweet)
        if score > 0:
            positive += 1
            neutral -= 1
        elif score < 0:
            negative += 1
            neutral -= 1

    # make positive, negative, and neutral into percentages

    positive = (positive / total_tweets) * 100
    negative = (negative / total_tweets) * 100
    neutral = (neutral / total_tweets) * 100

    # generate chart
    chart = helpers.chart(positive, negative, neutral)

    # render results
    return render_template("search.html", chart=chart, screen_name=screen_name)
示例#31
0
def search():

    # validate screen_name
    screen_name = request.args.get("screen_name", "").lstrip("@")

    if screen_name == None:
        return redirect(url_for("index"))

    # get screen_name's tweets

    positives = os.path.join(sys.path[0], "positive-words.txt")
    negatives = os.path.join(sys.path[0], "negative-words.txt")
    analyzer = Analyzer(positives, negatives)

    tweets = helpers.get_user_timeline(screen_name, 100)

    if tweets == None:
        return redirect(url_for("index"))

    red_score = 0.0
    yellow_score = 0.0
    green_score = 0.0

    for t in tweets:
        fscore = analyzer.analyze(t)  #Analyze every tweet and score it

        if fscore > 0:
            green_score += 1.0
        elif fscore < 0:
            red_score += 1.0
        else:
            yellow_score += 1.0

    # TODO

    # generate chart
    chart = helpers.chart(green_score, red_score, yellow_score)

    # render results
    return render_template("search.html", chart=chart, screen_name=screen_name)
示例#32
0
def search():
    
    # validate screen_name
    screen_name = request.args.get("screen_name", "").lstrip("@")
    if not screen_name:
        return redirect(url_for("index"))

    # get screen_name's tweets
    tweets = helpers.get_user_timeline(screen_name, 100)
    if tweets is None:
        return redirect(url_for("index"))
    
    # absolute paths to lists
    positives = os.path.join(sys.path[0], "positive-words.txt")
    negatives = os.path.join(sys.path[0], "negative-words.txt")

    # instantiate analyzer
    analyzer = Analyzer(positives, negatives)
    
    # set up counters of positive, negative and neutral tweets
    positive = 0
    negative = 0
    neutral = 0
    
    # iterate over each tweet
    for tweet in tweets:
        # analyze each word of tweets, sum up the total score and add it to the correct sentiment tally
        score = analyzer.analyze(tweet)
        if score > 0.0:
            positive += 1
        elif score < 0.0:
            negative += 1
        else:
            neutral += 1

    # generate chart
    chart = helpers.chart(positive, negative, neutral)

    # render results
    return render_template("search.html", chart=chart, screen_name=screen_name)
示例#33
0
def search():

    # validate screen_name
    screen_name = request.args.get("screen_name", "")
    if not screen_name:
        return redirect(url_for("index"))

    # get screen_name's tweets
    tweets = helpers.get_user_timeline(screen_name, 100)

    if tweets == None:
        sys.exit("Couldn't get user timeline: Invalid twitter handle")

    # absolute paths to lists
    positives = os.path.join(sys.path[0], "positive-words.txt")
    negatives = os.path.join(sys.path[0], "negative-words.txt")

    # instantiate analyzer
    analyzer = Analyzer(positives, negatives)

    positive_counter = 0
    negative_counter = 0
    neutral_counter = 0

    for i in tweets:
        sentiment = analyzer.analyze(i)
        if sentiment > 0:
            positive_counter += 1
        elif sentiment < 0:
            negative_counter += 1
        else:
            neutral_counter += 1

        positive, negative, neutral = positive_counter, negative_counter, neutral_counter

    # generate chart
    chart = helpers.chart(positive, negative, neutral)

    # render results
    return render_template("search.html", chart=chart, screen_name=screen_name)
示例#34
0
def search():

    # validate screen_name
    screen_name = request.args.get("screen_name", "").lstrip("@")
    if not screen_name:
        return redirect(url_for("index"))

    # absolute paths to lists
    positives = os.path.join(sys.path[0], "positive-words.txt")
    negatives = os.path.join(sys.path[0], "negative-words.txt")

    # instantiate analyzer
    analyzer = Analyzer(positives, negatives)
    
    # get screen_name's most recent 100 tweets
    tweets = helpers.get_user_timeline(screen_name, 100)
    
    # return to index if screen_name doesn't exist
    if tweets == None:
        return redirect(url_for("index"))
        
    # create positive, negative and neutral count
    positive, negative, neutral = 0, 0, 0
    
    # analyze each tweet & increase corresponding sentimen count
    for tweet in tweets:
        score = analyzer.analyze(tweet)
        if score > 0.0:
            positive += 1
        elif score < 0.0:
            negative += 1
        else:
            neutral += 1

    # generate chart
    chart = helpers.chart(positive, negative, neutral)

    # render results
    return render_template("search.html", chart=chart, screen_name=screen_name)