Added is_first field to database to make sure markov phrases start naturally.

This commit is contained in:
Jeremy Fincher 2003-04-11 19:50:41 +00:00
parent 78b44e7a1e
commit fd4c20c258
1 changed files with 12 additions and 3 deletions

View File

@ -68,6 +68,7 @@ class Markov(callbacks.Privmsg, ChannelDBHandler):
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
first TEXT, first TEXT,
second TEXT, second TEXT,
is_first BOOLEAN,
UNIQUE (first, second) ON CONFLICT IGNORE UNIQUE (first, second) ON CONFLICT IGNORE
)""") )""")
cursor.execute("""CREATE TABLE follows ( cursor.execute("""CREATE TABLE follows (
@ -86,15 +87,22 @@ class Markov(callbacks.Privmsg, ChannelDBHandler):
db = self.getDb(channel) db = self.getDb(channel)
cursor = db.cursor() cursor = db.cursor()
words = msg.args[1].split() words = msg.args[1].split()
isFirst = True
for (first, second, follower) in window(words, 3): for (first, second, follower) in window(words, 3):
cursor.execute("""INSERT INTO pairs VALUES (NULL, %s, %s)""", if isFirst:
first, second) cursor.execute("""INSERT OR REPLACE
INTO pairs VALUES (NULL, %s, %s, 1)""",
first, second)
isFirst = False
else:
cursor.execute("INSERT INTO pairs VALUES (NULL, %s, %s, 0)",
first, second)
cursor.execute("""SELECT id FROM pairs cursor.execute("""SELECT id FROM pairs
WHERE first=%s AND second=%s""", first, second) WHERE first=%s AND second=%s""", first, second)
id = int(cursor.fetchone()[0]) id = int(cursor.fetchone()[0])
cursor.execute("""INSERT INTO follows VALUES (NULL, %s, %s)""", cursor.execute("""INSERT INTO follows VALUES (NULL, %s, %s)""",
id, follower) id, follower)
cursor.execute("""INSERT INTO pairs VALUES (NULL, %s, %s)""", cursor.execute("""INSERT INTO pairs VALUES (NULL, %s, %s, 0)""",
second, follower) second, follower)
cursor.execute("""SELECT id FROM pairs cursor.execute("""SELECT id FROM pairs
WHERE first=%s AND second=%s""", second, follower) WHERE first=%s AND second=%s""", second, follower)
@ -116,6 +124,7 @@ class Markov(callbacks.Privmsg, ChannelDBHandler):
cursor = db.cursor() cursor = db.cursor()
words = [] words = []
cursor.execute("""SELECT id, first, second FROM pairs cursor.execute("""SELECT id, first, second FROM pairs
WHERE is_first=1
ORDER BY random() ORDER BY random()
LIMIT 1""") LIMIT 1""")
(id, first, second) = cursor.fetchone() (id, first, second) = cursor.fetchone()