### Install RusStress with Pip Source: https://github.com/mashapo/russtress/blob/master/README.rst Install the RusStress library using pip. This is the simplest way to get started. ```bash pip install russtress ``` -------------------------------- ### Process Files with RusStress Source: https://context7.com/mashapo/russtress/llms.txt Read text from files, apply stress marks, and save the output. Includes an example for line-by-line processing of large files. ```python from russtress import Accent accent = Accent() # Read input file with open('input.txt', 'r', encoding='utf-8') as f: text = f.read() # Add stress marks stressed_text = accent.put_stress(text) # Write output file with open('output_stressed.txt', 'w', encoding='utf-8') as f: f.write(stressed_text) # Process line by line for large files with open('large_input.txt', 'r', encoding='utf-8') as infile: with open('large_output.txt', 'w', encoding='utf-8') as outfile: for line in infile: stressed_line = accent.put_stress(line) outfile.write(stressed_line) ``` -------------------------------- ### Preserve Punctuation and Formatting with put_stress Source: https://context7.com/mashapo/russtress/llms.txt The `put_stress` method preserves original formatting, punctuation, and capitalization when adding stress marks. This example shows stress being added to a sentence with a period. ```python from russtress import Accent accent = Accent() # Sentence with punctuation preserved sentence = 'Мама мыла раму.' print(accent.put_stress(sentence)) # Output: "Ма'ма мы'ла ра'му." ``` -------------------------------- ### Custom Category Tokenization Source: https://context7.com/mashapo/russtress/llms.txt The `tokenize` function allows defining custom character categories for segmentation. This example specifies categories for digits, whitespace, and Cyrillic characters. ```python from russtress.tokenizer import tokenize # Custom category tokenization custom_categories = [ '0123456789', ' ', 'абвгдеёжзийклмнопрстуфхцчшщъыьэюяАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ' ] tokens = tokenize('Тест 123', categories=custom_categories) print(tokens) # Output: ['Тест', ' ', '123'] ``` -------------------------------- ### Tokenization Preserving All Characters Source: https://context7.com/mashapo/russtress/llms.txt The `tokenize` function from `russtress.tokenizer` preserves all characters, including numbers and punctuation, when segmenting text. This example demonstrates tokenizing a sentence with numbers. ```python from russtress.tokenizer import tokenize # Tokenization preserves all characters text_with_numbers = 'В 2024 году будет 366 дней.' tokens = tokenize(text_with_numbers) print(tokens) # Output: ['В', ' ', '2024', ' ', 'году', ' ', 'будет', ' ', '366', ' ', 'дней', '.'] ``` -------------------------------- ### Use Custom Stress Symbol Source: https://context7.com/mashapo/russtress/llms.txt The `put_stress` method allows specifying a custom stress symbol. This example uses the combining acute accent character (U+0301) instead of an apostrophe. ```python from russtress import Accent accent = Accent() # Custom stress symbol text = 'Привет мир' accented = accent.put_stress(text, stress_symbol='́') # Using combining acute accent print(accented) # Output: "Приве́т ми́р" ``` -------------------------------- ### Initialize Accent Model and Stress a Phrase Source: https://context7.com/mashapo/russtress/llms.txt Initialize the Accent model by importing the Accent class and creating an instance. Then, use the `put_stress` method to add stress marks to a Russian phrase. The output will have stress marks inserted after the stressed vowel. ```python from russtress import Accent # Initialize the accent model (loads LSTM weights) accent = Accent() # Basic usage - stress a simple phrase text = 'Проставь, пожалуйста, ударения' accented_text = accent.put_stress(text) print(accented_text) # Output: "Проста'вь, пожа'луйста, ударе'ния" ``` -------------------------------- ### Basic Tokenization Source: https://context7.com/mashapo/russtress/llms.txt Import the `tokenize` function from `russtress.tokenizer` to split Russian text into tokens. This function preserves character categories like digits, spaces, punctuation, and Cyrillic characters. ```python from russtress.tokenizer import tokenize # Basic tokenization text = 'Привет, мир! Как дела?' tokens = tokenize(text) print(tokens) # Output: ['Привет', ',', ' ', 'мир', '!', ' ', 'Как', ' ', 'дела', '?'] ``` -------------------------------- ### Tokenizer Module Source: https://context7.com/mashapo/russtress/llms.txt Provides text segmentation functionality that splits Russian text into tokens while preserving character categories. ```APIDOC ## [FUNCTION] tokenize ### Description Splits a string into a list of tokens based on character categories. ### Parameters #### Request Body - **text** (string) - Required - The text to tokenize. - **categories** (list) - Optional - Custom character categories for tokenization. ### Response #### Success Response (200) - **tokens** (list) - A list of strings representing the segmented text. ### Request Example { "text": "Привет, мир!" } ### Response Example { "tokens": ["Привет", ",", " ", "мир", "!"] } ``` -------------------------------- ### Batch Process Multiple Texts with RusStress Source: https://context7.com/mashapo/russtress/llms.txt Reuse a single Accent instance to process multiple strings efficiently without reloading the model. ```python from russtress import Accent # Initialize once, use many times accent = Accent() texts = [ 'Доброе утро!', 'Как поживаете?', 'Спасибо, хорошо.', 'До свидания!' ] # Process batch of texts stressed_texts = [accent.put_stress(text) for text in texts] for original, stressed in zip(texts, stressed_texts): print(f'{original} -> {stressed}') # Output: # Доброе утро! -> До'брое у'тро! # Как поживаете? -> Как пожива'ете? # Спасибо, хорошо. -> Спаси'бо, хорошо'. # До свидания! -> До свида'ния! ``` -------------------------------- ### Accent.put_stress Method Source: https://context7.com/mashapo/russtress/llms.txt The primary method for adding stress marks to Russian text. It preserves original formatting, punctuation, and capitalization while adding stress annotations to words with two or more syllables. ```APIDOC ## [METHOD] put_stress ### Description Adds stress marks to Russian text using an LSTM model. It considers word context to handle homographs and preserves original text structure. ### Parameters #### Request Body - **text** (string) - Required - The Russian text to process. - **stress_symbol** (string) - Optional - The symbol used to mark stress (default is an apostrophe). ### Response #### Success Response (200) - **result** (string) - The input text with stress marks inserted after stressed vowels. ### Request Example { "text": "Мама мыла раму.", "stress_symbol": "'" } ### Response Example { "result": "Ма'ма мы'ла ра'му." } ``` -------------------------------- ### Stress Multi-Sentence Paragraphs Source: https://context7.com/mashapo/russtress/llms.txt The `put_stress` method can handle multi-sentence paragraphs, preserving newlines and punctuation while adding stress marks to words with two or more syllables. ```python from russtress import Accent accent = Accent() # Multi-sentence paragraph paragraph = '''Русский язык — один из восточнославянских языков. Он является одним из наиболее распространённых языков мира.''' print(accent.put_stress(paragraph)) # Output preserves newlines and punctuation with stress marks added ``` -------------------------------- ### Apply Stress Marks to Text Source: https://github.com/mashapo/russtress/blob/master/README.rst Use the Accent class from the russtress library to automatically add stress marks to Russian text. Import the Accent class and instantiate it, then call the put_stress method with your text. ```python from russtress import Accent accent = Accent() text = 'Проставь, пожалуйста, ударения' accented_text = accent.put_stress(text) accented_text ``` -------------------------------- ### Check if Word is Small (Monosyllabic) Source: https://context7.com/mashapo/russtress/llms.txt The `is_small` utility function determines if a word has one or fewer syllables. Words identified as 'small' will not have stress marks applied by the `put_stress` method. ```python from russtress.utils import is_small # Check if word has 1 or fewer syllables (won't be stressed) print(is_small('я')) # True - single vowel print(is_small('дом')) # True - single vowel print(is_small('мама')) # False - two vowels (two syllables) print(is_small('привет')) # False - two vowels (two syllables) ``` -------------------------------- ### Parse Text into Words Source: https://context7.com/mashapo/russtress/llms.txt Use the `parse_the_phrase` utility function to parse text into words, marking clause boundaries. The output is a list of lowercase words and empty strings, with '_' indicating clause breaks. ```python from russtress.utils import parse_the_phrase # Parse text into words with clause boundaries marked text = 'Привет, мир! Как дела?' words = parse_the_phrase(text) print(words) # Output: ['привет', '', '_', '', 'мир', '', '_', '', 'как', '', 'дела', '', '_', ''] ``` -------------------------------- ### Stress a Single Word Source: https://context7.com/mashapo/russtress/llms.txt Use the `put_stress` method of the Accent class to add stress marks to a single Russian word. The method returns the word with an apostrophe after the stressed vowel. ```python from russtress import Accent accent = Accent() # Single word stress word = 'молоко' print(accent.put_stress(word)) # Output: "молоко'" ``` -------------------------------- ### Check if Word is in Dictionary Source: https://context7.com/mashapo/russtress/llms.txt The `is_in_dictionary` utility function checks if a given word exists in the stress prediction system's dictionary. It returns `True` if the word is found, `False` otherwise. ```python from russtress.utils import is_in_dictionary # Check if a word is in the stress dictionary print(is_in_dictionary('молоко')) # Output: True or False depending on dictionary contents ``` -------------------------------- ### Context-Dependent Stress Prediction Source: https://context7.com/mashapo/russtress/llms.txt The LSTM model uses word context to predict stress, handling homographs. Words with two or more syllables receive stress marks, while monosyllabic words do not. ```python from russtress import Accent accent = Accent() # The model considers context for stress prediction # Words with 2+ syllables get stress marks text1 = 'Красивые мечты сбываются' print(accent.put_stress(text1)) # Output: "Краси'вые мечты' сбыва'ются" ``` ```python from russtress import Accent accent = Accent() # Short words (1 syllable) are not stressed text2 = 'Я иду в лес' print(accent.put_stress(text2)) # Output: "Я иду' в лес" (only 'иду' gets stress - others are monosyllabic) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.