Skip to content

Instantly share code, notes, and snippets.

@longcao longcao/words.scala
Created May 9, 2016

Embed
What would you like to do?
i did a bad
/**
* 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. :)
@bodnaristvan

This comment has been minimized.

Copy link

bodnaristvan commented May 9, 2016

needs emoji support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.