-
Notifications
You must be signed in to change notification settings - Fork 5
/
typogenerator.py
executable file
·248 lines (197 loc) · 7.87 KB
/
typogenerator.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/usr/bin/python
import sys
import os
import json
import subprocess
import SocketServer
import SimpleXMLRPCServer
from nltk.corpus import wordnet
from daemon import daemon, pidlockfile
PORT = 2828
LISTEN = "0"
DICTIONARY = "/usr/share/dict/words"
if not os.path.isfile(DICTIONARY):
raise FileNotFound("Could not find dictionary: %s" % DICTIONARY)
PID_FILE = '/home/self/backup/dev/TypoGenerator/typogenerator.pid'
alphabet = "abcdefghijklmnopqrstuvwxyz0123456789"
vowels = "aeiouy"
connectives = [
"I", "the", "of", "and", "to", "a", "in", "that",
"is", "was", "he", "for", "it", "with", "as", "his",
"on", "be", "at", "by", "i", "this", "had", "not",
"are", "but", "from", "or", "have", "an", "they",
"which", "one", "you", "were", "her", "all", "she",
"there", "would", "their", "we", "him", "been", "has",
"when", "who", "will", "more", "no", "if", "out",
"so", "said", "what", "u", "its", "about", "into",
"than", "them", "can", "only", "other", "new", "some",
"could", "time", "these", "two", "may", "then", "do",
"first", "any", "my", "now", "such", "like", "our",
"over", "man", "me", "even", "most", "made", "after",
"also", "did", "many", "before", "must", "through",
"back", "years", "where", "much", "your", "way",
"well", "down", "should", "because", "each", "just",
"those", "eople", "mr", "how", "too", "little",
"state", "good", "very", "make", "world", "still",
"own", "see", "men", "work", "long", "get", "here",
"between", "both", "life", "being", "under", "never",
"day", "same", "another", "know", "while", "last",
"might", "us", "great", "old", "year", "off",
"come", "since", "against", "go", "came", "right",
"used", "take", "three",
"whoever", "nonetheless", "therefore", "although",
"consequently", "furthermore", "whereas",
"nevertheless", "whatever", "however", "besides",
"henceforward", "yet", "until", "alternatively",
"meanwhile", "notwithstanding", "whenever",
"moreover", "despite", "similarly", "firstly",
"secondly", "lastly", "eventually", "gradually",
"finally", "thus", "hence", "accordingly",
"otherwise", "indeed", "though", "unless"
]
class FileNotFound(Exception):
pass
class TypoGenerator:
def insertedKey(self, s):
"""Produce a list of keywords using the `inserted key' method
"""
kwds = []
for i in range(0, len(s)):
for char in alphabet:
kwds.append(s[:i+1] + char + s[i+1:])
return json.dumps(kwds)
def skipLetter(self, s):
"""Produce a list of keywords using the `skip letter' method
"""
kwds = []
for i in range(1, len(s)+1):
kwds.append(s[:i-1] + s[i:])
return json.dumps(kwds)
def doubleLetter(self, s):
"""Produce a list of keywords using the `double letter' method
"""
kwds = []
for i in range(0, len(s)+1):
kwds.append(s[:i] + s[i-1] + s[i:])
return json.dumps(kwds)
def reverseLetter(self, s):
"""Produce a list of keywords using the `reverse letter' method
"""
kwds = []
for i in range(0, len(s)):
letters = s[i-1:i+1:1]
if len(letters) != 2:
continue
reverse_letters = letters[1] + letters[0]
kwds.append(s[:i-1] + reverse_letters + s[i+1:])
return json.dumps(kwds)
def wrongVowel(self, s):
"""Produce a list of keywords using the `wrong vowel' method (for soundex)
"""
kwds = []
for i in range(0, len(s)):
for letter in vowels:
if s[i] in vowels:
for vowel in vowels:
s_list = list(s)
s_list[i] = vowel
kwd = "".join(s_list)
kwds.append(kwd)
return json.dumps(kwds)
def wrongKey(self, s):
"""Produce a list of keywords using the `wrong key' method
"""
kwds = []
for i in range(0, len(s)):
for letter in alphabet:
kwd = s[:i] + letter + s[i+1:]
kwds.append(kwd)
return json.dumps(kwds)
def _is_connective(self, word):
""" Guesses whether the word is a connective.
Connectives are conjunctions such as "and", "or", "but",
transition signals such as "moreover", "finally",
and words like "I", "she".
It's useful to filter out connectives
when guessing the concept of a piece of text.
... you don't want "whatever" to be the most important word
parsed from a text.
"""
if word.lower() in connectives:
return True
else:
return False
def _findWords(self, s):
"""Produces a list of words found in string `s'
"""
matches = []
words = file(DICTIONARY).read()
for word in words.split('\n'):
if s.find(word) != -1 and word != '' and len(word) > 1: # and not self._is_connective(word):
matches.append(word)
return matches
def _getSynonyms(self, word):
"""Returns a list of synonyms for `word'
"""
synset = []
for word_type in [wordnet.ADJ, wordnet.ADV, wordnet.NOUN, wordnet.VERB]:
synset += [lemma.name.lower().replace("_", "")
for lemma in sum([ss.lemmas
for ss in wordnet.synsets(word, word_type)],[])]
return synset
def synonymSubstitution(self, s):
"""Produces a list of strings with alternative synonyms from the words found in `s'
"""
alt_strings = []
for word in self._findWords(s):
for synonym in self._getSynonyms(word):
orig_s = s
alt_strings.append( orig_s.replace(word, synonym))
return json.dumps(list(set(alt_strings)))
def rhymeSubstitution(self, s):
"""Produces a list of strings with alternative rhyming words.
"""
# http://packages.debian.org/sid/text/rhyme
alt_strings = []
for word in self._findWords(s):
process = subprocess.Popen(["rhyme", word], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
outp = process.stdout.read()
if "wasn't found" in outp:
continue
rhymes = outp.replace("Finding perfect rhymes for city...", "").replace(
"\n1:", "").replace("\n2:", "").replace("\n3:", "").replace(
"\n4:", "").replace("\n", ",").replace(" ", "").split(",")
for rhyme in rhymes:
orig_s = s
if (not "(" in rhyme) and (
not "'" in rhyme) and (
not "." in rhyme):
alt_strings.append( orig_s.replace(word, rhyme))
return json.dumps(list(set(alt_strings)))
# def getAllTypos(self, s):
# """Calls all our typo generation methods on a string and return the result
# """
# kwds = []
# kwds += self.insertedKey(s)
# kwds += self.wrongKey(s)
# kwds += self.skipLetter(s)
# kwds += self.doubleLetter(s)
# kwds += self.reverseLetter(s)
# kwds += self.wrongVowel(s)
# kwds += self.synonymSubstitution(s)
# return kwds
class SimpleThreadedXMLRPCServer(SocketServer.ThreadingMixIn, SimpleXMLRPCServer.SimpleXMLRPCServer):
pass
if __name__ == "__main__":
pidlock = pidlockfile.PIDLockFile(PID_FILE)
if pidlock.is_locked():
sys.exit(1)
context = daemon.DaemonContext()
context.pidfile = pidlock
context.open()
try:
server = SimpleThreadedXMLRPCServer((LISTEN, PORT))
server.register_instance(TypoGenerator())
server.serve_forever()
finally:
context.close()