File size: 11,744 Bytes
f380bb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\ranajoy.bhattacharya\\Anaconda3\\lib\\site-packages\\gensim\\similarities\\__init__.py:15: UserWarning: The gensim.similarities.levenshtein submodule is disabled, because the optional Levenshtein package <https://pypi.org/project/python-Levenshtein/> is unavailable. Install Levenhstein (e.g. `pip install python-Levenshtein`) to suppress this warning.\n",
      "  warnings.warn(msg)\n"
     ]
    }
   ],
   "source": [
    "from flair.data import Sentence\n",
    "from flair.models import MultiTagger"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from flair.tokenization import SciSpacyTokenizer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "sentence = Sentence(\"Behavioral abnormalities in the Fmr1 KO2 Mouse Model of Fragile X Syndrome\",\n",
    "                    use_tokenizer=SciSpacyTokenizer())\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022-10-27 15:38:14,268 https://nlp.informatik.hu-berlin.de/resources/models/hunflair_smallish_models/cellline/hunflair-celline-v1.0.pt not found in cache, downloading to C:\\Users\\RANAJO~1.BHA\\AppData\\Local\\Temp\\tmpsn3vv9l1\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 1104886372/1104886372 [02:45<00:00, 6695019.03B/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022-10-27 15:40:59,814 copying C:\\Users\\RANAJO~1.BHA\\AppData\\Local\\Temp\\tmpsn3vv9l1 to cache at C:\\Users\\ranajoy.bhattacharya\\.flair\\models\\hunflair-celline-v1.0.pt\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022-10-27 15:41:02,302 removing temp file C:\\Users\\RANAJO~1.BHA\\AppData\\Local\\Temp\\tmpsn3vv9l1\n",
      "2022-10-27 15:41:02,536 loading file C:\\Users\\ranajoy.bhattacharya\\.flair\\models\\hunflair-celline-v1.0.pt\n",
      "2022-10-27 15:41:08,505 SequenceTagger predicts: Dictionary with 8 tags: <unk>, O, S-CellLine, B-CellLine, I-CellLine, E-CellLine, <START>, <STOP>\n",
      "2022-10-27 15:41:09,549 https://nlp.informatik.hu-berlin.de/resources/models/hunflair_allcorpus_models/huner-chemical/hunflair-chemical-full-v1.0.pt not found in cache, downloading to C:\\Users\\RANAJO~1.BHA\\AppData\\Local\\Temp\\tmpw4ze4yrc\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 1104888221/1104888221 [02:06<00:00, 8735803.50B/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022-10-27 15:43:16,584 copying C:\\Users\\RANAJO~1.BHA\\AppData\\Local\\Temp\\tmpw4ze4yrc to cache at C:\\Users\\ranajoy.bhattacharya\\.flair\\models\\hunflair-chemical-full-v1.0.pt\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022-10-27 15:43:18,094 removing temp file C:\\Users\\RANAJO~1.BHA\\AppData\\Local\\Temp\\tmpw4ze4yrc\n",
      "2022-10-27 15:43:18,208 loading file C:\\Users\\ranajoy.bhattacharya\\.flair\\models\\hunflair-chemical-full-v1.0.pt\n",
      "2022-10-27 15:43:22,887 SequenceTagger predicts: Dictionary with 8 tags: <unk>, O, S-Chemical, B-Chemical, I-Chemical, E-Chemical, <START>, <STOP>\n",
      "2022-10-27 15:43:23,826 https://nlp.informatik.hu-berlin.de/resources/models/hunflair_allcorpus_models/huner-disease/hunflair-disease-full-v1.0.pt not found in cache, downloading to C:\\Users\\RANAJO~1.BHA\\AppData\\Local\\Temp\\tmpy18ixxme\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 1104886193/1104886193 [01:33<00:00, 11804678.71B/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022-10-27 15:44:57,965 copying C:\\Users\\RANAJO~1.BHA\\AppData\\Local\\Temp\\tmpy18ixxme to cache at C:\\Users\\ranajoy.bhattacharya\\.flair\\models\\hunflair-disease-full-v1.0.pt\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022-10-27 15:44:59,866 removing temp file C:\\Users\\RANAJO~1.BHA\\AppData\\Local\\Temp\\tmpy18ixxme\n",
      "2022-10-27 15:45:00,019 loading file C:\\Users\\ranajoy.bhattacharya\\.flair\\models\\hunflair-disease-full-v1.0.pt\n",
      "2022-10-27 15:45:06,491 SequenceTagger predicts: Dictionary with 8 tags: <unk>, O, B-Disease, E-Disease, I-Disease, S-Disease, <START>, <STOP>\n",
      "2022-10-27 15:45:07,641 https://nlp.informatik.hu-berlin.de/resources/models/hunflair_allcorpus_models/huner-gene/hunflair-gene-full-v1.0.pt not found in cache, downloading to C:\\Users\\RANAJO~1.BHA\\AppData\\Local\\Temp\\tmp1qgb402b\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 1104887211/1104887211 [01:22<00:00, 13387243.47B/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022-10-27 15:46:30,726 copying C:\\Users\\RANAJO~1.BHA\\AppData\\Local\\Temp\\tmp1qgb402b to cache at C:\\Users\\ranajoy.bhattacharya\\.flair\\models\\hunflair-gene-full-v1.0.pt\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022-10-27 15:46:32,696 removing temp file C:\\Users\\RANAJO~1.BHA\\AppData\\Local\\Temp\\tmp1qgb402b\n",
      "2022-10-27 15:46:32,831 loading file C:\\Users\\ranajoy.bhattacharya\\.flair\\models\\hunflair-gene-full-v1.0.pt\n",
      "2022-10-27 15:46:38,569 SequenceTagger predicts: Dictionary with 8 tags: <unk>, O, S-Gene, B-Gene, I-Gene, E-Gene, <START>, <STOP>\n",
      "2022-10-27 15:46:39,870 https://nlp.informatik.hu-berlin.de/resources/models/hunflair_allcorpus_models/huner-species/hunflair-species-full-v1.1.pt not found in cache, downloading to C:\\Users\\RANAJO~1.BHA\\AppData\\Local\\Temp\\tmpj1y4y_6v\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 1104886931/1104886931 [01:16<00:00, 14508392.59B/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022-10-27 15:47:56,571 copying C:\\Users\\RANAJO~1.BHA\\AppData\\Local\\Temp\\tmpj1y4y_6v to cache at C:\\Users\\ranajoy.bhattacharya\\.flair\\models\\hunflair-species-full-v1.1.pt\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022-10-27 15:47:58,616 removing temp file C:\\Users\\RANAJO~1.BHA\\AppData\\Local\\Temp\\tmpj1y4y_6v\n",
      "2022-10-27 15:47:58,765 loading file C:\\Users\\ranajoy.bhattacharya\\.flair\\models\\hunflair-species-full-v1.1.pt\n",
      "2022-10-27 15:48:04,720 SequenceTagger predicts: Dictionary with 8 tags: <unk>, O, S-Species, B-Species, I-Species, E-Species, <START>, <STOP>\n"
     ]
    }
   ],
   "source": [
    "# load biomedical tagger\n",
    "tagger = MultiTagger.load(\"hunflair\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Span[0:2]: \"Behavioral abnormalities\" β†’ Disease (0.6736)\n",
      "Span[9:12]: \"Fragile X Syndrome\" β†’ Disease (0.99)\n",
      "Span[4:5]: \"Fmr1\" β†’ Gene (0.838)\n",
      "Span[6:7]: \"Mouse\" β†’ Species (0.9979)\n"
     ]
    }
   ],
   "source": [
    "for annotation_layer in sentence.annotation_layers.keys():\n",
    "    for entity in sentence.get_spans(annotation_layer):\n",
    "        print(entity)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022-10-27 18:06:43,204 loading file C:\\Users\\ranajoy.bhattacharya\\.flair\\models\\hunflair-chemical-full-v1.0.pt\n",
      "2022-10-27 18:06:49,684 SequenceTagger predicts: Dictionary with 8 tags: <unk>, O, S-Chemical, B-Chemical, I-Chemical, E-Chemical, <START>, <STOP>\n"
     ]
    }
   ],
   "source": [
    "sentence = Sentence(\"Polycrystalline Li4+MxSi1-xO4 (M = B, Al) samples were prepared from reagent grade H3BO3 Al2O3, SiO2 and either LiOH*H2O (H samples) or Li2CO3 (C samples). Intimate stoichiometric mixtures were placed in an Al2O3 crucible or an Au foil boat and fired.\")\n",
    "\n",
    "# load biomedical tagger\n",
    "tagger = MultiTagger.load(\"hunflair-chemical\")\n",
    "\n",
    "# tag sentence\n",
    "tagger.predict(sentence)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Span[1:2]: \"Li4\" β†’ Chemical (0.754)\n",
      "Span[3:4]: \"MxSi1-xO4\" β†’ Chemical (0.8234)\n",
      "Span[9:10]: \"Al\" β†’ Chemical (0.9942)\n",
      "Span[17:18]: \"H3BO3\" β†’ Chemical (0.7564)\n",
      "Span[18:19]: \"Al2O3\" β†’ Chemical (0.5558)\n",
      "Span[20:21]: \"SiO2\" β†’ Chemical (0.973)\n",
      "Span[23:24]: \"LiOH\" β†’ Chemical (0.9721)\n",
      "Span[25:26]: \"H2O\" β†’ Chemical (0.8947)\n",
      "Span[31:32]: \"Li2CO3\" β†’ Chemical (0.9224)\n",
      "Span[44:45]: \"Al2O3\" β†’ Chemical (0.9847)\n",
      "Span[48:49]: \"Au\" β†’ Chemical (0.9787)\n"
     ]
    }
   ],
   "source": [
    "for annotation_layer in sentence.annotation_layers.keys():\n",
    "    for entity in sentence.get_spans(annotation_layer):\n",
    "        print(entity)"
   ]
  },
  {
   "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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}