# dictionary of tweets indexed on date # will contain following structure: # [[list of sentiment for days tweets], stock_market_close - stock_market_open] trump_tweets = {} # pull all tweets api = twitter.Api(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET) screen_name = 'realDonaldTrump' timeline = get_tweets(api=api, screen_name=screen_name) # create ToneAnalyzer IBM Watson object tone_analyzer = ToneAnalyzerV3( version='2017-09-21', iam_apikey='n54NtzfmzFug46PryonqHU9sGWWaGKcT_vQ_zQhOvdrR', url='https://gateway.watsonplatform.net/tone-analyzer/api' ) first_tweet = 0 earliest_date = '' recent_date = '' # add tweets and sentiment to dictionary for tweet in timeline: dt = parser.parse(tweet._json['created_at']) date = datetime.datetime.strptime(str(dt.day) + str(dt.month) + str(dt.year), '%d%m%Y').date() if first_tweet == 0: recent_date = date first_tweet = 1
import json,httplib,urllib from watson_developer_cloud import ToneAnalyzerV3 import csv import pandas os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'WhizLeadsInsights.json' consumer_key= '4F4rkhWlzJx1geKY7EIFoyOyp' consumer_secret= '6uvr35kgy7CziY8zzGlHbywAVNEb8qzMaxs0DnL5lupH8HYH9D' access_token='801128036364091392-3edsjInInkhwUR87PblYwKsuGmPsHob' access_token_secret='KEL1QWcLZy1TsG4gloLB1w1wmme5Iu6b65wje5VubNjxM' auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth) tone_analyzer = ToneAnalyzerV3( username='******', password='******', version='2016-05-19') PARSE_PRODUCTION_APLICATION_ID= "9LT6MCUSdT4mnzlNkG2pS8L51wvMWvugurQJnjwB" PARSE_PRODUCTION_REST_API_KEY = "6gwEVURQBIkh9prcc3Bgy8tRiJTFYFbJJkQsB45w" PARSE_DEVELOPMENT_APLICATION_ID= "9LT6MCUSdT4mnzlNkG2pS8L51wvMWvugurQJnjwB" PARSE_DEVELOPMENT_REST_API_KEY = "6gwEVURQBIkh9prcc3Bgy8tRiJTFYFbJJkQsB45w" if (PRODUCTION_ENVIRONMENT): { Parse_Application_Id = PARSE_PRODUCTION_APLICATION_ID Parse_REST_API_Key = PARSE_PRODUCTION_REST_API_KEY } else:
def watson(): tone_analyzer = ToneAnalyzerV3( version='2017-09-21', username='******', password='******') f = open('sent.txt', 'r') stri = "" for line in f: stri = stri + line text = stri content_type = 'application/json' color = [0, 0, 0, 0] tone = tone_analyzer.tone({"text": text}, content_type, sentences=False) obj = json.dumps(tone, indent=2) l = len(tone['document_tone']['tones']) for i in range(0, l): if (tone['document_tone']['tones'][i]): temp_obj_score = tone['document_tone']['tones'][i]['score'] temp_obj_id = tone['document_tone']['tones'][i]['tone_id'] if (temp_obj_id == 'joy'): color[0] = temp_obj_score elif (temp_obj_id == 'sadness'): color[1] = temp_obj_score elif (temp_obj_id == 'tentative'): color[2] = temp_obj_score elif (temp_obj_id == 'analytical'): color[3] = temp_obj_score output_file("graph.html") emotions = ['Joy', 'Sadness', 'Tentative', 'Analytical'] repos = ['average', 'you'] data = { 'Emotions': emotions, 'average': [0.58125, 0.532308, 0.717059, 0.696667], 'you': color } source = ColumnDataSource(data=data) p = figure(x_range=emotions, y_range=(0, 1), plot_width=500, plot_height=800, title="Sentiment Averages", toolbar_location=None, tools="") p.vbar(x=dodge('Emotions', -0.15, range=p.x_range), top='average', width=0.3, source=source, color='#c9d9d3', legend=value("Average")) p.vbar(x=dodge('Emotions', 0.15, range=p.x_range), top='you', width=0.3, source=source, color='#718dbf', legend="Your repo") p.y_range.start = 0 p.x_range.range_padding = 0.1 p.xaxis.major_label_orientation = 1 p.xgrid.grid_line_color = None show(p)
from __future__ import print_function from watson_developer_cloud import ToneAnalyzerV3 import pandas as pd import json #Instantiate TA Object with your Credentials service = ToneAnalyzerV3( #url is optional, and defaults to the URL below. Use the correct URL for your region. url='https://gateway.watsonplatform.net/tone-analyzer/api', version='2017-09-21', iam_apikey='apikey') #read data data = pd.read_csv( "/Users/pudin/Desktop/digital/project1/russian_dataset_with_datesplit_clean.csv" ) #make a test copy test_data = data.head(50).copy() #get result for index, review in data['Ad_Text '].iteritems(): result = json.dumps(service.tone(tone_input=review, content_type="text/plain").get_result(), indent=2) for i in json.loads(result)['document_tone']['tones']: print(index, i['tone_name'], i['score']) #save tone and score to dataframe data.at[index, str(i['tone_name'])] = float(i['score']) data.to_csv('test_data.csv')
# -*- coding: utf-8 -*- import json from watson_developer_cloud import ToneAnalyzerV3 # BEGIN of python-dotenv section from os.path import join, dirname from dotenv import load_dotenv import os dotenv_path = join(dirname(__file__), '.env') load_dotenv(dotenv_path) # END of python-dotenv section tone_analyzer = ToneAnalyzerV3(username=os.environ.get("TONE_USERNAME"), password=os.environ.get("TONE_PASSWORD"), version='2016-05-19') def parse_toneanalyzer_response(json_data): """Parses the JSON response from ToneAnalyzer to return a dictionary of emotions and their corresponding score. Parameters ---------- json_data: {dict} a json response from ToneAnalyzer (see Notes) Returns ------- dict : a {dict} whose keys are emotion ids and values are their corresponding score. """ emotions = {}
#pass the transcript to the tone analyzer, which recognizes emotions; #return predominat emotion from __future__ import print_function import json from watson_developer_cloud import ToneAnalyzerV3 from io import StringIO tone_analyzer = ToneAnalyzerV3(username="******", password="******", version="2016-05-19") def getPredominantEmotion(transcript): tone_raw_json = StringIO() json.dump( tone_analyzer.tone(tone_input=transcript, content_type="text/plain"), tone_raw_json) #extract the emotions out of the json score = 0 tone_str_json = (json.loads(tone_raw_json.getvalue())) for i in range(0, 5): aux_score = (tone_str_json["document_tone"]["tone_categories"][0] ["tones"][i]["score"]) aux_emotion = tone_str_json["document_tone"]["tone_categories"][0][ "tones"][i]["tone_name"] print(aux_emotion + " => " + str(aux_score)) #get maximum score emotion if (aux_score > score):
from watson_developer_cloud import ToneAnalyzerV3 tone_analyzer = ToneAnalyzerV3( version='2018-11-17', iam_apikey='0BhiGpM_2XfxIs6vPS1J6Eg8FSdrHM2kyf8yiF1h35HT', url='https://gateway.watsonplatform.net/tone-analyzer/api') def analyze(text): tone_analysis = tone_analyzer.tone( { 'text': text }, 'text/plain', ).get_result() return tone_analysis
# Author: Sahil Verma '''Importing necessary lib csv for output, watson_developer_cloud for Analyse matplotlib for ploting, numpy to make array ''' import csv from watson_developer_cloud import ToneAnalyzerV3 import matplotlib.pyplot as plt import numpy as np # Taking input texted = input("Enter the sentence to analyse \n") # API request from IBM Watson tone_analyzer = ToneAnalyzerV3( username='', # Enter the username and password provided by bluemix password='', version='2016-05-19 ') result = tone_analyzer.tone( text=texted) # Getting output as a file of dictionary xyz = [] for x in result["document_tone"]["tone_categories"][0].get( "tones"): # Selecting specific Emotion is result xyz.append(x) print(xyz) # Printing Emotions as text # Writing Input and Output to csv file def write2csv(): f = open("output.csv", "w")
prompt = " [y/N] " else: raise ValueError("invalid default answer: '%s'" % default) while True: choice = input(question + prompt).lower() #choice = raw_input().lower() if default is not None and choice == '': return valid[default] elif choice in valid: return valid[choice] else: print("Please respond with 'yes' or 'no' "\ "(or 'y' or 'n').\n") tone_analyzer = ToneAnalyzerV3(username='', password='', version='2017-09-26') while True: utterance = input( "Enter the sentence to analyze for tone (can put more than one): ") print("\nanalyzing....\n") print( json.dumps(tone_analyzer.tone(tone_input=utterance, content_type="text/plain"), indent=2)) again = query_yes_no("Want to try another?") if not again: break
#!usr/bin/env python from __future__ import print_function import json import os from os.path import join, dirname from watson_developer_cloud import ToneAnalyzerV3 tone_analyzer = ToneAnalyzerV3( username='******', password='******', version='2017-09-26') #KodK0d122017. #print("tone_chat() example 1:\n") #utterances = [{'text': '"More that all @HerdFB away games and half' + 'their of their \n ' #'home games. Oh its True ... ' #'Damn True! #IamBecauseWeAre https://t.co/GZ1qHgixNK"', 'user': '******'},{'text': 'It is a good day.', 'user': '******'},{'text': 'I am very happy. It is a good day.', 'user': '******'}] #print(json.dumps(tone_analyzer.tone_chat(utterances), indent=2)) directory = './resources/' OutFile = '' print("hi") print("Analyzing all files in " + directory) for fn in os.listdir(directory): print("File " + fn + " " +fn[len(fn)-4:len(fn)]) if fn[len(fn)-4:len(fn)] == 'json': OutFile = directory +fn[0:len(fn)-4]+'_ans.json' print("working on file"+fn) #with open(join(dirname(__file__),'./resources/tone.json')) as tone_json: # tone = tone_analyzer.tone(json.load(tone_json)['text'], "text/plain")
from django.shortcuts import render, get_object_or_404 from .forms import PostForm from django.shortcuts import redirect import json import requests from watson_developer_cloud import ToneAnalyzerV3 from watson_developer_cloud import LanguageTranslatorV3 language_translator = LanguageTranslatorV3( version='2018-05-01', iam_apikey='h3H1OGBNlGD3QK37rcuGUMCaJpmQHWAewE1ZpzTgd_Zm') service = ToneAnalyzerV3( version='2017-09-21', iam_apikey='Ruy6zs3ZAw5TmvGXMxVJQ-QUP86LucgVWHkHC1cPrkdb', ) def post_list(request): posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date') for post in posts: posting = post.text translation = language_translator.translate(text=posting, model_id='en-es').get_result() obj = (json.dumps(translation, indent=2, ensure_ascii=False)) obj2 = json.loads(obj) post.obj2 = obj2['translations'][0]['translation'] post.w_count = obj2['word_count']
from __future__ import print_function import json import firebase_admin from firebase_admin import credentials from firebase_admin import db import threading, time from pprint import pprint from os.path import join, dirname from watson_developer_cloud import ToneAnalyzerV3 from watson_developer_cloud.tone_analyzer_v3 import ToneInput #If service instance provides API key authentication service = ToneAnalyzerV3( version='2018-11-01', url='https://gateway-syd.watsonplatform.net/tone-analyzer/api', iam_apikey='J4a1j1p3o6zKwoSPqJwYQ_l40lNRU85DaTLrQFVKGfbq') #################################3 cred = credentials.Certificate( 'C:/Users/Nirvan Dogra/PycharmProjects/TwitterAPI/simulation.json') firebase_admin.initialize_app( cred, {'databaseURL': 'https://simulation-1270b.firebaseio.com/'}) while (1): result = db.reference('messages/msg').get() print(result) if result == "empty": print("null" + result) continue
from watson_developer_cloud import ToneAnalyzerV3 tone_analyzer = ToneAnalyzerV3( username='******', password='******', version='2016-05-19')
from flask import Flask from flask_restful import Api, Resource, reqparse import json from watson_developer_cloud import ToneAnalyzerV3 import APIkey app = Flask(__name__) api = Api(app) tone_analyzer = ToneAnalyzerV3( version= APIkey.version, iam_apikey=APIkey.iam_apikey, url=APIkey.url ) def anger_score(data): doc_tone = data.get('document_tone') tones = doc_tone.get('tones') threshold = 0 for tone in tones: score = tone.get("score") tone_id = tone.get("tone_id") if score > threshold and tone_id == "anger": return score class Tone(Resource): def get(self, input): text = input tone_analysis = tone_analyzer.tone({'text': text},'application/json').get_result() data = json.loads(json.dumps(tone_analysis, indent=2))