JIANWEI LI
Data Mining the City
2 min readDec 23, 2017

--

Generate a sentence with icons and flags of countries

import json
#import subprocess ( This library is no working in Windows)
def setup():
global sentence, hexColor, nounIcon,flag
size(1000,200)

with open('passages.json') as file:
passageData = json.load(file)
passages = passageData["passages"]
randomNumPassage = int(random(0, len(passages)))
passage = passages[randomNumPassage]

with open('countries.json') as file:
flagcountriesData = json.load(file)
# print flagcountriesData

# flagcountries = countriesData["countries"]
# flagandomNumCountries = int(random(0, len(flagcountries)))
# country = countries[randomNumPassage]

with open('countriesnam.json') as file:
countriesData = json.load(file)
countries = countriesData["countries"]
randomNumCountries = int(random(0, len(countries)))
country = countries[randomNumPassage]

# mydict = {'george':16,'amber':19}
# print mydict.keys()[mydict.values().index(16)]

countryId = flagcountriesData.keys()[flagcountriesData.values().index(country)]
id = countryId.lower()+".png"

flag = loadImage(id)


# "crayola" will generate color from text
with open('crayola.json') as file:
crayolaData = json.load(file)
numColors = len(crayolaData["colors"])
randomColor = int(random(0, numColors))
hexColor = crayolaData["colors"][randomColor]["hex"]
crayola = crayolaData["colors"][randomColor]["color"]
with open('roomsandicons.json') as file:
roomsData = json.load(file)
roomsDict = roomsData["rooms"]
roomKeys = roomsDict.keys()
roomIndex = int(random(0, len(roomKeys)))
room = roomKeys[roomIndex]
roomUrlId = roomsDict[room]
url = 'https://d30y9cdsu7xlg0.cloudfront.net/png/'"%s"'-84.png' % roomUrlId
nounIcon = loadImage(url)
sentence = \
" design a " + str(crayola) +" " + passage + " for a " + room + " in "+ country + "."

def draw():
hexColorS = hexColor.lstrip('#')
backgroundColor = tuple(int(hexColorS[i:i+2], 16) for i in (0, 2 ,4))

background(*backgroundColor)
fill(0)
textSize(30)
text(sentence,50,100)
image(nounIcon, width-100, height-100)
image(flag, 50, height-80)
# not working in Window?
#if frameCount == 2:
# result = subprocess.check_output(['say', '"%s"' % sentence])

--

--