def get_simple_sentiment(event, context): results = search(event['queryStringParameters']) summary = summarize(results) output = [] for tweet in results: clean = { 'id_str': tweet['id_str'], 'user_screen_name': tweet['user']['screen_name'], 'user_verified': tweet['user']['verified'], 'text': tweet['text'], 'created_at': tweet['created_at'], 'afinn_sentiment': tweet['afinn_sentiment'], 'vader_sentiment_compound': tweet['vader_sentiment']['compound'], 'is_media': tweet['is_media'] } output.append(clean) return { 'statusCode': 200, 'body': json.dumps({ 'results': output, 'summary': summary, }), 'headers': { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Credentials": True }, }
def test_search_results_count(count): """ Test result count with different parameters. """ results = search({ 'q': 'seahawks', 'count': count, }) assert count == len(results)
def test_search_results_have_vader_sentiment_keys(): """ Test if sentiment key is being applied. """ results = search({ 'q': 'burdock', 'count': 1, }) sentiment_keys = sorted(results[0]['vader_sentiment'].keys()) expected_keys = ['compound', 'neg', 'neu', 'pos'] assert expected_keys == sentiment_keys
def get_sentiment(event, context): """ Get sentiment for a search term. """ results = search(event['queryStringParameters']) summary = summarize(results) return { 'statusCode': 200, 'body': json.dumps({ 'results': results, 'summary': summary, }), 'headers': { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Credentials": True }, }
def test_summarize(): results = search({ 'q': 'rosemary', 'count': 100 }) summary = summarize(results)
def test_search_results_have_afinn_score(): results = search({ 'q': 'zatar', 'count': 1, }) assert isinstance(results[0]['afinn_sentiment'], float)