Skip to content

Instantly share code, notes, and snippets.

@RemyPorter
Last active July 8, 2021 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RemyPorter/2956f334cf66af322445330484eda4be to your computer and use it in GitHub Desktop.
Save RemyPorter/2956f334cf66af322445330484eda4be to your computer and use it in GitHub Desktop.
Inspired by playing around with attractors (https://github.com/RemyPorter/AttractiveToy), I decided to map that onto a wildly different vector space- language. This is a "fractal" poetry generator, which leaps through vector space to construct its poetry
"""
This uses the chaos-game algorithm to generate a sierpinski-gasket type pattern, but applies that pattern to wordspace.
Each word is a vector (in `-f`ull mode, that's 300 dimensions). Starting with a random position in that space,
this randomly moves the tracepoint towards one of your targets, based on the `-l`erp distance. Rinse, repeat.
The result is strangely structured, but also grammarless output.
You'll need to pip install gensim and numpy
"""
import gensim.downloader as api
import numpy as np
from random import choice, random
from argparse import ArgumentParser
parser = ArgumentParser("Attractive Poetry Generator")
parser.add_argument("seeds", type=str, help='Quoted list of seed words, like: "zebra interstellar lonely"')
parser.add_argument("n", nargs="?", default=100, type=int, help="number of words to generate")
parser.add_argument("-f", action="store_const", const=True, default=False, help="activate 'full' mode (300 dimensions), which is slower but richer")
parser.add_argument("-l", nargs="?", default=0.5, type=float, help="Lerp distance between vectors")
parser.add_argument("-p", nargs="?", default=0.25, type=float, help="Paragraph split probability")
args = parser.parse_args()
if (args.f):
SIZE=300 # 300 for real work, 50 for testing
else:
SIZE=50
LERP = args.l
PARA = 1.0 - args.p
vsm = api.load(f"glove-wiki-gigaword-{SIZE}")
seeds = args.seeds.split()
trace_vec = (np.random.random((SIZE,)) * 2.0 - 1.0).astype(np.float32)
trace_point = choice(vsm.similar_by_vector(trace_vec))
for i in range(args.n):
target = choice(seeds)
target_vec = vsm.get_vector(target)
trace_vec = (target_vec - trace_vec) * LERP + trace_vec
trace_point = choice(vsm.similar_by_vector(trace_vec))
print(trace_point[0], end=" ")
if random() > PARA:
print(" ")
fact ornamental
compassion saguaro cactus pear saguaro cacti saguaro humility saguaro appreciate aware humility strangers goodness kindness
cacti mesquite cactuses thoughtfulness kindness saguaro goodness succulents appreciate mesquite mesquite peyote prickly
pear thoughtfulness appreciate knowing peyote
cactuses
know prickly cactus shrubs compassion honesty compassion
affection saguaro thoughtfulness pear
affection gentleness
selflessness generosity empathy succulents prickly obviously mesquite
generosity honesty kindness always obviously prickly understand compassion pear
kindness
appreciate
saguaro cacti peyote obviously
obviously humility
prickly cactus cactuses gentleness kindness
honesty
affection knowing understand humility fact affection honesty
appreciate know prickly
obviously empathy
kindness understand
understood kindness unaware aware
crossing best
way bread sausages
well journeys roast journey trip
sausages
arduous trip
journeys perilous journey meat you
winning bacon journeys
good way way
winning work quest ever
pork cooked well way ever bacon cheese voyage
roast cheese cooked
meat excellent
time winning cheese best
gravy favorite
journey travels cheese journey
chicken sausage cheese best best cooked sausage voyage
you time
time
good adventure winning best
travels gravy better chorizo ride one
roast
adventure perilous arduous well
travels way
better
life
racing riding horse ride cavalry electric
pack battery
components lithium ingredient
sheep incorrect battery rechargeable bread repeating rechargeable
food correct race breeders animal correct
light
carriage thoroughbred
fix batteries
fix
charge
batteries
supply
thoroughbred mainstay thoroughbred rode wrong favorite consistent
wrong horse meat appropriate
thoroughbred
horse
charge batteries
anti-aircraft recharge
charge rechargeable thoroughbred dog
incorrect item fodder
horses riding correct staple
incorrect corrected correcting device
ride variety corrected racing artillery imported ingredient sheep meat supply
# Generated with an -l of 1.5
waiting tenets unused basic emptied lithium-ion core beside
emptied beside fundamental
batteries
lithium values
recharging
batteries deserted anti-aircraft values focus core
cores filled abandoned unused fill vacant
lithium-ion powered
sitting
anti-aircraft competencies lithium principles
units vacant infantry core empty
vacant sitting battery
crowded room
fill recharge artillery unused emptied fundamental
emptied leaving fundamental key underlying
lithium-ion
vacant focus lithium-ion core fill volt
lithium
batteries
charge
sitting unused
filling component
main component
emptied
volt non unused component antiaircraft
curriculum
lithium-ion batteries
underlying filling filled
formula_17 infrared boredom
despair
hopelessness microwave universe
frustration
microwaves frustration refrigerator
infrared boredom ovens infinite desperation loneliness
sorrow hopelessness despair formula_4 finite infinite sorrow
cosmic desperation anguish radiation
formula_1
sadness
grief despair
microwave formula_1 oven
despair endless infinite oven
formula_4 infinite
microwave microwaves
boredom hopelessness sadness formula_3 infinite
formula_2 limitless frequencies microwave grief formula_2 endless integers
unbounded finite loneliness oven despair
antennas misery despair
microwaves
ovens
microwave universe
unbounded integers
despair sorrow
infinite frequency cosmic sadness despair grief sadness despair
finite loneliness
ovens
discrete grief unending
anguish limitless frequencies
antennas oven infinite despair sadness antennas
refrigerator cosmic infrared
microwave infinite microwave ovens antennas
frequency formula_1 hopelessness hopelessness unbounded formula_2 electromagnetic microwaves microwave formula_1 desperation formula_1 anguish
microwave
formula_2 loneliness sorrow
microwave
infrared refrigerator hopelessness anguish
sorrow
loneliness microwave refrigerator
infinite loneliness
ovens grief infinite endless hopelessness
antennas formula_1 electromagnetic boredom ovens
boredom hopelessness sadness
hopelessness loneliness infinite unbounded discrete anguish
mediocrity
gazing spit abyss disgrace mediocrity
fade cursing littering echoed reverberating echo rumbling reminiscent
scratching
cursing
disgrace obscurity eternity
obscurity fade
chewing
echoes chewing cursing
disgrace echoing biting resonated fades consigned obscurity irrelevance faintly
eternity
spit reverberating resonated spitting resonates yelling cursing
shoving butting spit chewing echoing butting butting
spitting littering echoes echoes mediocrity
ominous eternity spit cursing abyss obscurity fade spitting spits obscurity
faint echo
shadows cursing resonated resonates fades irrelevance spit darkness ominous
obscurity abyss darkness irrelevance nostalgia littering butting faintly echoes cursing
reverberating
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment