This repository has been archived by the owner on Jan 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoac_nlp_demo.py
70 lines (53 loc) · 1.83 KB
/
oac_nlp_demo.py
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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 18 14:17:36 2019
@author: danielbean
"""
# demo the annotator class
import os
import pandas as pd
from DrugNLP import OACAnnotator
annr = OACAnnotator()
#this has to be tuned to your docs
#its the number of characters expected when a drug list is present but empty
annr.empty_list_size = 70
doc_list = os.listdir('./demo/docs')
doc_list = [x for x in doc_list if x.endswith('.txt')]
all_results = {}
rows = []
for doc_fname in doc_list:
fname = './demo/docs/%s' % doc_fname
print "###########################"
print doc_fname
with open(fname,'r') as doc_input:
doc = doc_input.read()
#doc = doc.encode('utf8')
result = annr.annotate(doc)
result['status']['doc'] = int(doc_fname[:-4])
rows.append(result['status'])
all_results[doc_fname] = result
#create dataframe of results
df = pd.DataFrame(rows)
df = df.set_index('doc').sort_index()
#load expected annotations and reformat
expected = pd.read_csv('./demo/expected.csv')
del expected['notes']
expected.set_index('doc', inplace=True)
expected = expected.fillna(0).astype(bool)
expected = expected[df.columns]
#compare results to expected
correct = expected == df
print ""
print "All documents annotated as expected:"
print correct.all()
# check specific details
# prasugrel not detected in doc 3 because drug list found and only mentioned
# in body
assert all_results['3.txt']['pt_data']['prasugrel']['mentioned'] == False
# use fallback for doc 4
assert all_results['4.txt']['pt_data']['source'] == "NOT FOUND"
# in doc 5 warfarin is switched to rivaroxaban
assert all_results['5.txt']['pt_data']['warfarin']['mentioned'] == True
assert all_results['5.txt']['pt_data']['warfarin']['negated'] == True
assert all_results['5.txt']['pt_data']['rivaroxaban']['mentioned'] == True