Follow

and now a nano-NaNoGenMo entry cc @nickmofo

python -c "from collections import defaultdict as d;import sys,random;w=sys.stdin.read().split(' ');x=list(zip(w[:-2],w[1:-1],w[2:]));y=d(list);[y[j,l].append(k) for j,k,l in x];print(' '.join(w[:2]+[random.choice(y[a,c])for a,b,c in x[1:]]))" <pg2489.txt

replaces words in stdin at random with other words occurring in the same 1-word context window—sort of a minimalist word vector similarity replacement

sample output: gist.github.com/aparrish/dee20

· · Web · 4 · 5 · 12

@er1n In this case it looks to me like @aparrish means something like, if the text has phrases like "a fine plan" and "a strange plan", then any use of "a […] plan" could have the middle word replaced by either "fine" or "strange" at random. So it's the context a word appears in, where context is determined by looking one word before and one word after.

@jamey @aparrish so scan whole text, find all contexts, and then swap between randomly

@er1n @aparrish yeah, that's my reading of that Python anyway. although it's not precisely "swapping" since each instance of a context can be replaced with any other instance, including itself; in very unlikely circumstances all instances could be replaced by the same word every time.

@aparrish btw this is an astonishingly compact implementation and I think it's super cool that you can do interesting generative text so succinctly! (cc @er1n)

@aparrish @nickmofo

*bleats helplessly at that for about ten straight minutes in pure envy*

@aparrish Beautiful! I don't want to share *all* of my explication and ruin the fun, but in case it piques other people's interest, here's some of it:

from collections import defaultdict
import sys
import random
words = sys.stdin.read().split(' ')
word_triples = list(zip(words[:-2], words[1:-1], words[2:]))
context_dict = defaultdict(list)
for (j, k, l) in word_triples:
context_dict[j,l].append(k)
...

@aparrish My favorite part of the code is the w[:2] to ensure the output begins with "**The Project"

Sign in to participate in the conversation
Friend Camp

Hometown is adapted from Mastodon, a decentralized social network with no ads, no corporate surveillance, and ethical design.