<jupyter_start><jupyter_text>Welcome to StanfordNLP!![Latest Version](https://img.shields.io/pypi/v/stanfordnlp.svg?colorB=bc4545)![Python Versions](https://img.shields.io/pypi/pyversions/stanfordnlp.svg?colorB=bc4545)StanfordNLP is a Python NLP toolkit for core sentence analysis tasks like tokenization, lemmatization, part-of-speech tagging, and dependency parsing. It is built with highly accurate neural network components that enable efficient training and evaluation with your own annotated data, and it comes with pretrained models built on over 70+ [UD](https://universaldependencies.org/) treebanks that support 50+ human languages. Additionally, StanfordNLP provides a stable, officially maintained Python interface to the Java [Stanford CoreNLP](https://stanfordnlp.github.io/CoreNLP/) toolkit.In this tutorial, we will demonstrate how to set up StanfordNLP and annotate text with its native neural network NLP models. For the use of the Python CoreNLP interface, please see other tutorials.  1. Installing StanfordNLPStanfordNLP only supports Python 3.6 and above and uses [the PyTorch library](https://pytorch.org/). Installing and importing StanfordNLP are as simple as running the following commands:<jupyter_code># Install; note that the prefix "!" is not needed if you are running in a terminal
!pip install stanfordnlp

# Import the package
import stanfordnlp<jupyter_output><empty_output><jupyter_text>More InformationFor common troubleshooting, please visit our [troubleshooting page](https://stanfordnlp.github.io/stanfordnlp/installation_usage.htmltroubleshooting).  2. Downloading ModelsYou can download models with the `stanfordnlp.download` command. The language can be specified with either a full language name (e.g., "english"), or a short ISO 639 code (e.g., "en"). By default, models will be saved to your `~/stanfordnlp_resources` directory. If you want to specify your own path to save the model files, you can pass a `resource_dir=your_path` argument.<jupyter_code># Download an English model into the default directory
# Note that if force=True is not specified, the command will be followed with prompts asking for your confirmation.
stanfordnlp.download('en', force=True)

# Similarly, download a (traditional) Chinese model
stanfordnlp.download('zh', force=True)<jupyter_output><empty_output><jupyter_text>More InformationPretrained models are provided for 50+ different languages. For all languages and the corresponding short language codes, please check out the [models page](https://stanfordnlp.github.io/stanfordnlp/models.htmlhuman-languages-supported-by-stanfordnlp).  3. Processing Text  Constructing PipelineTo process a piece of text, you'll need to first construct a `Pipeline` with different `Processor` units. The pipeline is language-specific, so again you'll need to first specify the language (see examples).- By default, the pipeline will include all processors, including tokenization, multi-word token expansion, part-of-speech tagging, lemmatization and dependency parsing. However, you can always specify what processors you want to include with the `processors` argument.- Note that the backend device has to be specified when building the pipeline. By default, the pipeline will always use a CUDA-compatible GPU if it exists on your device to accelerate computing, and it'll fall back to CPU if no such GPU can be found. You can force the pipeline to use CPU regardless by setting `use_gpu=False`.<jupyter_code># Build an English pipeline, with all processors by default
en_nlp = stanfordnlp.Pipeline(lang='en')

# Build a Chinese pipeline, with customized processor list, and force it to use CPU
zh_nlp = stanfordnlp.Pipeline(lang='zh', processors='tokenize,lemma,pos,depparse', use_gpu=False)<jupyter_output><empty_output><jupyter_text>Annotating TextAfter a pipeline is successfully constructed, you can get annotations of a piece of text simply by passing the string into the pipeline object. The pipeline will return a `Document` object, which can be used to access detailed annotations from. For example:<jupyter_code># Processing English text
en_doc = en_nlp("<NAME> was born in Hawaii.  He was elected president in 2008.")
print(type(en_doc))

# Processing Chinese text
zh_doc = zh_nlp("達沃斯世界經濟論壇是每年全球政商界領袖聚在一起的年度盛事。")
print(type(zh_doc))<jupyter_output><empty_output><jupyter_text>More InformationFor more information on how to construct a pipeline and information on different processors, please visit our [pipeline page](https://stanfordnlp.github.io/stanfordnlp/pipeline.html).  4. Accessing AnnotationsAnnotations can be accessed from the returned `Document` object. A `Document` contains a list of `Sentence`s, and a `Sentence` contains a list of `Token`s and `Word`s. For the most part `Token`s and `Word`s overlap, but some tokens can be divided into mutiple words, for instance the French token `aux` is divided into the words `à` and `les`. Note that dependency parses are derived over `Word`s.The following example iterates over all English sentences and words, and prints the word information one by one:<jupyter_code>for i, sent in enumerate(en_doc.sentences):
    print("[Sentence {}]".format(i+1))
    for word in sent.words:
        print("{:12s}\t{:12s}\t{:6s}\t{:d}\t{:12s}".format(\
              word.text, word.lemma, word.pos, word.governor, word.dependency_relation))
    print("")<jupyter_output><empty_output><jupyter_text>Similarly for the Chinese text:<jupyter_code>for i, sent in enumerate(zh_doc.sentences):
    print("[Sentence {}]".format(i+1))
    for word in sent.words:
        print("{:12s}\t{:12s}\t{:6s}\t{:d}\t{:12s}".format(\
              word.text, word.lemma, word.pos, word.governor, word.dependency_relation))
    print("")<jupyter_output><empty_output><jupyter_text>Alternatively, you can directly print a `Word` object to view all its annotations:<jupyter_code>word = en_doc.sentences[0].words[0]
print(word)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08667898538551991, 'fraction_numerical': 0.007559213841760457, 'mean_word_length': 5.720090293453724, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 30, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '651744', 'license': 'Apache-2.0', 'n_tokens_mistral': 1754, 'n_tokens_neox': 1686, 'n_words': 658, 'path': 'demo/StanfordNLP_Beginners_Guide.ipynb', 'repo_name': 'xinkeyu/stanfordnlp', 'size': '5959'}