sok.file_operations.subtitle_language
The language of a subtitle file, from its name or from its text.
The name is read first: "fr", "fre", "French", "VOSTFR" and the like say the language. When the name says nothing, the text is read and its language guessed with langdetect, only when the guess is sure: enough dialogue, a high probability, and both halves of the text agree. A subtitle is never given a language S.O.K is unsure of.
The language is written as a two-letter code ("fr"), the form Kodi, Jellyfin and Plex read.
Functions
dialogue
def dialogue(text: str, extension: str) -> List[str]:The lines spoken in a subtitle, without numbers, timings or tags.
Parameters
| Name | Type | Description |
|---|---|---|
text | str | |
extension | str |
flags_from_name
def flags_from_name(name: str) -> List[str]:The flags of a name, in the order of FLAGS ("English SDH" → ["sdh"]).
Parameters
| Name | Type | Description |
|---|---|---|
name | str |
guess_language
def guess_language(lines: List[str]) -> Optional[str]:The language of some dialogue, only when the guess is sure.
Parameters
| Name | Type | Description |
|---|---|---|
lines | List[str] |
language_from_name
def language_from_name(name: str) -> Optional[str]:The language a name says, or None ("Movie.fr.forced" → "fr").
"Portuguese (Brazil)" is Brazilian Portuguese, not Portuguese.
Parameters
| Name | Type | Description |
|---|---|---|
name | str |
language_from_text
def language_from_text(path: Path) -> Optional[str]:The language of a subtitle file read from its content, or None.
A VobSub .idx says it on its "id:" lines; a text subtitle is guessed from its dialogue. A picture subtitle (VobSub .sub) cannot be read.
Parameters
| Name | Type | Description |
|---|---|---|
path | Path |
name_words
def name_words(name: str) -> List[str]:The words of a name, folded: "2_English [SDH]" → ["2", "english", "sdh"].
Parameters
| Name | Type | Description |
|---|---|---|
name | str |
read_text
def read_text(path: Path) -> Optional[str]:The text of a subtitle file, or None when its encoding is unsure.
UTF-8 and UTF-16 (with a byte order mark) are read as they are. Other files are read as Windows-1252 only when nearly all their letters are plain ASCII: a Cyrillic or Greek file in an old code page would come out as nonsense and get a wrong language.
Parameters
| Name | Type | Description |
|---|---|---|
path | Path |
Attributes
| Name | Value |
|---|---|
FLAGS | ('forced', 'sdh', 'cc', 'hi') |
MAX_BYTES | 1024 * 1024 |
MAX_SAMPLE | 20000 |
MIN_LETTERS | 600 |
MIN_LINES | 30 |
MIN_PROBABILITY | 0.95 |
TEXT_EXTENSIONS | ('.srt', '.ass', '.ssa', '.vtt', '.sub') |
WORD_LANGUAGES | {word: code for code, words in _LANGUAGES.items() for word in words} |
logger | logging.getLogger(__name__) |