Jean-Baptiste/email_parser
20
1{2 "cells": [3 {4 "cell_type": "code",5 "execution_count": null,6 "id": "spiritual-swift",7 "metadata": {},8 "outputs": [],9 "source": [10 "%config Completer.use_jedi = False\n",11 "%load_ext autoreload\n",12 "%autoreload 2"13 ]14 },15 {16 "cell_type": "code",17 "execution_count": 1,18 "id": "stopped-single",19 "metadata": {},20 "outputs": [],21 "source": [22 "import tensorflow\n",23 "import regex"24 ]25 },26 {27 "cell_type": "code",28 "execution_count": 2,29 "id": "numeric-handle",30 "metadata": {},31 "outputs": [],32 "source": [33 "from transformers import pipeline"34 ]35 },36 {37 "cell_type": "code",38 "execution_count": 3,39 "id": "numerous-overall",40 "metadata": {},41 "outputs": [],42 "source": [43 "from email_parser import nlp"44 ]45 },46 {47 "cell_type": "code",48 "execution_count": 4,49 "id": "studied-oracle",50 "metadata": {},51 "outputs": [],52 "source": [53 "text = \"\"\"tel: 512 222 5555\"\"\""54 ]55 },56 {57 "cell_type": "code",58 "execution_count": 5,59 "id": "pacific-walter",60 "metadata": {},61 "outputs": [62 {63 "data": {64 "text/plain": [65 "'en'"66 ]67 },68 "execution_count": 5,69 "metadata": {},70 "output_type": "execute_result"71 }72 ],73 "source": [74 "lang = nlp.f_detect_language(text)\n",75 "lang"76 ]77 },78 {79 "cell_type": "code",80 "execution_count": 6,81 "id": "every-gardening",82 "metadata": {},83 "outputs": [84 {85 "data": {86 "text/html": [87 "<div>\n",88 "<style scoped>\n",89 " .dataframe tbody tr th:only-of-type {\n",90 " vertical-align: middle;\n",91 " }\n",92 "\n",93 " .dataframe tbody tr th {\n",94 " vertical-align: top;\n",95 " }\n",96 "\n",97 " .dataframe thead th {\n",98 " text-align: right;\n",99 " }\n",100 "</style>\n",101 "<table border=\"1\" class=\"dataframe\">\n",102 " <thead>\n",103 " <tr style=\"text-align: right;\">\n",104 " <th></th>\n",105 " <th>entity</th>\n",106 " <th>value</th>\n",107 " <th>start</th>\n",108 " <th>end</th>\n",109 " <th>score</th>\n",110 " </tr>\n",111 " </thead>\n",112 " <tbody>\n",113 " <tr>\n",114 " <th>0</th>\n",115 " <td>TEL</td>\n",116 " <td>512 222 5555</td>\n",117 " <td>5</td>\n",118 " <td>17</td>\n",119 " <td>1</td>\n",120 " </tr>\n",121 " </tbody>\n",122 "</table>\n",123 "</div>"124 ],125 "text/plain": [126 " entity value start end score\n",127 "0 TEL 512 222 5555 5 17 1"128 ]129 },130 "execution_count": 6,131 "metadata": {},132 "output_type": "execute_result"133 }134 ],135 "source": [136 "df_result = nlp.f_ner(text, lang=lang)\n",137 "df_result"138 ]139 },140 {141 "cell_type": "code",142 "execution_count": null,143 "id": "operating-recorder",144 "metadata": {},145 "outputs": [],146 "source": []147 },148 {149 "cell_type": "code",150 "execution_count": 16,151 "id": "delayed-overhead",152 "metadata": {},153 "outputs": [154 {155 "data": {156 "text/html": [157 "<div>\n",158 "<style scoped>\n",159 " .dataframe tbody tr th:only-of-type {\n",160 " vertical-align: middle;\n",161 " }\n",162 "\n",163 " .dataframe tbody tr th {\n",164 " vertical-align: top;\n",165 " }\n",166 "\n",167 " .dataframe thead th {\n",168 " text-align: right;\n",169 " }\n",170 "</style>\n",171 "<table border=\"1\" class=\"dataframe\">\n",172 " <thead>\n",173 " <tr style=\"text-align: right;\">\n",174 " <th></th>\n",175 " <th>entity</th>\n",176 " <th>value</th>\n",177 " <th>start</th>\n",178 " <th>end</th>\n",179 " <th>score</th>\n",180 " </tr>\n",181 " </thead>\n",182 " <tbody>\n",183 " <tr>\n",184 " <th>0</th>\n",185 " <td>SIGNATURE</td>\n",186 " <td>JB</td>\n",187 " <td>119</td>\n",188 " <td>122</td>\n",189 " <td>0.955208</td>\n",190 " </tr>\n",191 " </tbody>\n",192 "</table>\n",193 "</div>"194 ],195 "text/plain": [196 " entity value start end score\n",197 "0 SIGNATURE JB 119 122 0.955208"198 ]199 },200 "execution_count": 16,201 "metadata": {},202 "output_type": "execute_result"203 }204 ],205 "source": [206 "nlp.f_detect_email_signature(text, lang=\"fr\")"207 ]208 },209 {210 "cell_type": "code",211 "execution_count": 33,212 "id": "frozen-jones",213 "metadata": {},214 "outputs": [215 {216 "data": {217 "text/plain": [218 "[('je', None), (\"m'appelle\", None), ('Jean-Baptiste', 'PER')]"219 ]220 },221 "execution_count": 33,222 "metadata": {},223 "output_type": "execute_result"224 }225 ],226 "source": [227 "iter_match = regex.finditer(\"\\s|$\", text)\n",228 "list_values = []\n",229 "start_pos = 0\n",230 "for match in iter_match:\n",231 " word = match.string[start_pos:match.start()]\n",232 " \n",233 " df_entity = df_result.query(f\"start>={start_pos} & end<={match.start()}\").head(1)\n",234 " if len(df_entity)==1:\n",235 " entity = df_entity[\"entity\"].values[0]\n",236 " else:\n",237 " entity = None\n",238 "# list_values\n",239 " list_values.append((word, entity))\n",240 " start_pos = match.end()\n",241 "list_values\n",242 " "243 ]244 },245 {246 "cell_type": "code",247 "execution_count": null,248 "id": "solid-speaker",249 "metadata": {},250 "outputs": [],251 "source": []252 }253 ],254 "metadata": {255 "kernelspec": {256 "display_name": "Python 3",257 "language": "python",258 "name": "python3"259 },260 "language_info": {261 "codemirror_mode": {262 "name": "ipython",263 "version": 3264 },265 "file_extension": ".py",266 "mimetype": "text/x-python",267 "name": "python",268 "nbconvert_exporter": "python",269 "pygments_lexer": "ipython3",270 "version": "3.7.10"271 }272 },273 "nbformat": 4,274 "nbformat_minor": 5275}276 