def extract_sentiment(lyrics): alchemy = AlchemyAPI() response = alchemy.sentiment('text', lyrics) if 'docSentiment' not in response.keys(): return 0.0 if 'score' in response['docSentiment'].keys(): return float(response['docSentiment']['score']) return 0.0
def get_keywords(documents, text_accessor=lambda x: x): alchemy_api = AlchemyAPI() keywords = [] for document in documents: response = alchemy_api.keywords('text', text_accessor(document), {'sentiment': 1}) if response['status'] == 'OK': keywords.append(set(map(lambda x: x['text'], response['keywords']))) else: keywords.append(set()) print('Error in keyword extraction call') return keywords
def tag_list(image_url): alchemyapi = AlchemyAPI() alchemy_json = alchemyapi.imageTagging("url", image_url) try: image_keywords = alchemy_json["imageKeywords"] except KeyError: return None else: result_list = { image_keyword["text"]: float(image_keyword["score"]) for image_keyword in image_keywords } return result_list
def main(): from alchemyapi.alchemyapi import AlchemyAPI # Extract text from a webpage for analysis url = 'http://www.bloombergview.com/articles/2014-09-26/the-secret-goldman-sachs-tapes' article = Webpage(url) text = article.get_text() # Extract keywords alchemyapi = AlchemyAPI() output = alchemyapi.keywords('text', text) keywords = parse_keyword_output(output) keywords.sort(reverse=True) # Print to console print('\nThe URL used for this assignment is:\n\n%s\n' % url) print_keywords(keywords[:10])
class AlchemyApiService(object): def __init__(self): self.alchemy_api = AlchemyAPI() def get_keywords(self, text): # TODO(simplyfaisal): Refine error handling logic. try: response = self.alchemy_api.keywords('text', text, {'sentiment': 1}) if response['status'] == 'OK': return response['keywords'] else: print ('Error in keyword extraction') print response except Exception as e: print e return []
print(str(divText.div.strong.prettify) + " AUTHOR NAME TEST") print(str(divText.div.blockquote.prettify) + " QUOTE TEXT TEST") def build_quotes(TextSoup): divText = TextSoup.find("div", {"id": "main_body"} , {"class": "right"}) Allauthors = [] Allquotes = [] for tag in divText: author = tag.find('strong') if author not in (-1, None): Allauthors.append(author.contents) quotes = tag.find('blockquote') if quotes not in (-1, None): quote = quotes.findAll('p', text = True) Allquotes.append(quote) print(str(Allauthors[0]) + str(Allquotes[0]) + "FIRST QUOTE - RESULTS TEST") return(Allquotes) Allquotes = build_quotes(soup) alchemyapi = AlchemyAPI() response = alchemyapi.keywords('text', Allquotes) keywords = response.values() keywords = keywords[2] print(str(keywords[0]) + " KEYWORD SLICE CHECK") df = pd.DataFrame(keywords) df.index = df.index + 1 print df[0:10]
from alchemyapi.alchemyapi import AlchemyAPI from json import load, dump import time songs = load(open('lyrics.json')) alchemyapi = AlchemyAPI() keywords = {} for song in songs: response = alchemyapi.keywords('text', songs[song]) for k in [ c['text'] for c in response['keywords'] ]: if k not in keywords: keywords[k] = [] keywords[k].append(song) dump(keywords, open('keywords.json', 'w'))
__author__ = 'Yingqi' import re import json import pandas as pd from collections import defaultdict from alchemyapi.alchemyapi import AlchemyAPI alchemyapi = AlchemyAPI() code = {'AL': '01', 'AZ': '04', 'AR': '05', 'CA': '06', 'CO': '08', 'CT': '09', 'DC': '11', 'FL': '12', 'GA': '13', 'ID': '16', 'IL': '17', 'IN': '18', 'IA': '19', 'KS': '20', 'KY': '21', 'LA': '22', 'ME': '23', 'MD': '24', 'MA': '25', 'MI': '26', 'MN': '27', 'MS': '28', 'MO': '29', 'MT': '30', 'NE': '31', 'NV': '32', 'NJ': '34', 'NH': '33', 'NM': '35', 'NY': '36', 'DE': '10', 'NC': '37', 'ND': '38', 'OH': '39', 'OK': '40', 'OR': '41', 'PA': '42', 'RI': '44', 'SC': '45', 'SD': '46', 'TN': '47', 'TX': '48', 'UT': '49', 'VT': '50', 'VA': '51', 'WA': '53', 'WV': '54', 'WI': '55', 'WY': '56' } mask = defaultdict(float, [(i,1) for i in code.values()]) print(mask) def word_in_text(word, text): word = word.lower() text = text.lower() match = re.search(word, text) if match: return True return False tweet_path = 'health.txt' tweet_file = open(tweet_path, 'r') tweet_data = [] i = 0 for line in tweet_file:
from alchemyapi.alchemyapi import AlchemyAPI from json import load, dumps from collections import Counter from sys import stdout, exit import feedparser RSS_URL = 'http://feeds.bbci.co.uk/news/technology/rss.xml' #RSS_URL = 'http://feeds.bbci.co.uk/news/science_and_environment/rss.xml' keywords = load(open('keywords.json')) d = feedparser.parse(RSS_URL) alchemyapi = AlchemyAPI() songs = Counter() print ' Analysing: %s ' % (d['feed']['subtitle']) count = len(d['entries']) i = 0.0 stdout.write(' Working: 0%') for t in d['entries']: i += 1 title = t['title'] url = t['link'] if 'VIDEO:' not in title and 'AUDIO:' not in title: response = alchemyapi.keywords('url', url) if response['status'] == 'OK':
def __init__(self): self.alchemy_api = AlchemyAPI()