Sentiment analysis library for Elixir based on the AFINN word list. Fast, simple, and effective for analyzing the emotional tone of text.
The library is highly influenced by other afinn implementations and uses the well-established AFINN lexicon for sentiment scoring.
- 🚀 Fast and lightweight - Minimal dependencies, optimized for performance
- 🌍 Multi-language support - English 🇬🇧, Danish 🇩🇰, Finnish 🇫🇮, French 🇫🇷, Polish 🇵🇱, Swedish 🇸🇪, Turkish 🇹🇷, and Emoticons 😊
- 📊 Numeric scoring - Get sentiment scores from -5 (very negative) to +5 (very positive)
- 📝 Word classification - Convert scores to human-readable categories (positive, negative, neutral)
- 🔧 Simple API - Easy to integrate into any Elixir project
- Installation
- Usage
- Dictionaries
- Similar libraries in other programming languages
- Documentation
- Contributing
The package can be installed by adding afinn to your list of dependencies in mix.exs:
def deps do [ {:afinn, "~> 0.3.0"} ] end| Language | Symbol | Dictionary Size |
|---|---|---|
| English | :en | 1,677 words |
| Danish | :dk | 1,763 words |
| Finnish | :fi | 1,288 words |
| French | :fr | 1,096 words |
| Polish | :pl | 2,203 words |
| Swedish | :sv | 1,685 words |
| Turkish | :tr | 1,691 words |
| Emoticons | :emoticon | 48 emoticons |
# English sentiment analysis text = "I love this!" Afinn.score(text, :en) #=> 3 Afinn.score_to_words(text, :en) #=> :positive # Danish sentiment analysis Afinn.score("Dårligt produkt!", :dk) #=> -3 Afinn.score_to_words("Dårligt produkt!", :dk) #=> :negative # Finnish sentiment analysis Afinn.score("Rakastan tätä!", :fi) #=> 5 # French sentiment analysis Afinn.score("adorer ce produit", :fr) #=> 3 # Polish sentiment analysis Afinn.score("To jest wspaniałe!", :pl) #=> 5 # Swedish sentiment analysis Afinn.score("Jag älskar det här!", :sv) #=> 3 # Turkish sentiment analysis Afinn.score("güzel harika", :tr) #=> 6 # Emoticon sentiment analysis Afinn.score("I love this product :) :D", :emoticon) #=> 5# Analyzing mixed sentiment mixed_text = "I love the design but hate the performance" Afinn.score(mixed_text, :en) #=> 1 (love: +3, hate: -3, overall slightly positive due to "love" weight) # Neutral text neutral_text = "This is a product" Afinn.score_to_words(neutral_text, :en) #=> :neutral # Very positive text Afinn.score("Awesome! Great! Excellent! Love it!", :en) #=> 16 # Very negative text Afinn.score("Terrible! Awful! Horrible! Hate it!", :en) #=> -14The score_to_words/2 function returns:
:positive- for scores > 0:negative- for scores < 0:neutral- for scores = 0
The AFINN lexicon is a list of words rated for valence with an integer between -5 (very negative) and +5 (very positive). The dictionaries used in this library are from the project by Finn Årup Nielsen.
- Dictionary source: AFINN Data on GitHub
- Reference documentation: Corpus Text - AFINN Sentiment
- Academic paper: A new ANEW: Evaluation of a word list for sentiment analysis in microblogs
The library tokenizes input text and matches words against the AFINN dictionary. Each matched word contributes its sentiment score, and the total is summed to produce the final sentiment score. Words not in the dictionary contribute 0 to the score.
| Language | Repository | Description |
|---|---|---|
| Python | fnielsen/afinn | Original AFINN implementation |
| JavaScript | darenr/afinn | AFINN for Node.js |
| Ruby | prograils/afinn | AFINN for Ruby |
Returns the sentiment score for the given text.
@spec score(String.t(), :en | :dk) :: integer()Parameters:
text- The text to analyzelanguage- Language atom (:enfor English,:dkfor Danish)
Returns: Integer sentiment score
Returns a human-readable sentiment classification.
@spec score_to_words(String.t(), :en | :dk) :: :positive | :negative | :neutralParameters:
text- The text to analyzelanguage- Language atom (:enfor English,:dkfor Danish)
Returns: Atom representing sentiment (:positive, :negative, or :neutral)
Full documentation is available on HexDocs.
To generate documentation locally:
mix docs- Elixir 1.14 or higher
- Erlang/OTP 25 or higher
Contributions are welcome! Please feel free to submit a Pull Request.
# Clone the repository git clone https://github.com/kupolak/afinn.git cd afinn # Install dependencies mix deps.get # Run tests mix test # Run code formatter mix format # Run linter mix credo --strict- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is available as open source. See the LICENSE file for more info.
- AFINN word list: Created by Finn Årup Nielsen
- Library maintainer: @kupolak
Note: Sentiment analysis using word lists like AFINN is best suited for short texts like tweets, reviews, and comments. For more complex sentiment analysis tasks, consider using machine learning-based approaches.