mercredi 22 mars 2017

Trouble with random choice from values within specific key in dictionary

I'm attempting to make a rudimentary random story generator using alice in wonderland as a base text. I've made a dictionary, x that contains either bigrams or trigrams (two-tupled or three-tupled consecutive words) a with a list as the value that contains all words that appear in the test that follow that chain of words.

As I build the list, my script should be appending a random selection from the list of values associated with the keys that were previously appended.

eg.
[] -add random words from text.
['then', 'they'] - now find tuple a of ('then', 'they') in x
add random selection from list x[a] to list.
['then', 'they', 'walked'] - new tuple a is ('they', 'walked')...

I've used a lambda and filter to remove the punctuation to just see if I can complete the script (I said rudimentary - haha).

Problem is I keep getting TypeError: unhashable type: 'list'

As far as I can tell - from http://ift.tt/2o6oTuo I should be doing it correctly.

What gives?

b = filter(lambda a: a not in "';,.!?", random.choice(x[a])

*edited for clarity




Aucun commentaire:

Enregistrer un commentaire