Behaviour for sentiment-analysis backends.
Two backends are shipped with text:
Text.Sentiment.Backends.Lexicon(default) — fast, deterministic, multilingual lexicon-based scoring. Always available.Text.Sentiment.Backends.Bumblebee(optional) — neural sentiment via Bumblebee and a pre-trained multilingual transformer (XLM-RoBERTa). Higher quality but requires a model download and the optional:bumblebeedep.
Routing is controlled by the :backend option to
Text.Sentiment.analyze/2 (and label/2), or globally via the
:sentiment_backend application configuration:
# config/config.exs
config :text, :sentiment_backend, Text.Sentiment.Backends.BumblebeeCustom backends can be supplied by implementing this behaviour.
Summary
Callbacks
Returns a sentiment result for text.
Functions
Returns the backend module currently configured for use.
Types
@type result() :: %{ :label => :positive | :negative | :neutral, :compound => float(), optional(:sum) => float(), optional(:tokens) => non_neg_integer(), optional(:matched) => non_neg_integer(), optional(:language) => atom() | nil, optional(:backend) => module(), optional(:model) => String.t(), optional(:scores) => %{required(atom()) => float()} }
The structured result every backend returns.
Callbacks
Returns a sentiment result for text.
Backends are free to interpret options however they see fit, but every backend must:
Accept any UTF-8 binary as input.
Always return a map with at least
:labeland:compound.Either accept the standard
:languageoption (atom, string, orLocalize.LanguageTag— seeText.Language.normalize/1) or document loudly that they ignore it.
Functions
Returns the backend module currently configured for use.
The resolution order is:
- The
:backendkeyword option, if present. - The application config value at
Application.get_env(:text, :sentiment_backend). - The default —
Text.Sentiment.Backends.Lexicon.
Examples
iex> Text.Sentiment.Backend.resolve([])
Text.Sentiment.Backends.Lexicon
iex> Text.Sentiment.Backend.resolve(backend: Text.Sentiment.Backends.Lexicon)
Text.Sentiment.Backends.Lexicon