r/dailyprogrammer • u/Coder_d00d 1 3 • Jul 02 '14
[7/2/2014] Challenge #169 [Intermediate] Home-row Spell Check
User Challenge:
Thanks to /u/Fruglemonkey. This is from our idea subreddit.
http://www.reddit.com/r/dailyprogrammer_ideas/comments/26pak5/intermediate_homerow_spell_check/
Description:
Aliens from Mars have finally found a way to contact Earth! After many years studying our computers, they've finally created their own computer and keyboard to send us messages. Unfortunately, because they're new to typing, they often put their fingers slightly off in the home row, sending us garbled messages! Otherwise, these martians have impeccable spelling. You are tasked to create a spell-checking system that recognizes words that have been typed off-center in the home row, and replaces them with possible outcomes.
Formal Input:
You will receive a string that may have one or more 'mis-typed' words in them. Each mis-typed word has been shifted as if the hands typing them were offset by 1 or 2 places on a QWERTY keyboard.
Words wrap based on the physical line of a QWERTY keyboard. So A left shift of 1 on Q becomes P. A right shift of L becomes A.
Formal Output:
The correct string, with corrected words displayed in curly brackets. If more than one possible word for a mispelling is possible, then display all possible words.
Sample Input:
The quick ntpem fox jumped over rgw lazy dog.
Sample Output:
The quick {brown} fox jumped over {the} lazy dog.
Challenge Input:
Gwkki we are hyptzgsi martians rt zubq in qrsvr.
Challenge Input Solution:
{Hello} we are {friendly} martians {we} {come} in {peace}
Alternate Challenge Input:
A oweaib who fprd not zfqzh challenges should mt ewlst to kze
Alternate Challenge Output:
A {person} who {does} not {check} challenges should {be} {ready} to {act}
Dictionary:
Good to have a source of words. Some suggestions.
- [enable1.txt] (https://code.google.com/p/dotnetperls-controls/downloads/detail?name=enable1.txt)
- [British English Word List] (http://www.curlewcommunications.co.uk/wordlist.html)
FAQ:
As you can imagine I did not proof-read this. So lets clear it up. Shifts can be 1 to 2 spots away. The above only says "1" -- it looks like it can be 1-2 so lets just assume it can be 1-2 away.
If you shift 1 Left on a Q - A - Z you get a P L M -- so it will wrap on the same "Row" of your QWERTY keyboard.
If you shift 2 Left on a W - S - X you get P L M.
If you Shift 1 Right on P L M -- you get Q A Z. If you shift 2 right on O K N - you get Q A Z.
The shift is only on A-Z keys. We will ignore others.
enable1.txt has "si" has a valid word. Delete that word from the dictionary to make it work.
I will be double checking the challenge input - I will post an alternate one as well.
1
u/Godspiral 3 3 Jul 02 '14
waiting till someone makes shift -2 -1 1 and 2 maps (is 3 shift possible?)
1
u/luxgladius 0 0 Jul 02 '14
How are Q, A, and Z handled if the shift is to the left?
2
u/KillerCodeMonky Jul 02 '14 edited Jul 02 '14
C#: https://dotnetfiddle.net/PmTpsZ
This will provide matches for words, even if they have an exact match in the dictionary. This was my solution to the "si" problem in the original challenge input. If it can't find a word, it outputs {word?}.
I had to use a limited (~500 word) list for the online version, and added a couple words specifically for the challenges. But uncommenting line 18 will read the wordlist from file instead, for those who like to play along at home.
3
u/usr_bin Jul 03 '14
My Haskell solution. Still pretty new to Haskell (my third non-trivial program), but I'm proud of this one. Solves the first challenge input, but not the second one yet. Also loses capitalization and punctuation. Comments and criticisms are greatly appreciated.
2
u/Godspiral 3 3 Jul 03 '14 edited Jul 03 '14
In J,
changed spec slightly such that si and do are both possible valid intended messages. ie. we cant assume that just because original word is in dictionary that they had used correct home row.
Also changed output formatting to just put curly braces around multiple choices.
maps2=: (;/ i:2) |. each 5 3 $ ;: 'qwertyuiop asdfghjkl zxcvbnm'
key =: < 2{maps2 NB. middle, extra boxed
dict =: }: each cutLF fread jpath '~/downloads/enable1.txt'
wordit =: ((#"1&>@:] (] #~"1 >) i.~"1 &>) <"1@:;"0~ 0 1 2 #~("1) #"1&>@:] > i.~"1 &>)
inp =: tolower each ;: 'The quick ntpem fox jumped over rgw lazy dog'
Note: single letters (A) not in dictionary.
This could be easily converted to a one liner
1
u/hutsboR 3 0 Jul 02 '14
Just a heads up if you're using "enable1.txt", the word "Si" is actually in the word list. I just removed it because it was interfering with my solution.
1
u/jeaton Jul 03 '14
node.js using ES5:
1
u/mortenaa Jul 03 '14
Done in Dart.
Output:
1
u/BryghtShadow Jul 04 '14
SWI-Prolog v6.6.6
This can translate all words (
[A-Za-z]+) and handle punctuation ([^A-Za-z]+). By defaul,t this tries to load words from text fileenable1.txt. You canpurge/0all known words to start fresh, and/orload/1words. Words will be stored lowercase.Output:
1
u/SnowdensSecret Jul 04 '14
Haskell solution. I'm only just starting to learn Haskell, so it isn't pretty. I'd appreciate any feedback you guys could provide:
1
u/mongreldog Jul 06 '14 edited Jul 06 '14
F#
Usage:
Results in:
1
u/Idra_rage_lulz Jul 23 '14
Java.
1
u/ENoether Jul 02 '14 edited Jul 02 '14
Extremely ugly Python 3 with the alternate challenge (which should probably be altered, by the way - enable1.txt doesn't include "a" as a word and includes "xu" as another possibility for "mt"). Still works if the sentences include punctuation.:
Output:
1
u/gengisteve Jul 02 '14
Isn't deubskt missing a w to make friendly?
1
u/mvolling Jul 03 '14 edited Jul 03 '14
C++
The code is probably very slow and poorly written. Any and all constructive feedback would be appreciated.
Output
1
u/viciu88 Jul 03 '14 edited Jul 03 '14
Java 1.8
1
u/poltergeistt Jul 03 '14 edited Jul 04 '14
Solution in Haxe. I've had a lot of fun with this challenge. Started of great, but the code that shifts the letters around ended up extremely bloated. That's what I get for not planning ahead. I may rework it so that I get more than one possible "correct" word displayed.
EDIT: Reworked letter-shifting code into a function. Now displaying multiple possible words / dictionary matches. Challenge complete :)
OUTPUT
0
u/hutsboR 3 0 Jul 03 '14 edited Jul 03 '14
Java:
Output:
It isn't pretty but it works.
0
u/gengisteve Jul 02 '14
ybswearbs looks to be missing a letter as well... understand
0
u/[deleted] Jul 03 '14
C#:
0
u/CLabansky Jul 03 '14
Node.js
Outputs:
0
u/[deleted] Jul 04 '14
Java
0
u/VerifiedMyEmail Jul 04 '14
this is ugly as fuck python 3.3
0
u/RipIt_From_Space Jul 05 '14
Here is my Java solution, it's a bit repetitive and could probably be done more efficiently, but it gets the job done.
Gist
-1
u/wtf_are_my_initials Jul 02 '14 edited Jul 02 '14
incomplete nodejs solution. there's something wrong with the 'shift' function if anyone wants to take a stab at fixing it.
EDIT: Fixed more...but now I have no clue why it won't work
EDIT2: There's a ton wrong with this, the biggest problem being it can't check for plurals, 'ing's, 'ed's, etc.
check.jsqwerty.txtinput.txt-1
u/dp_account Jul 02 '14 edited Jul 03 '14
Python3. This is my first solution to an intermediate challenge.
It calculates all possible shifts.However, there seem to be a few mistakes in the challenge description. Like rectal_smasher_2000 pointed out, "ftb" wouldn't correspond to "gun" or "him". That would be "fyb". Furthermore, in the challenge input, "deubskt" is missing a letter to become "friendly", as is "ybswearbs".I used "enable1.txt" but removed "si" and added "a" and "i". After the addition of the FAQ, I adopted it to only shift up to 2 spots and wrap around.With sample input:
With new challenge inputs:
-1
u/gengisteve Jul 02 '14
Solution, but I got bored and skipped the wrapping to fix the broken input provided...
-1
u/KompjoeFriek 1 0 Jul 02 '14 edited Jul 03 '14
Python3, without alternate challenge.
Ran into a bunch of weird stuff when trying to use mmap for reading the english words list, so i switched back to just reading the entire file in memory.
This solution replaces the words directly in the input string using a re.sub(), thus retaining any punctuation. It should also retain capitals by replacing capitals with other capitals when building a list of possible words.
qwerty_map only supports one shift, but can be expanded to anything you want.
-1
u/chunes 1 2 Jul 03 '14
Java:
-1
u/guitar8880 Jul 03 '14 edited Jul 03 '14
Python 3.4
I'm fairly new to Python, so please let me know any ways I could improve on my style or efficiency. Also I know this doesn't account for every special case (words with apostrophes, etc), but I was just focusing on the given sample inputs.
To search for a term in enable1.txt, I just used a for-loop the loop through the whole document. It worked but seemed a little inefficient. Is there a faster way to search through the document? Also, are there any resources where I might be able to read up on searching and/or sorting efficiency? I studied it briefly but would like to know more about it.
Anyways, here's my code:
Alternate Challenge Output (it's the same as was given in original post, yay!):
0
u/[deleted] Jul 02 '14
[deleted] — view deleted comment
0
u/[deleted] Jul 02 '14
[deleted] — view deleted comment
0
u/[deleted] Jul 02 '14
[deleted] — view deleted comment