### Installation Source: https://github.com/nlpub/pymystem3/blob/master/README.rst Instructions on how to install the pymystem3 library using pip. ```APIDOC ## Installation ### Description Install the stable version of pymystem3 using pip. ### Command ```bash pip install pymystem3 ``` ### Description Install the latest version of pymystem3 directly from GitHub. ### Command ```bash pip install git+https://github.com/nlpub/pymystem3 ``` ``` -------------------------------- ### Install pymystem3 via pip Source: https://github.com/nlpub/pymystem3/blob/master/README.rst Instructions for installing the pymystem3 library from PyPI or directly from the GitHub repository. ```bash pip install pymystem3 pip install git+https://github.com/nlpub/pymystem3 ``` -------------------------------- ### Install pymystem3 via pip Source: https://context7.com/nlpub/pymystem3/llms.txt Installs the pymystem3 library using pip. This is the standard method for installing Python packages. ```bash pip install pymystem3 ``` -------------------------------- ### Automatic Mystem Binary Installation with autoinstall() Source: https://context7.com/nlpub/pymystem3/llms.txt The `autoinstall()` function automatically downloads and installs the Mystem binary if it's not already present on the system. It also shows how to check the current binary location and set a custom path via an environment variable. ```Python from pymystem3 import autoinstall, MYSTEM_BIN, MYSTEM_DIR import os # Check current binary location print(f"Mystem directory: {MYSTEM_DIR}") print(f"Mystem binary path: {MYSTEM_BIN}") # Manually trigger installation (normally done automatically) autoinstall() # Verify installation if os.path.isfile(MYSTEM_BIN): print("Mystem binary is installed") else: print("Installation required") # Set custom binary path via environment variable import os os.environ["MYSTEM_BIN"] = "/custom/path/to/mystem" m = Mystem() # Will use custom path ``` -------------------------------- ### Install Latest pymystem3 from GitHub Source: https://context7.com/nlpub/pymystem3/llms.txt Installs the latest development version of pymystem3 directly from its GitHub repository using pip. ```bash pip install git+https://github.com/nlpub/pymystem3 ``` -------------------------------- ### Manage Mystem Process with start() and close() Source: https://context7.com/nlpub/pymystem3/llms.txt The `start()` method pre-loads the Mystem binary to reduce latency for subsequent operations. The `close()` method properly terminates the subprocess. The context manager (`with Mystem() as m:`) provides automatic cleanup. ```Python from pymystem3 import Mystem m = Mystem() # Pre-start the binary to avoid first-call latency m.start() # Process multiple texts efficiently texts = [ "Мама мыла раму", "Красивая погода сегодня", "Программирование это интересно" ] for text in texts: lemmas = m.lemmatize(text) print(''.join(lemmas)) # Explicitly close the process when done m.close() # Alternative: use context manager for automatic cleanup with Mystem() as m: for text in texts: lemmas = m.lemmatize(text) print(''.join(lemmas)) ``` -------------------------------- ### Analyze Text Example Source: https://github.com/nlpub/pymystem3/blob/master/README.rst Shows how to get detailed grammatical information and lemmas for each token in a text. ```APIDOC ## Analyze Text ### Description This example demonstrates how to use the `analyze` method to obtain comprehensive morphological analysis, including lemmas and grammatical attributes, for each word in a text. ### Method ```python import json from pymystem3 import Mystem text = "Красивая мама красиво мыла раму" m = Mystem() analysis = m.analyze(text) print("lemmas:", ''.join([ana.get('analysis', [{}])[0].get('lex', ana['text']) for ana in analysis if ana.get('analysis')])) print("full info:", json.dumps(analysis, ensure_ascii=False)) ``` ### Output ``` lemmas: красивый мама красиво мыть рама full info: [{"text": "Красивая", "analysis": [{"lex": "красивый", "gr": "A=им,ед,полн,жен"}]}, {"text": " "}, {"text": "мама", "analysis": [{"lex": "мама", "gr": "S,жен,од=им,ед"}]}, {"text": " "}, {"text": "красиво", "analysis": [{"lex": "красиво", "gr": "ADV="}]}, {"text": " "}, {"text": "мыла", "analysis": [{"lex": "мыть", "gr": "V,несов,пе=прош,ед,изъяв,жен"}]}, {"text": " "}, {"text": "раму", "analysis": [{"lex": "рама", "gr": "S,жен,неод=вин,ед"}]}, {"text": "\n"}] ``` ``` -------------------------------- ### Lemmatization Example Source: https://github.com/nlpub/pymystem3/blob/master/README.rst Demonstrates how to perform basic text lemmatization using the Mystem analyzer. ```APIDOC ## Lemmatization ### Description This example shows how to use the `lemmatize` method to get the base form of words in a given text. ### Method ```python from pymystem3 import Mystem text = "Красивая мама красиво мыла раму" m = Mystem() lemmas = m.lemmatize(text) print(''.join(lemmas)) ``` ### Output ``` красивый мама красиво мыть рама ``` ``` -------------------------------- ### Initialize Mystem Class in Python Source: https://context7.com/nlpub/pymystem3/llms.txt Demonstrates how to initialize the Mystem class for morphological analysis. It shows both basic initialization with default settings and advanced initialization with various configuration options. The example also includes using the Mystem class as a context manager for automatic resource cleanup. ```python from pymystem3 import Mystem # Basic initialization with default settings m = Mystem() # Advanced initialization with custom options m = Mystem( mystem_bin=None, # Path to mystem binary (auto-downloads if None) grammar_info=True, # Print grammatical information (-i) disambiguation=True, # Apply POS disambiguation (-d) entire_input=True, # Copy entire input including spaces (-c) glue_grammar_info=True, # Glue grammatical info for same lemmas (-g) weight=False, # Print context-independent lemma weight generate_all=False, # Generate all hypotheses for non-dict words no_bastards=False, # Print only dictionary words (-w) end_of_sentence=False, # Print end of sentence marks (-s) fixlist=None, # Path to custom dictionary file use_english_names=False # Use English names for grammemes (--eng-gr) ) # Using context manager for automatic resource cleanup with Mystem() as m: result = m.lemmatize("Красивая мама красиво мыла раму") print(''.join(result)) ``` -------------------------------- ### Lemmatize Text using lemmatize() in Python Source: https://context7.com/nlpub/pymystem3/llms.txt Shows how to use the `lemmatize()` method to reduce words to their base forms. Examples include basic lemmatization, lemmatization with and without whitespace preservation, and lemmatizing text from a file. ```python from pymystem3 import Mystem m = Mystem() # Basic lemmatization text = "Красивая мама красиво мыла раму" lemmas = m.lemmatize(text) print(''.join(lemmas)) # Lemmatization without whitespace preservation m_compact = Mystem(entire_input=False) lemmas_compact = m_compact.lemmatize("Мама мыла раму") print(lemmas_compact) # Lemmatization with whitespace preserved (default) m_full = Mystem(entire_input=True) lemmas_full = m_full.lemmatize("Мама мыла раму") print(lemmas_full) # Lemmatize from file lemmas_from_file = m.lemmatize(file_path="/path/to/russian_text.txt") print(''.join(lemmas_from_file)) ``` -------------------------------- ### Apply Custom Dictionary (Fixlist) for Lemmatization Source: https://context7.com/nlpub/pymystem3/llms.txt This example shows how to use a custom dictionary file (fixlist) to override or supplement the default analysis for specific words. The `fixlist` parameter in the Mystem constructor takes the path to the custom dictionary file. ```Python from pymystem3 import Mystem # Create a custom dictionary file (fixlist format) # Each line: word{lemma:grammar} # Example fixlist.txt content: # спб{санкт-петербург:S,муж,неод=им,ед} # питер{санкт-петербург:S,муж,неод=им,ед} # Initialize with custom dictionary m = Mystem(fixlist="/path/to/fixlist.txt") # Analyze with custom dictionary applied analysis = m.analyze("Я живу в СПб") # Custom lemmatization will be applied for 'СПб' ``` -------------------------------- ### Perform Full Morphological Analysis with analyze() in Python Source: https://context7.com/nlpub/pymystem3/llms.txt Demonstrates the `analyze()` method for detailed morphological analysis, returning structured data for each token. Includes examples of analyzing text, analyzing from a file, and accessing individual token analysis results. ```python import json from pymystem3 import Mystem m = Mystem() # Full morphological analysis text = "Красивая мама красиво мыла раму" analysis = m.analyze(text) print(json.dumps(analysis, ensure_ascii=False, indent=2)) # Analyze from file analysis_from_file = m.analyze(file_path="/path/to/russian_text.txt") # Access individual token analysis for token in analysis: if 'analysis' in token and token['analysis']: lemma = token['analysis'][0]['lex'] grammar = token['analysis'][0]['gr'] print(f"Word: {token['text']}, Lemma: {lemma}, Grammar: {grammar}") ``` -------------------------------- ### Get Human-Readable Token Representation with get_printable_repr() Source: https://context7.com/nlpub/pymystem3/llms.txt The static method `get_printable_repr()` formats token analysis results into a human-readable string. This is helpful for debugging and understanding the detailed analysis output, especially when dealing with ambiguous words. ```Python from pymystem3 import Mystem m = Mystem() text = "мыла" analysis = m.analyze(text) # Get human-readable representation for each token for token in analysis: print(Mystem.get_printable_repr(token)) # For text with multiple hypotheses (when disambiguation is off) m_no_disambig = Mystem(disambiguation=False) analysis_ambiguous = m_no_disambig.analyze("мыла") for token in analysis_ambiguous: print(Mystem.get_printable_repr(token)) ``` -------------------------------- ### Perform Lemmatization and Morphological Analysis Source: https://github.com/nlpub/pymystem3/blob/master/README.rst Demonstrates how to initialize the Mystem analyzer to lemmatize text and retrieve full morphological information including part-of-speech tags. ```python import json from pymystem3 import Mystem text = "Красивая мама красиво мыла раму" m = Mystem() # Lemmatization lemmas = m.lemmatize(text) print(''.join(lemmas)) # Full morphological analysis analysis = m.analyze(text) print(json.dumps(analysis, ensure_ascii=False)) ``` -------------------------------- ### analyze() - Full Morphological Analysis Source: https://context7.com/nlpub/pymystem3/llms.txt Returns detailed morphological information for each token, including lemma, part-of-speech tags, and grammatical attributes. ```APIDOC ## analyze(text, file_path=None) ### Description Performs full morphological analysis, returning a structured dictionary containing the original token, its lemma, and grammatical tags. ### Method Method call (Python) ### Parameters #### Arguments - **text** (string) - Optional - The raw Russian text to analyze. - **file_path** (string) - Optional - Path to a file containing text to analyze. ### Request Example ```python m = Mystem() analysis = m.analyze("Мама") ``` ### Response #### Success Response (List) - **list** (array) - A list of dictionaries containing 'text' and 'analysis' keys. #### Response Example [{"text": "Мама", "analysis": [{"lex": "мама", "gr": "S,жен,од=им,ед"}]}] ``` -------------------------------- ### Configure English Grammeme Names Source: https://context7.com/nlpub/pymystem3/llms.txt This snippet demonstrates how to configure the Mystem analyzer to use English names for grammatical categories instead of the default Russian abbreviations. This is achieved by setting the `use_english_names` parameter during Mystem initialization. ```Python import json from pymystem3 import Mystem # Using Russian grammeme names (default) m_rus = Mystem(use_english_names=False) analysis_rus = m_rus.analyze("мама") print(json.dumps(analysis_rus, ensure_ascii=False)) # Using English grammeme names m_eng = Mystem(use_english_names=True) analysis_eng = m_eng.analyze("мама") print(json.dumps(analysis_eng, ensure_ascii=False)) ``` -------------------------------- ### lemmatize() - Text Lemmatization Source: https://context7.com/nlpub/pymystem3/llms.txt Performs morphological analysis and returns a list of lemmas (base dictionary forms) for each token in the input text. ```APIDOC ## lemmatize(text, file_path=None) ### Description Reduces words in the input string or file to their base dictionary form (lemmas). ### Method Method call (Python) ### Parameters #### Arguments - **text** (string) - Optional - The raw Russian text to process. - **file_path** (string) - Optional - Path to a file containing text to process. ### Request Example ```python m = Mystem() lemmas = m.lemmatize("Красивая мама") ``` ### Response #### Success Response (List) - **list** (array) - A list of strings representing the lemmas and original whitespace/punctuation. #### Response Example ["красивый", " ", "мама"] ``` -------------------------------- ### Extract Part-of-Speech Tags with get_pos() Source: https://context7.com/nlpub/pymystem3/llms.txt The static method `get_pos()` extracts the main part-of-speech tag from a token's analysis result. This is useful for filtering tokens by their grammatical category, such as identifying verbs or nouns. ```Python from pymystem3 import Mystem m = Mystem() text = "Красивая мама красиво мыла раму" analysis = m.analyze(text) # Extract POS tags for each token for token in analysis: pos = Mystem.get_pos(token) if pos: print(f"Word: {token['text']}, POS: {pos}") # Filter only verbs verbs = [token for token in analysis if Mystem.get_pos(token) == 'V'] print([v['text'] for v in verbs]) # Filter only nouns nouns = [token for token in analysis if Mystem.get_pos(token) == 'S'] print([n['text'] for n in nouns]) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.