Ana içeriğe geç

turkrag.stem

Pluggable Turkish stemmers.

Built-in stemmers (select by name with :func:get_stemmer):

  • "snowball" (default): fast rule-based Snowball stemmer.
  • "prefix": keep the first N characters (default 5), a strong Turkish IR baseline.
  • "zemberek": dictionary-based lemmatizer; needs turkrag[zemberek].
  • "none": no stemming.

Examples:

>>> from turkrag.stem import get_stemmer
>>> get_stemmer("snowball").analyze("Kitaplarımızdan birini okudum.")
['kitap', 'bir', 'okudu']

IdentityStemmer

Bases: Stemmer

Return words unchanged. Useful as a benchmark baseline.

Examples:

>>> IdentityStemmer()("kitaplarımızdan")
'kitaplarımızdan'

stem(word)

Return word unchanged.

Parameters:

Name Type Description Default
word str

Input word.

required

Returns:

Type Description
str

The same word.

PrefixStemmer

Bases: Stemmer

Truncate words to their first length characters.

Fixed-prefix truncation ("F5") is a surprisingly strong baseline for Turkish information retrieval, because Turkish is almost purely suffixing.

Parameters:

Name Type Description Default
length int

Number of characters to keep.

5

Examples:

>>> PrefixStemmer(length=5)("kitaplarımızdan")
'kitap'

stem(word)

Return the first length characters of word.

Parameters:

Name Type Description Default
word str

Input word.

required

Returns:

Type Description
str

The truncated word.

SnowballStemmer

Bases: Stemmer

Turkish Snowball stemmer from the snowballstemmer package.

Fast, rule-based and lightweight; it removes inflectional suffixes but does not use a dictionary, so stems are not always real words (türkiye -> türki).

Parameters:

Name Type Description Default
cache_size int

Maximum number of cached words (0 disables the cache).

100000
protected_words Iterable[str] | None

Optional collection of words that should not be stemmed.

None

Examples:

>>> SnowballStemmer()("kitaplarımızdan")
'kitap'
>>> SnowballStemmer(protected_words={"türkiye"})("türkiye")
'türkiye'

stem(word)

Stem a lowercase Turkish word.

Parameters:

Name Type Description Default
word str

Input word.

required

Returns:

Type Description
str

The Snowball stem, or the word unchanged if protected.

Stemmer

Bases: ABC

Base class for stemmers.

Subclasses implement :meth:stem for a single lowercase word. Results are cached per instance, because the same words repeat constantly in a corpus.

Parameters:

Name Type Description Default
cache_size int

Maximum number of cached words (0 disables the cache).

100000

__call__(word)

Stem word with caching; an empty stem falls back to word.

analyze(text)

Tokenize text with :func:turkrag.text.tokenize and stem each token.

Parameters:

Name Type Description Default
text str

Raw text.

required

Returns:

Type Description
list[str]

Stems in order.

stem(word) abstractmethod

Return the stem of a single word.

Parameters:

Name Type Description Default
word str

A lowercase word without punctuation (as produced by :func:turkrag.text.tokenize).

required

Returns:

Type Description
str

The stem. Must be non-empty for non-empty input.

stem_tokens(tokens)

Stem a sequence of tokens.

Parameters:

Name Type Description Default
tokens Iterable[str]

Lowercase tokens.

required

Returns:

Type Description
list[str]

Stems in the same order.

ZemberekStemmer

Bases: Stemmer

Morphological analysis with Zemberek.

More accurate than Snowball (it knows consonant mutation: kitabı -> kitap, nehri -> nehir) but much slower and heavier. Install with pip install "turkrag[zemberek]".

Word-level analysis is ambiguous; this stemmer prefers common-word lemmas over proper nouns (nehri -> nehir, not the name Nehri) and keeps a word unchanged when it is itself a lemma. Unknown words are returned unchanged.

Parameters:

Name Type Description Default
mode Literal['lemma', 'stem']

"lemma" (dictionary form, e.g. gitmek) or "stem" (surface stem, e.g. gid).

'lemma'
cache_size int

Maximum number of cached words.

200000

stem(word)

Return the Zemberek lemma (or stem) of word.

Parameters:

Name Type Description Default
word str

Lowercase input word.

required

Returns:

Type Description
str

The lowercase lemma/stem, or word if Zemberek does not know it.

get_stemmer(stemmer='snowball', **kwargs)

Resolve a stemmer from a name, an instance or None.

Parameters:

Name Type Description Default
stemmer StemmerLike

A registered name ("snowball", "prefix", "zemberek", "none"), a :class:Stemmer instance (returned as is) or None (same as "none").

'snowball'
**kwargs Any

Constructor arguments when stemmer is a name.

{}

Returns:

Type Description
Stemmer

A stemmer instance.

register_stemmer(name)

Class decorator that registers a :class:Stemmer under name.

Parameters:

Name Type Description Default
name str

Short lowercase name, e.g. "snowball".

required

Returns:

Type Description
Any

The decorator.

Examples:

>>> @register_stemmer("upper-demo")
... class UpperStemmer(Stemmer):
...     def stem(self, word: str) -> str:
...         return word.upper()
>>> get_stemmer("upper-demo")("kitap")
'KITAP'