Skip to content

Instantly share code, notes, and snippets.

@divanvisagie
Last active October 1, 2017 10:01
Show Gist options
  • Save divanvisagie/fbbe38ee0d45acdc423ab46366a31937 to your computer and use it in GitHub Desktop.
Save divanvisagie/fbbe38ee0d45acdc423ab46366a31937 to your computer and use it in GitHub Desktop.
def sentimentCounter(list: List[String]): List[Int] = {
list.foldLeft(List[Int](0, 0, 0))((acc, item) => {
item match {
case "Positive" => List(acc.head + 1, acc(1), acc(2))
case "Very positive" => List(acc.head + 2, acc(1), acc(2))
case "Negative" => List(acc.head, acc(1) + 1, acc(2))
case _ => List(acc.head, acc(1), acc(2) + 1)
}
})
}
def analyzeWithCoreNLP(text: String): Double = {
if (text.isEmpty) return 0.0
val props = new Properties()
props.setProperty("annotators","tokenize, ssplit, pos, lemma, parse, sentiment")
val pipeline = new StanfordCoreNLP(props)
val annotation = pipeline.process(text)
val sentences = annotation.get(classOf[CoreAnnotations.SentencesAnnotation])
val sentimentStringValuesBuf = scala.collection.mutable.ArrayBuffer.empty[String]
sentences.forEach { sentence =>
val sentiment = sentence.get(classOf[SentimentCoreAnnotations.SentimentClass])
sentimentStringValuesBuf += sentiment
}
indexOfHighest(sentimentCounter(sentimentStringValuesBuf.toList)) match {
case 0 => 1.0
case 1 => -1.0
case 2 => 0.0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment