Text.Sentiment.Lexicons.AFINN (Text v0.5.0)

Copy Markdown View Source

Bundled AFINN sentiment lexicons.

AFINN is a list of words rated for sentiment polarity on an integer scale from -5 (extremely negative) to +5 (extremely positive), with 0 reserved for neutral terms. The English list (AFINN-165) has roughly 3,400 entries; community translations cover ~100 additional languages at varying quality.

All AFINN data is distributed under the Apache 2.0 license, the same as this package, and is bundled in priv/sentiment/.

This module exposes:

  • lexicon/1 — returns the AFINN word→score map for a language tag, plus two synthetic lexicons:

    • :emoticon — text emoticons (:-), :(, <3, …).
    • :emoji — Unicode emoji, derived from the Emoji Sentiment Ranking 1.0 corpus and rescaled to AFINN's −5..+5 range.
  • negators/1 — returns the per-language list of negation phrases used by Text.Sentiment.Backends.Lexicon to flip polarity for the word that follows. The English fallback is used when a language has no negator list.

  • available/0 — every loaded language tag, including :emoji and :emoticon.

Quality note

The English (:en), Danish (:da), Finnish (:fi), French (:fr), Polish (:pl), Swedish (:sv), and Turkish (:tr) lexicons are hand-curated by the upstream AFINN project. The other ~95 language lexicons are machine-translated derivatives from the same upstream source — useful as a baseline, but expect noisier scores for low-resource languages.

Tag list

Language tags are the upstream filenames lower-cased: typically ISO 639-1 two-letter codes (:en, :de, :ja), occasionally three-letter codes (:ceb, :haw, :hmn), and one BCP-47-style tag (:"zh-tw"). Note that the upstream uses the legacy ISO codes :iw for Hebrew and :jw for Javanese; modern code values are :he and :jv respectively.

Use available/0 to enumerate the tags loaded into the running build.

Summary

Types

An AFINN language tag, plus the synthetic :emoticon / :emoji.

Functions

Returns the list of every loaded AFINN language tag, sorted.

Returns true if tag has a curated negator list.

Returns the bundled AFINN lexicon for tag as a %{token => integer} map.

Returns the list of negation phrases for the given language tag, used by Text.Sentiment.Backends.Lexicon to flip the polarity of the word immediately following a negator.

Types

tag()

@type tag() :: atom()

An AFINN language tag, plus the synthetic :emoticon / :emoji.

Functions

available()

@spec available() :: [tag()]

Returns the list of every loaded AFINN language tag, sorted.

The list always includes the synthetic :emoticon and :emoji lexicons.

Examples

iex> :en in Text.Sentiment.Lexicons.AFINN.available()
true

iex> :emoji in Text.Sentiment.Lexicons.AFINN.available()
true

has_negators?(tag)

@spec has_negators?(atom()) :: boolean()

Returns true if tag has a curated negator list.

Examples

iex> Text.Sentiment.Lexicons.AFINN.has_negators?(:en)
true

iex> Text.Sentiment.Lexicons.AFINN.has_negators?(:nonexistent)
false

lexicon(tag)

@spec lexicon(tag()) :: %{required(String.t()) => integer()}

Returns the bundled AFINN lexicon for tag as a %{token => integer} map.

Raises ArgumentError if tag is not in available/0.

Examples

iex> Text.Sentiment.Lexicons.AFINN.lexicon(:en) |> Map.get("good")
3

iex> Text.Sentiment.Lexicons.AFINN.lexicon(:fr) |> Map.get("excellent")
4

iex> Text.Sentiment.Lexicons.AFINN.lexicon(:emoticon) |> Map.get(":-)")
2

iex> Text.Sentiment.Lexicons.AFINN.lexicon(:emoji) |> Map.get("😂")
2

negators(tag)

@spec negators(atom()) :: [String.t()]

Returns the list of negation phrases for the given language tag, used by Text.Sentiment.Backends.Lexicon to flip the polarity of the word immediately following a negator.

Falls back to a built-in English negator list when the tag has no curated entry.

Examples

iex> "not" in Text.Sentiment.Lexicons.AFINN.negators(:en)
true

iex> Text.Sentiment.Lexicons.AFINN.negators(:fr) |> Enum.take(1) |> length()
1