Skip to content

Instantly share code, notes, and snippets.

@longcao
Created May 9, 2016 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save longcao/c8d348e3af3155d212e076f7c552c03d to your computer and use it in GitHub Desktop.
Save longcao/c8d348e3af3155d212e076f7c552c03d to your computer and use it in GitHub Desktop.
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
Copy link

needs emoji support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment