{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-08-30T14:58:50.904280Z",
     "start_time": "2019-08-30T14:58:50.721556Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "text: جارك \tlemma: جَارَك\tupos: X\txpos: U---------\n",
      "\t root _\n",
      "text: رجل \tlemma: رَجُل\tupos: NOUN\txpos: N------S2I\n",
      "\t nmod Case=Gen|Definite=Ind|Number=Sing\n",
      "text: طيب \tlemma: طيب\tupos: X\txpos: U---------\n",
      "\t nmod _\n",
      "text: . \tlemma: .\tupos: PUNCT\txpos: G---------\n",
      "\t punct _\n"
     ]
    }
   ],
   "source": [
    "from sagas.nlu.corenlp_helper import CoreNlp, get_nlp\n",
    "sents='جارك رجل طيب.'\n",
    "nlp=get_nlp('ar')\n",
    "\n",
    "def list_words(sents):\n",
    "    doc = nlp(sents)\n",
    "    sentence=doc.sentences[0]\n",
    "    for word in sentence.words:\n",
    "        print(f'text: {word.text+\" \"}\\tlemma: {word.lemma}\\tupos: {word.upos}\\txpos: {word.xpos}')\n",
    "        print('\\t', word.dependency_relation, word.feats)\n",
    "list_words(sents)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-08-30T15:11:17.761979Z",
     "start_time": "2019-08-30T15:11:17.418416Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>text</th>\n",
       "      <th>lemma</th>\n",
       "      <th>upos</th>\n",
       "      <th>xpos</th>\n",
       "      <th>dep</th>\n",
       "      <th>head</th>\n",
       "      <th>feats</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>عمري</td>\n",
       "      <td>عمري</td>\n",
       "      <td>X</td>\n",
       "      <td>U---------</td>\n",
       "      <td>root</td>\n",
       "      <td>0</td>\n",
       "      <td>_</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>تسعة</td>\n",
       "      <td>تِسعَة</td>\n",
       "      <td>NUM</td>\n",
       "      <td>QV----M-4R</td>\n",
       "      <td>dep</td>\n",
       "      <td>1</td>\n",
       "      <td>Case=Acc|Definite=Cons|Gender=Masc|NumForm=Wor...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>عشر</td>\n",
       "      <td>عَشَرَة</td>\n",
       "      <td>NUM</td>\n",
       "      <td>QY----M---</td>\n",
       "      <td>nummod</td>\n",
       "      <td>2</td>\n",
       "      <td>Gender=Masc|NumForm=Word|NumValue=2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>عاماً</td>\n",
       "      <td>عَام</td>\n",
       "      <td>NOUN</td>\n",
       "      <td>N------S4I</td>\n",
       "      <td>nmod</td>\n",
       "      <td>3</td>\n",
       "      <td>Case=Acc|Definite=Ind|Number=Sing</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>.</td>\n",
       "      <td>.</td>\n",
       "      <td>PUNCT</td>\n",
       "      <td>G---------</td>\n",
       "      <td>punct</td>\n",
       "      <td>1</td>\n",
       "      <td>_</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    text    lemma   upos        xpos     dep  head  \\\n",
       "0   عمري     عمري      X  U---------    root     0   \n",
       "1   تسعة   تِسعَة    NUM  QV----M-4R     dep     1   \n",
       "2    عشر  عَشَرَة    NUM  QY----M---  nummod     2   \n",
       "3  عاماً     عَام   NOUN  N------S4I    nmod     3   \n",
       "4      .        .  PUNCT  G---------   punct     1   \n",
       "\n",
       "                                               feats  \n",
       "0                                                  _  \n",
       "1  Case=Acc|Definite=Cons|Gender=Masc|NumForm=Wor...  \n",
       "2                Gender=Masc|NumForm=Word|NumValue=2  \n",
       "3                  Case=Acc|Definite=Ind|Number=Sing  \n",
       "4                                                  _  "
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import sagas\n",
    "def words_table(sents, lang):\n",
    "    nlp=get_nlp(lang)\n",
    "    doc = nlp(sents)\n",
    "    sentence=doc.sentences[0]\n",
    "    rows=[[word.text, word.lemma, word.upos, word.xpos, \n",
    "           word.dependency_relation, word.governor,\n",
    "           word.feats] for word in sentence.words]\n",
    "    return sagas.to_df(rows, ['text','lemma', 'upos', 'xpos', 'dep', 'head', 'feats'])\n",
    "\n",
    "words_table('عمري تسعة عشر عاماً.', 'ar')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-08-30T15:12:54.553171Z",
     "start_time": "2019-08-30T15:12:54.174410Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>text</th>\n",
       "      <th>lemma</th>\n",
       "      <th>upos</th>\n",
       "      <th>xpos</th>\n",
       "      <th>dep</th>\n",
       "      <th>head</th>\n",
       "      <th>feats</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>آخرین</td>\n",
       "      <td>آخرین</td>\n",
       "      <td>ADJ</td>\n",
       "      <td>ADJ_SUP</td>\n",
       "      <td>amod</td>\n",
       "      <td>2</td>\n",
       "      <td>Degree=Sup</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>کسی</td>\n",
       "      <td>کس</td>\n",
       "      <td>NOUN</td>\n",
       "      <td>N_SING</td>\n",
       "      <td>nsubj</td>\n",
       "      <td>9</td>\n",
       "      <td>Number=Sing</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>که</td>\n",
       "      <td>که</td>\n",
       "      <td>SCONJ</td>\n",
       "      <td>CON</td>\n",
       "      <td>mark</td>\n",
       "      <td>6</td>\n",
       "      <td>_</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>به</td>\n",
       "      <td>به</td>\n",
       "      <td>ADP</td>\n",
       "      <td>P</td>\n",
       "      <td>case</td>\n",
       "      <td>5</td>\n",
       "      <td>_</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>کامپیوتر</td>\n",
       "      <td>کامپیو</td>\n",
       "      <td>NOUN</td>\n",
       "      <td>N_SING</td>\n",
       "      <td>nmod</td>\n",
       "      <td>6</td>\n",
       "      <td>Number=Sing</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>وصل</td>\n",
       "      <td>وصل</td>\n",
       "      <td>ADJ</td>\n",
       "      <td>ADJ</td>\n",
       "      <td>acl:relcl</td>\n",
       "      <td>2</td>\n",
       "      <td>Degree=Pos</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>شد</td>\n",
       "      <td>کرد#کن</td>\n",
       "      <td>AUX</td>\n",
       "      <td>V_PA</td>\n",
       "      <td>cop</td>\n",
       "      <td>6</td>\n",
       "      <td>Number=Sing|Person=3|Tense=Past</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>،</td>\n",
       "      <td>،</td>\n",
       "      <td>PUNCT</td>\n",
       "      <td>DELM</td>\n",
       "      <td>punct</td>\n",
       "      <td>9</td>\n",
       "      <td>_</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>کی</td>\n",
       "      <td>کی</td>\n",
       "      <td>ADV</td>\n",
       "      <td>ADV_I</td>\n",
       "      <td>root</td>\n",
       "      <td>0</td>\n",
       "      <td>PronType=Int</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>بود</td>\n",
       "      <td>بود#باش</td>\n",
       "      <td>AUX</td>\n",
       "      <td>V_PA</td>\n",
       "      <td>cop</td>\n",
       "      <td>9</td>\n",
       "      <td>Number=Sing|Person=3|Tense=Past</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>؟</td>\n",
       "      <td>؟</td>\n",
       "      <td>PUNCT</td>\n",
       "      <td>DELM</td>\n",
       "      <td>punct</td>\n",
       "      <td>9</td>\n",
       "      <td>_</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        text    lemma   upos     xpos        dep  head  \\\n",
       "0      آخرین    آخرین    ADJ  ADJ_SUP       amod     2   \n",
       "1        کسی       کس   NOUN   N_SING      nsubj     9   \n",
       "2         که       که  SCONJ      CON       mark     6   \n",
       "3         به       به    ADP        P       case     5   \n",
       "4   کامپیوتر   کامپیو   NOUN   N_SING       nmod     6   \n",
       "5        وصل      وصل    ADJ      ADJ  acl:relcl     2   \n",
       "6         شد   کرد#کن    AUX     V_PA        cop     6   \n",
       "7          ،        ،  PUNCT     DELM      punct     9   \n",
       "8         کی       کی    ADV    ADV_I       root     0   \n",
       "9        بود  بود#باش    AUX     V_PA        cop     9   \n",
       "10         ؟        ؟  PUNCT     DELM      punct     9   \n",
       "\n",
       "                              feats  \n",
       "0                        Degree=Sup  \n",
       "1                       Number=Sing  \n",
       "2                                 _  \n",
       "3                                 _  \n",
       "4                       Number=Sing  \n",
       "5                        Degree=Pos  \n",
       "6   Number=Sing|Person=3|Tense=Past  \n",
       "7                                 _  \n",
       "8                      PronType=Int  \n",
       "9   Number=Sing|Person=3|Tense=Past  \n",
       "10                                _  "
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "words_table('آخرین کسی که به کامپیوتر وصل شد، کی بود؟', 'fa')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-08-30T14:27:47.477278Z",
     "start_time": "2019-08-30T14:27:47.147888Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "text: من \tlemma: مِن\tupos: ADP\txpos: P---------\n",
      "\t AdpType=Prep\n",
      "text: الممكن \tlemma: مُمكِن\tupos: ADJ\txpos: A-----MS2D\n",
      "\t Case=Gen|Definite=Def|Gender=Masc|Number=Sing\n",
      "text: أن \tlemma: أَنَّ\tupos: CCONJ\txpos: C---------\n",
      "\t _\n",
      "text: تتجمد \tlemma: تَجَمَّد\tupos: VERB\txpos: VISA-3FS--\n",
      "\t Aspect=Imp|Gender=Fem|Mood=Sub|Number=Sing|Person=3|VerbForm=Fin|Voice=Act\n",
      "text: الأسبوع \tlemma: أُسبُوع\tupos: NOUN\txpos: N------S4D\n",
      "\t Case=Acc|Definite=Def|Number=Sing\n",
      "text: القادم \tlemma: قَادِم\tupos: ADJ\txpos: A-----MS4D\n",
      "\t Case=Acc|Definite=Def|Gender=Masc|Number=Sing\n",
      "text: . \tlemma: .\tupos: PUNCT\txpos: G---------\n",
      "\t _\n"
     ]
    }
   ],
   "source": [
    "sents_ls=[('It may freeze next week.', 'من الممكن أن تتجمد الأسبوع القادم.'),\n",
    " (\"Please don't cry.\", 'لا تبكِ من فضلك.'),\n",
    " ('I am 19 years old.', 'عمري تسعة عشر عاماً.'),\n",
    " ('You look very pale.', 'تبدو شاحب الوجه.'),\n",
    " ('What time do you usually go to bed?', 'متى تخلد إلى النوم عادةً؟')]\n",
    "\n",
    "list_words(sents_ls[0][1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-08-30T14:28:09.278688Z",
     "start_time": "2019-08-30T14:28:09.034739Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "text: لا \tlemma: لَا\tupos: PART\txpos: F---------\n",
      "\t _\n",
      "text: تبكِ \tlemma: بَكَد\tupos: VERB\txpos: VIJA-2MS--\n",
      "\t Aspect=Imp|Gender=Masc|Mood=Jus|Number=Sing|Person=2|VerbForm=Fin|Voice=Act\n",
      "text: من \tlemma: مِن\tupos: ADP\txpos: P---------\n",
      "\t AdpType=Prep\n",
      "text: فضلك \tlemma: فضلك\tupos: NOUN\txpos: N------S2I\n",
      "\t Case=Gen|Definite=Ind|Number=Sing\n",
      "text: . \tlemma: .\tupos: PUNCT\txpos: G---------\n",
      "\t _\n"
     ]
    }
   ],
   "source": [
    "list_words(sents_ls[1][1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-08-30T14:30:13.903736Z",
     "start_time": "2019-08-30T14:30:13.681022Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "text: متى \tlemma: مَتَى\tupos: DET\txpos: S---------\n",
      "\t obl _\n",
      "text: تخلد \tlemma: خَلِد\tupos: VERB\txpos: VIIA-3FS--\n",
      "\t root Aspect=Imp|Gender=Fem|Mood=Ind|Number=Sing|Person=3|VerbForm=Fin|Voice=Act\n",
      "text: إلى \tlemma: إِلَى\tupos: ADP\txpos: P---------\n",
      "\t case AdpType=Prep\n",
      "text: النوم \tlemma: نَوم\tupos: NOUN\txpos: N------S2D\n",
      "\t obl:arg Case=Gen|Definite=Def|Number=Sing\n",
      "text: عادة \tlemma: عَادَة\tupos: NOUN\txpos: N------S4I\n",
      "\t obl Case=Acc|Definite=Ind|Number=Sing\n",
      "text: ً \tlemma: ً\tupos: NOUN\txpos: N------S4I\n",
      "\t obl Case=Acc|Definite=Ind|Number=Sing\n",
      "text: ؟ \tlemma: ؟\tupos: PUNCT\txpos: G---------\n",
      "\t punct _\n"
     ]
    }
   ],
   "source": [
    "list_words(sents_ls[4][1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-08-30T14:48:53.698786Z",
     "start_time": "2019-08-30T14:48:53.508870Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "text: ها \tlemma: هُوَ\tupos: PRON\txpos: SP---3FS4-\n",
      "\t nsubj Case=Acc|Gender=Fem|Number=Sing|Person=3|PronType=Prs\n",
      "text: ها \tlemma: هُوَ\tupos: PRON\txpos: SP---3FS4-\n",
      "\t nsubj Case=Acc|Gender=Fem|Number=Sing|Person=3|PronType=Prs\n",
      "text: هو \tlemma: هُوَ\tupos: PRON\txpos: SP---3MS1-\n",
      "\t cc Case=Nom|Gender=Masc|Number=Sing|Person=3|PronType=Prs\n",
      "text: كلبك \tlemma: كلبك\tupos: X\txpos: U---------\n",
      "\t root _\n",
      "text: . \tlemma: .\tupos: PUNCT\txpos: G---------\n",
      "\t punct _\n"
     ]
    }
   ],
   "source": [
    "list_words('ها هو كلبك.')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-08-30T14:55:18.969037Z",
     "start_time": "2019-08-30T14:55:18.772538Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "text: هنا \tlemma: هُنَا\tupos: ADV\txpos: D---------\n",
      "\t root _\n",
      "text: هنا \tlemma: هُنَا\tupos: ADV\txpos: D---------\n",
      "\t advmod _\n",
      "text: كلبك \tlemma: كلبك\tupos: X\txpos: U---------\n",
      "\t dep _\n",
      "text: . \tlemma: .\tupos: PUNCT\txpos: G---------\n",
      "\t punct _\n"
     ]
    }
   ],
   "source": [
    "list_words('هنا كلبك.')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-08-30T14:50:02.688894Z",
     "start_time": "2019-08-30T14:50:02.491467Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "text: هل \tlemma: هَل\tupos: PART\txpos: F---------\n",
      "\t root _\n",
      "text: هذه \tlemma: هٰذَا\tupos: DET\txpos: SD----FS1-\n",
      "\t det Case=Nom|Gender=Fem|Number=Sing|PronType=Dem\n",
      "text: كلابك \tlemma: كلابك\tupos: X\txpos: U---------\n",
      "\t nsubj _\n",
      "text: ؟ \tlemma: ؟\tupos: PUNCT\txpos: G---------\n",
      "\t punct _\n"
     ]
    }
   ],
   "source": [
    "s=('Are these your dogs?', 'هل هذه كلابك؟')\n",
    "list_words(s[1])"
   ]
  }
 ],
 "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
}
