File size: 3,517 Bytes
a4ff801
1c3741b
 
 
d532f55
 
1c3741b
 
 
 
 
d532f55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a127264
 
d532f55
a127264
d532f55
 
a127264
d532f55
a127264
 
d532f55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c3741b
25a02b8
1c3741b
d532f55
 
 
a4ff801
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
import streamlit as st
import transformers
from transformers import  pipeline, TokenClassificationPipeline, BertForTokenClassification , AutoTokenizer

x = st.text_area('enter')

#model.to("cpu")
tokenizer = AutoTokenizer.from_pretrained("dmis-lab/biobert-large-cased-v1.1", truncation = True, padding=True, model_max_length=512,)
model_checkpoint = BertForTokenClassification.from_pretrained("dexay/Ner2HgF", )
token_classifier = pipeline("token-classification", tokenizer = tokenizer,model=model_checkpoint,  )


biotext = x

#split document or text into sentences

lstbiotext = []

flag = 0
tempsen = ""
for e in biotext:
  tempsen += e
  if e=="(":
      flag = 1
  if e==")":
      flag = 0
  if (e =="." or e =="?" or e ==":" ) and flag == 0 :
      lstbiotext += [tempsen.strip()]
      tempsen = ""

ddata = lstbiotext

#tokenized_dat = tokenize_function(ddata) 

az = token_classifier(ddata)


#code to convert NER output to  RE input compatible format

#tg_inorder are decoding of labels on which the model was fine tuned on 

tg_inorder = ['O',
 'B-HORMONE',
 'B-EXP_PER',
 'I-HORMONE',
 'I-CANCER',
 'I-EDC',
 'B-RECEPTOR',
 'B-CANCER',
 'I-RECEPTOR',
 'B-EDC',
 'PAD']

lstSentEnc = []
lstSentbilbl = []
lstSentEnt = []
for itsent in az:
  
  sentaz = itsent
  ph = []
  phl = []
  for e in sentaz:
    if e["word"][0]=="#" and len(ph)!=0:
      ph[-1]+= e["word"][2:]
    else:
      ph += [e["word"]]
      phl += [e["entity"]]


  phltr = []
  for e in phl:
    phltr += [tg_inorder[int(e[-1])] if len(e)==7 else  tg_inorder[int(e[-2:])]]
  

  nwph = []
  nwphltr = []
  flag = 0
  for i in range(len(phltr)-2):
    if phltr[i]=="O" and flag != 3 :
      nwph += [ph[i]]
      nwphltr += [phltr[i]]
      continue
    elif flag == 3:
      nwph[-1] += " "+ph[i]
      flag = 1
      continue
    elif phltr[i][2:]==phltr[i+1][2:] and phltr[i+1][0]=="I" and flag == 0:
      nwph += [ph[i]]
      nwphltr += [phltr[i]]
      flag = 1
      continue
    elif phltr[i][2:]==phltr[i+1][2:] and phltr[i+1][0]=="I" and flag == 1:
      nwph[-1] += " "+ph[i]
      continue
# xox with flag == 3
    elif phltr[i][2:]==phltr[i+2][2:] and phltr[i+1]=="O" and phltr[i+2][0]=="I" and flag == 0:
      nwph += [ph[i]]
      nwphltr += [phltr[i]]
      flag = 3
      continue
    elif phltr[i][2:]==phltr[i+2][2:] and phltr[i+1]=="O" and phltr[i+2][0]=="I" and flag == 1:
      nwph[-1] += " "+ph[i]
      flag = 3
      continue
#\ xox
    elif flag == 1:
      nwph[-1] += " "+ph[i]
      flag = 0
      continue
    else :
      nwph += [ph[i]]
      nwphltr += [phltr[i]]
      continue
      

  # nwph,nwphltr,len(nwph),len(nwphltr)
  

  if nwphltr.count("O") <= len(nwphltr)-2:
    for i in range(len(nwph)-1):
      if nwphltr[i] != "O":
        for j in range(i,len(nwph)):
          if nwphltr[j] != "O" and nwphltr[j] != nwphltr[i] and {nwphltr[j], nwphltr[i]} != {"B-CANCER","B-RECEPTOR"}:
            sen2ad = ""
            for g in range(i):
              sen2ad += nwph[g]+" "
            sen2ad += "<e1>"+nwph[i]+"</e1> "

            for t in range(i+1,j):
              sen2ad += nwph[t]+" "
            sen2ad += "<e2>"+nwph[j]+"</e2>"
            if j<len(nwph):
              for l in range(j+1,len(nwph)):
                sen2ad += " "+nwph[l]
            lstSentEnc += [sen2ad]
            lstSentbilbl += [[nwphltr[i],nwphltr[j]]]
            lstSentEnt += [[nwph[i],nwph[j]]]
      


#lstSentEnc,lstSentEnt,lstSentbilbl

if x:
  out = token_classifier(x)
  st.markdown(lstSentEnc)