Skip to content

Instantly share code, notes, and snippets.

@blakemerryman
Created December 21, 2014 06:04
Show Gist options
  • Save blakemerryman/a74a5a982186fe209649 to your computer and use it in GitHub Desktop.
Save blakemerryman/a74a5a982186fe209649 to your computer and use it in GitHub Desktop.
My Swift solution to the /r/DailyProgrammer Challenge # 193
#!/usr/bin/env xcrun swift
/*
/r/DailyProgrammer Challenge # 193
Reddit" : "http://www.reddit.com/r/dailyprogrammer/
Blake Merryman
Github" : "https://github.com/blakemerryman
Notes:
* DO NOT use single tick quotes ('...') when entering arguments in the terminal!
* To run,
1. Make executable (chmod +x filename.swift)
2. Run by entering the following into the console:
> ./filename.swift "..."
or
> ./filename.swift ...
*/
import Foundation
// MARK: - Lingo Dictionary
let keyWordList = [
"lol" : "laugh out loud",
"dw" : "don't worry",
"hf" : "have fun",
"gg" : "good game",
"brb" : "be right back",
"g2g" : "got to go",
"wtf" : "what the fuck",
"wp" : "well played",
"gl" : "good luck",
"imo" : "in my opinion"
]
// MARK: - Computer Property for NSCharacterSet (whitespace, punctuation, & symbols)
var nonAlphanumericCharacterSet: NSCharacterSet {
var characterSet = NSMutableCharacterSet(charactersInString: " ")
characterSet.formUnionWithCharacterSet(NSCharacterSet.punctuationCharacterSet())
characterSet.formUnionWithCharacterSet(NSCharacterSet.symbolCharacterSet())
return characterSet
}
// MARK: - Utility Functions
/// Main utility method that invokes all others and returns the fully processed value. All data will be stored in arrays during processing.
func expandSentence(sentence: [String]) -> String {
let wordsWithSpacesBetween = insertSpacesBetweenWords(sentence)
let removedPunctuation = seperatePunctuationFromWords(wordsWithSpacesBetween)
let expandedWords = expandWords(removedPunctuation)
let expandedSentence = (expandedWords as NSArray).componentsJoinedByString("")
return expandedSentence
}
/// After seperating each component and getting rid of the whitespace between them, we add it back for easier recombination later.
func insertSpacesBetweenWords(words: [String]) -> [String] {
var mutableWords = words
let spacesToInsert = 2 * words.count - 1
var index = 1
while index < spacesToInsert {
mutableWords.insert(" ", atIndex: index)
index = index + 2
}
return mutableWords
}
/// Pull punctuation from words and store in proper place in array.
func seperatePunctuationFromWords(words: [String]) -> [String] {
var punctuationParsedWords = [String]()
for word in words {
let unicodeWord = word.unicodeScalars
var wordBuffer = String()
for (index,character) in enumerate(unicodeWord) {
let punctuationSet = NSCharacterSet.punctuationCharacterSet()
let charIsPunctuation = punctuationSet.longCharacterIsMember(character.value)
if charIsPunctuation {
if !wordBuffer.isEmpty {
punctuationParsedWords.append(wordBuffer)
wordBuffer = String()
}
punctuationParsedWords.append("\(character)")
}
else {
wordBuffer.append(character)
}
if index == (countElements(unicodeWord) - 1) && !wordBuffer.isEmpty {
punctuationParsedWords.append(wordBuffer)
}
}
}
return punctuationParsedWords
}
/// Process the array, replacing any abbreviations for their full versions.
func expandWords(abbreviatedWords: [String]) -> [String] {
var expandedWords = [String]()
for word in abbreviatedWords {
var currentWord = word
if let phrase = keyWordList[word] {
currentWord = phrase
}
expandedWords.append(currentWord)
}
return expandedWords
}
// MARK: ------------------------- MAIN OPERATION ------------------------------
// MARK: Handle Various Input Cases Properly
var input = [String]()
// Grab input from console
for argument in Process.arguments {
input.append("\(argument)")
}
// Get rid of "program name" process-argument
input.removeAtIndex(0)
if input.count == 0 {
println("Error: No input detected!")
} else if input.count == 1 {
// Quote bound string
input = (input[0] as NSString).componentsSeparatedByString(" ") as [String]
}
// Else we have a quoteless string that is already seperated by word.string
// MARK: Perform Expansion
println(expandSentence(input))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment