forked from ncss-2013/ForkingStories
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_spellcheck.py
37 lines (35 loc) · 1.19 KB
/
handler_spellcheck.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
import Spellcheck.spellcheck as spell
import json
import string
def spellcheck(response):
text = response.get_field('text', default='')
if text:
fixed = {}
words = set(text.split())
for word in words:
if word[0] in string.ascii_uppercase:
word = word.lower()
word = word.strip(string.punctuation)
result = spell.suggest_corrections(word)
if result is not None:
result_2 = list()
for word_2 in result:
word_2 = word_2[0].upper() + word_2[1:]
result_2.append(word_2)
word = word[0].upper() + word[1:]
fixed[word] = result_2
else:
word = word.lower()
word = word.strip(string.punctuation)
result = spell.suggest_corrections(word)
if result is not None:
fixed[word] = result
response.write(json.dumps(fixed))
else:
response.write('''
<form method='post'>
<textarea name='text'>
</textarea>
<input type='submit' />
</form>
''')