package com.github.bot.curiosone.core.analysis;

import static java.util.Comparator.comparing;

import com.github.bot.curiosone.core.nlp.Meaning;
import com.github.bot.curiosone.core.nlp.POS;
import com.github.bot.curiosone.core.nlp.Token;
import java.util.List;
import java.util.OptionalDouble;

/**
 * Calculates the Sentiment of a Sentence.
 * Provides methods to calcolate the sentiment score from a single Token or a Token List.
 */

public class TokenScorer {

  /**
   * Stores the dictionary instance, used to compute the sentiment index value.
   */
  private static DictionaryLoader dict = DictionaryLoader.getInstance();

  /**
   * Calculates the sentiment value for a given Token.
   * This value ranges between -1.0 and 1.0.
   * @param token The Token to be analysed.
   * @return a Double value, representing the sentiment index for the given Token.
   *     This index ranges between -1.00 (sad) and 1.00 (happy), 0.00 is a netural score
   */
  public static double calculateScore(Token token) {
    POS pos = token.getMeanings().stream()
        .sorted(comparing(Meaning::getFrequency).reversed())
        .findFirst()
        .get().getPOS();
    return (pos == POS.ADJ || pos == POS.V || pos == POS.ADV || pos == POS.N)
        ? dict.getScore(token.getLemma()) : 0.0;
  }

  /**
   * Calculates the sentiment value for a given Token List.
   * This value ranges between -1.0 and 1.0.
   * @param tokenList The Token List to be analysed.
   * @return a Double value, representing the sentiment index for the given Token List.
   *     This index ranges between -1.00 (sad) and 1.00 (happy), 0.00 is a netural score
   */
  public static double calculateScore(List<Token> tokenList) {
    OptionalDouble score = tokenList.stream()
        .mapToDouble(TokenScorer::calculateScore)
        .filter(x -> x != 0.0)
        .average();
    return score.isPresent() ? score.getAsDouble() : 0.0;
  }
}

@@ -322,16 +322,22 @@
 ntiment 
+score 
 of a Sen
@@ -370,17 +370,17 @@
  to calc
-o
+u
 late the
@@ -433,16 +433,65 @@
 n List.%0A
+ * @see  com.github.bot.curiosone.core.nlp.Token%0A
  */%0A%0Apub
@@ -574,25 +574,25 @@
 compute the 
-s
+S
 entiment ind
@@ -799,21 +799,35 @@
  @param 
+ 
 token
+%0A   *        
  The Tok
@@ -850,32 +850,33 @@
 d.%0A   * @return 
+ 
 a Double value, 
@@ -930,32 +930,37 @@
 Token.%0A   *     
+ 
+    
 This index range
@@ -1003,34 +1003,34 @@
 y), 0.00 is a ne
-t
 u
+t
 ral score%0A   */%0A
@@ -1015,32 +1015,83 @@
 a neutral score%0A
+   * @see  com.github.bot.curiosone.core.nlp.Token%0A
    */%0A  public s
@@ -1538,16 +1538,17 @@
  @param 
+ 
 tokenLis
@@ -1548,16 +1548,29 @@
 okenList
+%0A   *        
  The Tok
@@ -1606,16 +1606,17 @@
 @return 
+ 
 a Double
@@ -1687,24 +1687,29 @@
 t.%0A   *     
+    
+ 
 This index r
@@ -1764,18 +1764,18 @@
  is a ne
-t
 u
+t
 ral scor
@@ -1772,24 +1772,75 @@
 utral score%0A
+   * @see  com.github.bot.curiosone.core.nlp.Token%0A
    */%0A  publ

improve TokenScorer javadoc