Created
May 9, 2016 03:27
-
-
Save longcao/c8d348e3af3155d212e076f7c552c03d to your computer and use it in GitHub Desktop.
i did a bad
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
/** | |
* Intersperses words with spaces and trims ends, accounts for basic smilies. | |
*/ | |
def wordsToSentence(words: Vector[String]): String = { | |
val punctuation = Set(":", ";", ".", ",", "!", "?") | |
val smilies = Set(":)", ":(", ":<", ":}", ":|", ":o") | |
// @TODO really bad implementation probably, clean this up later | |
words.zipWithIndex.foldLeft("") { case (acc, (word, i)) => | |
val isLastWordPairSmilie = if (words.isDefinedAt(i - 1)) { | |
smilies(words(i - 1) + word) | |
} else { | |
false | |
} | |
if (words.isDefinedAt(i + 1) && !isLastWordPairSmilie) { | |
val nextWord = words(i + 1) | |
if (punctuation(word) && !smilies(word + nextWord)) { | |
acc + word | |
} else if (smilies(word + nextWord)) { | |
acc + " " + (word + nextWord) | |
} else { | |
acc + " " + word | |
} | |
} else if (!isLastWordPairSmilie) { | |
acc + " " + word | |
} else { | |
acc | |
} | |
}.trim | |
} | |
val words = Vector("i", "am", ";", "a", ":", "simple", "sentence", ":", "(", ",", "a", "simple", "one", ".", ":", ")") | |
val sentence = wordsToSentence(words) | |
println(sentence) | |
// i am; a: simple sentence :(, a simple one. :) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
needs emoji support.