### Install Lingua-Py Source: https://github.com/pemistahl/lingua-py/blob/main/_autodocs/README.md Install the lingua-language-detector package using pip. ```bash pip install lingua-language-detector ``` -------------------------------- ### Install Development Dependencies and Run Mypy Source: https://github.com/pemistahl/lingua-py/blob/main/README.md Install development dependencies using Poetry and run static type checking with Mypy. ```bash poetry install --no-root --only dev poetry run mypy ``` -------------------------------- ### Lingua-Py Full Migration Example Source: https://github.com/pemistahl/lingua-py/blob/main/_autodocs/migration-guide.md A complete code example comparing lingua 1.x and 2.x for common detection tasks, including enum iteration and member access. ```python # Before (1.x): from lingua import Language, LanguageDetectorBuilder languages = [Language.ENGLISH, Language.FRENCH, Language.GERMAN] detector = LanguageDetectorBuilder.from_languages(*languages).build() for language in Language: print(language) lang_name = "GERMAN" lang = Language[lang_name] result = detector.detect_language_of("hello world") ``` ```python # After (2.x): from lingua import Language, LanguageDetectorBuilder languages = [Language.ENGLISH, Language.FRENCH, Language.GERMAN] detector = LanguageDetectorBuilder.from_languages(*languages).build() for language in sorted(Language.all()): print(language) lang_name = "GERMAN" lang = Language.from_str(lang_name) result = detector.detect_language_of("hello world") ``` -------------------------------- ### Basic Language Detection Source: https://github.com/pemistahl/lingua-py/blob/main/language-testdata/sentences/en.txt Detects the language of a given text string. Ensure the Lingua-Py library is installed. ```python from lingua import LanguageDetectorBuilder text = "This is a sample text." detector = LanguageDetectorBuilder.from_all_languages().build() language = detector.detect_language_of(text) print(f"The detected language is: {language}") ``` -------------------------------- ### Simple Text Segmentation Example Source: https://github.com/pemistahl/lingua-py/blob/main/_autodocs/api-reference/detection-result.md Demonstrates basic text segmentation by splitting a string into parts based on detected languages. It highlights how indices are used to define the start and end of each segment. Note that whitespace characters between segments are not included in any segment. ```python text = "Hello. Bonjour." # 012345 678901234 # Result 1: language=ENGLISH, start_index=0, end_index=6 segment1 = text[0:6] # "Hello." # Result 2: language=FRENCH, start_index=7, end_index=15 segment2 = text[7:15] # "Bonjour." # Note: index 6 (" ") is not included in either segment ``` -------------------------------- ### Build Lingua-Py Project Source: https://github.com/pemistahl/lingua-py/blob/main/README.md Clone the repository, set up a virtual environment, and install project dependencies including lingua. ```bash git clone https://github.com/pemistahl/lingua-py.git cd lingua-py python3 -m venv .venv source .venv/bin/activate pip install --find-links=lingua lingua-language-detector ``` -------------------------------- ### Install Script Dependencies with Poetry Source: https://github.com/pemistahl/lingua-py/blob/main/README.md Install dependencies for scripts using Poetry, specifically for the 'script' profile. ```bash poetry install --no-root --only script ```