Localize.Inflection.Dictionary (Localize v1.4.3)

Copy Markdown View Source

Low-level access to the lexical dictionary for a locale.

Words map to grammatical properties (grammemes) represented as integer bitmasks, and to the inflection patterns they belong to. This module mirrors the upstream DictionaryMetaData and DictionaryExposableMorphology APIs.

Summary

Functions

Returns the combined bitmask for a list of grammeme names.

Returns the combined grammeme bitmask for a surface form, or nil if the word is not in the dictionary.

Returns the grammeme bitmask sets for each reading of a word.

Returns true if the word has all the properties in mask.

Returns the inflection rows of pattern that could have produced surface_form given the word's combined grammemes.

Returns the inflection patterns for a word.

Returns the grammeme name for a mask with exactly one bit set.

Returns the list of grammeme names present in mask.

Functions

binary_properties(locale, names)

Returns the combined bitmask for a list of grammeme names.

Names unknown to the locale are skipped, as upstream: the Arabic definiteness lookup asks for "definite" although no dictionary entry carries it.

Arguments

  • locale is a locale identifier.

  • names is a list of grammeme names, such as ["noun", "plural"].

Returns

  • The combined bitmask, or nil when none of the names is known.

Examples

iex> mask = Localize.Inflection.Dictionary.binary_properties(:en, ["noun", "plural"])
iex> Localize.Inflection.Dictionary.has_all_properties?(:en, "cats", mask)
true

iex> Localize.Inflection.Dictionary.binary_properties(:en, ["not-a-grammeme"])
nil

combined_grammemes(locale, word)

Returns the combined grammeme bitmask for a surface form, or nil if the word is not in the dictionary.

Arguments

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

  • word is the surface form to look up.

Returns

  • The combined bitmask of all grammemes of all readings of word, or nil if word is unknown.

Examples

iex> mask = Localize.Inflection.Dictionary.combined_grammemes(:en, "cats")
iex> Enum.sort(Localize.Inflection.Dictionary.property_names(:en, mask))
["consonant-start", "noun", "plural", "present", "simple", "singular", "third", "verb"]

iex> Localize.Inflection.Dictionary.combined_grammemes(:en, "xyzzy")
nil

grammeme_sets(locale, word)

Returns the grammeme bitmask sets for each reading of a word.

A word that belongs to inflection patterns gets one grammeme set per matching inflection row (the row grammemes combined with the pattern part of speech). A word without patterns returns its combined grammemes as the single set.

Arguments

  • locale is a locale identifier.

  • word is the word to look up.

Returns

  • A list of grammeme bitmasks, one for each reading, or [] when the word is unknown.

Examples

iex> for set <- Localize.Inflection.Dictionary.grammeme_sets(:en, "cats") do
...>   Enum.sort(Localize.Inflection.Dictionary.property_names(:en, set))
...> end
[["present", "simple", "singular", "third", "verb"], ["noun", "plural"]]

has_all_properties?(locale, word, mask)

Returns true if the word has all the properties in mask.

Arguments

  • locale is a locale identifier.

  • word is the word to look up.

  • mask is a grammeme bitmask.

Returns

  • true when the word's combined grammemes include every bit of mask, and false otherwise or when the word is unknown.

Examples

iex> mask = Localize.Inflection.Dictionary.binary_properties(:en, ["noun", "plural"])
iex> Localize.Inflection.Dictionary.has_all_properties?(:en, "cats", mask)
true
iex> Localize.Inflection.Dictionary.has_all_properties?(:en, "xyzzy", mask)
false

matching_inflections(surface_form, from_grammemes, arg3)

Returns the inflection rows of pattern that could have produced surface_form given the word's combined grammemes.

Only rows whose grammemes are a subset of from_grammemes and whose suffix is a suffix of the surface form qualify; among those, only the rows with the longest matching suffix are returned.

Arguments

  • surface_form is the word as written.

  • from_grammemes is the word's combined grammeme bitmask.

  • pattern is a pattern map from patterns_for_word/2.

Returns

  • A list of {grammemes, suffix} rows, or [] when none qualifies.

Examples

iex> [pattern | _rest] = Localize.Inflection.Dictionary.patterns_for_word(:en, "cats")
iex> grammemes = Localize.Inflection.Dictionary.combined_grammemes(:en, "cats")
iex> for {_grammemes, suffix} <-
...>       Localize.Inflection.Dictionary.matching_inflections("cats", grammemes, pattern),
...>     do: suffix
["s"]

patterns_for_word(locale, word)

Returns the inflection patterns for a word.

Arguments

  • locale is a locale identifier.

  • word is the word to look up.

Returns

  • A list of pattern maps, each with :name, :inflections, :pos_mask and :lemma_suffix, or [] when the word is unknown or uninflectable.

Examples

iex> length(Localize.Inflection.Dictionary.patterns_for_word(:en, "cats"))
2
iex> Localize.Inflection.Dictionary.patterns_for_word(:en, "xyzzy")
[]

property_name(locale, mask)

Returns the grammeme name for a mask with exactly one bit set.

Arguments

  • locale is a locale identifier.

  • mask is a grammeme bitmask.

Returns

  • The grammeme name, or nil when mask does not have exactly one bit set or names no grammeme of the locale.

Examples

iex> mask = Localize.Inflection.Dictionary.binary_properties(:en, ["plural"])
iex> Localize.Inflection.Dictionary.property_name(:en, mask)
"plural"

property_names(locale, mask)

Returns the list of grammeme names present in mask.

Arguments

  • locale is a locale identifier.

  • mask is a grammeme bitmask.

Returns

  • A list of grammeme names, empty where the locale has no inflection data.

Examples

iex> mask = Localize.Inflection.Dictionary.binary_properties(:en, ["noun", "plural"])
iex> Enum.sort(Localize.Inflection.Dictionary.property_names(:en, mask))
["noun", "plural"]