Mercurial > hg > wordstream
diff wordstream/api.py @ 2:df84e61ae1e4
add dissociation
author | k0s <k0scist@gmail.com> |
---|---|
date | Fri, 12 Feb 2010 00:38:25 -0500 |
parents | 8af3412e907a |
children | bad7e66f4f24 |
line wrap: on
line diff
--- a/wordstream/api.py Fri Jan 01 20:01:02 2010 -0500 +++ b/wordstream/api.py Fri Feb 12 00:38:25 2010 -0500 @@ -1,4 +1,5 @@ import urllib2 +from random import shuffle class Corpus(dict): @@ -9,8 +10,11 @@ self.setdefault(word, []).append(association) def eat(self, word): - if word in self and self[word]: - return self[word].pop() + if word in self: + if self[word]: + return self[word].pop() + else: + del self[word] def feed_stream(self, stream): if isinstance(stream, basestring): @@ -27,6 +31,10 @@ text = file(arg).read() self.feed_stream(text) + def scramble(self): + for i in self: + shuffle(self[i]) + def save(self, filename): named = False if isinstance(f, basestring):