Created
July 3, 2014 16:07
-
-
Save repustate/ca80d33fc3092e1109c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: UTF-8 -*- | |
""" | |
Combining the Repustate API with the SurveyMonkey API to understand open ended | |
responses. | |
""" | |
import requests | |
import json | |
SM_HOST = 'https://api.surveymonkey.net' | |
# This token is the one you get after doing the OAUTH 2 handshake. | |
SM_TOKEN = 'FOO_TOKEN' | |
# Get this key after signing up with SurveyMonkey. | |
SM_API_KEY = 'SURVEYMONKEY_API_KEY' | |
# Sign up at https://www.repustate.com to get a free API key. | |
REPUSTATE_API_KEY = 'REPUSTATE_API_KEY' | |
def chunks(a_list, N): | |
""" | |
Create evenly sized chunks of a list. | |
""" | |
for i in xrange(0, len(a_list), N): | |
yield a_list[i:i+N] | |
def run(survey_id, question_ids): | |
client = requests.session() | |
client.headers = { | |
"Authorization": "bearer %s" % SM_TOKEN, | |
"Content-Type": "application/json" | |
} | |
post_args = { | |
'survey_id':SM_SURVEY_ID, | |
'page':1 # defaults to the first 1000 respondents. | |
# NB: If you have lots of respondents, you'll have to page through them by | |
# passing increasing values for N. | |
} | |
respondent_data = client.post(SM_HOST + '/v2/surveys/get_respondent_list?api_key=%s' % SM_API_KEY, post_args).json() | |
# Get the individual respondent_ids. | |
respondent_ids = [] | |
for respondent in respondent_data['data']['respondents']: | |
respondent_ids.append(respondent['respondent_id']) | |
# Now get our responses for each of our respondents. The SurveyMonkey API let's | |
# you get at most 100 at a time, so you'll have to page through your list of IDs. | |
for groups_of_ids in chunks(respondent_ids, 100): | |
post_args = { | |
'survey_id':survey_id, | |
'respondent_ids':groups_of_ids | |
} | |
response_data = client.post(SM_HOST + '/v2/surveys/get_responses?api_key=%s' % SM_API_KEY, post_args).json() | |
for response_set in response_data['data']: | |
# Get the ID for this person. | |
respondent_id = response_set['respondent_id'] | |
for question in response_set['questions']: | |
# Loop over each question, looking for the ones that match the | |
# IDs were interested in. When we find them, extract the open | |
# ended response and then send to Repustate for analysis. | |
if question['question_id'] in question_ids: | |
# Take a look at how answers are formatted: | |
# https://developer.surveymonkey.com/mashery/get_responses | |
answer_text = question['answers'][0]['text'] | |
# Here you can persist the text to your database or | |
# whatever, but we'll analyze it real-time. | |
repustate_response = requests.post( | |
'https://api.repustate.com/v2/%s/named_entities.json' % REPUSTATE_API_KEY, {'text':text} | |
).json() | |
entities = repustate_response['entities'] | |
# Now do what you will with the entities. Run them through | |
# Repustate's topical sentiment analysis, or generate the | |
# n-grams or adjectives surrounding this term. Lots of | |
# possibilities here. | |
if __name__ == '__main__': | |
# The survey we want to analyze. | |
# We know the survey ID we want, but if you didn't, you can access your surveys via this API call: | |
# surveys = client.post(SM_HOST+'/v2/surveys/get_survey_list', params).json() | |
survey_id = 'YOUR_SURVEY_ID' | |
# These would be the IDs of the open ended questions were interested in. | |
question_ids = set(['1', '2', '3', '4']) | |
# Run it! | |
run(survey_id, question_ids) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment