{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Running German Stanza\n",
    "\n",
    "Download this NB from [https://gatenlp.github.io/python-gatenlp/german-stanza.ipynb](https://gatenlp.github.io/python-gatenlp/german-stanza.ipynb)\n",
    "\n",
    "Requirements:\n",
    "* Python 3.7 or later\n",
    "* `pip install ipykernel ipython notebook gatenlp stanza`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gatenlp import Document\n",
    "import stanza \n",
    "from gatenlp.lib_stanza import apply_stanza\n",
    "from IPython.display import display\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "text = \"\"\"\n",
    "Die Meinung der LehrerInnen ist uns wichtig. \n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "doc = Document(text)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:stanza:Loading these models for language: de (German):\n",
      "=======================\n",
      "| Processor | Package |\n",
      "-----------------------\n",
      "| tokenize  | gsd     |\n",
      "| mwt       | gsd     |\n",
      "| pos       | gsd     |\n",
      "| lemma     | gsd     |\n",
      "| depparse  | gsd     |\n",
      "| sentiment | sb10k   |\n",
      "| ner       | conll03 |\n",
      "=======================\n",
      "\n",
      "INFO:stanza:Use device: cpu\n",
      "INFO:stanza:Loading: tokenize\n",
      "INFO:stanza:Loading: mwt\n",
      "INFO:stanza:Loading: pos\n",
      "INFO:stanza:Loading: lemma\n",
      "INFO:stanza:Loading: depparse\n",
      "INFO:stanza:Loading: sentiment\n",
      "INFO:stanza:Loading: ner\n",
      "INFO:stanza:Done loading processors!\n"
     ]
    }
   ],
   "source": [
    "# If necessary, first download the Stanza model (uncomment)\n",
    "# stanza.download(\"de\")\n",
    "\n",
    "# Load the german stanza model\n",
    "nlp = stanza.Pipeline(lang=\"de\", use_gpu=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Run stanza on the gatenlp document and convert annotations to gatenlp representation\n",
    "doc = apply_stanza(nlp, doc)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<div>\n",
       "<style>\n",
       "#CHQWGEPJMU-content {\n",
       "    width: 100%;\n",
       "    height: 100%;\n",
       "    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;\n",
       "}\n",
       "\n",
       ".CHQWGEPJMU-row {\n",
       "    width: 100%;\n",
       "    display: flex;\n",
       "    flex-direction: row;\n",
       "    flex-wrap: nowrap;\n",
       "}\n",
       "\n",
       ".CHQWGEPJMU-col {\n",
       "    border: 1px solid grey;\n",
       "    display: inline-block;\n",
       "    min-width: 200px;\n",
       "    padding: 5px;\n",
       "    /* white-space: normal; */\n",
       "    /* white-space: pre-wrap; */\n",
       "    overflow-y: auto;\n",
       "}\n",
       "\n",
       ".CHQWGEPJMU-hdr {\n",
       "    font-size: 1.2rem;\n",
       "    font-weight: bold;\n",
       "}\n",
       "\n",
       ".CHQWGEPJMU-label {\n",
       "    margin-bottom: -15px;\n",
       "    display: block;\n",
       "}\n",
       "\n",
       ".CHQWGEPJMU-input {\n",
       "    vertical-align: middle;\n",
       "    position: relative;\n",
       "    *overflow: hidden;\n",
       "}\n",
       "\n",
       "#CHQWGEPJMU-popup {\n",
       "    display: none;\n",
       "    colour: white;\n",
       "    position: absolute;\n",
       "    margin-top: 10%;\n",
       "    margin-left: 10%;\n",
       "    background: #ff88ff;\n",
       "    width: 60%;\n",
       "    height: 60%;\n",
       "    z-index: 50;\n",
       "    padding: 25px 25px 25px;\n",
       "    border: 1px solid black;\n",
       "    overflow: auto;\n",
       "}\n",
       "\n",
       ".CHQWGEPJMU-selection {\n",
       "    margin-bottom: 5px;\n",
       "}\n",
       "\n",
       ".CHQWGEPJMU-featuretable {\n",
       "    margin-top: 10px;\n",
       "}\n",
       "\n",
       ".CHQWGEPJMU-fname {\n",
       "    text-align: left !important;\n",
       "    font-weight: bold;\n",
       "    margin-right: 10px;\n",
       "}\n",
       ".CHQWGEPJMU-fvalue {\n",
       "    text-align: left !important;\n",
       "}\n",
       "</style>\n",
       "  <div id=\"CHQWGEPJMU-content\">\n",
       "        <div id=\"CHQWGEPJMU-popup\" style=\"display: none;\">\n",
       "        </div>\n",
       "        <div class=\"CHQWGEPJMU-row\" id=\"CHQWGEPJMU-row1\" style=\"height:67vh; min-height:100px;\">\n",
       "            <div id=\"CHQWGEPJMU-text-wrapper\" class=\"CHQWGEPJMU-col\" style=\"width:70%;\">\n",
       "                <div class=\"CHQWGEPJMU-hdr\" id=\"CHQWGEPJMU-dochdr\"></div>\n",
       "                <div id=\"CHQWGEPJMU-text\">\n",
       "                </div>\n",
       "            </div>\n",
       "            <div id=\"CHQWGEPJMU-chooser\" class=\"CHQWGEPJMU-col\" style=\"width:30%; border-left-width: 0px;\"></div>\n",
       "        </div>\n",
       "        <div class=\"CHQWGEPJMU-row\" id=\"CHQWGEPJMU-row2\" style=\"height:30vh; min-height: 100px;\">\n",
       "            <div id=\"CHQWGEPJMU-details\" class=\"CHQWGEPJMU-col\" style=\"width:100%; border-top-width: 0px;\">\n",
       "            </div>\n",
       "        </div>\n",
       "    </div>\n",
       "    \n",
       "    <script type=\"application/json\" id=\"CHQWGEPJMU-data\">\n",
       "    {\"annotation_sets\": {\"\": {\"name\": \"detached-from:\", \"annotations\": [{\"type\": \"Token\", \"start\": 1, \"end\": 4, \"id\": 0, \"features\": {\"id\": 1, \"text\": \"Die\", \"lemma\": \"der\", \"upos\": \"DET\", \"xpos\": \"ART\", \"head\": 1, \"deprel\": \"det\", \"ner\": \"O\", \"Case\": \"Nom\", \"Definite\": \"Def\", \"Gender\": \"Fem\", \"Number\": \"Sing\", \"PronType\": \"Art\"}}, {\"type\": \"Token\", \"start\": 5, \"end\": 12, \"id\": 1, \"features\": {\"id\": 2, \"text\": \"Meinung\", \"lemma\": \"Meinung\", \"upos\": \"NOUN\", \"xpos\": \"NN\", \"head\": 6, \"deprel\": \"nsubj\", \"ner\": \"O\", \"Case\": \"Nom\", \"Gender\": \"Fem\", \"Number\": \"Sing\"}}, {\"type\": \"Token\", \"start\": 13, \"end\": 16, \"id\": 2, \"features\": {\"id\": 3, \"text\": \"der\", \"lemma\": \"der\", \"upos\": \"DET\", \"xpos\": \"ART\", \"head\": 3, \"deprel\": \"det\", \"ner\": \"O\", \"Case\": \"Gen\", \"Definite\": \"Def\", \"Number\": \"Plur\", \"PronType\": \"Art\"}}, {\"type\": \"Token\", \"start\": 17, \"end\": 28, \"id\": 3, \"features\": {\"id\": 4, \"text\": \"LehrerInnen\", \"lemma\": \"Lehrerin\", \"upos\": \"NOUN\", \"xpos\": \"NN\", \"head\": 1, \"deprel\": \"nmod\", \"ner\": \"O\", \"Case\": \"Gen\", \"Gender\": \"Neut\", \"Number\": \"Plur\"}}, {\"type\": \"Token\", \"start\": 29, \"end\": 32, \"id\": 4, \"features\": {\"id\": 5, \"text\": \"ist\", \"lemma\": \"sein\", \"upos\": \"AUX\", \"xpos\": \"VAFIN\", \"head\": 6, \"deprel\": \"cop\", \"ner\": \"O\", \"Mood\": \"Ind\", \"Number\": \"Sing\", \"Person\": \"3\", \"Tense\": \"Pres\", \"VerbForm\": \"Fin\"}}, {\"type\": \"Token\", \"start\": 33, \"end\": 36, \"id\": 5, \"features\": {\"id\": 6, \"text\": \"uns\", \"lemma\": \"wir\", \"upos\": \"PRON\", \"xpos\": \"PPER\", \"head\": 6, \"deprel\": \"iobj\", \"ner\": \"O\", \"Case\": \"Dat\", \"Number\": \"Plur\", \"Person\": \"1\", \"PronType\": \"Prs\"}}, {\"type\": \"Token\", \"start\": 37, \"end\": 44, \"id\": 6, \"features\": {\"id\": 7, \"text\": \"wichtig\", \"lemma\": \"wichtig\", \"upos\": \"ADJ\", \"xpos\": \"ADJD\", \"head\": 8, \"deprel\": \"root\", \"ner\": \"O\"}}, {\"type\": \"Token\", \"start\": 44, \"end\": 45, \"id\": 7, \"features\": {\"id\": 8, \"text\": \".\", \"lemma\": \".\", \"upos\": \"PUNCT\", \"xpos\": \"$.\", \"head\": 6, \"deprel\": \"punct\", \"ner\": \"O\"}}, {\"type\": \"Sentence\", \"start\": 1, \"end\": 45, \"id\": 8, \"features\": {}}], \"next_annid\": 9}}, \"text\": \"\\nDie Meinung der LehrerInnen ist uns wichtig. \\n\", \"features\": {}, \"offset_type\": \"j\", \"name\": \"\"}\n",
       "    </script>\n",
       "    <script type=\"text/javascript\">\n",
       "        gatenlp_run(\"CHQWGEPJMU-\");\n",
       "    </script>\n",
       "  </div>\n",
       "<div>ID: CHQWGEPJMU</div>"
      ],
      "text/plain": [
       "Document(\n",
       "Die Meinung der LehrerInnen ist uns wichtig. \n",
       ",features=Features({}),anns={'': AnnotationSet([Annotation(1,4,Token,features=Features({'id': 1, 'text': 'Die', 'lemma': 'der', 'upos': 'DET', 'xpos': 'ART', 'head': 1, 'deprel': 'det', 'ner': 'O', 'Case': 'Nom', 'Definite': 'Def', 'Gender': 'Fem', 'Number': 'Sing', 'PronType': 'Art'}),id=0), Annotation(1,45,Sentence,features=Features({}),id=8), Annotation(5,12,Token,features=Features({'id': 2, 'text': 'Meinung', 'lemma': 'Meinung', 'upos': 'NOUN', 'xpos': 'NN', 'head': 6, 'deprel': 'nsubj', 'ner': 'O', 'Case': 'Nom', 'Gender': 'Fem', 'Number': 'Sing'}),id=1), Annotation(13,16,Token,features=Features({'id': 3, 'text': 'der', 'lemma': 'der', 'upos': 'DET', 'xpos': 'ART', 'head': 3, 'deprel': 'det', 'ner': 'O', 'Case': 'Gen', 'Definite': 'Def', 'Number': 'Plur', 'PronType': 'Art'}),id=2), Annotation(17,28,Token,features=Features({'id': 4, 'text': 'LehrerInnen', 'lemma': 'Lehrerin', 'upos': 'NOUN', 'xpos': 'NN', 'head': 1, 'deprel': 'nmod', 'ner': 'O', 'Case': 'Gen', 'Gender': 'Neut', 'Number': 'Plur'}),id=3), Annotation(29,32,Token,features=Features({'id': 5, 'text': 'ist', 'lemma': 'sein', 'upos': 'AUX', 'xpos': 'VAFIN', 'head': 6, 'deprel': 'cop', 'ner': 'O', 'Mood': 'Ind', 'Number': 'Sing', 'Person': '3', 'Tense': 'Pres', 'VerbForm': 'Fin'}),id=4), Annotation(33,36,Token,features=Features({'id': 6, 'text': 'uns', 'lemma': 'wir', 'upos': 'PRON', 'xpos': 'PPER', 'head': 6, 'deprel': 'iobj', 'ner': 'O', 'Case': 'Dat', 'Number': 'Plur', 'Person': '1', 'PronType': 'Prs'}),id=5), Annotation(37,44,Token,features=Features({'id': 7, 'text': 'wichtig', 'lemma': 'wichtig', 'upos': 'ADJ', 'xpos': 'ADJD', 'head': 8, 'deprel': 'root', 'ner': 'O'}),id=6), Annotation(44,45,Token,features=Features({'id': 8, 'text': '.', 'lemma': '.', 'upos': 'PUNCT', 'xpos': '$.', 'head': 6, 'deprel': 'punct', 'ner': 'O'}),id=7)])})"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Show the annotated document\n",
    "display(doc)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python gatenlp",
   "language": "python",
   "name": "gatenlp"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
