def get_text_sentiment(text): alchemy_api_key = os.environ.get("ALCHEMY_API_KEY") alchemy_language = AlchemyLanguage(api_key=alchemy_api_key) result = alchemy_language.sentiment(text=text) if result['docSentiment']['type'] == 'neutral': return 'netural', 0 return result['docSentiment']['type'], result['docSentiment']['score']
from watson_developer_cloud import AlchemyLanguageV1 from alchemy_sentiment import api_key as ak alchemy_language = AlchemyLanguageV1(api_key=ak) class Sentiment: def getSentiment(tweet): try: result = alchemy_language.sentiment(text=tweet) if result['status'] == 'OK': sentimentsList = [] for item in result['docSentiment'].items(): sentimentsList.append(item[0] + ": " + item[1]) return sentimentsList except Exception: return []
import json from os.path import join, dirname from watson_developer_cloud import AlchemyLanguageV1 alchemy_language = AlchemyLanguageV1(api_key='YOUR API KEY') url = 'https://developer.ibm.com/watson/blog/2015/11/03/price-reduction-for' \ '-watson-personality-insights/' print( json.dumps(alchemy_language.targeted_sentiment( text='I love cats! Dogs are smelly.', targets=['cats', 'dogs'], language='english'), indent=2)) # print(json.dumps(alchemy_language.targeted_emotion(text='I love apples. I # hate bananas', # targets=['apples', # 'bananas'], language='english'), indent=2)) # print(json.dumps(alchemy_language.author(url=url), indent=2)) # print(json.dumps(alchemy_language.concepts(max_items=2, url=url), indent=2)) # print(json.dumps(alchemy_language.dates(url=url, anchor_date='2016-03-22 # 00:00:00'), indent=2)) # print(json.dumps(alchemy_language.emotion(url=url), indent=2)) # print(json.dumps(alchemy_language.entities(url=url), indent=2)) # print(json.dumps(alchemy_language.keywords(max_items=5, url=url), indent=2)) # print(json.dumps(alchemy_language.category(url=url), indent=2)) # print(json.dumps(alchemy_language.typed_relations(url=url), indent=2)) # print(json.dumps(alchemy_language.relations(url=url), indent=2)) # print(json.dumps(alchemy_language.language(url=url), indent=2))
# Imports from watson_developer_cloud import AlchemyLanguageV1 from dotenv import load_dotenv, find_dotenv import json import os # Load .env load_dotenv(find_dotenv()) alchemy_language = AlchemyLanguageV1(api_key=os.environ.get("ALCHEMY_SERVICE_KEY")) # URL to pass url = 'https://developer.ibm.com/watson/blog/2015/11/03/price-reduction-for-watson-personality-insights/' # API Test combined_operations = ['page-image', 'entity', 'keyword', 'title', 'author', 'taxonomy', 'concept', 'doc-emotion'] print(json.dumps(alchemy_language.combined(url=url, extract=combined_operations), indent=2))
from flask import Flask, make_response app = Flask(__name__) # newsApi stuff newsApiKey = '793a5531cfaf45b6b6f9221a49c81a74' newsSources = ['bloomberg', 'bbc-news'] data = [] for source in newsSources: newsUrl = ('https://newsapi.org/v1/articles?source=%s&sortBy=top&apiKey=' % source) + newsApiKey response = urllib.urlopen(newsUrl) data.append(json.loads(response.read())) #alchelmyApi stuff from watson_developer_cloud import AlchemyLanguageV1 alchelmyKey = 'fbce84c5734fe5e587b438cedc8ce78fa76b3293' alchelmyApi = AlchemyLanguageV1(api_key = alchelmyKey) titles = [] ratings = [] counter = 0 #for sourceData in data: #for item in sourceData['articles']: #res = alchelmyApi.combined(text = item['title'] + '. ' + item['description'], extract='entities,keywords,doc-emotion', sentiment=1, max_items=1) @app.route('/headlines', methods=['GET']) def headlines(): resp = make_response('{"response": ''test''}') resp.headers['Content-Type'] = "application/json" return resp
import json from watson_developer_cloud import AlchemyLanguageV1 API_KEY = 'b4e9c20260c444b37c1a34a8265b04f4e17dce4d' alchemy_language = AlchemyLanguageV1(api_key=API_KEY) def getdata(tmsg): return json.dumps(alchemy_language.sentiment(text=tmsg), indent=2)
from flask import Flask, render_template, request, redirect, url_for import os import json from watson_developer_cloud import AlchemyLanguageV1 alchemy_language = AlchemyLanguageV1( api_key='7eb93ec8c9b1f828f3b6cc0798e7594a1b06dcc6') app = Flask(__name__) port = int(os.getenv('VCAP_APP_PORT', 8080)) @app.route('/') def index_load(): return render_template('index.html') def extract_concept(input_text): return (json.dumps(alchemy_language.concepts(text=input_text), indent=2)) @app.route('/CV/') def load_cv_form(): return render_template('name.html') @app.route('/CV/', methods=['POST']) def cv_form(): name = request.form['fullname'] cv = request.form['CV']
# -*- coding: utf-8 -*- import tweepy import json from watson_developer_cloud import AlchemyLanguageV1 from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from sentiment_declarative import TweetText, Image, Base alchemy_language = AlchemyLanguageV1(api_key='apikey') class TweepyAuth(object): """ Our TweepyAuth class that manages the creation of a tweepy.OAuthHandler object needed for the initialization of a tweepy.API instance. """ def __init__(self, consumer_key='consumerkey', consumer_secret='consumersecret', access_token='accesstoken', access_token_secret='accesssecret'): self.auth = tweepy.OAuthHandler(consumer_key, consumer_secret) self.auth.set_access_token(access_token, access_token_secret) class TweetHarvester(object):