def get_text_tone(text): tone_username = "******" tone_password = "******" tone_analyzer = ToneAnalyzer(username=tone_username, password=tone_password, version='2016-05-19 ') tone_result = tone_analyzer.tone(text=text) output = {} for category in tone_result['document_tone']['tone_categories']: for item in category['tones']: output[item['tone_id']] = item['score'] return output
import json from watson_developer_cloud import ToneAnalyzerV1Experimental as ToneAnalyzer tone_analyzer = ToneAnalyzer(username='******', password='******') print(json.dumps(tone_analyzer.scorecards(), indent=2)) print(json.dumps(tone_analyzer.synonym(words=['happy']), indent=2)) print(json.dumps(tone_analyzer.tone(text='Hello how are you?'), indent=2))
from watson_developer_cloud import ToneAnalyzerV2Experimental as ToneAnalyzer import os IBM_USERNAME = os.environ.get('IBM_USERNAME') IBM_PASSWORD = os.environ.get('IBM_PASSWORD') tone_analyzer = ToneAnalyzer(username=IBM_USERNAME, password=IBM_PASSWORD)
import sys import requests from channels import Group from bs4 import BeautifulSoup from watson_developer_cloud import LanguageTranslatorV2 as LanguageTranslator from watson_developer_cloud import ToneAnalyzerV3 as ToneAnalyzer from watson_developer_cloud import VisualRecognitionV3 as VisualRecognition language_translator = LanguageTranslator( username='******', password='******') visual_recognition = VisualRecognition( '2016-05-20', api_key='4beec9a677ee8140f62645d7dcb8d52bffbcd0c3') tone_analyzer = ToneAnalyzer(version='2016-05-19', username='******', password='******') def send_channel_message(room, label, channel_layer, message, context): data = {'handle': 'Watson', 'message': message} m = room.messages.create(**data) text = json.dumps(m.as_dict()) room.context = json.dumps(context) room.save() Group('chat-' + label, channel_layer=channel_layer).send({'text': text}) def send_message_to_watson(room, conversation, channel_layer, command, label, context): print("context:\n" + json.dumps(context, indent=2))
import json, os from os.path import join, dirname from watson_developer_cloud import ToneAnalyzerV3 as ToneAnalyzer from watson_developer_cloud import LanguageTranslatorV2 as LanguageTranslator from dotenv import load_dotenv # load service credentials from .env file load_dotenv(os.path.join(os.path.dirname(__file__), "crd.env")) ta_username = os.environ.get("TA_USERNAME") ta_password = os.environ.get("TA_PASSWORD") # initialize watson service tone_analyzer = ToneAnalyzer(username=ta_username, password=ta_password, version='2016-05-19') tr_username = os.environ.get("TR_USERNAME") tr_password = os.environ.get("TR_PASSWORD") language_translator = LanguageTranslator(username=tr_username, password=tr_password) def get_language_translator_results(data): ''' translates results i.e. tweets from Arabic to English. Note if tweets are already in English, this function takes no effect ''' return language_translator.translate(text=data, source='ar', target='en') def get_tone_analyser_results(data):
# -*- coding: utf-8 -*- import json from watson_developer_cloud import ToneAnalyzerV3Beta as ToneAnalyzer import pymongo from Config_Utils.config import config WATSON_KEY = config.get_environment_variable('WATSON_KEY') WATSON_PASSWORD = config.get_environment_variable('WATSON_PASSWORD') # create Bluemix account to get service_username and service_password tone_analyzer = ToneAnalyzer(username=WATSON_KEY, password=WATSON_PASSWORD, version='2016-02-11') def get_tweets_from_db(): db = pymongo.MongoClient().tweets cursor = db.tweets.find({}) return cursor def get_sentiment_from_watson(cursor): # selecting 100 tweets cursor = cursor[:100] for document in cursor: tweet = document['text'] sentimentData = tone_analyzer.tone(text=tweet) anger = sentimentData["document_tone"][ "tone_categories"][0]["tones"][0]["score"]