Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/stanfordnlp/stanza.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/demo
diff options
context:
space:
mode:
authorYuhao Zhang <zyh@stanford.edu>2020-03-17 04:18:12 +0300
committerYuhao Zhang <zyh@stanford.edu>2020-03-17 04:18:12 +0300
commit1fcf9e3b5e56cf1f927fbf034c66fc9533d1b8ff (patch)
tree345b0a5f4a8ab36c808e283fc0426ea18c7d6e59 /demo
parent5bff1194d7239e39c3b70ec1fbd45b4efaa09650 (diff)
Edit all notebooks with new names and interfaces
Diffstat (limited to 'demo')
-rw-r--r--demo/Stanza_Beginners_Guide.ipynb111
-rw-r--r--demo/Stanza_CoreNLP_Interface.ipynb52
2 files changed, 104 insertions, 59 deletions
diff --git a/demo/Stanza_Beginners_Guide.ipynb b/demo/Stanza_Beginners_Guide.ipynb
index 560d33de..b246c77e 100644
--- a/demo/Stanza_Beginners_Guide.ipynb
+++ b/demo/Stanza_Beginners_Guide.ipynb
@@ -3,8 +3,7 @@
"nbformat_minor": 0,
"metadata": {
"colab": {
- "name": "StanfordNLP-101.ipynb",
- "version": "0.3.2",
+ "name": "Stanza-Beginners-Guide.ipynb",
"provenance": [],
"collapsed_sections": [],
"toc_visible": true
@@ -22,14 +21,14 @@
"colab_type": "text"
},
"source": [
- "# Welcome to StanfordNLP!\n",
+ "# Welcome to Stanza!\n",
"\n",
- "![Latest Version](https://img.shields.io/pypi/v/stanfordnlp.svg?colorB=bc4545)\n",
- "![Python Versions](https://img.shields.io/pypi/pyversions/stanfordnlp.svg?colorB=bc4545)\n",
+ "![Latest Version](https://img.shields.io/pypi/v/stanza.svg?colorB=bc4545)\n",
+ "![Python Versions](https://img.shields.io/pypi/pyversions/stanza.svg?colorB=bc4545)\n",
"\n",
- "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.\n",
+ "Stanza is a Python NLP toolkit that supports 60+ human languages. It is built with highly accurate neural network components that enable efficient training and evaluation with your own annotated data, and offers pretrained models on 100 treebanks. Additionally, Stanza provides a stable, officially maintained Python interface to Java Stanford CoreNLP Toolkit.\n",
"\n",
- "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."
+ "In this tutorial, we will demonstrate how to set up Stanza and annotate text with its native neural network NLP models. For the use of the Python CoreNLP interface, please see other tutorials."
]
},
{
@@ -39,9 +38,9 @@
"colab_type": "text"
},
"source": [
- "## 1. Installing StanfordNLP\n",
+ "## 1. Installing Stanza\n",
"\n",
- "StanfordNLP 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:"
+ "Note that Stanza only supports Python 3.6 and above. Installing and importing Stanza are as simple as running the following commands:"
]
},
{
@@ -53,10 +52,10 @@
},
"source": [
"# Install; note that the prefix \"!\" is not needed if you are running in a terminal\n",
- "!pip install stanfordnlp\n",
+ "!pip install stanza\n",
"\n",
"# Import the package\n",
- "import stanfordnlp"
+ "import stanza"
],
"execution_count": 0,
"outputs": []
@@ -82,9 +81,9 @@
"source": [
"## 2. Downloading Models\n",
"\n",
- "You 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\"). \n",
+ "You can download models with the `stanza.download` command. The language can be specified with either a full language name (e.g., \"english\"), or a short code (e.g., \"en\"). \n",
"\n",
- "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.\n"
+ "By default, models will be saved to your `~/stanza_resources` directory. If you want to specify your own path to save the model files, you can pass a `dir=your_path` argument.\n"
]
},
{
@@ -96,11 +95,13 @@
},
"source": [
"# Download an English model into the default directory\n",
- "# Note that if force=True is not specified, the command will be followed with prompts asking for your confirmation.\n",
- "stanfordnlp.download('en', force=True)\n",
+ "print(\"Downloading English model...\")\n",
+ "stanza.download('en')\n",
"\n",
- "# Similarly, download a (traditional) Chinese model\n",
- "stanfordnlp.download('zh', force=True)"
+ "# Similarly, download a (simplified) Chinese model\n",
+ "# Note that you can use verbose=False to turn off all printed messages\n",
+ "print(\"Downloading Chinese model...\")\n",
+ "stanza.download('zh', verbose=False)"
],
"execution_count": 0,
"outputs": []
@@ -114,7 +115,7 @@
"source": [
"### More Information\n",
"\n",
- "Pretrained 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.html#human-languages-supported-by-stanfordnlp).\n"
+ "Pretrained models are provided for 60+ different languages. For all languages, available models and the corresponding short language codes, please check out the [models page](https://stanfordnlp.github.io/stanza/models.html).\n"
]
},
{
@@ -138,9 +139,11 @@
"\n",
"To 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).\n",
"\n",
- "- 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.\n",
+ "- By default, the pipeline will include all processors, including tokenization, multi-word token expansion, part-of-speech tagging, lemmatization, dependency parsing and named entity recognition (for supported languages). However, you can always specify what processors you want to include with the `processors` argument.\n",
"\n",
- "- 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`."
+ "- Stanza's pipeline is CUDA-aware, meaning that a CUDA-device will be used whenever it is available, otherwise CPUs will be used when a GPU is not found. You can force the pipeline to use CPU regardless by setting `use_gpu=False`.\n",
+ "\n",
+ "- Again, you can suppress all printed messages by setting `verbose=False`."
]
},
{
@@ -152,10 +155,12 @@
},
"source": [
"# Build an English pipeline, with all processors by default\n",
- "en_nlp = stanfordnlp.Pipeline(lang='en')\n",
+ "print(\"Building an English pipeline...\")\n",
+ "en_nlp = stanza.Pipeline('en')\n",
"\n",
- "# Build a Chinese pipeline, with customized processor list, and force it to use CPU\n",
- "zh_nlp = stanfordnlp.Pipeline(lang='zh', processors='tokenize,lemma,pos,depparse', use_gpu=False)"
+ "# Build a Chinese pipeline, with customized processor list and no logging, and force it to use CPU\n",
+ "print(\"Building a Chinese pipeline...\")\n",
+ "zh_nlp = stanza.Pipeline('zh', processors='tokenize,lemma,pos,depparse', verbose=False, use_gpu=False)"
],
"execution_count": 0,
"outputs": []
@@ -185,7 +190,7 @@
"print(type(en_doc))\n",
"\n",
"# Processing Chinese text\n",
- "zh_doc = zh_nlp(\"達沃斯世界經濟論壇是每年全球政商界領袖聚在一起的年度盛事。\")\n",
+ "zh_doc = zh_nlp(\"达沃斯世界经济论坛是每年全球政商界领袖聚在一起的年度盛事。\")\n",
"print(type(zh_doc))"
],
"execution_count": 0,
@@ -214,9 +219,12 @@
"\n",
"Annotations can be accessed from the returned `Document` object. \n",
"\n",
- "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.\n",
+ "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`, while in English a word and a token are equivalent. Note that dependency parses are derived over `Word`s.\n",
+ "\n",
+ "Additionally, a `Span` object is used to represent annotations that are part of a document, such as named entity mentions.\n",
+ "\n",
"\n",
- "The following example iterates over all English sentences and words, and prints the word information one by one:"
+ "The following example iterate over all English sentences and words, and print the word information one by one:"
]
},
{
@@ -231,7 +239,7 @@
" print(\"[Sentence {}]\".format(i+1))\n",
" for word in sent.words:\n",
" print(\"{:12s}\\t{:12s}\\t{:6s}\\t{:d}\\t{:12s}\".format(\\\n",
- " word.text, word.lemma, word.pos, word.governor, word.dependency_relation))\n",
+ " word.text, word.lemma, word.pos, word.head, word.deprel))\n",
" print(\"\")"
],
"execution_count": 0,
@@ -240,11 +248,36 @@
{
"cell_type": "markdown",
"metadata": {
+ "id": "-AUkCkNIrusq",
+ "colab_type": "text"
+ },
+ "source": [
+ "The following example iterate over all extracted named entity mentions and print out their character spans and types."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "5Uu0-WmvsnlK",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "print(\"Mention text\\tType\\tStart-End\")\n",
+ "for ent in en_doc.ents:\n",
+ " print(\"{}\\t{}\\t{}-{}\".format(ent.text, ent.type, ent.start_char, ent.end_char))"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
"id": "Ql1SZlZOnMLo",
"colab_type": "text"
},
"source": [
- "Similarly for the Chinese text:"
+ "And similarly for the Chinese text:"
]
},
{
@@ -259,7 +292,7 @@
" print(\"[Sentence {}]\".format(i+1))\n",
" for word in sent.words:\n",
" print(\"{:12s}\\t{:12s}\\t{:6s}\\t{:d}\\t{:12s}\".format(\\\n",
- " word.text, word.lemma, word.pos, word.governor, word.dependency_relation))\n",
+ " word.text, word.lemma, word.pos, word.head, word.deprel))\n",
" print(\"\")"
],
"execution_count": 0,
@@ -272,7 +305,7 @@
"colab_type": "text"
},
"source": [
- "Alternatively, you can directly print a `Word` object to view all its annotations:"
+ "Alternatively, you can directly print a `Word` object to view all its annotations as a Python dict:"
]
},
{
@@ -298,7 +331,7 @@
"source": [
"### More Information\n",
"\n",
- "For all information on different data objects, please visit our [data objects page](https://stanfordnlp.github.io/stanfordnlp/data_objects.html)."
+ "For all information on different data objects, please visit our [data objects page](https://stanfordnlp.github.io/stanza/data_objects.html)."
]
},
{
@@ -308,13 +341,17 @@
"colab_type": "text"
},
"source": [
- "## 5. Other Resources\n",
+ "## 5. Resources\n",
+ "\n",
+ "Apart from this interactive tutorial, we also provide tutorials on our website that cover a variety of use cases such as how to use different model \"packages\" for a language, how to use spaCy as a tokenizer, how to process pretokenized text without running the tokenizer, etc. For these tutorials please visit [our Tutorials page](https://stanfordnlp.github.io/stanza/tutorials.html).\n",
+ "\n",
+ "Other resources that you may find helpful include:\n",
"\n",
- "- [StanfordNLP Homepage](https://stanfordnlp.github.io/stanfordnlp/index.html)\n",
- "- [FAQs](https://stanfordnlp.github.io/stanfordnlp/faq.html)\n",
- "- [GitHub Repo](https://github.com/stanfordnlp/stanfordnlp)\n",
- "- [Reporting Issues](https://github.com/stanfordnlp/stanfordnlp/issues)\n",
- "- [System Paper](https://nlp.stanford.edu/pubs/qi2018universal.pdf)\n"
+ "- [Stanza Homepage](https://stanfordnlp.github.io/stanza/index.html)\n",
+ "- [FAQs](https://stanfordnlp.github.io/stanza/faq.html)\n",
+ "- [GitHub Repo](https://github.com/stanfordnlp/stanza)\n",
+ "- [Reporting Issues](https://github.com/stanfordnlp/stanza/issues)\n",
+ "- [Stanza System Description Paper](http://arxiv.org/abs/2003.07082)\n"
]
}
]
diff --git a/demo/Stanza_CoreNLP_Interface.ipynb b/demo/Stanza_CoreNLP_Interface.ipynb
index b3b7b864..76540a05 100644
--- a/demo/Stanza_CoreNLP_Interface.ipynb
+++ b/demo/Stanza_CoreNLP_Interface.ipynb
@@ -3,8 +3,7 @@
"nbformat_minor": 0,
"metadata": {
"colab": {
- "name": "StanfordNLP-CoreNLP-Interface.ipynb",
- "version": "0.3.2",
+ "name": "Stanza-CoreNLP-Interface.ipynb",
"provenance": [],
"collapsed_sections": [],
"toc_visible": true
@@ -22,15 +21,15 @@
"colab_type": "text"
},
"source": [
- "# StanfordNLP: Python CoreNLP Interface\n",
+ "# Stanza: A Tutorial on the Python CoreNLP Interface\n",
"\n",
- "![Latest Version](https://img.shields.io/pypi/v/stanfordnlp.svg?colorB=bc4545)\n",
- "![Python Versions](https://img.shields.io/pypi/pyversions/stanfordnlp.svg?colorB=bc4545)\n",
+ "![Latest Version](https://img.shields.io/pypi/v/stanza.svg?colorB=bc4545)\n",
+ "![Python Versions](https://img.shields.io/pypi/pyversions/stanza.svg?colorB=bc4545)\n",
"\n",
- "While the StanfordNLP library implements accurate neural network modules for basic functionalities such as part-of-speech tagging and dependency parsing, the [Stanford CoreNLP Java library](https://stanfordnlp.github.io/CoreNLP/) has been developed for years and offers more complementary features such as coreference resolution and relation extraction. To unlock these features, the StanfordNLP library also offers an officially maintained Python interface to the CoreNLP Java library. This interface allows you to get NLP anntotations from CoreNLP by writing native Python code.\n",
+ "While the Stanza library implements accurate neural network modules for basic functionalities such as part-of-speech tagging and dependency parsing, the [Stanford CoreNLP Java library](https://stanfordnlp.github.io/CoreNLP/) has been developed for years and offers more complementary features such as coreference resolution and relation extraction. To unlock these features, the Stanza library also offers an officially maintained Python interface to the CoreNLP Java library. This interface allows you to get NLP anntotations from CoreNLP by writing native Python code.\n",
"\n",
"\n",
- "This tutorial walks you through the installation, setup and basic usage of this Python CoreNLP interface. If you want to learn how to use the neural network components in StanfordNLP, please refer to other tutorials."
+ "This tutorial walks you through the installation, setup and basic usage of this Python CoreNLP interface. If you want to learn how to use the neural network components in Stanza, please refer to other tutorials."
]
},
{
@@ -52,9 +51,9 @@
"colab_type": "text"
},
"source": [
- "### Installing StanfordNLP\n",
+ "### Installing Stanza\n",
"\n",
- "Installing and importing StanfordNLP are as simple as running the following commands:"
+ "Installing and importing Stanza are as simple as running the following commands:"
]
},
{
@@ -65,11 +64,11 @@
"colab": {}
},
"source": [
- "# Install stanfordnlp; note that the prefix \"!\" is not needed if you are running in a terminal\n",
- "!pip install stanfordnlp\n",
+ "# Install stanza; note that the prefix \"!\" is not needed if you are running in a terminal\n",
+ "!pip install stanza==1.0.0.rc0\n",
"\n",
- "# Import stanfordnlp\n",
- "import stanfordnlp"
+ "# Import stanza\n",
+ "import stanza"
],
"execution_count": 0,
"outputs": []
@@ -136,7 +135,7 @@
"\n",
"At a high level, the CoreNLP Python interface works by first starting a background Java CoreNLP server process, and then initializing a client instance in Python which can pass the text to the background server process, and accept the returned annotation results.\n",
"\n",
- "We wrap these functionalities in a `CoreNLPClient` class. Therefore, we need to start by importing this class from StanfordNLP."
+ "We wrap these functionalities in a `CoreNLPClient` class. Therefore, we need to start by importing this class from Stanza."
]
},
{
@@ -148,7 +147,7 @@
},
"source": [
"# Import client module\n",
- "from stanfordnlp.server import CoreNLPClient"
+ "from stanza.server import CoreNLPClient"
],
"execution_count": 0,
"outputs": []
@@ -164,7 +163,7 @@
"\n",
"Additionally, the client constructor accepts a `memory` argument, which specify how much memory will be allocated to the background Java process. An `endpoint` option can be used to specify a port number used by the communication between the server and the client. The default port is 9000, however, since this port is token in Colab, we'll manually set it to 9001 in the following example.\n",
"\n",
- "For more options in constructing the clients, please refer to the [CoreNLP Client Options List](https://stanfordnlp.github.io/stanfordnlp/corenlp_client.html#corenlp-client-options)."
+ "For more options in constructing the clients, please refer to the [CoreNLP Client Options List](https://stanfordnlp.github.io/stanza/corenlp_client.html#corenlp-client-options)."
]
},
{
@@ -333,6 +332,16 @@
{
"cell_type": "markdown",
"metadata": {
+ "id": "Qp66wjZ10xia",
+ "colab_type": "text"
+ },
+ "source": [
+ "**Note**: Since the Stanza CoreNLP client interface simply ports the CoreNLP annotation results to native Python objects, for a comprehensive lists of available annotators and how their annotation results can be accessed, you will need to visit the [Stanford CoreNLP website](https://stanfordnlp.github.io/CoreNLP/)."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
"id": "IPqzMK90X0w3",
"colab_type": "text"
},
@@ -368,7 +377,7 @@
"source": [
"### More Information\n",
"\n",
- "For more information on how to use the `CoreNLPClient`, please go to the [CoreNLPClient documentation page](https://stanfordnlp.github.io/stanfordnlp/corenlp_client.html)."
+ "For more information on how to use the `CoreNLPClient`, please go to the [CoreNLPClient documentation page](https://stanfordnlp.github.io/stanza/corenlp_client.html)."
]
},
{
@@ -380,11 +389,10 @@
"source": [
"## 5. Other Resources\n",
"\n",
- "- [StanfordNLP Homepage](https://stanfordnlp.github.io/stanfordnlp/index.html)\n",
- "- [FAQs](https://stanfordnlp.github.io/stanfordnlp/faq.html)\n",
- "- [GitHub Repo](https://github.com/stanfordnlp/stanfordnlp)\n",
- "- [Reporting Issues](https://github.com/stanfordnlp/stanfordnlp/issues)\n",
- "- [System Paper](https://nlp.stanford.edu/pubs/qi2018universal.pdf)\n"
+ "- [Stanza Homepage](https://stanfordnlp.github.io/stanza/)\n",
+ "- [FAQs](https://stanfordnlp.github.io/stanza/faq.html)\n",
+ "- [GitHub Repo](https://github.com/stanfordnlp/stanza)\n",
+ "- [Reporting Issues](https://github.com/stanfordnlp/stanza/issues)\n"
]
}
]