-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqueries.py
90 lines (59 loc) · 2.93 KB
/
queries.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import datetime
import fleschkincaid
def get_lines_by_nick(executor, nick):
sql = '''select full_text from text where nick=? order by date asc'''
return exector.query(sql, (nick,))
def get_distinct_lines_by_nick(executor, nick):
sql = '''select distinct full_text from text where nick=? order by date asc'''
return executor.query(sql, (nick,))
def get_line_count_by_nick(executor, nick):
sql = '''select count(full_text) from text where nick=?'''
return executor.query_one_row(sql, (nick,))[0]
def get_line_count_like_pattern(executor, nick, pattern):
sql = '''select count(full_text) from text where nick=? and full_text like ?'''
return executor.query_one_row(sql, (nick,pattern,))[0]
def get_distinct_line_count_since_date(executor, nick, date):
sql = '''select count(distinct full_text) from text where nick=? and date>?'''
return executor.query_one_row(sql, (nick,date,))[0]
def get_distinct_line_count_by_nick(executor, nick):
sql = '''select count(distinct full_text) from text where nick=?'''
return executor.query_one_row(sql, (nick,))[0]
def reading_level(executor, nick):
sql = '''select avg(pretentious) from text where nick=?'''
return executor.query_one_row(sql, (nick,))[0]
def get_random_line(executor, nick, source):
sql = '''select distinct full_text from text where nick=? and target=? order by random() limit 1'''
row = executor.query_one_row(sql, (nick,source,))
if row != None:
return row[0]
else:
return None
def get_random_lines_like(executor, nick, source, like,count):
sql = ('''select distinct full_text from text where nick=? and ''' +
'''target=? and full_text like ? order by random() limit ?''')
rows = executor.query_all_rows(sql, (nick,source,like,count,))
return [row[0] for row in rows]
def get_random_line_like(executor, nick, source, like):
sql = ('''select distinct full_text from text where nick=? and ''' +
'''target=? and full_text like ? order by random() limit 1''')
row = executor.query_one_row(sql, (nick,source,like,))
if row is not None:
return row[0]
else:
return None
def add_line(executor, nick, target, full_text):
sql = '''insert into text values (?,?,?,?,?)'''
now = datetime.datetime.utcnow().isoformat()
rl = _sentence_reading_level(full_text)
executor.execute(sql, (now,nick,target,full_text,rl,))
def _sentence_reading_level(s):
return max(0.0, fleschkincaid.grade_level(s))
def all_nicks(executor):
sql = '''select distinct(nick) from text order by nick'''
return [nick[0] for nick in executor.query_all_rows(sql)]
def all_active_nicks(executor):
time = datetime.timedelta(weeks=1)
date = datetime.datetime.utcnow()
date = (date - time).isoformat()
sql = '''select nick from text where date>? group by nick having count(nick)>15 order by nick'''
return [nick[0] for nick in executor.query_all_rows(sql, (date,))]