ggb / elm-sentiment / Sentiment

elm-sentiment is an Elm module that uses the AFINN-111 wordlist to perform sentiment analysis on arbitrary blocks of input text. Other wordlists are easy to integrate.

It is inspired by the the Sentiment-module for Node.js.

Please note that a wordlist-based approach for sentiment analysis might not be the best available approach for every (your) application. It is a simple and easy to use solution that does not need training like a Bayes classifier, that might perform better in classifying sentiments.


type alias Result =
{ tokens : List String
, score : Basics.Int
, words : List String
, positive : List Basics.Int
, negative : List Basics.Int
, comparative : Basics.Float 
}

The Result-type describes the information returned by a call to analyse or analyseWith. The struct contains all tokens, the sum of positive and negative scores etc. The value comparative is the overall score divided by the number of words in the input string.

Helper

trim : String -> String

Trim a given input string.

import Sentiment

Sentiment.trim "  --- Hello!, "

-- Hello

tokenize : String -> List String

Split a string into words, turn all words into lowercase and remove everything that is not necessary (e. g. whitespace or special characters).

import Sentiment

Sentiment.tokenize " --- Hello, World! :) "

-- ["hello","world"]

Analysis

analyse : String -> Result

Analyse a string and return a struct of type Result. The function basically calls analyseWith, but with (good) defaults.

import Sentiment

result = Sentiment.analyse "Best movie ever!"

-- result.score == 3

analyseWith : Dict String Basics.Int -> Dict String Basics.Int -> (String -> List String) -> String -> Result

Analyse a given string and return a struct of type Result. This function expects a dictionary containing a word list, a dictionary with additional sentiment information and a tokenizer function.

import Sentiment
import WordList.Afinn as Afinn
import String
import Dict

result = 
  Sentiment.analyseWith 
    Afinn.get
    (Dict.fromList [(":-)",3),(":-|", 0),(":-(", -3)])
    (String.toLower >> String.words)
    "Best movie ever! :-)"

-- result.score == 6