Generate Spam-style English Atom Feed 0

In Brief Something I was playing with; turns out you can make fairly decent spam-email-style English easily. Parses any strings you give it and returns a gibberish response to any query.... more
# 's
 1import sqlalchemy as sa
2import sqlalchemy.orm as orm
3from sqlalchemy.sql import select, func
4from random import random
5import re
6
7engine = sa.create_engine('sqlite:///file.db')
8metadata = sa.MetaData()
9
10data_table = sa.Table('data', metadata,
11 sa.Column('id', sa.Integer, primary_key = True),
12 sa.Column('source', sa.String),
13 sa.Column('target', sa.String),
14 sa.Column('count', sa.Float))
15
16metadata.create_all(engine)
17
18# tables should now be init-ed
19
20class Datum(object):
21 def __init__(self, source, target, count=1):
22 self.set(source, target, count)
23
24 def set(self, source, target, count=1):
25 self.source = source
26 self.target = target
27 self.count = count
28
29orm.mapper(Datum, data_table)
30
31Session = orm.sessionmaker(bind=engine)
32session = Session()
33
34def read(string):
35
36 words = [w.strip() for w in re.split('\W+', string)]
37
38 for i, word in enumerate(words):
39 print '%i of %i' % (i, len(words))
40 #if i + 3 >= len(words): break
41 add_datapoint(word, words[i+1], 1)
42 #add_datapoint(word, words[i+2], 0.5)
43 #add_datapoint(word, words[i+3], 0.25)
44
45 session.commit()
46
47def add_datapoint(source, target, count):
48 obj = session.query(Datum).filter_by(source=source, target=target).first()
49 if obj:
50 obj.count += count
51 else:
52 session.add(Datum(source, target, count))
53
54def cli():
55
56 while(True):
57 input = raw_input('###>')
58 for i in xrange(100):
59 targets = session.query(Datum).filter_by(source=input).all()
60 if not targets: break
61 summ = sum(target.count for target in targets)
62 num = random() * summ
63 while num > 0:
64 target = targets.pop(0)
65 num -= target.count
66 print target.target,
67 input = target.target
68 print

Something I was playing with; turns out you can make fairly decent spam-email-style English easily. Parses any strings you give it and returns a gibberish response to any query.

Read in some strings (like news articles) with read() and then run cli(). Type a word and hit enter to see some good times.

For example, I read in two New York Times articles (about the economy and jobs and about Apolo Ohno winning a medal), ran cli(), and then typed:

###>the

And got this:

tranquility of the rink at a supermarket and Latin America Automation has applied everywhere she is to bronze Celski was so enraged that women from Ohno said What is supposed to find themselves in the last weekend and Ohno said Her 1994 Dodge Caravan broke down in his latest medal after medal count For the final turn Sung and he adds in 1990 and his face on public assistance for this week For the sold out of their hiring can Ohno has only way I m qualified for employers to 1 500 monthly mortgage with X rated material finding in

You'll need SQLAlchemy installed.