Commit e763aa15 authored by manetta's avatar manetta

adding the situation embeddings exercise

parent 8e1eae3b
# Table embeddings
# written during Algolit
# 13 April 2018
# Notes: https://pad.constantvzw.org/p/130418_algolit_word2vec2
import os
from collections import Counter
import pprint
import random
pp = pprint.PrettyPrinter(indent=4)
filename = 'embeddings.train'
text = open('{}.txt'.format(filename),'r').readlines()
# [['empty, bankcard, pen holder\n'],
# ['bankcard, pen holder, notebook\n'],
# ['pen holder, notebook, keys\n']]
main = {}
# main = {
# 'human' {
# Counter({
# 'cup' : 1,
# 'laptop' : 1,
# })
# }
# }
for ngram in text:
ngram = ngram.split(', ')
center = ngram[1]
left = ngram[0]
right = ngram[2].replace('\n','')
if not center in main:
main[center] = Counter()
main[center][left] +=1
main[center][right] +=1
pp.pprint(main)
print('************')
numbers = {1 : 'one', 2 : 'two', 3 : 'three', 4 : 'four', 5 : 'five', 6 : 'six', 7 : 'seven', 8 : 'eight', 9 : 'nine', 10 : 'ten'}
determiners = {3 : 'a few', 5 :'many', 7: 'a lot of', 6 : 'most', 2 : 'some', 1 : 'any', 4 : 'enough'}
sentences = []
x = 0
for cw, c in main.items():
x += 1
wwords = []
wwordsandcounts = []
wwordsanddeterminers = []
for word, count in c.items():
wwords.append(word)
if count > 1:
word = word+'s'
determiner = determiners[count]
wwordsandcounts.append(numbers[count] + ' ' + word)
wwordsanddeterminers.append(determiner + ' ' + word)
intro = ''
templates = [
'What makes a {0} a {0}, is its closeness to {1} and {2}.'.format(cw, ', '.join(wwordsanddeterminers[:-1]), wwordsanddeterminers[-1]),
'You can identify a {} when it appears in the company of {} and {}.'.format(cw, ', '.join(wwordsandcounts[:-1]), wwordsandcounts[-1]),
'A {} can be recognized when it lies next to a {} and a {}.'.format(cw, ', '.join(wwords[:-1]), wwords[-1])
]
if x == 5:
sentences.append('\nMeanwhile ...\n')
x = 0
s = random.choice(templates)
sentences.append(s)
for sentence in sentences:
print(sentence)
head = '''<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body{
position:relative;
width:700px;
margin:3em auto;
}
.meanwhile{
margin: 2em 0 2em 1em;
}
img{
width:50%;
}
.left{
float:left;
margin:0 2em 1em -2em;
}
.right{
float:right;
margin:0 -2em 1em 2em;
}
</style>
</head>
<body>
<h1>situation embeddings</h1>
'''
foot = '''</body>
</html>
'''
images = [f for f in os.listdir('./img/{}'.format(filename))]
with open('{}.html'.format(filename),'w') as html:
html.write(head)
html.write('<img src="./img/{}/{}" class="right">'.format(filename, random.choice(images)))
i = 0
for sentence in sentences:
if 'Meanwhile' in sentence:
html.write('<p class="meanwhile">'+sentence+'</p>')
print(i, i % 2)
if i % 2 == 0:
html.write('<img src="./img/{}/{}" class="left">'.format(filename, random.choice(images)))
else:
html.write('<img src="./img/{}/{}" class="right">'.format(filename, random.choice(images)))
else:
html.write('<p>'+sentence+'</p>')
i += 1
html.write(foot)
print('*html file written*')
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body{
position:relative;
width:700px;
margin:3em auto;
}
.meanwhile{
margin: 2em 0 2em 1em;
}
img{
width:50%;
}
.left{
float:left;
margin:0 2em 1em -2em;
}
.right{
float:right;
margin:0 -2em 1em 2em;
}
</style>
</head>
<body>
<h1>situation embeddings</h1>
<img src="./img/embeddings.table/DSC_0889.JPG" class="right"><p>You can identify a hand when it appears in the company of one charger and one keys.</p><p>A cup can be recognized when it lies next to a charger, mouse, laptop, notebook, phone and a human.</p><p>What makes a box a box, is its closeness to any charger and any laptop.</p><p>You can identify a keys when it appears in the company of one hand, one notebook, one bottle and one extension block.</p><p class="meanwhile">
Meanwhile ...
</p><img src="./img/embeddings.table/DSC_0883.JPG" class="left"><p>What makes a pen holder a pen holder, is its closeness to any bankcard and any notebook.</p><p>You can identify a lego piece when it appears in the company of one laptop and one extension block.</p><p>A bowl can be recognized when it lies next to a sheet and a human.</p><p>A plate can be recognized when it lies next to a cable and a laptop.</p><p>What makes a notebook a notebook, is its closeness to any cup, any keys, any pen holder and any human.</p><p class="meanwhile">
Meanwhile ...
</p><img src="./img/embeddings.table/DSC_0879.JPG" class="right"><p>You can identify a extension block when it appears in the company of one lego piece and one keys.</p><p>What makes a charger a charger, is its closeness to any hand, some cups, any sheet, any box, any bottle, any laptop and any speaker.</p><p>What makes a sheet a sheet, is its closeness to any bowl and any charger.</p><p>What makes a bottle a bottle, is its closeness to any charger and any keys.</p><p>You can identify a speaker when it appears in the company of one charger and one phone.</p><p class="meanwhile">
Meanwhile ...
</p><img src="./img/embeddings.table/DSC_0879.JPG" class="left"><p>A bankcard can be recognized when it lies next to a empty and a pen holder.</p><p>What makes a cable a cable, is its closeness to any plate and any laptop.</p><p>A laptop can be recognized when it lies next to a lego piece, cup, box, charger, laptop, plate, cable and a human.</p><p>What makes a human a human, is its closeness to any cup, any mouse, any bowl, a few laptops, any notebook, any phone and enough humans.</p><p>You can identify a phone when it appears in the company of one cup and one speaker.</p><p class="meanwhile">
Meanwhile ...
</p><img src="./img/embeddings.table/DSC_0871.JPG" class="right"><p>What makes a mouse a mouse, is its closeness to any cup and any human.</p></body>
</html>
empty, bankcard, pen holder
bankcard, pen holder, notebook
pen holder, notebook, keys
notebook, keys, hand
keys, hand, charger
hand, charger, box
charger, box, laptop
box, laptop, cup
laptop, cup, mouse
cup, mouse, human
mouse, human, laptop
human, laptop, plate
laptop, plate, cable
plate, cable, laptop
cable, laptop, human
laptop, human, bowl
human, bowl, sheet
bowl, sheet, charger
sheet, charger, bottle
charger, bottle, keys
bottle, keys, extension block
keys, extension block, lego piece
extension block, lego piece, laptop
lego piece, laptop, human
laptop, human, human
human, human, notebook
human, notebook, cup
notebook, cup, phone
cup, phone, speaker
phone, speaker, charger
speaker, charger, cup
charger, cup, laptop
cup, laptop, charger
laptop, charger, cup
charger, cup, laptop
cup, laptop, laptop
laptop, laptop, cup
laptop, cup, human
cup, human, human
human, human, phone
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body{
position:relative;
width:700px;
margin:3em auto;
}
.meanwhile{
margin: 2em 0 2em 1em;
}
img{
width:50%;
}
.left{
float:left;
margin:0 2em 1em -2em;
}
.right{
float:right;
margin:0 -2em 1em 2em;
}
</style>
</head>
<body>
<h1>situation embeddings</h1>
<img src="./img/embeddings.train/DSC_0833.JPG" class="right"><p>What makes a human a human, is its closeness to any window and any chair.</p><p>You can identify a backpack when it appears in the company of one feet and one trash bin.</p><p>What makes a trash bin a trash bin, is its closeness to any backpack, some windows and any nothing.</p><p>A nothing can be recognized when it lies next to a feet, nothing, trash bin and a aisle.</p><p class="meanwhile">
Meanwhile ...
</p><img src="./img/embeddings.train/DSC_0834.JPG" class="left"><p>A feet can be recognized when it lies next to a backpack and a nothing.</p><p>What makes a window a window, is its closeness to any human, some trash bins and any chair.</p><p>You can identify a chair when it appears in the company of one human, two aisles and three chairs.</p><p>You can identify a aisle when it appears in the company of two nothings and two chairs.</p></body>
</html>
chair, chair, aisle
chair, aisle, chair
aisle, chair, chair
chair, chair, human
chair, human, window
human, window, trash bin
window, trash bin, backpack
trash bin, backpack, feet
backpack, feet, nothing
feet, nothing, aisle,
nothing, aisle, nothing
aisle, nothing, nothing
nothing, nothing, trash bin
nothing, trash bin, window
trash bin, window, chair
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment