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
( |
final |
frozenset[str]
|
Abbreviations that may end a sentence ( |
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: |
from_lines(lines)
classmethod
¶
Parse the abbreviations.txt format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lines
|
Iterable[str]
|
Lines with |
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: |
None
|
extra_abbreviations
|
Iterable[str]
|
Additional never-final abbreviations, e.g. domain
specific ones such as |
()
|
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 ( |
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 |
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: |
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 |
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. |
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 |
Examples:
>>> upper_tr("ışık istanbul")
'IŞIK İSTANBUL'