{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# GATE Slave\n",
    "\n",
    "The GATE Slave is a module that allows to run anything in a Java GATE process from Python and interchange documents between Python and Java.\n",
    "\n",
    "One possible use of this is to run an existing GATE pipeline on a Python GateNLP document.\n",
    "\n",
    "This is done by the python module communicating with a Java process over a socket connection. \n",
    "Java calls on the Python side are sent over to Java, executed and the result is send back to Python. \n",
    "\n",
    "For this to work, GATE and Java have to be installed on the machine that runs the GATE Slave.\n",
    "\n",
    "The easiest way to run this is by first manually starting the GATE Slave in the Java GATE GUI and then \n",
    "connecting to it from the Python side. \n",
    "\n",
    "## Manually starting the GATE Slave from GATE\n",
    "\n",
    "1. Start GATE\n",
    "2. Load the Python plugin using the CREOLE Plugin Manager\n",
    "3. Create a new Language Resource: \"PythonSlaveLr\"\n",
    "\n",
    "When creating the PyhonSlaveLr, the following initialization parameters can be specified:\n",
    "* `authToken`: this is used to prevent other processes from connecting to the slave. You can either specify \n",
    "  some string here or with `useAuthToken` set to `true` let GATE choose a random one and display it in the \n",
    "  message pane after the resource has been created. \n",
    "* `host`:  The host name or address to bind to. The default 127.0.0.1 makes the slave only visible on the same\n",
    "  machine. In order to make it visible on other machines, use the host name or IP address on the network\n",
    "  or use 0.0.0.0 \n",
    "* `logActions`: if this is set to true, the actions requested by the Python process are logged to the message pane. \n",
    "* `port`: the port number to use. Each slave requires their own port number so if more than one slave is running\n",
    "  on a machine, they need to use different, unused port numbers. \n",
    "* `useAuthToken`: if this is set to false, no auth token is generated and used, and the connection can be \n",
    "  established by any process connecting to that port number. \n",
    "\n",
    "A GATE Slave started via the PythonSlaveLr keeps running until the resource is deleted or GATE is ended.\n",
    "\n",
    "\n",
    "## Using the GATE Slave from Python\n",
    "\n",
    "Once the PythonSlaveLr resource has been created it is ready to get used by a Python program:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gatenlp.gateslave import GateSlave"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To connect to an already running slave process, the parameter `start=False` must be specified. \n",
    "In addition the auth token must be provided and the port and host, if they differ from the default."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "gs = GateSlave(start=False, auth_token=\"841e634a-d1f0-4768-b763-a7738ddee003\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The gate slave instance can now be used to run arbitrary Java methods on the Java side. \n",
    "The gate slave instance provides a number of useful methods directly (see [PythonDoc for gateslave](https://gatenlp.github.io/python-gatenlp/pythondoc/gatenlp/gateslave.html) )\n",
    "* `gs.load_gdoc(filepath, mimetype=None`: load a GATE document on the Java side and return it to Python\n",
    "* `gs.save_gdoc(gatedocument, filepath, mimetype=None)`: save a GATE document on the Java side\n",
    "* `gs.gdoc2pdoc(gatedocument)`: convert the Java GATE document as a Python GateNLP document and return it\n",
    "* `gs.pdoc2gdoc(doc)`: convert the Python GateNLP document to a Java GATE document and return it\n",
    "* `gs.del_gdoc(gatedocument)`: remove a Java GATE document on the Java side (this necessary to release memory)\n",
    "* `gs.load_pdoc(filepath, mimetype=None)`: load a document on the Java side using the file format specified via the mime type and return it as a Python GateNLP document\n",
    "* `gs.log_actions(trueorfalse)`: switch logging of actions on the slave side off/on\n",
    "\n",
    "In addition, there is a larger number of utility methods which are available through `gs.slave` (see \n",
    "[PythonSlave Source code](https://github.com/GateNLP/gateplugin-Python/blob/master/src/main/java/gate/plugin/python/PythonSlave.java), here are a few examples:\n",
    "\n",
    "* `loadMavenPlugin(group, artifact, version)`: make the plugin identified by the given Maven coordinates available\n",
    "* `loadPipelineFromFile(filepath)`: load the pipeline/controller from the given file path and return it\n",
    "* `loadDocumentFromFile(filepath)`: load a GATE document from the file and return it\n",
    "* `loadDocumentFromFile(filepath, mimetype)`: load a GATE document from the file using the format corresponding to the given mime type and return it\n",
    "* `saveDocumentToFile(gatedocument, filepath, mimetype)`: save the document to the file, using the format corresponding to the mime type\n",
    "* `createDocument(content)`: create a new document from the given String content and return it\n",
    "* `run4Document(pipeline, document)`: run the given pipeline on the given document\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "JavaObject id=o0"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Create a new Java document from a string\n",
    "gdoc1 = gs.slave.createDocument(\"This is a 💩 document. It mentions Barack Obama and George Bush and New York.\")\n",
    "gdoc1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "GATE Document_00014\n",
      "{'gate.SourceURL': 'created from String'}\n"
     ]
    }
   ],
   "source": [
    "# you can call the API methods for the document directly from Python\n",
    "print(gdoc1.getName())\n",
    "print(gdoc1.getFeatures())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'This is a 💩 document. It mentions Barack Obama and George Bush and New York.'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# so far the document only \"lives\" in the Java process. In order to copy it to Python, it has to be converted\n",
    "# to a Python GateNLP document:\n",
    "pdoc1 = gs.gdoc2pdoc(gdoc1)\n",
    "pdoc1.text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Let's load ANNIE on the Java side and run it on that document:\n",
    "# First we have to load the ANNIE plugin:\n",
    "gs.slave.loadMavenPlugin(\"uk.ac.gate.plugins\", \"annie\", \"8.6\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ANNIE'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# now load the prepared ANNIE pipeline from the plugin\n",
    "pipeline = gs.slave.loadPipelineFromPlugin(\"uk.ac.gate.plugins\",\"annie\", \"/resources/ANNIE_with_defaults.gapp\")\n",
    "pipeline.getName()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>#IPRLVTERZZ-wrapper { color: black !important; }</style>\n",
       "<div id=\"IPRLVTERZZ-wrapper\">\n",
       "\n",
       "<div>\n",
       "<style>\n",
       "#IPRLVTERZZ-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",
       ".IPRLVTERZZ-row {\n",
       "    width: 100%;\n",
       "    display: flex;\n",
       "    flex-direction: row;\n",
       "    flex-wrap: nowrap;\n",
       "}\n",
       "\n",
       ".IPRLVTERZZ-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",
       ".IPRLVTERZZ-hdr {\n",
       "    font-size: 1.2rem;\n",
       "    font-weight: bold;\n",
       "}\n",
       "\n",
       ".IPRLVTERZZ-label {\n",
       "    margin-bottom: -15px;\n",
       "    display: block;\n",
       "}\n",
       "\n",
       ".IPRLVTERZZ-input {\n",
       "    vertical-align: middle;\n",
       "    position: relative;\n",
       "    *overflow: hidden;\n",
       "}\n",
       "\n",
       "#IPRLVTERZZ-popup {\n",
       "    display: none;\n",
       "    color: black;\n",
       "    position: absolute;\n",
       "    margin-top: 10%;\n",
       "    margin-left: 10%;\n",
       "    background: #aaaaaa;\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",
       ".IPRLVTERZZ-selection {\n",
       "    margin-bottom: 5px;\n",
       "}\n",
       "\n",
       ".IPRLVTERZZ-featuretable {\n",
       "    margin-top: 10px;\n",
       "}\n",
       "\n",
       ".IPRLVTERZZ-fname {\n",
       "    text-align: left !important;\n",
       "    font-weight: bold;\n",
       "    margin-right: 10px;\n",
       "}\n",
       ".IPRLVTERZZ-fvalue {\n",
       "    text-align: left !important;\n",
       "}\n",
       "</style>\n",
       "  <div id=\"IPRLVTERZZ-content\">\n",
       "        <div id=\"IPRLVTERZZ-popup\" style=\"display: none;\">\n",
       "        </div>\n",
       "        <div class=\"IPRLVTERZZ-row\" id=\"IPRLVTERZZ-row1\" style=\"height:67vh; min-height:100px;\">\n",
       "            <div id=\"IPRLVTERZZ-text-wrapper\" class=\"IPRLVTERZZ-col\" style=\"width:70%;\">\n",
       "                <div class=\"IPRLVTERZZ-hdr\" id=\"IPRLVTERZZ-dochdr\"></div>\n",
       "                <div id=\"IPRLVTERZZ-text\">\n",
       "                </div>\n",
       "            </div>\n",
       "            <div id=\"IPRLVTERZZ-chooser\" class=\"IPRLVTERZZ-col\" style=\"width:30%; border-left-width: 0px;\"></div>\n",
       "        </div>\n",
       "        <div class=\"IPRLVTERZZ-row\" id=\"IPRLVTERZZ-row2\" style=\"height:30vh; min-height: 100px;\">\n",
       "            <div id=\"IPRLVTERZZ-details\" class=\"IPRLVTERZZ-col\" style=\"width:100%; border-top-width: 0px;\">\n",
       "            </div>\n",
       "        </div>\n",
       "    </div>\n",
       "    <script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js\"></script><script src=\"https://unpkg.com/gatenlp-ann-viewer@1.0.10/gatenlp-ann-viewer.js\"></script>\n",
       "    <script type=\"application/json\" id=\"IPRLVTERZZ-data\">\n",
       "    {\"annotation_sets\": {\"\": {\"name\": \"detached-from:\", \"annotations\": [{\"type\": \"Token\", \"start\": 0, \"end\": 4, \"id\": 0, \"features\": {\"orth\": \"upperInitial\", \"string\": \"This\", \"kind\": \"word\", \"length\": \"4\", \"category\": \"DT\"}}, {\"type\": \"SpaceToken\", \"start\": 4, \"end\": 5, \"id\": 1, \"features\": {\"string\": \" \", \"kind\": \"space\", \"length\": \"1\"}}, {\"type\": \"Token\", \"start\": 5, \"end\": 7, \"id\": 2, \"features\": {\"orth\": \"lowercase\", \"string\": \"is\", \"kind\": \"word\", \"length\": \"2\", \"category\": \"VBZ\"}}, {\"type\": \"SpaceToken\", \"start\": 7, \"end\": 8, \"id\": 3, \"features\": {\"string\": \" \", \"kind\": \"space\", \"length\": \"1\"}}, {\"type\": \"Token\", \"start\": 8, \"end\": 9, \"id\": 4, \"features\": {\"orth\": \"lowercase\", \"string\": \"a\", \"kind\": \"word\", \"length\": \"1\", \"category\": \"DT\"}}, {\"type\": \"SpaceToken\", \"start\": 9, \"end\": 10, \"id\": 5, \"features\": {\"string\": \" \", \"kind\": \"space\", \"length\": \"1\"}}, {\"type\": \"Token\", \"start\": 10, \"end\": 12, \"id\": 6, \"features\": {\"string\": \"\\ud83d\\udca9\", \"kind\": \"symbol\", \"length\": \"2\", \"category\": \"NN\"}}, {\"type\": \"SpaceToken\", \"start\": 12, \"end\": 13, \"id\": 7, \"features\": {\"string\": \" \", \"kind\": \"space\", \"length\": \"1\"}}, {\"type\": \"Token\", \"start\": 13, \"end\": 21, \"id\": 8, \"features\": {\"orth\": \"lowercase\", \"string\": \"document\", \"kind\": \"word\", \"length\": \"8\", \"category\": \"NN\"}}, {\"type\": \"Token\", \"start\": 21, \"end\": 22, \"id\": 9, \"features\": {\"string\": \".\", \"kind\": \"punctuation\", \"length\": \"1\", \"category\": \".\"}}, {\"type\": \"SpaceToken\", \"start\": 22, \"end\": 23, \"id\": 10, \"features\": {\"string\": \" \", \"kind\": \"space\", \"length\": \"1\"}}, {\"type\": \"Token\", \"start\": 23, \"end\": 25, \"id\": 11, \"features\": {\"orth\": \"upperInitial\", \"string\": \"It\", \"kind\": \"word\", \"length\": \"2\", \"category\": \"PRP\"}}, {\"type\": \"SpaceToken\", \"start\": 25, \"end\": 26, \"id\": 12, \"features\": {\"string\": \" \", \"kind\": \"space\", \"length\": \"1\"}}, {\"type\": \"Token\", \"start\": 26, \"end\": 34, \"id\": 13, \"features\": {\"orth\": \"lowercase\", \"string\": \"mentions\", \"kind\": \"word\", \"length\": \"8\", \"category\": \"VBZ\"}}, {\"type\": \"SpaceToken\", \"start\": 34, \"end\": 35, \"id\": 14, \"features\": {\"string\": \" \", \"kind\": \"space\", \"length\": \"1\"}}, {\"type\": \"Token\", \"start\": 35, \"end\": 41, \"id\": 15, \"features\": {\"orth\": \"upperInitial\", \"string\": \"Barack\", \"kind\": \"word\", \"length\": \"6\", \"category\": \"NNP\"}}, {\"type\": \"SpaceToken\", \"start\": 41, \"end\": 42, \"id\": 16, \"features\": {\"string\": \" \", \"kind\": \"space\", \"length\": \"1\"}}, {\"type\": \"Token\", \"start\": 42, \"end\": 47, \"id\": 17, \"features\": {\"orth\": \"upperInitial\", \"string\": \"Obama\", \"kind\": \"word\", \"length\": \"5\", \"category\": \"NNP\"}}, {\"type\": \"SpaceToken\", \"start\": 47, \"end\": 48, \"id\": 18, \"features\": {\"string\": \" \", \"kind\": \"space\", \"length\": \"1\"}}, {\"type\": \"Token\", \"start\": 48, \"end\": 51, \"id\": 19, \"features\": {\"orth\": \"lowercase\", \"string\": \"and\", \"kind\": \"word\", \"length\": \"3\", \"category\": \"CC\"}}, {\"type\": \"SpaceToken\", \"start\": 51, \"end\": 52, \"id\": 20, \"features\": {\"string\": \" \", \"kind\": \"space\", \"length\": \"1\"}}, {\"type\": \"Token\", \"start\": 52, \"end\": 58, \"id\": 21, \"features\": {\"orth\": \"upperInitial\", \"string\": \"George\", \"kind\": \"word\", \"length\": \"6\", \"category\": \"NNP\"}}, {\"type\": \"SpaceToken\", \"start\": 58, \"end\": 59, \"id\": 22, \"features\": {\"string\": \" \", \"kind\": \"space\", \"length\": \"1\"}}, {\"type\": \"Token\", \"start\": 59, \"end\": 63, \"id\": 23, \"features\": {\"orth\": \"upperInitial\", \"string\": \"Bush\", \"kind\": \"word\", \"length\": \"4\", \"category\": \"NNP\"}}, {\"type\": \"SpaceToken\", \"start\": 63, \"end\": 64, \"id\": 24, \"features\": {\"string\": \" \", \"kind\": \"space\", \"length\": \"1\"}}, {\"type\": \"Token\", \"start\": 64, \"end\": 67, \"id\": 25, \"features\": {\"orth\": \"lowercase\", \"string\": \"and\", \"kind\": \"word\", \"length\": \"3\", \"category\": \"CC\"}}, {\"type\": \"SpaceToken\", \"start\": 67, \"end\": 68, \"id\": 26, \"features\": {\"string\": \" \", \"kind\": \"space\", \"length\": \"1\"}}, {\"type\": \"Token\", \"start\": 68, \"end\": 71, \"id\": 27, \"features\": {\"orth\": \"upperInitial\", \"string\": \"New\", \"kind\": \"word\", \"length\": \"3\", \"category\": \"NNP\"}}, {\"type\": \"SpaceToken\", \"start\": 71, \"end\": 72, \"id\": 28, \"features\": {\"string\": \" \", \"kind\": \"space\", \"length\": \"1\"}}, {\"type\": \"Token\", \"start\": 72, \"end\": 76, \"id\": 29, \"features\": {\"orth\": \"upperInitial\", \"string\": \"York\", \"kind\": \"word\", \"length\": \"4\", \"category\": \"NNP\"}}, {\"type\": \"Token\", \"start\": 76, \"end\": 77, \"id\": 30, \"features\": {\"string\": \".\", \"kind\": \"punctuation\", \"length\": \"1\", \"category\": \".\"}}, {\"type\": \"Lookup\", \"start\": 0, \"end\": 4, \"id\": 31, \"features\": {\"majorType\": \"time_modifier\"}}, {\"type\": \"Lookup\", \"start\": 5, \"end\": 7, \"id\": 32, \"features\": {\"majorType\": \"country_code\"}}, {\"type\": \"Lookup\", \"start\": 23, \"end\": 25, \"id\": 33, \"features\": {\"majorType\": \"stop\"}}, {\"type\": \"Lookup\", \"start\": 35, \"end\": 47, \"id\": 34, \"features\": {\"majorType\": \"person_full\", \"gender\": \"male\"}}, {\"type\": \"Lookup\", \"start\": 42, \"end\": 47, \"id\": 35, \"features\": {\"majorType\": \"person_full\", \"gender\": \"male\"}}, {\"type\": \"Lookup\", \"start\": 52, \"end\": 63, \"id\": 36, \"features\": {\"majorType\": \"person_full\", \"gender\": \"male\"}}, {\"type\": \"Lookup\", \"start\": 68, \"end\": 76, \"id\": 37, \"features\": {\"majorType\": \"location\", \"minorType\": \"city\"}}, {\"type\": \"Lookup\", \"start\": 72, \"end\": 76, \"id\": 38, \"features\": {\"majorType\": \"location\", \"minorType\": \"city\"}}, {\"type\": \"Split\", \"start\": 21, \"end\": 22, \"id\": 39, \"features\": {\"kind\": \"internal\"}}, {\"type\": \"Split\", \"start\": 76, \"end\": 77, \"id\": 40, \"features\": {\"kind\": \"internal\"}}, {\"type\": \"Sentence\", \"start\": 0, \"end\": 22, \"id\": 41, \"features\": {}}, {\"type\": \"Sentence\", \"start\": 23, \"end\": 77, \"id\": 42, \"features\": {}}, {\"type\": \"Person\", \"start\": 35, \"end\": 47, \"id\": 55, \"features\": {\"firstName\": \"Barack\", \"ruleFinal\": \"PersonFinal\", \"gender\": \"male\", \"surname\": \"Obama\", \"kind\": \"fullName\", \"rule\": \"GazPerson\"}}, {\"type\": \"Person\", \"start\": 52, \"end\": 63, \"id\": 56, \"features\": {\"firstName\": \"George\", \"ruleFinal\": \"PersonFinal\", \"gender\": \"male\", \"surname\": \"Bush\", \"kind\": \"fullName\", \"rule\": \"GazPerson\"}}, {\"type\": \"Location\", \"start\": 68, \"end\": 76, \"id\": 57, \"features\": {\"ruleFinal\": \"LocFinal\", \"rule\": \"Location1\", \"locType\": \"city\"}}], \"next_annid\": 58}}, \"text\": \"This is a \\ud83d\\udca9 document. It mentions Barack Obama and George Bush and New York.\", \"features\": {\"gate.SourceURL\": \"created from String\"}, \"offset_type\": \"j\", \"name\": \"\"}\n",
       "    </script>\n",
       "    <script type=\"text/javascript\">\n",
       "        gatenlp_run(\"IPRLVTERZZ-\");\n",
       "    </script>\n",
       "  </div>\n",
       "\n",
       "</div></div>"
      ],
      "text/plain": [
       "Document(This is a 💩 document. It mentions Barack Obama and George Bush and New York.,features=Features({'gate.SourceURL': 'created from String'}),anns={'': AnnotationSet([Annotation(0,4,Token,features=Features({'orth': 'upperInitial', 'string': 'This', 'kind': 'word', 'length': '4', 'category': 'DT'}),id=0), Annotation(0,4,Lookup,features=Features({'majorType': 'time_modifier'}),id=31), Annotation(0,21,Sentence,features=Features({}),id=41), Annotation(4,5,SpaceToken,features=Features({'string': ' ', 'kind': 'space', 'length': '1'}),id=1), Annotation(5,7,Token,features=Features({'orth': 'lowercase', 'string': 'is', 'kind': 'word', 'length': '2', 'category': 'VBZ'}),id=2), Annotation(5,7,Lookup,features=Features({'majorType': 'country_code'}),id=32), Annotation(7,8,SpaceToken,features=Features({'string': ' ', 'kind': 'space', 'length': '1'}),id=3), Annotation(8,9,Token,features=Features({'orth': 'lowercase', 'string': 'a', 'kind': 'word', 'length': '1', 'category': 'DT'}),id=4), Annotation(9,10,SpaceToken,features=Features({'string': ' ', 'kind': 'space', 'length': '1'}),id=5), Annotation(10,11,Token,features=Features({'string': '💩', 'kind': 'symbol', 'length': '2', 'category': 'NN'}),id=6), Annotation(11,12,SpaceToken,features=Features({'string': ' ', 'kind': 'space', 'length': '1'}),id=7), Annotation(12,20,Token,features=Features({'orth': 'lowercase', 'string': 'document', 'kind': 'word', 'length': '8', 'category': 'NN'}),id=8), Annotation(20,21,Token,features=Features({'string': '.', 'kind': 'punctuation', 'length': '1', 'category': '.'}),id=9), Annotation(20,21,Split,features=Features({'kind': 'internal'}),id=39), Annotation(21,22,SpaceToken,features=Features({'string': ' ', 'kind': 'space', 'length': '1'}),id=10), Annotation(22,24,Token,features=Features({'orth': 'upperInitial', 'string': 'It', 'kind': 'word', 'length': '2', 'category': 'PRP'}),id=11), Annotation(22,24,Lookup,features=Features({'majorType': 'stop'}),id=33), Annotation(22,76,Sentence,features=Features({}),id=42), Annotation(24,25,SpaceToken,features=Features({'string': ' ', 'kind': 'space', 'length': '1'}),id=12), Annotation(25,33,Token,features=Features({'orth': 'lowercase', 'string': 'mentions', 'kind': 'word', 'length': '8', 'category': 'VBZ'}),id=13), Annotation(33,34,SpaceToken,features=Features({'string': ' ', 'kind': 'space', 'length': '1'}),id=14), Annotation(34,40,Token,features=Features({'orth': 'upperInitial', 'string': 'Barack', 'kind': 'word', 'length': '6', 'category': 'NNP'}),id=15), Annotation(34,46,Lookup,features=Features({'majorType': 'person_full', 'gender': 'male'}),id=34), Annotation(34,46,Person,features=Features({'firstName': 'Barack', 'ruleFinal': 'PersonFinal', 'gender': 'male', 'surname': 'Obama', 'kind': 'fullName', 'rule': 'GazPerson'}),id=55), Annotation(40,41,SpaceToken,features=Features({'string': ' ', 'kind': 'space', 'length': '1'}),id=16), Annotation(41,46,Token,features=Features({'orth': 'upperInitial', 'string': 'Obama', 'kind': 'word', 'length': '5', 'category': 'NNP'}),id=17), Annotation(41,46,Lookup,features=Features({'majorType': 'person_full', 'gender': 'male'}),id=35), Annotation(46,47,SpaceToken,features=Features({'string': ' ', 'kind': 'space', 'length': '1'}),id=18), Annotation(47,50,Token,features=Features({'orth': 'lowercase', 'string': 'and', 'kind': 'word', 'length': '3', 'category': 'CC'}),id=19), Annotation(50,51,SpaceToken,features=Features({'string': ' ', 'kind': 'space', 'length': '1'}),id=20), Annotation(51,57,Token,features=Features({'orth': 'upperInitial', 'string': 'George', 'kind': 'word', 'length': '6', 'category': 'NNP'}),id=21), Annotation(51,62,Lookup,features=Features({'majorType': 'person_full', 'gender': 'male'}),id=36), Annotation(51,62,Person,features=Features({'firstName': 'George', 'ruleFinal': 'PersonFinal', 'gender': 'male', 'surname': 'Bush', 'kind': 'fullName', 'rule': 'GazPerson'}),id=56), Annotation(57,58,SpaceToken,features=Features({'string': ' ', 'kind': 'space', 'length': '1'}),id=22), Annotation(58,62,Token,features=Features({'orth': 'upperInitial', 'string': 'Bush', 'kind': 'word', 'length': '4', 'category': 'NNP'}),id=23), Annotation(62,63,SpaceToken,features=Features({'string': ' ', 'kind': 'space', 'length': '1'}),id=24), Annotation(63,66,Token,features=Features({'orth': 'lowercase', 'string': 'and', 'kind': 'word', 'length': '3', 'category': 'CC'}),id=25), Annotation(66,67,SpaceToken,features=Features({'string': ' ', 'kind': 'space', 'length': '1'}),id=26), Annotation(67,70,Token,features=Features({'orth': 'upperInitial', 'string': 'New', 'kind': 'word', 'length': '3', 'category': 'NNP'}),id=27), Annotation(67,75,Lookup,features=Features({'majorType': 'location', 'minorType': 'city'}),id=37), Annotation(67,75,Location,features=Features({'ruleFinal': 'LocFinal', 'rule': 'Location1', 'locType': 'city'}),id=57), Annotation(70,71,SpaceToken,features=Features({'string': ' ', 'kind': 'space', 'length': '1'}),id=28), Annotation(71,75,Token,features=Features({'orth': 'upperInitial', 'string': 'York', 'kind': 'word', 'length': '4', 'category': 'NNP'}),id=29), Annotation(71,75,Lookup,features=Features({'majorType': 'location', 'minorType': 'city'}),id=38), Annotation(75,76,Token,features=Features({'string': '.', 'kind': 'punctuation', 'length': '1', 'category': '.'}),id=30), Annotation(75,76,Split,features=Features({'kind': 'internal'}),id=40)])})"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# run the pipeline on the document and convert it to a GateNLP Python document and display it\n",
    "gs.slave.run4Document(pipeline, gdoc1)\n",
    "pdoc1 = gs.gdoc2pdoc(gdoc1)\n",
    "pdoc1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Manually starting the GATE Slave from Python\n",
    "\n",
    "After installation of Python `gatenlp`, the command `gatenlp-gate-slave` is available. \n",
    "\n",
    "You can run `gatenlp-gate-slave --help` to get help information:\n",
    "\n",
    "```\n",
    "usage: gatenlp-gate-slave [-h] [--port PORT] [--host HOST] [--auth AUTH]\n",
    "                          [--noauth] [--gatehome GATEHOME]\n",
    "                          [--platform PLATFORM] [--log_actions] [--keep]\n",
    "\n",
    "Start Java GATE Slave\n",
    "\n",
    "optional arguments:\n",
    "  -h, --help           show this help message and exit\n",
    "  --port PORT          Port (25333)\n",
    "  --host HOST          Host to bind to (127.0.0.1)\n",
    "  --auth AUTH          Auth token to use (generate random)\n",
    "  --noauth             Do not use auth token\n",
    "  --gatehome GATEHOME  Location of GATE (environment variable GATE_HOME)\n",
    "  --platform PLATFORM  OS/Platform: windows or linux (autodetect)\n",
    "  --log_actions        If slave actions should be logged\n",
    "  --keep               Prevent shutting down the slave\n",
    "```\n",
    "\n",
    "For example to start a gate slave as with the PythonSlaveLr above, but this time re-using the exact same\n",
    "auth token and switching on logging of the actions:\n",
    "    \n",
    "```\n",
    "gatenlp-gate-slave --auth 841e634a-d1f0-4768-b763-a7738ddee003 --log_actions\n",
    "```\n",
    "\n",
    "Again the Python program can connect to the server as before:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<gatenlp.gateslave.GateSlave at 0x7f987fabcf50>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gs = GateSlave(start=False, auth_token=\"841e634a-d1f0-4768-b763-a7738ddee003\")\n",
    "gs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The GATE slave started that way keeps running until it is interrupted from the keyboard using \"Ctrl-C\" or \n",
    "until the GATE slave sends the \"close\" request:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "gs.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "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
}
