I need some help with pset6 I'm basically lost trying to figure out what I should be doing. I have my analyzer code which doesn't return any errors when I run it but at the same time returns no output either. I also have my tweets code which also returns no errors but at the same time no output as well. I haven't gotten started on the application.py because I don't know where I should be pulling the positive, negative and neutral percentages from. Here is my analyzer code:
import nltk
import sys
import string
from nltk.tokenize import TweetTokenizer
class Analyzer():
"""Implements sentiment analysis."""
def __init__(self, positives, negatives):
"""Initialize Analyzer."""
self.negatives = []
self.positives = []
with open(negatives) as negative:
for line in negative:
if line.startswith((";", " ")):
continue
else:
self.negatives.extend(line.split())
with open(positives) as positive:
for line in positive:
if line.startswith((";", " ")):
continue
else:
self.positives.extend(line.split())
def analyze(self, text):
"""Analyze text for sentiment, returning its score."""
tokenizer = nltk.tokenize.TweetTokenizer()
tokens = tokenizer.tokenize(text)
score = 0
for token in tokens:
token.lower()
if token in self.positives:
score += 1
elif token in self.negatives:
score -= 1
# TODO
return score
Here is my tweets code:
#!/usr/bin/env python3
# TODO
import os
import sys
import nltk
from analyzer import Analyzer
from termcolor import colored
from helpers import get_user_timeline
def main():
#Ensure proper usage
if len(sys.argv) != 2:
sys.exit("Usage: ./tweets twitterhandle")
positives = os.path.join(sys.path[0], "positive-words.txt")
negatives = os.path.join(sys.path[0], "negative-words.txt")
analyzer = Analyzer(positives, negatives)
final_score = analyzer.analyze(sys.argv[1])
for i in range (0, len(final_score)):
score = final_score[i]
print(score[0], end="")
if score[0] > 0.0:
print(colored(score, "green"))
elif score[0] < 0.0:
print(colored(score, "red"))
else:
print(colored(score, "yellow"))
if __name__ == "__main__":
main()
def checkTweet(self, text):
tweets = get_user_timeline(text, 50)
if tweets == None:
sys.exit("Error")
print(tweets)
final_score = []
for tweet in tweets:
score = 0
tokenizer = nltk.tokenize.TweetTokenizer()
tokens = tokenizer.tokenize(tweet)
for word in tokens:
if word in self.positives:
score += 1
elif word in self.negatives:
score -= 1
else:
score = 0
scores = (score, tweet)
final_score.append(scores)
return final_score
Lastly here is the untouched application.py:
from flask import Flask, redirect, render_template, request, url_for
import helpers
from analyzer import Analyzer
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/search")
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
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)
Please help if you can. I know it is a lot of code to go through. Thanks