我使用cloud9ide并使用默认的Web服务器。但是代码不会启动服务器(web服务器返回:这里似乎没有运行任何应用程序!)。
我仔细检查了代码的每一个实例,但我在烧瓶方面很薄弱。烧瓶代码是分配代码的一部分。在
代码:
应用程序.py(应该会启动服务器)from flask import Flask, redirect, render_template, request, url_for
import sys, 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", "")
if not screen_name:
return redirect(url_for("index"))
# get screen_name's tweets
tweets = helpers.get_user_timeline(screen_name)
# pass to analyze
analyzer = Analyzer("positive-words.txt", "negative-words.txt")
positive, negative, neutral = analyzer.analyze(tweets)
# generate chart
chart = helpers.chart(positive, negative, neutral)
# render results
return render_template("search.html", chart=chart, screen_name=screen_name)
在助手.py(所用方法应用程序.py)公司名称:
^{pr2}$
在分析器.py(所用方法应用程序.py)公司名称:# Tokenizing library
import nltk
class Analyzer():
# Defining template for the class with two arguments(file names are not hard coded)
def __init__(self, positives, negatives):
# Two dicts for - and + words
self.positives = {}
self.negatives = {}
# Method for reading files into dicts
def open_file (file_name, dictionary_name):
with open(file_name) as f:
# Loop to start with 35th line
for i in range(35):
next(f)
for line in f:
# Keys and values of a dict
dictionary_name[line.strip()] = line.strip()
# Calling methods
open_file(positives, self.positives)
open_file(negatives, self.negatives)
# Analyse tokens
def analyze(self, text):
# Access tools of nltk
tokenizer = nltk.tokenize.TweetTokenizer()
# Defining couning score variable
score_positive = 0
score_negative = 0
score_neutral = 0
# since the text is a list we itterate of the list element by element
for tweet in text:
tokens = tokenizer.tokenize(tweet)
# Checking each token
for token in tokens:
if token.lower() in self.positives:
score_positive += 1
elif token.lower() in self.negatives:
score_negative += 1
else:
score_neutral += 1
return score_positive, score_negative, score_neutral