### Start Stanza Demo Server Source: https://github.com/stanfordnlp/stanza/blob/main/stanza/pipeline/demo/README.md Set the Flask application and run the demo server from the Stanza directory. Ensure you have Flask installed. ```bash export FLASK_APP=demo_server.py flask run ``` -------------------------------- ### Install Stanza from Source Source: https://github.com/stanfordnlp/stanza/blob/main/README.md Clone the Stanza repository and install it in editable mode for development purposes. ```bash git clone https://github.com/stanfordnlp/stanza.git cd stanza pip install -e . ``` -------------------------------- ### Minimal English Pipeline Setup Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/configuration.md Initialize a basic English pipeline for standard text processing tasks. This is the simplest way to start using Stanza for English. ```python import stanza lp = stanza.Pipeline(lang='en') doc = nlp("Hello world.") ``` -------------------------------- ### Install Stanza via pip Source: https://github.com/stanfordnlp/stanza/blob/main/README.md Use this command to install Stanza and its dependencies using pip. This is the recommended installation method for most users. ```bash pip install stanza ``` -------------------------------- ### Download and Install Stanza Resources Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/README.md Download and install necessary Stanza language models and CoreNLP resources. Ensure these are installed before using the pipeline. ```python stanza.download('en') stanza.install_corenlp() ``` -------------------------------- ### Verify CoreNLP Installation Source: https://github.com/stanfordnlp/stanza/blob/main/demo/Stanza_CoreNLP_Interface.ipynb Lists the files in the CoreNLP installation directory to confirm successful installation. Requires the CORENLP_HOME environment variable to be set. ```bash # Examine the CoreNLP installation folder to make sure the installation is successful !ls $CORENLP_HOME ``` -------------------------------- ### Download and Install Stanford CoreNLP Source: https://github.com/stanfordnlp/stanza/blob/main/demo/Stanza_CoreNLP_Interface.ipynb Downloads the Stanford CoreNLP package using Stanza's installation command and sets the CORENLP_HOME environment variable. This process can take several minutes. ```python # Download the Stanford CoreNLP package with Stanza's installation command # This'll take several minutes, depending on the network speed corenlp_dir = './corenlp' stanza.install_corenlp(dir=corenlp_dir) # Set the CORENLP_HOME environment variable to point to the installation location import os os.environ["CORENLP_HOME"] = corenlp_dir ``` -------------------------------- ### Start CoreNLP Server Source: https://github.com/stanfordnlp/stanza/blob/main/demo/Stanza_CoreNLP_Interface.ipynb Starts the background CoreNLP server process. This is optional as the server starts automatically on the first annotation request. ```python client.start() import time; time.sleep(10) ``` -------------------------------- ### Install Stanza Source: https://github.com/stanfordnlp/stanza/blob/main/demo/Stanza_Beginners_Guide.ipynb Installs the Stanza package using pip. The '!' prefix is for environments like Jupyter notebooks. ```python # Install; note that the prefix "!" is not needed if you are running in a terminal !pip install stanza # Import the package import stanza ``` -------------------------------- ### Python Function Signature Example Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/README.md Illustrates the expected format for function signatures in the documentation, including parameters and their types. ```python def process(self, doc, processors=None) ``` -------------------------------- ### Stanza Dependency Parse Output Example Source: https://github.com/stanfordnlp/stanza/blob/main/README.md An example of the output format for dependency parsing, showing words, their head indices, and dependency relations. ```text ('Barack', '4', 'nsubj:pass') ('Obama', '1', 'flat') ('was', '4', 'aux:pass') ('born', '0', 'root') ('in', '6', 'case') ('Hawaii', '4', 'obl') ('.', '4', 'punct') ``` -------------------------------- ### Configure Pipeline Download Method Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/types.md Examples of how to configure the download_method parameter when initializing a Stanza Pipeline. This allows control over model download behavior, from offline to always updating. ```python import stanza from stanza import DownloadMethod # Offline mode lp = stanza.Pipeline(lang='en', download_method=DownloadMethod.NONE) # Update only missing models lp = stanza.Pipeline(lang='en', download_method=DownloadMethod.REUSE_RESOURCES) # Always get latest lp = stanza.Pipeline(lang='en', download_method=DownloadMethod.DOWNLOAD_RESOURCES) # As string lp = stanza.Pipeline(lang='en', download_method='none') ``` -------------------------------- ### Set CoreNLP Home Directory Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/INDEX.md Define the installation path for Stanford CoreNLP by setting the CORENLP_HOME environment variable. ```bash # CoreNLP home export CORENLP_HOME=/opt/corenlp ``` -------------------------------- ### Token ID Example Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/api-reference/document.md Demonstrates how to check if a token is a multi-word token and print its ID. This is useful for languages where tokens can expand. ```python for token in sentence.tokens: if isinstance(token.id, tuple): print(f"Multi-word token: {token.id[0]}-{token.id[1]}") else: print(f"Single-word token: {token.id}") ``` -------------------------------- ### Install Stanford CoreNLP Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/api-reference/resources.md Automatically download and extract Stanford CoreNLP jar files for Python client use. Supports installing the latest development version or a specific version to a custom directory. ```python import stanza # Install latest CoreNLP development version stanza.install_corenlp() # Install specific version stanza.install_corenlp(version='4.2.2') # Install to custom directory stanza.install_corenlp(dir='/opt/corenlp') # Set CORENLP_HOME environment variable to avoid specifying dir each time import os os.environ['CORENLP_HOME'] = '/opt/corenlp' stanza.install_corenlp() ``` -------------------------------- ### Install Stanza via Anaconda Source: https://github.com/stanfordnlp/stanza/blob/main/README.md Install Stanza using the conda package manager from the stanfordnlp channel. Note: This method may not work for Python 3.10. ```bash conda install -c stanfordnlp stanza ``` -------------------------------- ### Visualize Semgrex Search on a String Source: https://github.com/stanfordnlp/stanza/blob/main/demo/semgrex visualization.ipynb Initializes a Stanza pipeline for a given language and text, then calls a function to visualize Semgrex search results. Use this to start a visualization process from raw text. ```python def visualize_search_str(text, semgrex_queries, lang_code): """ Visualizes the deprel of the semgrex results from running semgrex search on a string with the given list of semgrex queries. Returns a list of the edited HTML strings. Each element in the list represents the HTML to render one of the sentences in the document. Internally, this function converts the string into a stanza doc object before processing the doc object. 'lang_code' is the two-letter language abbreviation for the language that the stanza doc object is written in. """ nlp = stanza.Pipeline(lang_code, processors="tokenize, pos, lemma, depparse") doc = nlp(text) return visualize_search_doc(doc, semgrex_queries, lang_code) ``` -------------------------------- ### Visualize Arabic Dependency Trees Source: https://github.com/stanfordnlp/stanza/blob/main/demo/CONLL_Dependency_Visualizer_Example.ipynb Generates visualizations for Arabic dependency trees from a CONLL-U file. This example specifically demonstrates visualization for a right-to-left language. ```python from stanza.utils.visualization.conll_deprel_visualization import conll_to_visual # testing right to left languages ar_file = "arabic_test.conllu.txt" conll_to_visual(ar_file, "ar") ``` -------------------------------- ### Multi-word Token Expansion Example Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/api-reference/document.md Shows how to identify and display the constituent words of a multi-word token. This is particularly relevant for languages like French where contractions are common. ```python # French "du" expands to "de" + "le" token = sentence.tokens[0] if len(token.words) > 1: print(f"Multi-word token '{token.text}' contains: {[w.text for w in token.words]}") ``` -------------------------------- ### Visualize Japanese Dependency Trees Source: https://github.com/stanfordnlp/stanza/blob/main/demo/CONLL_Dependency_Visualizer_Example.ipynb Generates visualizations for Japanese dependency trees from a CONLL-U file. This example shows the basic usage for a non-left-to-right language. ```python from stanza.utils.visualization.conll_deprel_visualization import conll_to_visual jp_file = "japanese_test.conllu.txt" conll_to_visual(jp_file, "ja") ``` -------------------------------- ### Multilingual Pipeline with Default Language Configurations Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/configuration.md Utilize defaultdict to dynamically configure processors for languages not explicitly defined in the main configuration. This simplifies setup for a wide range of languages. ```python # Using defaultdict for dynamic config lang_configs = defaultdict(lambda: {"processors": "tokenize,pos,lemma"}) lp = stanza.MultilingualPipeline(lang_configs=lang_configs) ``` -------------------------------- ### Optimizing GPU and Memory Usage Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/INDEX.md Shows how to optimize Stanza for GPU usage and reduce memory footprint by sharing a foundation cache across multiple pipelines. Also includes an example for offline/CPU-only usage. ```python import stanza # Share foundation cache across multiple pipelines to save memory cache = stanza.models.common.foundation_cache.FoundationCache() lp_en = stanza.Pipeline(lang='en', use_gpu=True, foundation_cache=cache) lp_fr = stanza.Pipeline(lang='fr', use_gpu=True, foundation_cache=cache) # Or for offline/CPU-only lp = stanza.Pipeline( lang='en', download_method=stanza.DownloadMethod.NONE, device='cpu' ) ``` -------------------------------- ### Initialize Pipeline with MWT Processor for French Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/api-reference/processors.md Expands multi-word tokens (contractions, compounds) into their constituent words. This example demonstrates its use with French, where 'au' expands to 'à' + 'le'. ```python import stanza # French with MWT expansion nlp = stanza.Pipeline(lang='fr', processors='tokenize,pos') doc = nlp("Je vais au cinéma.") # "au" (Token) expands to "à" + "le" (Words) # "du" would expand to "de" + "le" for sentence in doc.sentences: for token in sentence.tokens: if len(token.words) > 1: print(f"MWT: {token.text} → {[w.text for w in token.words]}") ``` -------------------------------- ### Basic Stanza Pipeline Usage Source: https://github.com/stanfordnlp/stanza/blob/main/README.md Demonstrates how to import Stanza, download a language model, initialize a pipeline, and process a sentence to print its dependencies. ```python import stanza stanza.download('en') # Optional: pre-download English models (Pipeline can auto-download if needed) nlp = stanza.Pipeline('en') # This sets up a default neural pipeline in English doc = nlp("Barack Obama was born in Hawaii. He was elected president in 2008.") doc.sentences[0].print_dependencies() ``` -------------------------------- ### Multilingual Pipeline with Dynamic Per-Language Config Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/api-reference/pipeline.md Illustrates using `defaultdict` for dynamic per-language configurations. This is useful when you want a default set of processors for all languages, with specific overrides. ```python # Dynamic per-language config using defaultdict from collections import defaultdict lang_configs = defaultdict(lambda: {"processors": "tokenize"}) # All langs tokenize only nlp_multi = stanza.MultilingualPipeline(lang_configs=lang_configs) ``` -------------------------------- ### Get loaded processors in Stanza Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/api-reference/pipeline.md Access the `loaded_processors` property to get a list of all loaded processor instances in their execution order. ```python for processor in nlp.loaded_processors: print(processor.__class__.__name__) ``` -------------------------------- ### Import and Use Stanza for Text Processing Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/INDEX.md Demonstrates how to import Stanza, download models, create a pipeline, process text, and access annotations like UPOS and lemma for each word. ```python import stanza # Download models (one-time) stanza.download('en') # Create pipeline lp = stanza.Pipeline(lang='en') # Process text doc = nlp("Barack Obama was born in Hawaii.") # Access annotations for sentence in doc.sentences: for word in sentence.words: print(f"{word.text} ({word.upos}) → {word.lemma}") ``` -------------------------------- ### Access Word Start Character Offset Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/api-reference/document.md Retrieve the character offset of the start of a word within the original text. This is useful for aligning word tokens with the raw input string. ```python @property start_char() ``` -------------------------------- ### Pipeline Initialization with DownloadMethod.REUSE_RESOURCES Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/api-reference/pipeline.md Initializes a Stanza pipeline to reuse existing resources and models, downloading only missing components. This balances using existing data with ensuring all necessary models are present. ```python # Reuse what exists, fill gaps only nlp = stanza.Pipeline(lang='en', download_method=stanza.DownloadMethod.REUSE_RESOURCES) ``` -------------------------------- ### Pipeline Initialization with DownloadMethod.DOWNLOAD_RESOURCES Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/api-reference/pipeline.md Initializes a Stanza pipeline to always download fresh resources and models, overwriting existing ones. This ensures you are using the latest available models. ```python # Always get latest nlp = stanza.Pipeline(lang='en', download_method=stanza.DownloadMethod.DOWNLOAD_RESOURCES) ``` -------------------------------- ### Upgrade Stanza via pip Source: https://github.com/stanfordnlp/stanza/blob/main/README.md Use this command to upgrade an existing Stanza installation to the latest version. ```bash pip install stanza -U ``` -------------------------------- ### Process Text and Access Annotations in English Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/INDEX.md Shows how to create an English pipeline, process text, and access word annotations including UPOS and lemma, as well as the dependency structure. ```python import stanza lp = stanza.Pipeline(lang='en') doc = nlp("The quick brown fox jumps over the lazy dog.") # Access tokens with annotations for word in doc.sentences[0].words: print(f"{word.text}\tPOS:{word.upos}\tLemma:{word.lemma}") # Access dependency structure for word in doc.sentences[0].words: if word.head > 0: head_word = doc.sentences[0].words[word.head - 1] print(f"{word.text} --{word.deprel}--> {head_word.text}") ``` -------------------------------- ### Get Coreference Chains Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/api-reference/document.md Access coreference chains from a loaded coreference processor. Returns an empty list if the processor was not run. ```python nlp = stanza.Pipeline(lang='en', processors='tokenize,pos,coref') doc = nlp("Barack Obama was born in Hawaii. He later became president.") for chain in doc.coref: print(f"Chain {chain.index}: {chain.representative_text}") for mention in chain.mentions: print(f" Mention at sentence {mention.sentence}, words {mention.start_word}-{mention.end_word}") ``` -------------------------------- ### Using Custom Model Paths Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/INDEX.md Demonstrates how to specify custom paths for Stanza model files when initializing the pipeline. This allows using locally downloaded or modified models. ```python nlp = stanza.Pipeline( lang='en', tokenize_model_path='/path/to/tokenizer.pt', pos_model_path='/path/to/pos.pt', depparse_model_path='/path/to/parser.pt', ner_model_path='/path/to/ner.pt' ) ``` -------------------------------- ### Programmatically Check Available Languages and Initialize Pipeline Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/types.md Demonstrates how to programmatically retrieve a list of available languages supported by Stanza and how to initialize a Stanza pipeline for a specific language. ```python # Check available languages programmatically from stanza.resources.common import load_resources_json resources = load_resources_json() available_langs = list(resources.keys()) print(f"Available languages: {', '.join(sorted(available_langs))}") # Pipeline for specific language lp = stanza.Pipeline(lang='ja') # Japanese ``` -------------------------------- ### Download Language Models for Stanza Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/INDEX.md Illustrates how to download default models for new languages like French and German, or specify processors for a language like Spanish. ```python import stanza # Download default models stanza.download('fr') # French stanza.download('de') # German # Download specific processors stanza.download('es', processors={'tokenize': None, 'pos': None, 'depparse': None}) ``` -------------------------------- ### Get Total Number of Words Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/api-reference/document.md Calculate the total number of words across all sentences in the Document. This count is after multi-word token expansion. ```python total_words = doc.num_words ``` -------------------------------- ### install_corenlp Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/api-reference/resources.md Automatically downloads and extracts Stanford CoreNLP jar files for Python client use. ```APIDOC ## install_corenlp ### Description Automatically download and extract Stanford CoreNLP jar files for Python client use. ### Method ```python install_corenlp( dir=DEFAULT_CORENLP_DIR, url=DEFAULT_CORENLP_URL, logging_level=None, proxies=None, version='main' ) ``` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters Table | Parameter | Type | Default | Description | |---|---|---|---| | dir | str | DEFAULT_CORENLP_DIR | Directory to extract CoreNLP (default: ~/.cache/stanza/corenlp). Can override with CORENLP_HOME environment variable. | | url | str | DEFAULT_CORENLP_URL | URL to CoreNLP distribution zip. Should contain {version} or {tag} placeholder. | | logging_level | str | None | Logging level during installation | | proxies | dict | None | HTTP proxy config dict | | version | str | 'main' | Version tag (e.g., 'main', '4.2.2'). Mapped to git tag 'v' + version. | ### Raises: - `RuntimeError`: Download or extraction failed - `ValueError`: Invalid version format ### Example: ```python import stanza # Install latest CoreNLP development version stanza.install_corenlp() # Install specific version stanza.install_corenlp(version='4.2.2') # Install to custom directory stanza.install_corenlp(dir='/opt/corenlp') # Set CORENLP_HOME environment variable to avoid specifying dir each time import os os.environ['CORENLP_HOME'] = '/opt/corenlp' stanza.install_corenlp() ``` ``` -------------------------------- ### Get Total Number of Tokens Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/api-reference/document.md Calculate the total number of tokens across all sentences in the Document. This count includes multi-word tokens. ```python total_tokens = doc.num_tokens ``` -------------------------------- ### Stanza Pipeline with Proxy Source: https://github.com/stanfordnlp/stanza/blob/main/README.md Shows how to configure Stanza to use proxies for downloading models, useful when facing connection errors. ```python import stanza proxies = {'http': 'http://ip:port', 'https': 'http://ip:port'} stanza.download('en', proxies=proxies) # Optional: pre-download English models (Pipeline can auto-download if needed) nlp = stanza.Pipeline('en') # This sets up a default neural pipeline in English doc = nlp("Barack Obama was born in Hawaii. He was elected president in 2008.") doc.sentences[0].print_dependencies() ``` -------------------------------- ### Extract and Print Named Entities Source: https://github.com/stanfordnlp/stanza/blob/main/demo/Stanza_Beginners_Guide.ipynb Iterate over all extracted named entity mentions to print their text, type, and character spans (start and end). ```python print("Mention text\tType\tStart-End") for ent in en_doc.ents: print("{}\t{}\t{}-{}".format(ent.text, ent.type, ent.start_char, ent.end_char)) ``` -------------------------------- ### Stanza Pipeline Constructor Parameters Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/INDEX.md Initialize a Stanza pipeline with various configuration options, including language, model directory, processors, and GPU usage. ```python Pipeline( lang='en', # Language dir='~/.cache/stanza/resources', # Model directory package='default', # Package variant processors={}, # Which processors use_gpu=True, # GPU usage device='cuda:0', # Specific device download_method=DownloadMethod.DOWNLOAD_RESOURCES, # Download strategy # ... processor-specific options ... ) ``` -------------------------------- ### Access Coreference Attachments Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/api-reference/document.md Get information about coreference attachments for a word, indicating which coreference chains the word belongs to. This is useful for anaphora resolution. ```python @property coref() ``` ```python for word in sentence.words: if word.coref: for attachment in word.coref: chain = attachment.chain print(f"{word.text} is part of coref chain {chain.index}") ``` -------------------------------- ### download_corenlp_models Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/api-reference/resources.md Downloads language-specific CoreNLP model jar files. These are separate from the core CoreNLP installation and are required for certain CoreNLP functionalities within Stanza. ```APIDOC ## download_corenlp_models ### Description Download language-specific CoreNLP model jar files (separate from core CoreNLP installation). ### Method ```python download_corenlp_models( model, version, dir=DEFAULT_CORENLP_DIR, url=DEFAULT_CORENLP_MODEL_URL, logging_level='INFO', proxies=None, force=True ) ``` ### Parameters #### Path Parameters (No path parameters defined) #### Query Parameters (No query parameters defined) #### Request Body (No request body defined) ### Parameters - **model** (str) - Required - Language model name. Options: 'arabic', 'chinese', 'english', 'english-extra', 'english-kbp', 'french', 'german', 'hungarian', 'italian', 'spanish' - **version** (str) - Required - Version string matching CoreNLP version (e.g., '4.2.2', 'main') - **dir** (str) - Optional - Directory to download model jar into. Defaults to `DEFAULT_CORENLP_DIR`. - **url** (str) - Optional - URL pattern for model jars. Supports {model}, {version}, {tag} placeholders. Defaults to `DEFAULT_CORENLP_MODEL_URL`. - **logging_level** (str) - Optional - Logging level during download. Defaults to 'INFO'. - **proxies** (dict) - Optional - HTTP proxy config dict. Defaults to None. - **force** (bool) - Optional - Download even if model file already exists locally. Defaults to True. ### Raises - `KeyError`: Model name not in AVAILABLE_MODELS - `ValueError`: model or version not specified - `RuntimeError`: Download failed ### Example ```python import stanza # Download English models for CoreNLP stanza.download_corenlp_models('english', '4.2.2') # Download additional language models stanza.download_corenlp_models('french', '4.2.2') stanza.download_corenlp_models('spanish', '4.2.2') # Download without overwriting existing stanza.download_corenlp_models('german', '4.2.2', force=False) ``` ``` -------------------------------- ### Import and Use Processor Name Constants Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/types.md Demonstrates importing and using string constants for processor names when configuring a Stanza Pipeline. This improves code readability and reduces errors compared to using raw strings. ```python from stanza.pipeline._constants import ( LANGID, TOKENIZE, MWT, POS, LEMMA, DEPPARSE, NER, SENTIMENT, CONSTITUENCY, COREF, MORPHSEG ) # Processor name constants LANGID = 'langid' # Language identification TOKENIZE = 'tokenize' # Tokenization and sentence splitting MWT = 'mwt' # Multi-word token expansion POS = 'pos' # Part-of-speech tagging LEMMA = 'lemma' # Lemmatization DEPPARSE = 'depparse' # Dependency parsing NER = 'ner' # Named entity recognition SENTIMENT = 'sentiment' # Sentiment analysis CONSTITUENCY = 'constituency' # Constituency parsing COREF = 'coref' # Coreference resolution MORPHSEG = 'morphseg' # Morphological segmentation ``` ```python import stanza from stanza.pipeline._constants import TOKENIZE, POS, LEMMA, DEPPARSE, NER # Use constants for processors lp = stanza.Pipeline( lang='en', processors={ TOKENIZE: None, POS: None, LEMMA: None, DEPPARSE: None, NER: None } ) # Or use string names directly lp = stanza.Pipeline(lang='en', processors='tokenize,pos,lemma,depparse,ner') ``` -------------------------------- ### Access Syntactic Head Index Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/api-reference/document.md Get the index of the syntactic head of a word in the dependency structure. A value of 0 indicates the word is the root of the sentence. ```python @property head() ``` ```python for word in sentence.words: head_word = sentence.words[word.head - 1] if word.head > 0 else None if head_word: print(f"{word.text} depends on {head_word.text}") ``` -------------------------------- ### Handle Model Download Failures with Proxies or Custom URLs Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/errors.md Addresses network issues during model downloads by showing how to use proxies or specify an alternate resources URL. ```python import stanza # Use proxy if behind corporate firewall proxies = {'https': 'https://proxy.example.com:8080'} stanza.download('en', proxies=proxies) # Or use custom resources URL stanza.download('en', resources_url='https://alternate-mirror.example.com') ``` -------------------------------- ### Resource Management Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/README.md Functions for managing Stanza models, including downloading and installing necessary resources. It also covers integration with CoreNLP and environment variable configurations. ```APIDOC ## Resource Management ### Function: download() #### Description Manages the downloading of Stanza models. ### Function: install_corenlp() #### Description Installs the CoreNLP component for integration. ### Function: download_corenlp_models() #### Description Downloads necessary models for CoreNLP integration. ### Environment Variables Information on environment variables that can be used to configure Stanza's resource paths and behavior. ``` -------------------------------- ### MultilingualPipeline Initialization Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/api-reference/pipeline.md Initializes a multilingual pipeline with options for model directory, language identification, per-language configurations, batch sizes, cache management, GPU usage, and download strategies. ```APIDOC ## `__init__` ### Description Initialize multilingual pipeline with language detection and per-language configurations. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters Table | Parameter | Type | Default | Description | |-----------|------|---------|-------------| | model_dir | str | DEFAULT_MODEL_DIR | Directory for all language models | | lang_id_config | dict | None | Configuration dict passed to language identification Pipeline. Override with `{'batch_size': 32}` etc. | | lang_configs | dict | None | Per-language config dicts. Format: `{'en': {'processors': 'tokenize,pos'}, 'fr': {...}}`. Supports defaultdict for dynamic configs. | | ld_batch_size | int | 64 | Batch size for language identification (internal use) | | max_cache_size | int | 10 | Maximum number of language-specific pipelines to keep in cache. LRU eviction when exceeded. | | use_gpu | bool | None | Use GPU if available | | restrict | bool | False | If True, restrict language detection to languages in lang_configs keys | | device | str | None | Device string ('cpu', 'cuda', etc.). If None, auto-selects. | | download_method | DownloadMethod | DOWNLOAD_RESOURCES | Model download strategy | | processors | str or list | None | Processors to use for all languages (unless overridden in lang_configs). Format: "tokenize,pos,lemma" ### Request Example ```python # Basic multilingual with auto language detection nlp_multi = stanza.MultilingualPipeline() doc = nlp_multi("Hello world") # English doc = nlp_multi("Bonjour le monde") # French # Per-language customization lang_configs = { "en": {"processors": "tokenize,pos,lemma,depparse,ner"}, "fr": {"processors": "tokenize,pos,lemma,depparse"}, # No NER for French } nlp_multi = stanza.MultilingualPipeline(lang_configs=lang_configs) # Dynamic per-language config using defaultdict from collections import defaultdict lang_configs = defaultdict(lambda: {"processors": "tokenize"}) # All langs tokenize only nlp_multi = stanza.MultilingualPipeline(lang_configs=lang_configs) ``` ``` -------------------------------- ### Iterate Through Coreference Mentions Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/api-reference/document.md Process coreference chains to identify and extract mentions of entities. This example demonstrates iterating through chains and their mentions, handling potential zero-anaphora cases. ```python for chain in doc.coref: for mention in chain.mentions: if mention.sentence < len(doc.sentences): sentence = doc.sentences[mention.sentence] # Handle zero-anaphora (empty nodes) if isinstance(mention.start_word, tuple): print(f"Zero-anaphora at sentence {mention.sentence}") else: words = sentence.words[mention.start_word:mention.end_word] mention_text = " ".join(w.text for w in words) print(f"Mention: {mention_text}") ``` -------------------------------- ### Pipeline Initialization with DownloadMethod.NONE Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/api-reference/pipeline.md Initializes a Stanza pipeline in offline mode, using only locally cached models. This is useful when network access is restricted or unavailable. ```python # Offline mode: use only cached models nlp = stanza.Pipeline(lang='en', download_method=stanza.DownloadMethod.NONE) ``` -------------------------------- ### Handle CUDA/GPU Not Available Errors Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/errors.md Shows how to check for GPU availability and fall back to CPU when a GPU is requested but not available. Includes options for explicit CPU device or auto-detection. ```python import stanza # Check GPU availability import torch print(f"GPU available: {torch.cuda.is_available()}") # Fall back to CPU lp = stanza.Pipeline(lang='en', device='cpu') # Or auto-detect lp = stanza.Pipeline(lang='en', use_gpu=None) # Auto-selects based on availability ``` -------------------------------- ### Initialize Stanza Pipeline Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/api-reference/pipeline.md Initialize a Pipeline for a specific language and set of processors. Use this to set up the NLP pipeline with desired configurations. ```python import stanza # Basic English pipeline with default processors nlp = stanza.Pipeline(lang='en') doc = nlp("Barack Obama was born in Hawaii.") print(doc.sentences[0].words[0].text) # "Barack" ``` ```python # Minimal tokenization-only pipeline nlp_minimal = stanza.Pipeline(lang='en', processors='tokenize') doc = nlp_minimal("This is a test.") ``` ```python # Custom processor packages nlp_custom = stanza.Pipeline( lang='en', package='ewt', # English Web Treebank processors={'tokenize': None, 'pos': None, 'lemma': None} ) ``` ```python # Use specific model files nlp_custom_models = stanza.Pipeline( lang='en', ner_model_path='/path/to/custom_ner.pt' ) ``` ```python # GPU with custom cache cache = stanza.models.common.foundation_cache.FoundationCache() nlp_gpu = stanza.Pipeline(lang='en', use_gpu=True, foundation_cache=cache) ``` -------------------------------- ### Find Nth Occurrence of Substring Source: https://github.com/stanfordnlp/stanza/blob/main/demo/semgrex visualization.ipynb A utility function to find the starting index of the nth occurrence of a substring within a larger string. Useful for parsing and manipulating text data. ```python def find_nth(haystack, needle, n): """ Returns the starting index of the nth occurrence of the substring 'needle' in the string 'haystack'. """ start = haystack.find(needle) while start >= 0 and n > 1: start = haystack.find(needle, start + len(needle)) n -= 1 return start ``` -------------------------------- ### Working with CoNLL-U Format Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/INDEX.md Illustrates how to load documents from CoNLL-U format, process them with a Stanza pipeline, and save the results back to CoNLL-U. Assumes pre-tokenized text. ```python from stanza.utils.conll import CoNLL # Load from file docs = CoNLL.conll2doc('data.conllu') # Create pipeline (skip tokenize since pre-tokenized) lp = stanza.Pipeline(lang='en', processors='pos,lemma,depparse') # Process for i, doc in enumerate(docs): docs[i] = nlp(doc, processors='pos,lemma,depparse') # Save back to CoNLL-U output = CoNLL.doc2conll(docs) with open('output.conllu', 'w') as f: f.write(output) ``` -------------------------------- ### Python 'with' Statement for CoreNLP Client Source: https://github.com/stanfordnlp/stanza/blob/main/demo/Stanza_CoreNLP_Interface.ipynb Use the 'with' statement to automatically manage the start and stop of the CoreNLP server process. This is the recommended approach to prevent orphaned server processes. ```python print("Starting a server with the Python \"with\" statement...") with CoreNLPClient(annotators=["tokenize","ssplit", "pos", "lemma", "ner"], memory='4G', endpoint='http://localhost:9001', be_quiet=True) as client: text = "Albert Einstein was a German-born theoretical physicist." document = client.annotate(text) print("{:30s}\t{}".format("Mention", "Type")) for sent in document.sentence: for m in sent.mentions: print("{:30s}\t{}".format(m.entityMentionText, m.entityType)) print("\nThe server should be stopped upon exit from the \"with\" statement.") ``` -------------------------------- ### Multilingual Pipeline with Per-Language Customization Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/api-reference/pipeline.md Shows how to customize processors for specific languages using a dictionary. This allows fine-grained control over the NLP pipeline for each language. ```python # Per-language customization lang_configs = { "en": {"processors": "tokenize,pos,lemma,depparse,ner"}, "fr": {"processors": "tokenize,pos,lemma,depparse"}, # No NER for French } nlp_multi = stanza.MultilingualPipeline(lang_configs=lang_configs) ``` -------------------------------- ### Stanza Pipeline Initialization and Semgrex Execution Source: https://github.com/stanfordnlp/stanza/blob/main/demo/semgrex visualization.ipynb Initializes a Stanza NLP pipeline with specified processors and executes Semgrex queries against a document. This is the main entry point for demonstrating Semgrex functionality. ```python import stanza def main(): nlp = stanza.Pipeline("en", processors="tokenize,pos,lemma,depparse") # doc = nlp("This a dummy sentence. Banning opal removed all artifact decks from the meta. I miss playing lantern. This is a dummy sentence.") doc = nlp("Banning opal removed artifact decks from the meta. Banning tennis resulted in players banning people.") # A single result .result[i].result[j] is a list of matches for sentence i on semgrex query j. queries = ["{pos:NN}=object ' with the desired language (e.g., 'en' for English). ```python import stanza stanza.download() ``` -------------------------------- ### Round-trip Conversion: Text to CoNLL-U and Back Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/api-reference/conll.md Demonstrates a round-trip conversion process: starting with raw text, processing it with Stanza, converting the Document to CoNLL-U, and then parsing it back into a Document object to verify data integrity. ```python from stanza.utils.conll import CoNLL import stanza # Start with raw text lp = stanza.Pipeline(lang='en') doc = nlp("This is a test.") # Convert to CoNLL-U conllu_str = CoNLL.doc2conll(doc) # Parse back to Document import io doc2 = CoNLL.conll2doc(io.StringIO(conllu_str))[0] # Verify equivalence assert doc.get('text') == doc2.get('text') ``` -------------------------------- ### Construct CoreNLPClient Instance Source: https://github.com/stanfordnlp/stanza/blob/main/demo/Stanza_CoreNLP_Interface.ipynb Instantiates a CoreNLPClient with specified annotators, memory allocation, and endpoint. Useful for setting up the NLP processing pipeline. ```python client = CoreNLPClient( annotators=['tokenize','ssplit', 'pos', 'lemma', 'ner'], memory='4G', endpoint='http://localhost:9001', be_quiet=True) print(client) ``` -------------------------------- ### Configure Constituency Parser Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/configuration.md Enable the constituency parser and print the resulting tree structure for each sentence. ```python nlp = stanza.Pipeline(lang='en', processors='tokenize,pos,constituency') doc = nlp("John runs quickly.") for sentence in doc.sentences: print(sentence.constituency) # Tree structure ``` -------------------------------- ### Pipeline Initialization and Processing Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/README.md The `Pipeline` class is the main entry point for NLP processing. It allows users to initialize a pipeline with specified processors and process text documents. The `MultilingualPipeline` class offers automatic language detection. ```APIDOC ## Pipeline Class ### Description Represents a natural language processing pipeline. It is the primary interface for users to process text. ### Class Signature ```python Pipeline(lang, dir=None, processors={}, verbose=True, **kwargs) ``` ### Parameters #### Parameters - **lang** (str) - The language of the documents to be processed. - **dir** (str, optional) - Directory where models are saved. Defaults to None. - **processors** (dict, optional) - A dictionary specifying the processors to use and their configurations. Defaults to an empty dictionary. - **verbose** (bool, optional) - If True, prints verbose output. Defaults to True. - **kwargs** - Additional keyword arguments for configuration. ### Method: process #### Description Processes a given text document. #### Signature ```python def process(self, doc, processors=None) ``` #### Parameters - **doc** (str or Document) - The text document to process, either as a string or a `Document` object. - **processors** (dict, optional) - Overrides the default processors for this specific call. #### Return Values A `Document` object containing the processed text and annotations. ### Class: MultilingualPipeline #### Description Handles automatic language detection and routing for multilingual text processing. #### Class Signature ```python MultilingualPipeline(lang='multilingual', dir=None, processors={}, verbose=True, **kwargs) ``` #### Parameters - **lang** (str) - Set to 'multilingual' for automatic language detection. - **dir** (str, optional) - Directory where models are saved. Defaults to None. - **processors** (dict, optional) - A dictionary specifying the processors to use and their configurations. Defaults to an empty dictionary. - **verbose** (bool, optional) - If True, prints verbose output. Defaults to True. - **kwargs** - Additional keyword arguments for configuration. ### Enum: DownloadMethod #### Description Controls the behavior of model downloading. #### Values - `DOWNLOAD_IF_MISSING` - `DOWNLOAD_ALWAYS` - `DOWNLOAD_NEVER` ``` -------------------------------- ### Check for Running Java Processes Source: https://github.com/stanfordnlp/stanza/blob/main/demo/Stanza_CoreNLP_Interface.ipynb Lists running processes to verify that the StanfordCoreNLPServer Java process is active. ```bash !ps -o pid,cmd | grep java ``` -------------------------------- ### Import Stanza Data Utilities and Models Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/README.md Import utilities for handling CoNLL data and the Document model for processed text. These are useful for data manipulation and analysis. ```python from stanza.utils.conll import CoNLL from stanza.models.common.doc import Document ``` -------------------------------- ### Import Stanza Multilingual Pipeline Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/README.md Import and initialize the MultilingualPipeline for automatic language detection and routing. Use this when processing text in multiple languages. ```python from stanza import MultilingualPipeline nlp = MultilingualPipeline() ``` -------------------------------- ### Using Multiple NER Models Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/api-reference/processors.md Demonstrates how to load and use multiple NER models simultaneously within a Stanza pipeline. Each word will have a list of NER tags if multiple models are specified. ```python import stanza lp_multi = stanza.Pipeline( lang='en', processors={'ner': ['default', 'ontonotes']} ) for word in doc.sentences[0].words: print(f"{word.text}: {word.multi_ner}") # List of NER tags ``` -------------------------------- ### Basic Multilingual Pipeline with Custom Language Configurations Source: https://github.com/stanfordnlp/stanza/blob/main/_autodocs/configuration.md Configure a multilingual pipeline by specifying processors for English and French. This is useful when different languages require different sets of processors. ```python from collections import defaultdict # Basic multilingual with custom lang configs lang_configs = { "en": {"processors": "tokenize,pos,lemma,depparse,ner"}, "fr": {"processors": "tokenize,pos,lemma,depparse"}, } lp = stanza.MultilingualPipeline(lang_configs=lang_configs) ```