-
Notifications
You must be signed in to change notification settings - Fork 0
/
words.py
45 lines (40 loc) · 1.3 KB
/
words.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from random_words import RandomWords
import unirest
import json
import time
import pprint
import pickle
randList = []
rw = RandomWords()
word_to_array = {}
def love():
global word_to_array
for x in xrange(0,10):
random_word = rw.random_word()
while random_word in word_to_array:
random_word = rw.random_word()
response = unirest.get("https://twinword-word-associations-v1.p.mashape.com/associations/?entry=" + random_word,headers={"X-Mashape-Key": "ipLwvszUNbmshWjvlVeG6uzWEFFnp1uZ62pjsn5Ot3dfqkzY0Z"})
body_info = response.body
#print body_info["associations_array"]
wordass_array = body_info.get("associations_array")
word_to_array[random_word] = wordass_array
time.sleep(.01)
def clean_data():
for key in word_to_array.keys():
if word_to_array.get(key) == None:
del word_to_array[key]
print "deleted: " + key
def pickleit():
global word_to_array
pkl_file = open('word.pkl', 'rb')
word_to_array = pickle.load(pkl_file)
pkl_file.close()
if type(word_to_array) is list:
word_to_array = {}
love()
clean_data()
outputfile = open('word.pkl', 'wb')
pickle.dump(word_to_array, outputfile)
outputfile.close()
#pprint.pprint(len(word_to_array.keys()))
pickleit()