RWET Assignment 5 & 7

I combined these two assignments together (API and function).

import urllib
import json
import sys
import random
kWords=[]

for line in sys.stdin:
line = line.strip()
words = line.split(” “)
for word in words:
if len(word)>=1:
word=word.lower()
kWords.append(word)
api_key = “a2a73e7b926c924fad7001ca3111acd55af2ffabf50eb4ae5”
for kWord in kWords:
# print kWord
# print type(kWord)

url = “http://api.wordnik.com:80/v4/word.json/”+kWord+”/relatedWords?useCanonical=true&relationshipTypes=synonym&limitPerRelationshipType=10&api_key=”+api_key
doc_str = urllib.urlopen(url).read()
doc_data = json.loads(doc_str)

try:
synonyms=doc_data[0][“words”]
synonym=random.choice(synonyms)
except IndexError:
synonym=”universal”

# try:
# synonyms=doc_data[0][“words”]
# synonym=random.choice(synonyms)
# except IndexError:
# print “error”
# synonym=”universal”

# synonyms=doc_data[0][“words”]
# synonym=random.choice(synonyms)
print synonym

So the course description is changed to be like this:

Leave a Reply

Your email address will not be published. Required fields are marked *