-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMedNaturalLanguageProcessing.py
More file actions
175 lines (145 loc) · 7.58 KB
/
Copy pathMedNaturalLanguageProcessing.py
File metadata and controls
175 lines (145 loc) · 7.58 KB
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
__author__ = 'arosado'
import re
import json
import types
import pymongo
class MedNaturalLanguageProcessing:
mongoClient = None
naturalLanguageDb = None
wikiMedInfoDb = None
icdDb = None
def buildCodeWikiSesarch(self, code, codeData):
searchData = {}
searchWordHolder = []
codeNameWordsRe = re.compile('\w+')
excludedWordsList = ['and', 'fevers']
codeNameWordsFindAll = codeNameWordsRe.findall(codeData['codeName'])
for word in codeNameWordsFindAll:
if word not in excludedWordsList:
searchWordHolder.append(word)
searchData['searchWords'] = searchWordHolder
return searchData
def breakdownCodeData(self, code, codeData):
newData = {}
billable = None
notbillableRe = re.compile('(?<=(not[\s]a))[\s](billable)')
billableRe = re.compile('(?<!(not[\s]a))[\s](billable)')
sentenceRe = re.compile('[^.!?\s][^.!?]*(?:[.!?](?![\S]?\s|$)[^.!?]*)*[.!?]?[\S]?(?=\s|$)')
clinicalData = None
clinicalDataRe = re.compile('(Clinical[\s]Information[\s])')
diagnosisRelatedGroupData = None
diagnosisRelatedGroupDataRe = re.compile('(Diagnostic[\s]Related[\s]Group[(]s[)][:])')
descriptiveSynonyms = None
descriptiveSynonymsRe = re.compile('(Description Synonyms)')
backReferencesData = None
backReferencesDataRe = re.compile('(back-references)')
applicableTo = None
applicableToRe = re.compile('(Applicable To)')
typeExcludesRe = re.compile('(Type\s[0-9]+\sExcludes)')
try:
sentenceHolder = []
typeExcludesHolder = []
if 'information' in codeData:
for infoList in codeData['information']:
for infoSubList in infoList:
for info in infoSubList:
sentences = sentenceRe.findall(info)
typeExcludesFindAll = typeExcludesRe.findall(info)
if clinicalData == None:
clinicalDataFindAll = clinicalDataRe.findall(info)
if len(clinicalDataFindAll) > 0:
newInfoList = infoList[1]
newData['clinicalInformation'] = newInfoList
if diagnosisRelatedGroupData == None:
diagnosisRelatedGroupFindAll = diagnosisRelatedGroupDataRe.findall(info)
if len(diagnosisRelatedGroupFindAll) > 0:
newSubInfo = infoSubList[1]
newData['diagnosisRelatedGroups'] = newSubInfo
if backReferencesData == None:
backReferencesFindAll = backReferencesDataRe.findall(info)
if len(backReferencesFindAll) > 0:
newInfoList = infoList[1]
newData['backReferences'] = newInfoList
if descriptiveSynonyms == None:
descriptiveSynonymsFindAll = descriptiveSynonymsRe.findall(info)
if len(descriptiveSynonymsFindAll) > 0:
newInfoList = infoList[1]
newData['descriptiveSynonyms'] = newInfoList
if applicableTo == None:
applicableToFindAll = applicableToRe.findall(info)
if len(applicableToFindAll) > 0:
newInfoList = infoList[1]
newData['applicableTo'] = newInfoList
if len(typeExcludesFindAll) > 0:
newInfoList = infoList[1]
typeExcludesHolder.append([len(typeExcludesHolder)+1, newInfoList])
if billable == None:
notBillFindAll = notbillableRe.findall(info)
billableFindAll = billableRe.findall(info)
if (len(notBillFindAll) + len(billableFindAll)) > 0:
if len(notBillFindAll) > 0:
newInfoList = infoList[1]
newData['icdInformation'] = newInfoList
billable = False
else:
newInfoList = infoList[1]
newData['icdInformation'] = newInfoList
billable = True
for sentence in sentences:
sentenceHolder.append(sentence)
newData['code'] = code['hierarchyGroup']
if 'hierarchy' in codeData:
newData['hierarchy'] = codeData['hierarchy']
#Every code's data does not include a code name.
if 'codeName' in codeData:
newData['codeName'] = codeData['codeName']
if 'codeIdentifier' in codeData:
newData['codeIdentifier'] = codeData['codeIdentifier']
newData['sentences'] = sentenceHolder
if billable != None:
newData['billable'] = billable
if len(typeExcludesHolder) > 0:
newData['typeExcludes'] = typeExcludesHolder
return newData
except:
print('Failed to breakdown medical code '+code['hierarchyGroup'] +'info list '+json.dumps(infoList))
def breakdownWikiData(self, wikiData):
if wikiData != None:
wikiLastModifiedText = wikiData['lastModified']
wikiRefInfo = wikiData['referenceInfo']
wikiSectionData = wikiData['sectionData']
wikiTitle = wikiData['title']
wikiInfoBoxes = wikiData['infoBoxes']
for infoBox in wikiInfoBoxes:
infoBoxSections = infoBox['sections']
for infoBoxSection in infoBoxSections:
infoBoxSectionName = infoBoxSection['sectionName']
infoBoxSectionData = infoBoxSection['data']
infoBoxSectionLinks = infoBoxSection['links']
infoBoxSectionImages = infoBoxSection['images']
pass
for ref in wikiRefInfo:
wikiRefText = ref['text']
wikiRefLinks = ref['links']
for link in wikiRefLinks:
pass
for section in wikiData['sectionData']:
sectionLinks = section['links']
sectionText = section['text']
sectionName = section['sectionName']
if 'imageLinks' in section:
sectionImageLinks = section['imageLinks']
for link in sectionImageLinks:
pass
if 'subSectionData' in section:
subSectionData = section['subSectionData']
pass
pass
def __init__(self):
try:
self.mongoClient = pymongo.MongoClient('localhost', 27017)
self.naturalLanguageDb = self.mongoClient.get_database('MedNLP')
self.icdDb = self.mongoClient.get_database('Icd10')
self.wikiMedInfoDb = self.mongoClient.get_database('WikiMedInfo')
except:
print('Failed to init natural language processing')