Git Product home page Git Product logo

philipp-eisen / alchemyapi-recipes-twitter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from alchemyapi/alchemyapi-recipes-twitter

0.0 1.0 0.0 194 KB

A simple example application that will connect to the Twitter API, run a search, gather tweets, and then calculate the sentiment of each Tweet using AlchemyAPI's text analysis functions for sentiment analysis.

License: Apache License 2.0

Python 83.13% R 16.87%

alchemyapi-recipes-twitter's Introduction

alchemyapi-recipes-twitter

Welcome to the AlchemyAPI Twitter analysis recipe!

For the walkthrough on how this software works, visit: http://www.alchemyapi.com/developers/getting-started-guide/twitter-sentiment-analysis/

In short, this recipe will:

  • Connect to Twitter's API
  • Run a search and gather Tweets
  • Enrich Tweets with sentiment analysis of the message body
  • Store data in a MongoDB instance
  • Visualize sentiment of the data

Related Links

Requirements

Quick start for users running Ubuntu 14.04

Here is a simple set of commands that will ensure you have all the necessary tools and modules needed to successfully run this recipe:

sudo easy_install pymongo datetime
sudo apt-get update
sudo apt-get install r-base-dev
sudo R
R> install.packages("ggplot2")
R> <Ctrl-D>
sudo apt-get install mongodb-org
# Starting your MongoDB daemon
# Alternatively, run as root: service mongodb start
mkdir -p data/db
mongod --dbpath data/db --smallfiles --quiet &

STEP 1: Gather and enrich the data

To run this tool, simply call with a search term and an integer reflecting the number of Tweets to analyze:

python recipe.py "<SEARCH_TERM>" <NUM_TWEETS>
  • Credentials: You must have API credentials for both Twitter and AlchemyAPI to successfully run this tool. Edit the file credentials.py before executing recipe.py to prevent any errors. Inside the file credentials.py, there are three parameters. Replace each of these with your respective values to run the analysis smoothly:

    twitter_consumer_key='YOUR_TWITTER_CONSUMER_KEY' 
    twitter_consumer_secret='YOUR_TWITTER_CONSUMER_SECRET'
    alchemy_apikey='YOUR_ALCHEMY_API_KEY'
  • Dependencies (see above for installation tips):

STEP 2: Visualize the data

With your MongoDB instance created and filled, you now have access to well-tabulated results! We can proceed with some Python and R scripts to write results to disk and visualize them.

  • Write your data: Here, we will write two types of files: one containing the raw scores of sentiment for each Tweet, and one containing timestamps for each Tweet. We will do this separately for positive and negative Tweets. To write these files, just call:

    $> python write.py

    This will result in a total of 4 text files being written: scores.pos, scores.neg, times.pos, and times.neg.

  • Dependencies: This tool requires the pymongo Python module, as well as the datetime module (see above).

  • Visualize your data: Here, we want to make some plots showing the data we wrote out in the previous steps. To do this, simply invoke the included R script as follows:

    R < plot.R --vanilla

    This tool will write 3 image files to disk:

    • twitter_sentiment_raw.png: a histogram showing the positive and negative Tweet scores
    • twitter_sentiment_kernel.png: a kernel density function corresponding to the above histograms
    • twitter_sentiment_volume.png: a plot showing Tweet volume (separated by sentiment) as a function of the hour in a day
  • Dependencies:

    • You must have R installed on your system to run this tool.
    • You must also install the 'ggplot2' library for R (see above).

STEP 3: (OPTIONAL) Delete your data

From time to time, it's good to empty out your cache! If you want to wipe the twitter_db instance in MongoDB that was created in STEP 1, run the following command:

python delete.py

alchemyapi-recipes-twitter's People

Contributors

alchemyapi avatar phdharp avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.