### Run Inflection Example Source: https://github.com/irbis-labs/rsmorphy/blob/master/README.md Execute the 'inflect' example using Cargo to demonstrate inflection and pluralization. ```bash cargo run --example inflect ``` -------------------------------- ### Create Russian Morphological Analyzer (Static) Source: https://context7.com/irbis-labs/rsmorphy/llms.txt Use `Russian::new_static()` to create an analyzer with a statically embedded dictionary. This is the simplest way to start. ```rust use rsmorphy::prelude::*; fn main() { // Create analyzer with statically embedded dictionary let morph = Russian::new_static(); // Create reusable parse buffer to avoid allocations let mut buffer = morph.make_buffer(); // Parse a Russian word let word = "яблоко"; for (i, parse) in buffer.parse(word).drain().enumerate() { println!(/*i + 1: {:?}*/, parse.lex.word_lower()); } } ``` -------------------------------- ### Inflection and Pluralization Output Source: https://github.com/irbis-labs/rsmorphy/blob/master/README.md Example output showing the results of inflection and pluralization for Russian words. ```text 1 яблоко + 4 яблока = 5 яблок 102 яблока - 11 яблок = 91 яблоко 1 яблоком сыт не будешь накормил 2 хлебами и 5 рыбами ``` -------------------------------- ### Create Ukrainian Morphological Analyzer (Static) Source: https://context7.com/irbis-labs/rsmorphy/llms.txt Use `Ukrainian::new_static()` to create an analyzer for Ukrainian with a statically bundled dictionary. ```rust use rsmorphy::prelude::*; fn main() { // Create Ukrainian analyzer with statically embedded dictionary let morph = Ukrainian::new_static(); // Create reusable parse buffer let mut buffer = morph.make_buffer(); // Parse a Ukrainian word let word = "яблуко"; for parse in buffer.parse(word).drain() { println!("Word: {}", parse.lex.word_lower()); } } ``` -------------------------------- ### Accessing Dictionary Grammemes in Rust Source: https://context7.com/irbis-labs/rsmorphy/llms.txt Demonstrates how to retrieve grammemes from the dictionary and use them to create sets for word inflection. ```rust use rsmorphy::prelude::*;\n\nfn main() {\n let morph = Russian::new_static();\n let grammemes = morph.dict().grammemes();\n\n // Find specific grammemes by their OpenCorpora codes\n let cases = ["nomn", "gent", "datv", "accs", "ablt", "loct"];\n\n println!("Russian cases:");\n for case_code in cases {\n if let Some(gram) = grammemes.find_gram(case_code) {\n println!(" {} - found", case_code);\n }\n }\n\n // Create a grammeme set for inflection\n let gent = grammemes.find_gram("gent").unwrap().as_set(); // genitive\n let plur = grammemes.find_gram("plur").unwrap().as_set(); // plural\n let target = gent | plur; // genitive plural\n\n let apple = morph.decode_token("ru:d:яблоко,247").unwrap();\n let inflected = apple.inflect(&morph, target).unwrap();\n println!("\nGenitive plural of яблоко: {}", inflected.word_lower());\n} ``` -------------------------------- ### Combining Inflection and Pluralization in Rust Source: https://context7.com/irbis-labs/rsmorphy/llms.txt Shows how to build grammatically correct phrases by chaining inflection and pluralization methods. ```rust use rsmorphy::prelude::*;\n\nfn main() {\n let morph = Russian::new_static();\n\n // Load word tokens\n let apple = morph.decode_token("ru:d:яблоко,247").unwrap();\n let bread = morph.decode_token("ru:d:хлеб,8d4").unwrap();\n let fish = morph.decode_token("ru:d:рыба,35").unwrap();\n let two = morph.decode_token("ru:d:два,51c").unwrap();\n let five = morph.decode_token("ru:d:пять,3a2").unwrap();\n\n let grms = morph.dict().grammemes();\n let ablt = grms.find_gram("ablt").unwrap().as_set(); // instrumental case\n\n // Build grammatically correct phrases\n println!(" ::: {} {} + {} {} = {} {}",\n 1, apple.find_plural(&morph, 1).unwrap().word_lower(),\n 4, apple.find_plural(&morph, 4).unwrap().word_lower(),\n 5, apple.find_plural(&morph, 5).unwrap().word_lower(),\n );\n\n println!(" ::: {} {} сыт не будешь",\n 1,\n apple.inflect(&morph, ablt).unwrap()\n .find_plural(&morph, 1).unwrap()\n .word_lower(),\n );\n\n println!(" ::: накормил {} {} и {} {}",\n two.inflect(&morph, ablt).unwrap().word_lower(),\n bread.inflect(&morph, ablt).unwrap()\n .find_plural(&morph, 2).unwrap()\n .word_lower(),\n five.inflect(&morph, ablt).unwrap().word_lower(),\n fish.inflect(&morph, ablt).unwrap()\n .find_plural(&morph, 5).unwrap()\n .word_lower(),\n );\n} ``` -------------------------------- ### Load PyMorphy2 Dictionary from Disk Source: https://context7.com/irbis-labs/rsmorphy/llms.txt Use `Russian::load_pymorphy2_dict()` to load a dictionary in PyMorphy2 format from a specified directory. This is useful for custom or updated dictionaries. ```rust use rsmorphy::prelude::*; fn main() -> Result<(), Box> { // Load dictionary from a directory containing PyMorphy2 files let dict_path = "/path/to/pymorphy2/ru"; let morph = Russian::load_pymorphy2_dict(dict_path)?; // Now use the analyzer let mut buffer = morph.make_buffer(); let parses = buffer.parse("стали"); for parse in parses.drain() { println!("Form: {}", parse.lex.word_lower()); } Ok(()) } ``` -------------------------------- ### Inflect words using grammemes Source: https://context7.com/irbis-labs/rsmorphy/llms.txt Transforms a word into a specific grammatical form by applying grammemes retrieved from the dictionary registry. ```rust use rsmorphy::prelude::*; fn main() { let morph = Russian::new_static(); // Get a known word token let apple = morph.decode_token("ru:d:яблоко,247").unwrap(); // Get grammeme registry to find grammatical categories let grms = morph.dict().grammemes(); // Find instrumental case grammeme (творительный падеж) let ablt = grms.find_gram("ablt").unwrap().as_set(); // Inflect to instrumental case let inflected = apple.inflect(&morph, ablt).unwrap(); println!("Instrumental case: {}", inflected.word_lower()); // Output: яблоком // Get dative case (дательный падеж) let datv = grms.find_gram("datv").unwrap().as_set(); let dative_form = apple.inflect(&morph, datv).unwrap(); println!("Dative case: {}", dative_form.word_lower()); // Output: яблоку } ``` -------------------------------- ### Iterate over a word lexeme Source: https://context7.com/irbis-labs/rsmorphy/llms.txt Enumerates all grammatical forms within a word's paradigm using an iterator. ```rust use rsmorphy::prelude::*; use rsmorphy::util::format::ExplainLex; fn main() { let morph = Russian::new_static(); let mut buffer = morph.make_buffer(); // Parse word and get first result let word = "яблоко"; let parse = buffer.parse(word).drain().next().unwrap(); // Get lemma (dictionary form) let lemma = morph.get_lemma(&parse.lex); println!("Lemma: {}", lemma.word_lower()); // Iterate over all forms in the lexeme println!("\nAll forms of '{}':", lemma.make_word_lower()); for (i, lex) in morph.iter_lexeme(&lemma).enumerate() { println!("{:2}. {}", i + 1, ExplainLex::new(&morph, &lex)); } } // Output: // Lemma: яблоко // All forms of 'яблоко': // 1. яблоко (nominative singular) // 2. яблока (genitive singular) // 3. яблоку (dative singular) // ... ``` -------------------------------- ### Retrieve word lemma Source: https://context7.com/irbis-labs/rsmorphy/llms.txt Extracts the base dictionary form of an inflected word. ```rust use rsmorphy::prelude::*; fn main() { let morph = Russian::new_static(); let mut buffer = morph.make_buffer(); // Parse an inflected word let word = "яблоками"; // instrumental plural let parse = buffer.parse(word).drain().next().unwrap(); // Get the lemma let lemma = morph.get_lemma(&parse.lex); println!("Input: {}", word); println!("Lemma: {}", lemma.word_lower()); println!("Normal form: {}", parse.lex.normal_form(morph.dict())); } // Output: // Input: яблоками // Lemma: яблоко // Normal form: яблоко ``` -------------------------------- ### Encode and Decode Lexeme Tokens Source: https://context7.com/irbis-labs/rsmorphy/llms.txt Use `encode_token()` and `decode_token()` to serialize and deserialize lexeme information to and from string representations. This is useful for storage or transmission. ```rust use rsmorphy::prelude::*; fn main() { let morph = Russian::new_static(); let mut buffer = morph.make_buffer(); // Parse a word let word = "яблоко"; let parse = buffer.parse(word).drain().next().unwrap(); // Encode the lexeme to a token string let token = morph.encode_token(&parse.lex).to_string(); println!("Encoded token: {}", token); // Output: ru:d:яблоко,247 // Decode the token back to a Lex let decoded_lex = morph.decode_token(&token).unwrap(); println!("Decoded word: {}", decoded_lex.word_lower()); // Output: яблоко } ``` -------------------------------- ### Parse Words Efficiently with ParseBuffers Source: https://context7.com/irbis-labs/rsmorphy/llms.txt The `ParseBuffers` struct allows for efficient parsing of multiple words by reusing a buffer, avoiding repeated memory allocations. Use `ExplainParse` for detailed output formatting. ```rust use rsmorphy::prelude::*; use rsmorphy::util::format::ExplainParse; fn main() { let morph = Russian::new_static(); let mut buffer = morph.make_buffer(); // Parse multiple words efficiently using the same buffer let words = ["яблоко", "хлеб", "рыба", "стали", "бежать"]; for word in words { println!("=== {} ===", word); for (i, parse) in buffer.parse(word).drain().enumerate() { // ExplainParse provides detailed formatting println!(/*{:2}. {}*/, i + 1, ExplainParse::new(&morph, &parse)); } println!(); } } ``` -------------------------------- ### Handling Unknown Words in Rust Source: https://context7.com/irbis-labs/rsmorphy/llms.txt Uses suffix-based prediction to analyze words not explicitly found in the dictionary. ```rust use rsmorphy::prelude::*;\nuse rsmorphy::util::format::ExplainParse;\n\nfn main() {\n let morph = Russian::new_static();\n let mut buffer = morph.make_buffer();\n\n // Parse words that may not be in the dictionary\n let words = [\n "бутявкать", // fictional verb\n "псевдокошку", // word with prefix\n "интернет-магазин", // hyphenated compound\n "pdf", // latin abbreviation\n "42", // number\n "XIII", // roman numeral\n ];\n\n for word in words {\n println!("=== {} ===", word);\n for (i, parse) in buffer.parse(word).drain().enumerate().take(2) {\n println!("{:2}. {}", i + 1, ExplainParse::new(&morph, &parse));\n }\n println!();\n }\n} ``` -------------------------------- ### Access grammatical tags Source: https://context7.com/irbis-labs/rsmorphy/llms.txt Retrieves the grammatical tag for a lexeme to inspect parts of speech and other categories. ```rust use rsmorphy::prelude::*; fn main() { let morph = Russian::new_static(); let mut buffer = morph.make_buffer(); // Parse different words let words = ["яблоко", "бежать", "красивый", "быстро"]; for word in words { if let Some(parse) = buffer.parse(word).drain().next() { let tag = morph.get_tag(&parse.lex); println!("{:15} -> {}", word, tag.fmt_int); } } } // Output: // яблоко -> NOUN,inan,neut sing,nomn // бежать -> INFN,impf,intr // красивый -> ADJF masc,sing,nomn // быстро -> ADVB ``` -------------------------------- ### Perform Ukrainian morphological analysis Source: https://context7.com/irbis-labs/rsmorphy/llms.txt The Ukrainian analyzer supports inflection, pluralization, and numeral agreement using the same API as the Russian analyzer. ```rust use rsmorphy::prelude::*; fn main() { let morph = Ukrainian::new_static(); // Decode Ukrainian word tokens let apple = morph.decode_token("uk:d:яблуко,4d6").unwrap(); let bread = morph.decode_token("uk:d:хліб,7").unwrap(); let fish = morph.decode_token("uk:d:риба,c5").unwrap(); let two = morph.decode_token("uk:d:два,657").unwrap(); let five = morph.decode_token("uk:d:п'ять,4df").unwrap(); let grms = morph.dict().grammemes(); let ablt = grms.find_gram("ablt").unwrap().as_set(); // Pluralization works with Ukrainian numeral agreement rules println!("{} {} + {} {} = {} {}", 1, apple.find_plural(&morph, 1).unwrap().word_lower(), 4, apple.find_plural(&morph, 4).unwrap().word_lower(), 5, apple.find_plural(&morph, 5).unwrap().word_lower(), ); // Inflection to instrumental case println!("нагодував {} {} та {} {}", two.inflect(&morph, ablt).unwrap().word_lower(), bread.inflect(&morph, ablt).unwrap() .find_plural(&morph, 2).unwrap() .word_lower(), five.inflect(&morph, ablt).unwrap().word_lower(), fish.inflect(&morph, ablt).unwrap() .find_plural(&morph, 5).unwrap() .word_lower(), ); } // Output: // 1 яблуко + 4 яблука = 5 яблук // нагодував двома хлібами та п'ятьма рибами ``` -------------------------------- ### Analyze word scores and confidence Source: https://context7.com/irbis-labs/rsmorphy/llms.txt Use the Score enum to distinguish between confirmed dictionary matches and predicted forms. The buffer-based parsing approach is recommended for efficient analysis. ```rust use rsmorphy::prelude::*; use rsmorphy::Score; fn main() { let morph = Russian::new_static(); let mut buffer = morph.make_buffer(); // Words with different score types let words = ["стали", "бутявкает"]; for word in words { println!("=== {} ===", word); for parse in buffer.parse(word).drain() { let (kind, score) = match parse.score { Score::Real(s) => ("Real", s), Score::Fake(s) => ("Fake", s), }; println!(" {} ({}): score={:.5}", parse.lex.word_lower(), kind, score); } } } // Output: // === стали === // стали (Real): score=0.50000 // стали (Real): score=0.50000 // === бутявкает === // бутявкает (Fake): score=0.10000 ``` -------------------------------- ### Pluralize words with numeral agreement Source: https://context7.com/irbis-labs/rsmorphy/llms.txt Returns the correct plural form of a word based on a provided numeral, handling language-specific agreement rules. ```rust use rsmorphy::prelude::*; fn main() { let morph = Russian::new_static(); // Decode a word token let apple = morph.decode_token("ru:d:яблоко,247").unwrap(); // Get plural forms for different numbers println!("1 {}", apple.find_plural(&morph, 1).unwrap().word_lower()); println!("2 {}", apple.find_plural(&morph, 2).unwrap().word_lower()); println!("5 {}", apple.find_plural(&morph, 5).unwrap().word_lower()); println!("11 {}", apple.find_plural(&morph, 11).unwrap().word_lower()); println!("21 {}", apple.find_plural(&morph, 21).unwrap().word_lower()); println!("102 {}", apple.find_plural(&morph, 102).unwrap().word_lower()); // Output: // 1 яблоко // 2 яблока // 5 яблок // 11 яблок // 21 яблоко // 102 яблока } ``` -------------------------------- ### Checking Word Knowledge Status in Rust Source: https://context7.com/irbis-labs/rsmorphy/llms.txt Determines if a parsed word exists in the dictionary or was predicted via suffix analysis. ```rust use rsmorphy::prelude::*;\n\nfn main() {\n let morph = Russian::new_static();\n let mut buffer = morph.make_buffer();\n\n let words = ["яблоко", "бутявкать", "pdf"];\n\n for word in words {\n if let Some(parse) = buffer.parse(word).drain().next() {\n let status = if parse.lex.is_known() {\n "dictionary word"\n } else {\n "predicted/unknown"\n };\n println!("{}: {}", word, status);\n }\n }\n} ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.