Пример #1
0
def resultbot():
    print(request.method)
    form = BotForm(request.form)
    (tweets, y) = getTweets(int(request.form['time']))
    #acc = accuracy_score(lr_predict_table(tweets),y)
    if 'file' not in request.files:
        yp = lr_predict_table(tweets, request.form['algo'],
                              request.form['language'])
    else:
        file = request.files['file']
        if file.filename == '':
            yp = lr_predict_table(tweets, request.form['algo'],
                                  request.form['language'])
        elif file and allowed_file(file.filename):
            yp = model_predict_table(tweets)
        else:
            yp = lr_predict_table(tweets, request.form['algo'],
                                  request.form['language'])
    #yp = lr_predict_table(tweets)
    i = 0
    right_prediction = 0
    array = []
    for p in y:
        if str(p) == str(yp[i]):
            right_prediction = right_prediction + 1
        else:
            array.append([str(p), str(yp[i])])
        i = i + 1
    precision = (right_prediction / len(y))
    precision = ("{0:.2f}".format(precision))
    #return str(type(y)) + str(type(yp))
    #return str(right_prediction/len(y)) + str(yp) + str(y) + str(right_prediction) + str(len(y)) + str(array)  + str(yp[0])
    return render_template('botresult.html', form=form, precision=precision)
Пример #2
0
def process(query):
    print 'start process'
    Tree = lambda: defaultdict(Tree)
    id = 0
    results = {}
    for item in twitter.getTweets(query):
        tree = Tree()
        hashtag = item['name']
        tweets = item['tweets']
        tree['name'] = hashtag
        tree['id'] = id
        tmp = {}
        for link in item['links']:
            link = link.decode("utf-8")
            tmp[link] = tmp.get(link, 0) + 1
        tree['links'] = sorted(tmp.iteritems(), key=itemgetter(1), reverse=True)
        sentiments = [sentiment(item.encode("utf-8")) for item in tweets]
        tree['score'] = sum(sentiments)/math.sqrt(len(sentiments))
        results[hashtag] = tree
        id += 1
    #convert the graph
    graph = Tree()
    graph['nodes'], graph['links'] = [], []
    i = 0
    for hashtag, value in results.iteritems():
        graph['nodes'].append({'name': value['name'], 'score': value['score']})
        for link, score in value['links']:
            graph['links'].append( {'source': results[link]['id'], 'target': value['id'], 'count': score})
        i += 1
    f = open('../data/graph.json', 'w')
    f.write(json.dumps(graph))
    return graph
Пример #3
0
def search_tweets():
    query = request.json['query']
    tweets = getTweets(query)
    predictions = predict_sentiment(tweets)
    predictions = [
        sentiments[prediction.index(max(prediction))]
        for prediction in predictions
    ]
    sentiment_counts = {'negative': 0, 'neutral': 0, 'positive': 0}
    for sentiment in predictions:
        sentiment_counts[sentiment] += 1
    return jsonify({'results': sentiment_counts}), 200
Пример #4
0
def user():
    user_ip = request.environ['REMOTE_ADDR'] if request.environ.get(
        'HTTP_X_FORWARDED_FOR'
    ) is None else request.environ['HTTP_X_FORWARDED_FOR']
    if user_ip not in session:
        flash("Please login before entering the user page.")
        return redirect(url_for('login'))
    changeUsernameError = False
    tweets = []
    if request.method == 'POST' and 'newUsername' in request.form:
        oldUsername = session[user_ip]
        newUsername, password = request.form['newUsername'].strip(
        ), request.form['password'].strip()
        if dbFuncs.validate(
                oldUsername, password
        ) and newUsername != '' and dbFuncs.isNewUsername(newUsername):
            dbFuncs.changeUsername(oldUsername, newUsername)
            session[user_ip] = newUsername
        else:
            changeUsernameError = True
    elif request.method == 'POST' and 'hashtag' in request.form:
        hashtag = request.form['hashtag'].strip()
        if any(punc in hashtag for punc in
               ' `~!@#$%^&*()_-+={}[]|\:;<,>.?/\'\"') or hashtag == '':
            return render_template('user.html',
                                   twitterError=True,
                                   tweets=[],
                                   changeUsernameError=False,
                                   username=session[user_ip])
        tweets = twitter.getTweets(hashtag, consumer_key, consumer_secret,
                                   token_key, token_secret)
    return render_template('user.html',
                           twitterError=False,
                           tweets=tweets,
                           changeUsernameError=changeUsernameError,
                           username=session[user_ip])
Пример #5
0
 def get(self, handle):
     results = '{"items" :' + getTweets(handle, "", "") + '}'
     return json.loads(results)
Пример #6
0
def result():
    if request.method == 'POST':
        handle = request.form['handle']
        tweets = getTweets(handle)
        return (tweet)