-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathothers-markov-n.py
73 lines (55 loc) · 1.72 KB
/
others-markov-n.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# -*- coding: utf-8 -*-
import sqlite3, random, sys
from friend import Friend,Message
conn = sqlite3.connect('messages.db')
c = conn.cursor()
allFriends = {}
for row in c.execute('SELECT text,handle.id,message.service,date,date_read,date_delivered,is_from_me FROM message INNER JOIN handle ON message.handle_id = handle.ROWID ORDER BY handle.ROWID,date'):
text = row[0]
handle = row[1]
service = row[2]
date = row[3]
dateRead = row[4]
dateDelivered = row[5]
isFromMe = row[6]
# allFriends.setdefault(handle, Friend(handle))
if handle not in allFriends:
allFriends[handle] = Friend(handle)
allFriends[handle].addMessage(Message(text, service, date, dateRead, dateDelivered, isFromMe))
# todo: add probabilities
# todo: autocomplete
for handle in allFriends.keys():
friend = allFriends[handle]
messageList = friend.messagesToMe
if len(messageList) < 10:
continue
print "FRIEND:" + handle
dictionary = {}
nonword = '\n'
nfactor = 1
previous = []
for i in xrange(nfactor):
previous.append(nonword)
for message in messageList:
if message.text != None:
myString = message.text.encode('utf-8')
for word in myString.split():
dictionary.setdefault(tuple(previous),[]).append(word)
previous = previous[1:]
previous.append(word);
dictionary.setdefault(tuple(previous), []).append(nonword)
previous = []
for i in xrange(nfactor):
previous.append(nonword)
maxwords = 10
for x in xrange(10):
for i in xrange(maxwords):
newword = random.choice(dictionary[tuple(previous)])
if newword == nonword:
previous = []
for i in xrange(nfactor):
previous.append(nonword)
sys.stdout.write(newword + ' ')
previous = previous[1:]
previous.append(newword)
print '\n'