Skip to content

Commit

Permalink
Fix psycopg2 syntax and PostgreSQL missing REPLACE
Browse files Browse the repository at this point in the history
Things were so easy with sqlite3...  PostgreSQL does things differently.

Signed-off-by: Stefan Hajnoczi <stefanha@gmail.com>
  • Loading branch information
stefanha committed Jul 28, 2012
1 parent e02bddf commit 5f64f7a
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions generator/generator.py
Expand Up @@ -67,14 +67,15 @@ def store_markov_chain(cursor, topic, mc):
for current in mc.chain:
bucket = mc.chain[current]
for next_ in bucket:
cursor.execute('INSERT OR REPLACE INTO markov_chains (topic_id, current, next, count, created_at, updated_at) VALUES (%s, %s, %s, %s, "", "")', (topic_id, current, next_, bucket[next_]))
cursor.execute('DELETE FROM markov_chains WHERE topic_id=%s AND current=%s AND next=%s', (topic_id, current, next_))
cursor.execute('INSERT INTO markov_chains (topic_id, current, next, count, created_at, updated_at) VALUES (%s, %s, %s, %s, NOW(), NOW())', (topic_id, current, next_, bucket[next_]))

def load_random_user(cursor, topic):
cursor.execute('SELECT sender FROM tweets WHERE topic_id=%s ORDER BY RANDOM() LIMIT 1', (load_topic_id(cursor, topic),))
return cursor.fetchone()[0]

def store_tweet(cursor, topic, sender, text, fake):
cursor.execute('INSERT INTO tweets (text, fake, topic_id, sender, created_at, updated_at) VALUES (%s, %s, %s, %s, "", "")', (text, fake, load_topic_id(cursor, topic), sender))
cursor.execute('INSERT INTO tweets (text, fake, topic_id, sender, created_at, updated_at) VALUES (%s, %s, %s, %s, NOW(), NOW())', (text, fake, load_topic_id(cursor, topic), sender))

class MarkovChain(object):
def __init__(self, cardinality):
Expand Down Expand Up @@ -130,10 +131,9 @@ def generate(self):
next_ = self._choose_next(self.chain[current])
current = ' '.join(current.split()[1:] + [next_])
if next_ == '@@start@@':
# Try again if we only generated one word. This happens when
# people start and end tweets with a hash tag, and therefore
# makes the algorithm generate just the hash tag.
if len(sentence.split()) == 1:
# Try again if we only generated one word or the sentence
# became too long.
if len(sentence.split()) == 1 or len(sentence) > 140:
current = self._choose_first_tuple()
sentence = ' '.join(current.split()[1:])
continue
Expand Down

0 comments on commit 5f64f7a

Please sign in to comment.