An inflectable string with grammatical constraints.
A concept wraps a word or phrase for a locale. Constraints (such
as number=plural or definiteness=definite) are applied with
put_constraint/3; grammatical properties are queried with
feature_value/2; and to_speakable_string/1 renders the phrase
with all constraints applied.
A concept can also carry forms of its own for sets of constraints,
given as :display_data to new/3. It then renders as the first
of its forms that holds every constraint put on it, and otherwise
as the locale's rules inflect the phrase.
This is the equivalent of the upstream InflectableStringConcept,
and with display data of the upstream SemanticConcept.
Summary
Functions
Returns true when the concept can be rendered with its constraints without guessing.
Returns the value of a feature for this concept.
Creates a concept for a word or phrase.
Puts a constraint on the concept.
Renders the concept with all constraints applied.
Types
@type t() :: %Localize.Inflection.Concept{ constraints: %{optional(binary()) => binary()}, display_data: [ {Localize.Inflection.SpeakableString.t(), %{optional(binary()) => binary()}} ], guess: boolean(), initial: %{optional(binary()) => binary()}, locale: atom(), value: Localize.Inflection.SpeakableString.t() }
Functions
Returns true when the concept can be rendered with its constraints without guessing.
Rendering without guessing includes the synthesizers' unchanged
passthrough: a word the dictionary does not know may still render
as itself, so exists?/1 answers "does rendering produce
something?", not "is the requested form attested in the
dictionary?". Use Localize.Inflection.known?/2 to test
dictionary membership.
Arguments
conceptis a concept fromnew/3.
Returns
truewhen rendering produces a form, andfalseotherwise or whenconceptis not a concept.
Examples
iex> {:ok, concept} = Localize.Inflection.Concept.new(:en, "cat", constraints: %{number: :plural})
iex> Localize.Inflection.Concept.exists?(concept)
true
Returns the value of a feature for this concept.
A stored constraint takes precedence. A concept with display data next takes the value its rendered form has; otherwise the locale's default feature function computes the value from the (possibly inflected) display string.
Arguments
conceptis a concept fromnew/3.nameis a feature name such as:numberor"gender".
Returns
- A speakable string, or nil when the feature has no value.
Examples
iex> {:ok, concept} = Localize.Inflection.Concept.new(:en, "light")
iex> Localize.Inflection.Concept.feature_value(concept, :number)
:singular
Creates a concept for a word or phrase.
Arguments
localeis a locale atom or string, canonically BCP47 (:en,"zh-TW"), for which data has been generated.valueis the word or phrase, either a binary or a{print, speak}tuple.optionsis a keyword list of options.
Options
:constraintsis a map of feature constraints to apply, as if set withput_constraint/3.:initialis a map of features known to hold for the value itself (for example a known grammatical gender), used when deriving other features rather than requested for rendering.:display_datais a list of{form, constraints}entries, forms of the concept and the features each has, such as{"octopodes", %{number: :plural}}. A form is a binary or a{print, speak}tuple. The concept renders as the first form, starting withvalueand its:initialfeatures, that holds every constraint put on it, and takes its features from that form; when no form does, the locale's rules inflectvalue.
Returns
{:ok, concept}or{:error, reason}when the locale data is unavailable, or a constraint or display data entry is invalid.
Examples
iex> {:ok, concept} = Localize.Inflection.Concept.new(:en, "cat", constraints: %{number: :plural})
iex> Localize.Inflection.Concept.to_speakable_string(concept)
"cats"
iex> {:error, error} = Localize.Inflection.Concept.new(:en, "cat", constraints: %{number: :dual})
iex> error.value
"dual"
iex> error.allowed_values
["plural", "singular"]
iex> display_data = [{"octopodes", %{number: :plural}}]
iex> {:ok, concept} = Localize.Inflection.Concept.new(:en, "octopus", display_data: display_data)
iex> {:ok, concept} = Localize.Inflection.Concept.put_constraint(concept, :number, :plural)
iex> Localize.Inflection.Concept.to_speakable_string(concept)
"octopodes"
Puts a constraint on the concept.
Arguments
conceptis a concept fromnew/3.nameis a feature name such as "number" or "definiteness".valueis the constraint value such as "plural".
Returns
{:ok, concept}or{:error, reason}when the feature is unknown or the value is not valid for a bounded feature.
Examples
iex> {:ok, concept} = Localize.Inflection.Concept.new(:en, "light")
iex> {:ok, concept} = Localize.Inflection.Concept.put_constraint(concept, :number, :plural)
iex> Localize.Inflection.Concept.to_speakable_string(concept)
"lights"
Renders the concept with all constraints applied.
The synthesizers may fall back to suffix-exemplar guessing for
words the dictionary does not know. Setting the concept's
guess field to false disables guessing: forms then change
only through attested dictionary paths, and a failed render
returns the value unchanged.
Arguments
Returns
- A speakable string (a binary, or
{print, speak}when the spoken form differs).
Examples
iex> {:ok, concept} = Localize.Inflection.Concept.new(:en, "cat", constraints: %{case: :genitive})
iex> Localize.Inflection.Concept.to_speakable_string(concept)
"cat’s"