{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-10T12:44:03.364238Z",
     "start_time": "2019-10-10T12:44:01.982667Z"
    }
   },
   "outputs": [],
   "source": [
    "import stanfordnlp\n",
    "from spacy_stanfordnlp import StanfordNLPLanguage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-10T12:46:57.049991Z",
     "start_time": "2019-10-10T12:46:54.219910Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Use device: cpu\n",
      "---\n",
      "Loading: tokenize\n",
      "With settings: \n",
      "{'model_path': '/pi/ai/corenlp/en_ewt_models/en_ewt_tokenizer.pt', 'lang': 'en', 'shorthand': 'en_ewt', 'mode': 'predict'}\n",
      "---\n",
      "Loading: pos\n",
      "With settings: \n",
      "{'model_path': '/pi/ai/corenlp/en_ewt_models/en_ewt_tagger.pt', 'pretrain_path': '/pi/ai/corenlp/en_ewt_models/en_ewt.pretrain.pt', 'lang': 'en', 'shorthand': 'en_ewt', 'mode': 'predict'}\n",
      "---\n",
      "Loading: lemma\n",
      "With settings: \n",
      "{'model_path': '/pi/ai/corenlp/en_ewt_models/en_ewt_lemmatizer.pt', 'lang': 'en', 'shorthand': 'en_ewt', 'mode': 'predict'}\n",
      "Building an attentional Seq2Seq model...\n",
      "Using a Bi-LSTM encoder\n",
      "Using soft attention for LSTM.\n",
      "Finetune all embeddings.\n",
      "[Running seq2seq lemmatizer with edit classifier]\n",
      "---\n",
      "Loading: depparse\n",
      "With settings: \n",
      "{'model_path': '/pi/ai/corenlp/en_ewt_models/en_ewt_parser.pt', 'pretrain_path': '/pi/ai/corenlp/en_ewt_models/en_ewt.pretrain.pt', 'lang': 'en', 'shorthand': 'en_ewt', 'mode': 'predict'}\n",
      "Done loading processors!\n",
      "---\n",
      "Barack Barack PROPN nsubj:pass\n",
      "Obama Obama PROPN flat\n",
      "was be AUX aux:pass\n",
      "born bear VERB root\n",
      "in in ADP case\n",
      "Hawaii Hawaii PROPN obl\n",
      ". . PUNCT punct\n",
      "He he PRON nsubj:pass\n",
      "was be AUX aux:pass\n",
      "elected elect VERB root\n",
      "president president PROPN xcomp\n",
      "in in ADP case\n",
      "2008 2008 NUM obl\n",
      ". . PUNCT punct\n"
     ]
    }
   ],
   "source": [
    "model_dir='/pi/ai/corenlp'\n",
    "snlp = stanfordnlp.Pipeline(models_dir=model_dir, treebank='en_ewt', lang=\"en\")\n",
    "nlp = StanfordNLPLanguage(snlp)\n",
    "\n",
    "doc = nlp(\"Barack Obama was born in Hawaii. He was elected president in 2008.\")\n",
    "for token in doc:\n",
    "    print(token.text, token.lemma_, token.pos_, token.dep_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-10T12:47:33.724224Z",
     "start_time": "2019-10-10T12:47:33.526776Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Barack Barack PROPN nsubj:pass\n",
      "Obama Obama PROPN flat\n",
      "was be AUX aux:pass\n",
      "born bear VERB root\n",
      "in in ADP case\n",
      "Hawaii Hawaii PROPN obl\n",
      ". . PUNCT punct\n",
      "He he PRON nsubj:pass\n",
      "was be AUX aux:pass\n",
      "elected elect VERB root\n",
      "president president PROPN xcomp\n",
      "in in ADP case\n",
      "2008 2008 NUM obl\n",
      ". . PUNCT punct\n"
     ]
    }
   ],
   "source": [
    "doc = nlp(\"Barack Obama was born in Hawaii. He was elected president in 2008.\")\n",
    "for token in doc:\n",
    "    print(token.text, token.lemma_, token.pos_, token.dep_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-10T22:54:51.696020Z",
     "start_time": "2019-10-10T22:54:47.314651Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Use device: cpu\n",
      "---\n",
      "Loading: tokenize\n",
      "With settings: \n",
      "{'model_path': '/pi/ai/corenlp/fa_seraji_models/fa_seraji_tokenizer.pt', 'lang': 'fa', 'shorthand': 'fa_seraji', 'mode': 'predict'}\n",
      "---\n",
      "Loading: mwt\n",
      "With settings: \n",
      "{'model_path': '/pi/ai/corenlp/fa_seraji_models/fa_seraji_mwt_expander.pt', 'lang': 'fa', 'shorthand': 'fa_seraji', 'mode': 'predict'}\n",
      "Building an attentional Seq2Seq model...\n",
      "Using a Bi-LSTM encoder\n",
      "Using soft attention for LSTM.\n",
      "Finetune all embeddings.\n",
      "---\n",
      "Loading: pos\n",
      "With settings: \n",
      "{'model_path': '/pi/ai/corenlp/fa_seraji_models/fa_seraji_tagger.pt', 'pretrain_path': '/pi/ai/corenlp/fa_seraji_models/fa_seraji.pretrain.pt', 'lang': 'fa', 'shorthand': 'fa_seraji', 'mode': 'predict'}\n",
      "---\n",
      "Loading: lemma\n",
      "With settings: \n",
      "{'model_path': '/pi/ai/corenlp/fa_seraji_models/fa_seraji_lemmatizer.pt', 'lang': 'fa', 'shorthand': 'fa_seraji', 'mode': 'predict'}\n",
      "Building an attentional Seq2Seq model...\n",
      "Using a Bi-LSTM encoder\n",
      "Using soft attention for LSTM.\n",
      "Finetune all embeddings.\n",
      "[Running seq2seq lemmatizer with edit classifier]\n",
      "---\n",
      "Loading: depparse\n",
      "With settings: \n",
      "{'model_path': '/pi/ai/corenlp/fa_seraji_models/fa_seraji_parser.pt', 'pretrain_path': '/pi/ai/corenlp/fa_seraji_models/fa_seraji.pretrain.pt', 'lang': 'fa', 'shorthand': 'fa_seraji', 'mode': 'predict'}\n",
      "Done loading processors!\n",
      "---\n"
     ]
    }
   ],
   "source": [
    "snlp_fa = stanfordnlp.Pipeline(models_dir=model_dir, \n",
    "                               treebank='fa_seraji', \n",
    "                               lang=\"fa\")\n",
    "nlp_fa = StanfordNLPLanguage(snlp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-10T22:55:23.745007Z",
     "start_time": "2019-10-10T22:55:23.190702Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "‫اینها ‫اینها INTJ discourse\n",
      "دانش دانش NOUN compound\n",
      "آموزان آموزان NOUN compound\n",
      "کلاس کلاس NOUN compound\n",
      "هستند هستند NOUN root\n",
      ".‬ .‬ PUNCT punct\n"
     ]
    }
   ],
   "source": [
    "doc = nlp_fa(\"‫اینها دانش آموزان کلاس هستند.‬\")\n",
    "for token in doc:\n",
    "    print(token.text, token.lemma_, token.pos_, token.dep_)"
   ]
  }
 ],
 "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.3"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
