def send_to_indico(): ''' This route handles the server's response when you post data to localhost:5000/crunch through the form on index.html ''' tweets_csv_string = request.form.get('tweets') csv_list = tweets_csv_string.replace('\r', '').splitlines() if len(csv_list) > 40: csv_list = csv_list[:40] tweet_list = [] for csv_tweet in csv_list: tweet_only = csv_tweet.split(',')[2:] tweet_list.append(','.join(tweet_only)) tweet_list = tweet_list[::-1] tweet_scores = indicoio.batch_sentiment(tweet_list, api_key="YOUR_API_KEY") return json.dumps({ 'scores': tweet_scores, 'tweets': tweet_list }) # dumps converts res to a JSON object
def get_indico_features(posts): texts = [ post.title.encode('UTF8') for post in posts] print "Getting sentiment scores..." sentiment = indicoio.batch_sentiment(texts) print "Getting political scores..." political = indicoio.batch_political(texts) return (sentiment, political)
def send_to_indico(): ''' This route handles the server's response when you post data to localhost:5000/crunch through the form on index.html ''' tweets_csv_string = request.form.get('tweets') csv_list = tweets_csv_string.replace('\r', '').splitlines() if len(csv_list) > 40: csv_list = csv_list[:40] tweet_list = [] for csv_tweet in csv_list: tweet_only = csv_tweet.split(',')[2:] tweet_list.append(','.join(tweet_only)) tweet_list = tweet_list[::-1] tweet_scores = indicoio.batch_sentiment(tweet_list, api_key="YOUR_API_KEY") return json.dumps({'scores': tweet_scores, 'tweets': tweet_list}) # dumps converts res to a JSON object
def post(self): query_string = self.request.body_arguments.get('query') query = TwitterSearchOrder() query.set_keywords(query_string) query.set_language('en') query.set_include_entities(False) results = TwitterClient.search_tweets(query) tweets = [tweet['text'] for tweet in results['content']['statuses']] sentiment = indicoio.batch_sentiment(tweets) pairs = sorted(zip(sentiment, tweets)) n_tweets = float(len(pairs)) top_n = 5 most_negative = pairs[:top_n] most_positive = list(reversed(pairs[-top_n:])) data = { 'most_positive': most_positive, 'most_negative': most_negative, 'average': sum(sentiment)/n_tweets } self.write(json.dumps(data))
def post(self): query_string = self.request.body_arguments.get('query') query = TwitterSearchOrder() query.set_keywords(query_string) query.set_language('en') query.set_include_entities(False) results = TwitterClient.search_tweets(query) tweets = [tweet['text'] for tweet in results['content']['statuses']] sentiment = indicoio.batch_sentiment(tweets) pairs = sorted(zip(sentiment, tweets)) n_tweets = float(len(pairs)) top_n = 5 most_negative = pairs[:top_n] most_positive = list(reversed(pairs[-top_n:])) data = { 'most_positive': most_positive, 'most_negative': most_negative, 'average': sum(sentiment) / n_tweets } self.write(json.dumps(data))
import indicoio import pandas as pd import csv step_size = 200 indicoio.config.api_key = 'cefae61c6968c29ea880d918e9a6095d' key2 = 'bff000095c5eb591fa1b4ab3da98063b' df = pd.read_csv('data/hotel_reviews.csv') reviews = df['text'].tolist() sentiment = [] steps = len(sentiment) / step_size leftover = len(sentiment) % step_size total_count = 0 for i in xrange(steps): total_count += step_size if total_count > 20000: indicoio.config.api_key = 'bff000095c5eb591fa1b4ab3da98063b' test = reviews[i * step_size:(i + 1) * step_size] sentiment.extend(indicoio.batch_sentiment(test)) test = reviews[total_count:] sentiment.extend(indicoio.batch_sentiment(test)) csv_sent = [[x] for x in sentiment] resultFile = open("sentiment_results.csv",'wb') wr = csv.writer(resultFile, dialect='excel') wr.writerows(csv_sent)
from indicoio import config, sentiment, batch_sentiment import csv config.api_key = "0285e71c8c80f055c102ad5f3a614fc2" tweets=[] with open('twitter_data.csv', 'rb') as f: reader = csv.reader(f) for row in reader: tweets.append(row[1]) del(tweets[0]) batch = batch_sentiment(tweets) for element in batch: if(element>0.5): print "Postive", print ",", else: print "Negative", print ",", print element
#!/usr/bin/env python # actually run the sentiment import indicoio import os import glob indicoio.config.api_key = 'oops' import glob for player in ['deandre','lamarcus']: opinions_in_threads = {} for files in glob.glob("/home/akshay/indicoio/{}/*.txt".format(player)): with open(files,'r') as f: comments = [line.decode('utf8') for line in f] sentiments = indicoio.batch_sentiment(comments) pos_sentiments = [sentiment for sentiment in sentiments if sentiment >=0.5] neg_sentiments = [sentiment for sentiment in sentiments if sentiment <0.5] pos_avg = sum(pos_sentiments)/(len(pos_sentiments) + 0.0) neg_avg = sum(neg_sentiments)/(len(neg_sentiments) + 0.0) opinions_in_threads[files.split('/')[-1].split('.')[0]] = (len(pos_sentiments),len(neg_sentiments),pos_avg,neg_avg) with open('results.txt.{}'.format(player),'w') as f: for sentiment in opinions_in_threads: f.write('Thread: {}, '.format(sentiment) + 'Pos Comments: %d, Neg Comments: %d, Avg Pos: %f, Avg Neg: %f\n' % opinions_in_threads[sentiment])
def test_batch_posneg(self): test_data = ['Worst song ever', 'Best song ever'] response = batch_sentiment(test_data, api_key=self.api_key) self.assertTrue(isinstance(response, list)) self.assertTrue(response[0] < 0.5)
import indicoio import numpy as np import matplotlib.pyplot as plt # This is an example of how the batch API works on three different words indicoio.batch_sentiment(["Happy", "Terrible", "Wonderful"]) text_file = open("call_of_the_wild.txt", "r") chunks = text_file.read().split() text_file.close() # We then slice the chunks to send to indico's sentiment API: def chunker(seq, size): return (seq[pos:pos + size] for pos in xrange(0, len(seq), 1)) # Split the data up into 30,000+ chunks of 100 words each data = [" ".join(group) for group in chunker(chunks, 100)] # Sending off data for remote processing sentiment_scores = indicoio.batch_sentiment(data) # Conversion to numpy arrays x = np.arange(len(data)) y = np.array(sentiment_scores) # Plotting and formatting plt.scatter(x, y) plt.ylim(ymax=1.0, ymin=0.0) plt.xlim(xmax=325, xmin=-1.0)
print(indicoio.sentiment('indico is so easy to use!', api_key="75b93ed62df0c77a6e58c8ebb1bb71f2", cloud="YOUR_SUBDOMAIN")) # option 2: set module variable indicoio.config.api_key = '75b93ed62df0c77a6e58c8ebb1bb71f2' indicoio.config.cloud = 'YOUR_SUBDOMAIN' print(indicoio.sentiment('indico is so easy to use!')) import indicoio indicoio.config.api_key = '75b93ed62df0c77a6e58c8ebb1bb71f2' # Version >= 0.9.0 indicoio.sentiment(['indico is so easy to use!', 'Still really easy, yiss']) # Version < 0.9.0 indicoio.batch_sentiment(['indico is so easy to use!', 'Still really easy, yiss']) #https://indico.io/docs #sentiment(data, [api_key], [cloud], [language]) #sentiment_hq(data, [api_key], [cloud]) #text_tags(data, [api_key], [cloud], [top_n], [threshold], [independent]) #language(data, [api_key], [cloud]) #political(data, [api_key], [cloud], [top_n], [threshold]) #keywords(data, [api_key], [version], [cloud], [top_n], [threshold], [relative] #people(data, [api_key], [cloud], [threshold]) #places(data, [api_key], [cloud], [threshold]) #organizations(data, [api_key], [cloud], [threshold]) #twitter_engagement(data, [api_key], [cloud]) #personality(data, [api_key], [cloud]) #relevance(data, queries, [api_key], [cloud])
query.set_include_entities(False) results = TwitterClient.search_tweets(query) #print results['content']['statuses'][0].keys() #tweets = [tweet['text'] for tweet in results['content']['statuses']] tweets = [] for tweet in results['content']['statuses']: data = {'tweet_id': tweet['id_str'], 'tweet_text': tweet['text']} req = urllib2.Request('http://45.33.90.42:5000/insert_data') req.add_header('Content-Type', 'application/json') response = urllib2.urlopen(req, json.dumps(data)) tweets.append(data) sentiment = indicoio.batch_sentiment(tweets) pairs = sorted(zip(sentiment, tweets)) n_tweets = float(len(pairs)) top_n = 5 most_negative = pairs[:top_n] #most_positive = list(reversed(pairs[-top_n:])) data = { #'most_positive': most_positive, 'most_negative': most_negative, #'average': sum(sentiment)/n_tweets } pprint(zip(tweets))