-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscNLP_NER.py
More file actions
executable file
·164 lines (148 loc) · 4.67 KB
/
Copy pathscNLP_NER.py
File metadata and controls
executable file
·164 lines (148 loc) · 4.67 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 24 01:49:15 2020
@author: smith
"""
from __future__ import unicode_literals, print_function
import pandas as pd
import os
if os.getcwd()!='/home/smith/scNLP':
os.chdir('/home/smith/scNLP/')
from config.SpaCyConfig import *
def info(title):
print("processID:", os.getpid(), title)
def getArgs():
from config.PubMedScraperUtils import cluster, comparison, clusterDirectory
from config.SpaCyConfig import modelName
return (cluster, comparison, clusterDirectory, modelName)
def multiProcessTextMinimal(textFile):
"""Process a text file with ACWS spaCy model.
Parameters
----------
textFile : string
Path to text file to process
Returns
-------
None. Saves results .csv file and entity frequency .xlsx file.
"""
info("Processing lit ")
cluster, comparison, clusterDirectory, modelName = getArgs()
setName = (
cluster + "_" + comparison + "_" + modelName + "_" + textFile.split(sep="/")[-2]
)
model = modelDirectory
iteration = 0
start = 0
end = None
save = True
import time
def getCurrentTime():
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
return current_time
t = str(getCurrentTime())
print(setName + " Start Time: " + t)
print("Saving results to " + clusterDirectory)
t = str(getCurrentTime())
nlp.max_length = 20000000
if not end:
end=nlp.max_length
with open(textFile, "r+") as f:
testText = f.read()
f.close()
print(
"Total text length: "
+ str(
len(testText),
)
+ " \n Processing chars "
+ str(start)
+ " through "
+ str(end)
)
if len(testText)>end:
diff = len(testText)-end
perc = round((1-(diff/len(testText)))*100, 0)
print("Cutting out " + str(diff) + " characters (" + str(perc)+"%)")
text = testText[start:end]
doc = nlp(text)
t = str(getCurrentTime())
print("NLP complete at " + t)
setName = setName + "_Iteration" + str(iteration)
entsList = list(doc.ents)
sentList = []
labList = []
for ent in doc.ents:
labList.append(ent.label_)
sentList.append(str(ent.sent))
sentDf = pd.DataFrame(sentList)
sentDf.columns = ["Sentence"]
sentDf = sentDf.drop_duplicates(subset="Sentence", keep="first")
sentDf.to_excel(
os.path.join(
clusterDirectory,
"Sentences_"
+ cluster
+ "/"
+ setName
+ "_NER_Filtered_Sentences.xlsx",
)
)
entCatDf = pd.DataFrame(data=[entsList, labList]).T
entCatDf.columns = ["entity", "category"]
entCatDf = entCatDf.drop_duplicates(subset="entity", keep="first")
entCatDf.to_excel(
os.path.join(
clusterDirectory,
"Categories_"
+ cluster
+ "/"
+ setName
+ "_NER_Results_Entities_Categories.xlsx",
)
)
funcs = entCatDf.loc[entCatDf["category"] == "FUNCTION"]
funcs = funcs.drop_duplicates(subset="entity", keep="first")
regions = entCatDf.loc[entCatDf["category"] == "REGION"]
regions = regions.drop_duplicates(subset="entity", keep="first")
NTs = entCatDf.loc[entCatDf["category"] == "NEUROTRANSMITTER"]
NTs = NTs.drop_duplicates(subset="entity", keep="first")
phys = entCatDf.loc[entCatDf["category"] == "PHYSIO"]
phys = phys.drop_duplicates(subset="entity", keep="first")
CTs = entCatDf.loc[entCatDf["category"] == "CELLTYPE"]
CTs = CTs.drop_duplicates(subset="entity", keep="first")
if save == True:
funcs.to_excel(
os.path.join(
clusterDirectory,
"Functions_" + cluster + "/" + setName + "_NER_Functions.xlsx",
)
)
regions.to_excel(
os.path.join(
clusterDirectory,
"Regions_" + cluster + "/" + setName + "_NER_Regions.xlsx",
)
)
NTs.to_excel(
os.path.join(
clusterDirectory,
"NTs_" + cluster + "/" + setName + "_NER_Neurotransmitters.xlsx",
)
)
CTs.to_excel(
os.path.join(
clusterDirectory,
"CellTypes_" + cluster + "/" + setName + "_NER_CellTypes.xlsx",
)
)
phys.to_excel(
os.path.join(
clusterDirectory,
"Physio_" + cluster + "/" + setName + "_NER_Physio.xlsx",
)
)
t = str(getCurrentTime())
print("Results file written at " + t)
# return(funcs, regions, NTs, phys, CTs)