-
Notifications
You must be signed in to change notification settings - Fork 21
/
dogma.py
180 lines (144 loc) · 5.33 KB
/
dogma.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# This program is part of prosaic.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from functools import reduce
import logging
from random import choice
import re
import sqlalchemy as sa
import prosaic.nlp as nlp
from prosaic.models import Phrase, Corpus, Source
from prosaic.util import match, is_empty, update
log = logging.getLogger('prosaic')
class Rule:
strength = 0
def weaken(self):
if self.strength > 0:
self.strength -= 1
def to_query(self):
return '1 = 1'
class SyllableCountRule(Rule):
__slots__ = ['syllables', 'strength',]
def __init__(self, syllables):
self.syllables = syllables
self.strength = syllables
def to_query(self):
if 0 == self.strength:
return super().to_query()
modifier = self.syllables - self.strength
return 'p.syllables >= {} and p.syllables <= {}'.format(
self.syllables + modifier,
self.syllables - modifier)
class KeywordRule(Rule):
__slots__ = ['keyword', 'phrase_cache', 'strength']
max_strength = 11
def __init__(self, keyword, conn, corpus_id):
self.strength = self.max_strength
self.keyword = nlp.stem_word(keyword)
self.prime_cache(conn, corpus_id)
def prime_cache(self, conn, corpus_id):
log.debug('building phrase cache')
sql = """
select p.line_no, p.source_id
from phrases p
join corpora_sources cs
on p.source_id = cs.source_id
where corpus_id = :corpus_id
and p.stems @> ARRAY[:keyword]
"""
self.phrase_cache = conn.execute(sa.text(sql)\
.params(keyword=self.keyword,
corpus_id=corpus_id))\
.fetchall()
if is_empty(self.phrase_cache):
self.strength = 0
def to_query(self):
if 0 == self.strength:
return super().to_query()
if self.max_strength == self.strength:
return "stems @> ARRAY['{}']".format(self.keyword)
phrase = choice(self.phrase_cache)
ok_distance = self.max_strength - self.strength
line_no, source_id = phrase
return "p.source_id = {} and abs({} - p.line_no) <= {}".format(
source_id, line_no, ok_distance
)
class FuzzyKeywordRule(KeywordRule):
def to_query(self):
if 0 == self.strength:
return super().to_query()
phrase = choice(self.phrase_cache)
ok_distance = 1 + self.max_strength - self.strength
line_no, source_id = phrase
return "p.source_id = {} and abs({} - p.line_no) <= {}".format(
source_id, line_no, ok_distance
)
zero_re = re.compile('0')
one_re = re.compile('1')
two_re = re.compile('2')
class RhymeRule(Rule):
__slots__ = ['sound', 'strength']
def __init__(self, rhyme):
self.strength = 3
self.sound = rhyme
def next_sound(self):
strength = self.strength
sound = self.sound
# TODO ugh
if 3 == strength:
return sound
elif 2 == strength:
if zero_re.search(sound):
return sound.replace('0', '1')
elif one_re.search(sound):
return sound.replace('1', '2')
elif two_re.search(sound):
return sound.replace('2', '0')
elif 1 == strength:
if zero_re.search(sound):
return sound.replace('0', '2')
elif one_re.search(sound):
return sound.replace('1', '0')
elif two_re.search(sound):
return sound.replace('2', '1')
def to_query(self):
if 0 == self.strength:
return super().to_query()
return "p.rhyme_sound = '{}'".format(self.next_sound())
class AlliterationRule(Rule):
__slots__ = ['strength', 'which']
def __init__(self, which):
self.strength = 1
self.which = which
def to_query(self):
if 0 == self.strength:
return super().to_query()
return "p.alliteration = true"
class BlankRule(Rule): pass
class RuleSet:
def __init__(self, rules):
self.rules = rules
def contains(self, rule_class):
return 0 < len(list(filter(lambda r: rule_class == type(r),
self.rules)))
def to_query(self, sess):
base_sql = """
select p.raw, p.source_id
from phrases p
join corpora_sources cs
on p.source_id = cs.source_id
where corpus_id = :corpus_id
"""
wheres = map(lambda r: r.to_query(), self.rules)
return base_sql + ' and ' + ' and '.join(wheres)
def weaken(self):
choice(self.rules).weaken()
return self