Ana içeriğe geç

turkrag.text

Turkish text utilities: casing, normalization, tokenization, sentence splitting.

This module has no third-party dependencies.

Examples:

>>> from turkrag.text import lower_tr, split_sentences
>>> lower_tr("IĞDIR")
'ığdır'
>>> split_sentences("Dr. Ayşe geldi. Toplantı başladı.")
['Dr. Ayşe geldi.', 'Toplantı başladı.']

Abbreviations dataclass

A set of abbreviations split into two behaviour classes.

Attributes:

Name Type Description
nonfinal frozenset[str]

Abbreviations whose trailing period never ends a sentence (dr, prof, t.c).

final frozenset[str]

Abbreviations that may end a sentence (vb, vs). Their period ends the sentence only when the next word is capitalized.

extend(nonfinal=(), final=())

Return a new set with extra abbreviations added.

Parameters:

Name Type Description Default
nonfinal Iterable[str]

Abbreviations that never end a sentence.

()
final Iterable[str]

Abbreviations that may end a sentence.

()

Returns:

Type Description
Abbreviations

A new :class:Abbreviations instance; self is not modified.

from_lines(lines) classmethod

Parse the abbreviations.txt format.

Parameters:

Name Type Description Default
lines Iterable[str]

Lines with [nonfinal] / [final] section headers, one abbreviation per line and # comments.

required

Returns:

Type Description
Abbreviations

The parsed abbreviation set.

Raises:

Type Description
ValueError

If an abbreviation appears before any section header or an unknown section is used.

SentenceSplitter

Split Turkish text into sentences.

Parameters:

Name Type Description Default
abbreviations Abbreviations | None

Abbreviation set to use. Defaults to the bundled dictionary (see :func:turkrag.text.load_abbreviations).

None
extra_abbreviations Iterable[str]

Additional never-final abbreviations, e.g. domain specific ones such as "md" or "yön".

()
split_on_newline bool

Treat every single line break as a sentence boundary. Useful for lists and headings without punctuation.

False
split_glued bool

Also split punctuation directly followed by a capitalized word or digit (geldi.Sonra). URLs, decimals and dates are kept.

False

Examples:

>>> SentenceSplitter().split("T.C. Sağlık Bakanlığı'ndan Dr. Ayşe geldi. Sonra gitti.")
["T.C. Sağlık Bakanlığı'ndan Dr. Ayşe geldi.", 'Sonra gitti.']

spans(text)

Yield (start, end) character offsets of each sentence.

text[start:end] is the sentence without leading/trailing whitespace.

Parameters:

Name Type Description Default
text str

Input text.

required

Yields:

Type Description
tuple[int, int]

Offsets into the original text.

split(text)

Split text into sentences.

Parameters:

Name Type Description Default
text str

Input text.

required

Returns:

Type Description
list[str]

Sentences with surrounding whitespace stripped, in order. Empty input

list[str]

gives an empty list.

capitalize_tr(text)

Uppercase the first character and lowercase the rest, using Turkish rules.

Parameters:

Name Type Description Default
text str

Input text.

required

Returns:

Type Description
str

The capitalized text.

Examples:

>>> capitalize_tr("iSTANBUL")
'İstanbul'

load_abbreviations() cached

Load the bundled Turkish abbreviation dictionary.

Returns:

Type Description
Abbreviations

The default :class:Abbreviations (cached after the first call).

load_stopwords() cached

Load the bundled Turkish stopword list.

Returns:

Type Description
frozenset[str]

Lowercased, tokenized stopwords. The result is cached after the first call.

lower_tr(text)

Lowercase text using Turkish rules.

Parameters:

Name Type Description Default
text str

Input text.

required

Returns:

Type Description
str

The lowercased text, where I becomes ı and İ becomes i.

Examples:

>>> lower_tr("IŞIK İSTANBUL")
'ışık istanbul'

search_key(text)

Build an aggressive matching key for lexical search.

The key is lowercased with Turkish rules, has no circumflex accents, unified quotes and all whitespace (including newlines) collapsed to single spaces.

Parameters:

Name Type Description Default
text str

Input text.

required

Returns:

Type Description
str

The search key.

Examples:

>>> search_key("  KÂR\nİSTANBUL’da ")
"kar istanbul'da"

split_sentences(text)

Split Turkish text into sentences with the default :class:SentenceSplitter.

Parameters:

Name Type Description Default
text str

Input text.

required

Returns:

Type Description
list[str]

List of sentences.

Examples:

>>> split_sentences("Prof. Dr. Ali Bey 1. Dünya Savaşı'nı anlattı. Çok ilginçti!")
["Prof. Dr. Ali Bey 1. Dünya Savaşı'nı anlattı.", 'Çok ilginçti!']

strip_circumflex(text)

Replace circumflexed vowels with their plain forms (kâr -> kar).

Parameters:

Name Type Description Default
text str

Input text.

required

Returns:

Type Description
str

Text without circumflex accents. Î maps to İ because î is a

str

dotted vowel in Turkish.

upper_tr(text)

Uppercase text using Turkish rules.

Parameters:

Name Type Description Default
text str

Input text.

required

Returns:

Type Description
str

The uppercased text, where i becomes İ and ı becomes I.

Examples:

>>> upper_tr("ışık istanbul")
'IŞIK İSTANBUL'