|
59 | 59 | }, |
60 | 60 | { |
61 | 61 | "cell_type": "code", |
62 | | - "execution_count": 2, |
| 62 | + "execution_count": 1, |
63 | 63 | "metadata": {}, |
64 | 64 | "outputs": [ |
65 | 65 | { |
66 | 66 | "data": { |
67 | 67 | "application/vnd.jupyter.widget-view+json": { |
68 | | - "model_id": "8c987b21d7c04f62b66651fdd555edff", |
| 68 | + "model_id": "46a8735bb95a411d9ef2513275e918cb", |
69 | 69 | "version_major": 2, |
70 | 70 | "version_minor": 0 |
71 | 71 | }, |
|
92 | 92 | }, |
93 | 93 | { |
94 | 94 | "cell_type": "code", |
95 | | - "execution_count": 3, |
| 95 | + "execution_count": 32, |
96 | 96 | "metadata": {}, |
97 | 97 | "outputs": [], |
98 | 98 | "source": [ |
|
109 | 109 | }, |
110 | 110 | { |
111 | 111 | "cell_type": "code", |
112 | | - "execution_count": 5, |
| 112 | + "execution_count": 33, |
113 | 113 | "metadata": {}, |
114 | 114 | "outputs": [], |
115 | 115 | "source": [ |
|
140 | 140 | }, |
141 | 141 | { |
142 | 142 | "cell_type": "code", |
143 | | - "execution_count": 9, |
| 143 | + "execution_count": 34, |
144 | 144 | "metadata": {}, |
145 | 145 | "outputs": [ |
146 | 146 | { |
147 | 147 | "name": "stdout", |
148 | 148 | "output_type": "stream", |
149 | 149 | "text": [ |
150 | | - "Processing file: C:\\Users\\damir\\Dropbox\\Develop\\python-tutorial-notebooks\\notebooks\\data\\medline_overview.txt\n", |
151 | | - "({'NLM': 'National Library of Medicine’s', 'MeSH': 'Medical Subject Headings', 'NCBI': 'National Center for Biotechnology Information', 'MEDLARS': 'MEDical Literature Analysis and Retrieval System', 'LSTRC': 'Literature Selection Technical Review Committee'}, {'NLM': 'National Library of Medicine’s', 'MeSH': 'Medical Subject Headings', 'NCBI': 'National Center for Biotechnology Information', 'MEDLARS': 'MEDical Literature Analysis and Retrieval System', 'LSTRC': 'Literature Selection Technical Review Committee'})\n" |
| 150 | + "Processing file: C:\\Users\\damir\\Dropbox\\Develop\\python-tutorial-notebooks\\notebooks\\data\\bio_1.txt\n", |
| 151 | + "({'ER': 'endoplasmic reticulum'}, {'ER': 'endoplasmic reticulum'})\n" |
152 | 152 | ] |
153 | 153 | } |
154 | 154 | ], |
|
157 | 157 | "print(abbreviations)" |
158 | 158 | ] |
159 | 159 | }, |
| 160 | + { |
| 161 | + "cell_type": "code", |
| 162 | + "execution_count": 35, |
| 163 | + "metadata": {}, |
| 164 | + "outputs": [], |
| 165 | + "source": [ |
| 166 | + "uri_prefix = \"http://www.indiana.edu/nlplab/bioterminology#\"" |
| 167 | + ] |
| 168 | + }, |
| 169 | + { |
| 170 | + "cell_type": "code", |
| 171 | + "execution_count": 36, |
| 172 | + "metadata": {}, |
| 173 | + "outputs": [], |
| 174 | + "source": [ |
| 175 | + "from rdflib.namespace import RDF, RDFS, SKOS, OWL, DC, DCTERMS, XSD, TIME, NamespaceManager\n", |
| 176 | + "from rdflib import Graph, URIRef, Literal, Namespace" |
| 177 | + ] |
| 178 | + }, |
| 179 | + { |
| 180 | + "cell_type": "code", |
| 181 | + "execution_count": 37, |
| 182 | + "metadata": {}, |
| 183 | + "outputs": [], |
| 184 | + "source": [ |
| 185 | + "dictionary = {}\n", |
| 186 | + "for x in abbreviations:\n", |
| 187 | + " entry = tuple(x.items())[0]\n", |
| 188 | + " dictionary[uri_prefix + \"\".join([ z.title() for z in entry[1].split() ])] = entry\n" |
| 189 | + ] |
| 190 | + }, |
| 191 | + { |
| 192 | + "cell_type": "code", |
| 193 | + "execution_count": 38, |
| 194 | + "metadata": {}, |
| 195 | + "outputs": [], |
| 196 | + "source": [ |
| 197 | + "from pprint import pprint\n", |
| 198 | + "g = Graph()\n", |
| 199 | + "vaem_acronym = URIRef(\"http://www.linkedmodel.org/schema/vaem#acronym\")\n", |
| 200 | + "for key in dictionary:\n", |
| 201 | + " g.add( (URIRef(key), RDFS.label, Literal(dictionary[key][1])) )\n", |
| 202 | + " g.add( (URIRef(key), vaem_acronym, Literal(dictionary[key][0])) )\n" |
| 203 | + ] |
| 204 | + }, |
| 205 | + { |
| 206 | + "cell_type": "code", |
| 207 | + "execution_count": 39, |
| 208 | + "metadata": {}, |
| 209 | + "outputs": [], |
| 210 | + "source": [ |
| 211 | + "import spacy\n", |
| 212 | + "import pytextrank" |
| 213 | + ] |
| 214 | + }, |
| 215 | + { |
| 216 | + "cell_type": "code", |
| 217 | + "execution_count": 50, |
| 218 | + "metadata": {}, |
| 219 | + "outputs": [ |
| 220 | + { |
| 221 | + "data": { |
| 222 | + "text/plain": [ |
| 223 | + "<pytextrank.base.BaseTextRankFactory at 0x22b2e203010>" |
| 224 | + ] |
| 225 | + }, |
| 226 | + "execution_count": 50, |
| 227 | + "metadata": {}, |
| 228 | + "output_type": "execute_result" |
| 229 | + } |
| 230 | + ], |
| 231 | + "source": [ |
| 232 | + "nlp = spacy.load(\"en_core_web_trf\") # en_core_web_sm\")\n", |
| 233 | + "nlp.add_pipe(\"textrank\")" |
| 234 | + ] |
| 235 | + }, |
| 236 | + { |
| 237 | + "cell_type": "code", |
| 238 | + "execution_count": 53, |
| 239 | + "metadata": {}, |
| 240 | + "outputs": [], |
| 241 | + "source": [ |
| 242 | + "with open(os.path.join(fc.selected_path, fc.selected_filename), mode='r', encoding='utf-8') as ifp:\n", |
| 243 | + " text = ifp.read()" |
| 244 | + ] |
| 245 | + }, |
| 246 | + { |
| 247 | + "cell_type": "code", |
| 248 | + "execution_count": 54, |
| 249 | + "metadata": {}, |
| 250 | + "outputs": [], |
| 251 | + "source": [ |
| 252 | + "doc = nlp(text)" |
| 253 | + ] |
| 254 | + }, |
| 255 | + { |
| 256 | + "cell_type": "markdown", |
| 257 | + "metadata": {}, |
| 258 | + "source": [ |
| 259 | + "for phrase in doc._.phrases:\n", |
| 260 | + " print(phrase.text)\n", |
| 261 | + " print(phrase.rank, phrase.count)\n", |
| 262 | + " print(phrase.chunks)\n", |
| 263 | + " print()" |
| 264 | + ] |
| 265 | + }, |
| 266 | + { |
| 267 | + "cell_type": "code", |
| 268 | + "execution_count": 46, |
| 269 | + "metadata": {}, |
| 270 | + "outputs": [ |
| 271 | + { |
| 272 | + "name": "stdout", |
| 273 | + "output_type": "stream", |
| 274 | + "text": [ |
| 275 | + "{}\n" |
| 276 | + ] |
| 277 | + } |
| 278 | + ], |
| 279 | + "source": [ |
| 280 | + "dictionary = {}\n", |
| 281 | + "for x in doc._.phrases:\n", |
| 282 | + " entry = x.text # tuple(x.items())[0]\n", |
| 283 | + " key = URIRef(uri_prefix + \"\".join([ z.title() for z in entry.split() ]))\n", |
| 284 | + " # = entry\n", |
| 285 | + " # for key in dictionary:\n", |
| 286 | + " g.add( (URIRef(key), RDFS.label, Literal(entry)) )\n", |
| 287 | + " # g.add( (URIRef(key), vaem_acronym, Literal(dictionary[key][0])) )\n", |
| 288 | + "print(dictionary)" |
| 289 | + ] |
| 290 | + }, |
| 291 | + { |
| 292 | + "cell_type": "code", |
| 293 | + "execution_count": 63, |
| 294 | + "metadata": {}, |
| 295 | + "outputs": [], |
| 296 | + "source": [ |
| 297 | + "nelabels = {\"ORG\": \"Organization\",\n", |
| 298 | + " \"PERSON\": \"Person\"}\n", |
| 299 | + "for k in nelabels:\n", |
| 300 | + " g.add( ( URIRef(uri_prefix + nelabels[k]), RDF.type, OWL.Class) )\n", |
| 301 | + "for ent in doc.ents:\n", |
| 302 | + " # print(ent.text, ent.label_)\n", |
| 303 | + " if ent.label_ in (\"ORG\", \"PERSON\"):\n", |
| 304 | + " key = URIRef(uri_prefix + \"\".join([ z.title() for z in ent.text.split() ]))\n", |
| 305 | + " type = nelabels[ent.label_]\n", |
| 306 | + " g.add( (key, RDF.type, URIRef(uri_prefix + type)) )\n", |
| 307 | + " g.add( (key, RDFS.label, Literal(ent.text)) )\n", |
| 308 | + " # print(ent.text, ent.label_)\n" |
| 309 | + ] |
| 310 | + }, |
| 311 | + { |
| 312 | + "cell_type": "code", |
| 313 | + "execution_count": 64, |
| 314 | + "metadata": {}, |
| 315 | + "outputs": [ |
| 316 | + { |
| 317 | + "data": { |
| 318 | + "text/plain": [ |
| 319 | + "<Graph identifier=N8793d07f04814241b59925a0d5c3aeae (<class 'rdflib.graph.Graph'>)>" |
| 320 | + ] |
| 321 | + }, |
| 322 | + "execution_count": 64, |
| 323 | + "metadata": {}, |
| 324 | + "output_type": "execute_result" |
| 325 | + } |
| 326 | + ], |
| 327 | + "source": [ |
| 328 | + "g.serialize(destination=\"data/test_graph.ttl\", format=\"turtle\", encoding=\"utf-8\")" |
| 329 | + ] |
| 330 | + }, |
160 | 331 | { |
161 | 332 | "attachments": {}, |
162 | 333 | "cell_type": "markdown", |
|
0 commit comments