Ejemplo n.º 1
0
 def __init__(self):
     logging.basicConfig()
     self.loop_flag = True
     self.bot_state = 0
     self.QuestionObject = Q.Question()
     trial1 = Pipeline([
         ('vectorizer',
          TfidfVectorizer(tokenizer=stemming_tokenizer,
                          stop_words=stopwords.words('english'))),
         ('classifier', MultinomialNB()),
     ])
     self.detector0 = train(trial1, xtrain, ytrain, xtest, ytest)
     self.sentiment = sent.classifier()
Ejemplo n.º 2
0
def search_and_return(subreddit, search_string):

	NBclassifier = classifier()
	# I may need to set this as a global
	# For now, I will call one every single time 
	# *NOT SCALABLE*	
	# r = praw.Reddit('search application')
	now = datetime.datetime.now()

	# search 
	search_results = r.search(search_string, 
	 						  subreddit = subreddit,
	 						  sort = True)

	for i in xrange(search_results.__sizeof__()):

		comments = []

		post = search_results.next()

		for comment in post.comments:
		    try:
		        comment_str = str(comment.body.encode('utf-8'))
		        comments.append(comment_str)
		        comments.append(comment.score)
		    except AttributeError:
		    	pass
		    	
		score = 0    	
		for i in range(0, len(comments), 2):
			for comment in comments[i].split('\n'):
				if any(i in str(comment) for i in search_string.lower().split(' ')):
 					#print comment
 					sentiment = NBclassifier.classify(word_feats(comment))
 					if sentiment == 'neg':
 						score += -1 * int(comments[i+1])
 					else:
 						score += 1 * int(comments[i+1]) 

	 	if score > 10:
	 		result = str(now) + " POSITIVE " + str(score)
	 	elif score < -10:
	 		result = str(now) + " NEGATIVE " + str(score) 
	 	else:
	 		result = str(now) + " NEUTRAL " + str(score)

 	#return result, max_post_comment, pos_score, max_neg_comment, neg_score 
 		return result
Ejemplo n.º 3
0
def search_and_return(subreddit, search_string):

    NBclassifier = classifier()
    # I may need to set this as a global
    # For now, I will call one every single time
    # *NOT SCALABLE*
    # r = praw.Reddit('search application')
    now = datetime.datetime.now()

    # search
    search_results = r.search(search_string, subreddit=subreddit, sort=True)

    for i in xrange(search_results.__sizeof__()):

        comments = []

        post = search_results.next()

        for comment in post.comments:
            try:
                comment_str = str(comment.body.encode('utf-8'))
                comments.append(comment_str)
                comments.append(comment.score)
            except AttributeError:
                pass

        score = 0
        for i in range(0, len(comments), 2):
            for comment in comments[i].split('\n'):
                if any(i in str(comment)
                       for i in search_string.lower().split(' ')):
                    #print comment
                    sentiment = NBclassifier.classify(word_feats(comment))
                    if sentiment == 'neg':
                        score += -1 * int(comments[i + 1])
                    else:
                        score += 1 * int(comments[i + 1])

        if score > 10:
            result = str(now) + " POSITIVE " + str(score)
        elif score < -10:
            result = str(now) + " NEGATIVE " + str(score)
        else:
            result = str(now) + " NEUTRAL " + str(score)

#return result, max_post_comment, pos_score, max_neg_comment, neg_score
        return result
Ejemplo n.º 4
0
        for i in range(0, len(comments), 2):
            for comment in comments[i].split('\n'):
                if any(i in str(comment)
                       for i in search_string.lower().split(' ')):
                    #print comment
                    sentiment = classifier.classify(word_feats(comment))
                    if sentiment == 'neg':
                        score += -1 * int(comments[i + 1])
                    else:
                        score += 1 * int(comments[i + 1])

        if score > 10:
            result = str(now) + " POSITIVE " + str(score)
        elif score < -10:
            result = str(now) + " NEGATIVE " + str(score)
        else:
            result = str(now) + " NEUTRAL " + str(score)

#return result, max_post_comment, pos_score, max_neg_comment, neg_score
        return result

#return flask.render_template('result.html',result=result)

NBclassifier = classifier()
print search_and_return('lebron', 'nba', NBclassifier)
print search_and_return('trump', 'politics', NBclassifier)
print search_and_return('peyton manning', 'nfl', NBclassifier)
print search_and_return('monty williams', 'nba', NBclassifier)
print search_and_return('frank underwood', 'houseofcards', NBclassifier)
print search_and_return('bernie sanders', 'politics', NBclassifier)
Ejemplo n.º 5
0
from copy import deepcopy

import numpy as np
from sklearn.covariance import GraphLassoCV
from sklearn import manifold, cluster
from matplotlib.collections import LineCollection
import pylab as pl

##############################################################################
# Retrieve the data
begin = datetime.date( 2011, 1, 3 )
end = datetime.date( 2013, 11, 27 )
tickerList = keywords.getTickerList()
keywordsMap = keywords.getKeywordToIndexMap()
sentCounter = count.SentimentWordCounter( keywordsMap, sentiment.classifier() )
mentionCounter = count.WordCounter( keywordsMap )

empiricalDf = matrices.getEmpiricalDataFrame( tickerList, begin, end )[ tickerList ]
getTfIdf = lambda wordCounter, aggregator: normalize.TfIdf()( matrices.getCountDataFrame( tickerList, wordCounter, empiricalDf.index, aggregator = aggregator ) )[ tickerList ]
tfIdfSentArticle = getTfIdf( sentCounter, None )[ tickerList ]
tfIdfSentDay = getTfIdf( sentCounter, np.sum )[ tickerList ]
tfIdfMentionArticle = getTfIdf( mentionCounter, None )[ tickerList ]
tfIdfMentionDay = getTfIdf( mentionCounter, np.sum )[ tickerList ]

matrices = { 'Empirical' : { 
                            'By Day' : { 'Data' : empiricalDf } 
              },
             'Signed Mentions' : {
                            'By Day' : { 'Data' : tfIdfSentDay },
                            'By Article' : { 'Data' : tfIdfSentArticle }
Ejemplo n.º 6
0
		    	pass

		for i in range(0, len(comments), 2):
			for comment in comments[i].split('\n'):
				if any(i in str(comment) for i in search_string.lower().split(' ')):
 					#print comment
 					sentiment = classifier.classify(word_feats(comment))
 					if sentiment == 'neg':
 						score += -1 * int(comments[i+1])
 					else:
 						score += 1 * int(comments[i+1]) 

	 	if score > 10:
	 		result = str(now) + " POSITIVE " + str(score)
	 	elif score < -10:
	 		result = str(now) + " NEGATIVE " + str(score) 
	 	else:
	 		result = str(now) + " NEUTRAL " + str(score)

 	#return result, max_post_comment, pos_score, max_neg_comment, neg_score 
 		return result
 	#return flask.render_template('result.html',result=result)


NBclassifier = classifier()
print search_and_return('lebron','nba',NBclassifier)
print search_and_return('trump' , 'politics', NBclassifier)
print search_and_return('peyton manning', 'nfl', NBclassifier )
print search_and_return('monty williams', 'nba', NBclassifier)
print search_and_return('frank underwood', 'houseofcards', NBclassifier)
print search_and_return('bernie sanders', 'politics', NBclassifier)