Localize.Inflection.Inflector (Localize v1.4.2)

Copy Markdown View Source

Inflects words to a target set of grammatical constraints using the lexical dictionary and its inflection patterns.

This module mirrors the upstream Inflector_InflectionPattern, MorphologicalAnalyzer and DictionaryLookupInflector behavior: a word's candidate readings are ranked (optionally biased by disambiguation grammemes and per-language priority tables) and the best matching inflection row supplies the replacement suffix.

Summary

Functions

Inflects a word to the given constraints.

Inflects a word without trying case variants.

Reinflects a surface form within one pattern from its current grammemes to the target constraint mask.

Functions

inflect(locale, word, word_grammemes, constraints, options \\ [])

Inflects a word to the given constraints.

Arguments

  • locale is any locale atom for which data has been generated.

  • word is the word to inflect.

  • word_grammemes is the combined grammeme mask for word (from Localize.Inflection.Dictionary.combined_grammemes/2).

  • constraints is a list of grammeme names to inflect to, such as ["plural"].

Options

  • :disambiguation is a list of grammeme names used to prefer readings, in priority order (for example ["noun"]).

  • :optional_constraints is a list of grammeme names preferred but not required in the target inflection row.

  • :priorities is a list of grammeme-name lists used to rank readings when disambiguation does not decide, mirroring the upstream per-language priority tables.

  • :fallback when true (the default) tries further readings when the best reading cannot be inflected.

Returns

  • {:ok, inflected} or :error when the word cannot be inflected to the constraints.

Examples

iex> grammemes = Localize.Inflection.Dictionary.combined_grammemes(:en, "cat")
iex> Localize.Inflection.Inflector.inflect(:en, "cat", grammemes, ["plural"])
{:ok, "cats"}

inflect_word(locale, word, word_grammemes, constraints, options)

Inflects a word without trying case variants.

Arguments

  • locale is any locale atom for which data has been generated.

  • word is the word to inflect, matched as written.

  • word_grammemes is the combined grammeme mask for word.

  • constraints is a list of grammeme names to inflect to, such as ["plural"].

  • options is a keyword list of options.

Options

Returns

  • {:ok, inflected} or :error when the word cannot be inflected to the constraints.

Examples

iex> grammemes = Localize.Inflection.Dictionary.combined_grammemes(:en, "cat")
iex> Localize.Inflection.Inflector.inflect_word(:en, "cat", grammemes, ["plural"], [])
{:ok, "cats"}

reinflect(pattern, from_grammemes, to_mask, optional_masks \\ [], surface_form, tie_break \\ :first)

Reinflects a surface form within one pattern from its current grammemes to the target constraint mask.

Arguments

  • pattern is a pattern map from Localize.Inflection.Dictionary.patterns_for_word/2.

  • from_grammemes is the grammeme mask of the reading the surface form has.

  • to_mask is the grammeme mask to inflect to; 0 returns the surface form unchanged.

  • optional_masks is a list of grammeme masks preferred but not required in the target row. The default is [].

  • surface_form is the word as written.

  • tie_break is :first to keep the first of equally scored rows or :last to take the last. The default is :first.

Returns

  • {:ok, inflected} or :error when the pattern has no row satisfying the constraints.

Examples

iex> [pattern] =
...>   for pattern <- Localize.Inflection.Dictionary.patterns_for_word(:en, "cats"),
...>       "noun" in Localize.Inflection.Dictionary.property_names(:en, pattern.pos_mask),
...>       do: pattern
iex> from = Localize.Inflection.Dictionary.binary_properties(:en, ["noun", "plural"])
iex> to = Localize.Inflection.Dictionary.binary_properties(:en, ["singular"])
iex> Localize.Inflection.Inflector.reinflect(pattern, from, to, "cats")
{:ok, "cat"}