Created
August 7, 2014 10:49
-
-
Save gravicle/72e48c0feaaca615217a to your computer and use it in GitHub Desktop.
A simple swift program that corrects for 1-key-right typo.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Author: Amit Jain | |
// License: MIT | |
// Date: 07/07/2014 | |
// [7/2/2014] Challenge #169 [Intermediate] Home-row Spell Check : dailyprogrammer | |
// http://www.reddit.com/r/dailyprogrammer/comments/29od55/722014_challenge_169_intermediate_homerow_spell/ | |
import Foundation | |
struct WordMeta { | |
var word: String | |
var correctedWord: String? | |
var index: Int | |
var isCorrect: Bool | |
} | |
class SpellCheck { | |
var allWords: [String] = [] | |
init() { | |
let dictionaryWords: [String]? = self.loadDictionary()! | |
if let words = dictionaryWords { | |
allWords = words | |
} | |
} | |
func loadDictionary() -> [String]? { | |
let file = "brit-a-z.txt" | |
var words: [String]? | |
let dirs: [String]? = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true) as [String]? | |
if (dirs != nil) { | |
let directories: [String] = dirs! | |
let dir = directories[0] | |
let path = dir.stringByAppendingPathComponent(file) | |
var error: NSError? | |
let text = String.stringWithContentsOfFile(path, encoding: NSUTF8StringEncoding, error: &error) | |
if let wordString = text { | |
words = wordString.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet()) | |
return words! | |
} else { | |
println(error) | |
return nil | |
} | |
} else { | |
return nil | |
} | |
} | |
func isCorrectWord (word: String) -> Bool { | |
for aWord in allWords { | |
if word == aWord { | |
return true | |
} | |
} | |
return false | |
} | |
func incorrectWordsInString (str: String) -> [WordMeta] { | |
//convert to lowercase | |
var string = str.lowercaseString | |
// Remove punctuations | |
var words = (string.componentsSeparatedByCharactersInSet(NSCharacterSet.letterCharacterSet().invertedSet)) | |
var processedWords: [WordMeta] = [] | |
for i in 0 ..< words.count { | |
var isCorrect = isCorrectWord (words[i]) | |
if isCorrect { | |
let word = WordMeta(word: words[i], correctedWord: nil, index: i, isCorrect: true) | |
processedWords += [word] | |
} else { | |
let word = WordMeta(word: words[i], correctedWord: nil, index: i, isCorrect: false) | |
processedWords += [word] | |
} | |
} | |
var incorrectWords: [WordMeta] = [] | |
for processedWord in processedWords { | |
if !processedWord.isCorrect { | |
incorrectWords += [processedWord] | |
} | |
} | |
return incorrectWords | |
} | |
func check (str: String) -> [WordMeta] { | |
var incorrectWords = incorrectWordsInString(str) | |
for i in 0..<incorrectWords.count { | |
var incorrectWord = incorrectWords[i] | |
var shiftedWord: String = "" | |
for char in incorrectWord.word { | |
shiftedWord += oneL(char) | |
} | |
let isCorrect = isCorrectWord(shiftedWord) | |
if isCorrect { | |
incorrectWord.correctedWord = shiftedWord | |
incorrectWords[i] = incorrectWord | |
} | |
} | |
return incorrectWords | |
} | |
func oneL(char: Character) -> Character { | |
switch char { | |
case "a": return "l" | |
case "b": return "v" | |
case "c": return "x" | |
case "d": return "s" | |
case "e": return "w" | |
case "f": return "d" | |
case "g": return "f" | |
case "h": return "g" | |
case "i": return "u" | |
case "j": return "h" | |
case "k": return "j" | |
case "l": return "k" | |
case "m": return "n" | |
case "n": return "b" | |
case "o": return "i" | |
case "p": return "o" | |
case "q": return "p" | |
case "r": return "e" | |
case "s": return "a" | |
case "t": return "r" | |
case "u": return "y" | |
case "v": return "c" | |
case "w": return "q" | |
case "x": return "z" | |
case "y": return "t" | |
case "z": return "m" | |
default: return " " | |
} | |
} | |
} | |
let spellChecker = SpellCheck() | |
let incorrectWords: [WordMeta] = spellChecker.check("Structures and vasddrd both idr initializer syntax for new omdysmvrd.") | |
for incorrectWord in incorrectWords { | |
println("Typo: \(incorrectWord.word) | Correction: \(incorrectWord.correctedWord)") | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment