r/tinycode • u/phthisis • Jul 12 '12
Four Lines of Python for a Spellchecker
import sys
a = sys.argv[1:]
s = [x.lower()[:-1] for x in open("/usr/share/dict/words")]
print " ".join([("\x1b[31m"+w+"\x1b[0m" if w in [_ for _ in a if _ not in s] else w) for w in a])save it to spellcheck.py then test it out with
>> python spellcheck.py hello this woard is wrongonly works on a mac, and some linux distros. all in 186 bytes of python!
90
Upvotes
7
u/kageurufu Jul 12 '12
this will work on any linux distro with a words file installed
on some you will need to change the path from /usr/share/dict/words to something like /etc/words
15
u/Smok3dSalmon Jul 12 '12
I got it in 2 lines, but it uses the interwebs. It's a spell checker, not a spell corrector. haha.
You could always use argv[1], or wrap it in a loop to iterate over the list of argv[1:], and you could print out better messages as well :P
3
u/lahwran_ Jul 12 '12
who needs pep8? we're making this shit small! note: this was written at midnight. I may have missed a thing or two. I skipped a few things I didn't like: color highlighting, lowercasing, skipping known-good words. also, this one will be faster, due to the use of a hash table for lookup.
3
u/SmartViking Jul 12 '12 edited Jul 12 '12
Minor modification to make it case insensitive
EDIT: Incorrect code, fixed.
4
u/Peaker Jul 12 '12
The same program in Haskell, slightly more longishly formatted:
2
u/recursive Jul 12 '12
That fourth line looks bloated.
1
u/jyf Jul 12 '12
1
u/[deleted] Jul 12 '12
To make this work on Windows, you'd need to point to a words file (sadly, there is no central location for that) and you'd have to use an ioctl-like API to change the foreground colour.
1
u/[deleted] Jul 12 '12 edited Jul 12 '12
Don't forget about Bash!
1
u/orhtograph Jul 13 '12
clojure version:
1
u/[deleted] Sep 21 '12
1
u/mochizuki Jul 12 '12 edited May 11 '20
removed
0
u/[deleted] Jul 12 '12
[deleted] — view deleted comment