from sklearn.datasets import load_files
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.pipeline import Pipeline
from sklearn.linear_model import SGDClassifier

from scraper import scrape_tweets


def evaluate_user(user_id):

    categories = ['neg', 'pos']

    twitter_train = load_files('./twitter_data/twitter_data-train', encoding='utf-8', decode_error='ignore',
                               categories=categories, load_content=True, shuffle=True, random_state=42)

    count_vect = CountVectorizer(decode_error='ignore')
    text_clf = Pipeline([('vect', CountVectorizer(ngram_range=(1, 2))),
                         ('tfidf', TfidfTransformer()),
                         ('clf', SGDClassifier(loss='hinge', penalty='l2',
                                               alpha=1e-3, n_iter=5, random_state=42))])
    text_clf = text_clf.fit(twitter_train.data, twitter_train.target)

    if scrape_tweets(user_id, 3) == 1:
        return 1

    f = open("scrapings.text", "r")
    neg_count = 0.0
    pos_count = 0.0
    for line in f:
        line_list = [line]
        predicted = str(text_clf.predict(line_list))
        category = str(twitter_train.target_names[int(predicted[1])])
        print(category)
        if category == "neg":
            neg_count += 1
        elif category == "pos":
            pos_count += 1
    print("neg: ", neg_count, " pos: ", pos_count)
    print("Percent Positive: " + str(pos_count / (pos_count + neg_count)))
    print("Percent Negative: " + str(neg_count / (pos_count + neg_count)))
    print("\n")
    return 0


if __name__ == "__main__":
    evaluate_user("potus")


@@ -1,20 +1,59 @@
+from __future__ import print_function%0A%0A
 from sklearn.dataset
@@ -314,16 +314,39 @@
 tweets%0A%0A
+from sys import stdout%0A
 %0Adef eva
@@ -363,16 +363,29 @@
 (user_id
+, output_file
 ):%0A%0A    
@@ -1175,24 +1175,147 @@
 count = 0.0%0A
+    doubleprint(user_id + %22's twitter classification:%5Cn%22, output_file)%0A    doubleprint(%22~~~~~~~~~~~~~~~~~%5Cn%22, output_file)%0A
     for line
@@ -1483,22 +1483,37 @@
     
-print(category
+doubleprint(line, output_file
 )%0A  
@@ -1563,24 +1563,77 @@
 _count += 1%0A
+            doubleprint(%22NEGATIVE%5Cn%5Cn%22, output_file)%0A
         elif
@@ -1687,23 +1687,92 @@
     
-print(%22neg: %22, 
+        doubleprint(%22POSITIVE%5Cn%22, output_file)%0A    doubleprint(%22%5CnNegative: %22 + str(
 neg_
@@ -1780,35 +1780,75 @@
 ount
-, %22 pos: %22, pos_count)%0A    
+) + %22 Positive: %22 +  str(pos_count) + %22%5Cn%22, output_file)%0A    double
 prin
@@ -1916,84 +1916,131 @@
 nt))
-)%0A    print(%22Percent Negative: %22 + str(neg_count / (pos_count + neg_count)))
+ + %22%5Cn%22, output_file)%0A    doubleprint(%22~~~~~~~~~~~~~~~~~%5Cn%22, output_file)%0A    return 0%0A%0Adef doubleprint(text, output_file):
 %0A   
@@ -2050,26 +2050,77 @@
 int(
-%22%5Cn%22
+text
 )%0A    
-return 0
+if output_file is not None:%0A        output_file.write(text)
 %0A%0A%0Ai
@@ -2170,11 +2170,17 @@
 (%22potus%22
+, None
 )%0A%0A

Update evaluate_user to function with the new GUI