Created
April 8, 2015 13:57
-
-
Save rlieberman/884cb9f1f16e833b5e2e 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
#RWET Assignment 3: Wordnik API Poems | |
#Rebecca Lieberman | |
#ITP Spring 2015 | |
import sys | |
import urllib | |
import json | |
word = sys.argv[1] | |
api_key = "8f1c6a85da698016e700d0eeb57066aeff88d029d09498010" | |
limit_num = random.randrange(2, 25) | |
url = "http://api.wordnik.com:80/v4/word.json/" + word + "/examples?includeDuplicates=false&useCanonical=false&skip=0&limit=" + str(limit_num) + "&api_key=" + api_key | |
response_str = urllib.urlopen(url).read() | |
response_dict = json.loads(response_str) | |
# #TEST JUST TO SEE WHAT RESOPONSE WE GET | |
# for item in response_dict['examples']: #print the examples | |
# each_example = item['text'] #convert each example into a list so we can slice it | |
# print each_example | |
# | |
for item in response_dict['examples']: #print the examples | |
each_example = item['text'].split() #convert each example into a list so we can slice it | |
if word in each_example: | |
loc = each_example.index(word) #find where the word occurs in the list | |
print " ".join(each_example[loc:loc+10]) | |
# THIS DIDN'T WORK WHEN I TRIED TO CONSTRUCT THE URL USING URL ENCODE -- DO I EVEN NEED TO? | |
# query = { | |
# "includeDuplicates": "false", | |
# "useCanonical": "false", | |
# "limit": "20", | |
# "api_key": "8f1c6a85da698016e700d0eeb57066aeff88d029d09498010" | |
# } | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment