-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPubTator_submit.py
More file actions
67 lines (56 loc) · 2.22 KB
/
PubTator_submit.py
File metadata and controls
67 lines (56 loc) · 2.22 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
import json
import os
from urllib.parse import unquote
import requests
from unidecode import unidecode
def pdf2json():
if not os.path.isdir("json_file"):
os.mkdir("json_file")
# running pdf2json command
os.system("pdf2json -f pdf_file -o json_file")
def extract_text():
if not os.path.isdir("submit_json"):
os.mkdir("submit_json")
path = "json_file"
json_path = os.listdir(path)
for json_name in json_path:
# extract text from json
extract_text = ""
with open(os.getcwd() + "/json_file/" + json_name, "r") as f:
process_json = json.load(f)
formImage = process_json['formImage']
Pages = formImage['Pages']
for page in Pages:
Texts = page['Texts']
for text in Texts:
R = text['R'][0]
extract_text += R['T']
# Saving the file
with open(os.getcwd() + "/submit_json/" + "submit_{}".format(json_name), "w", encoding='utf-8') as p:
p.write("{\"text\":\"")
p.write(unquote(str(extract_text)))
p.write("\"}")
def submit_json():
submit_json_path = os.listdir("submit_json")
with open("SessionNumber.txt", "a") as outputfile:
for submit_json_name in submit_json_path:
# load text
input_json = ""
with open(os.getcwd() + "/submit_json/" + submit_json_name, "r", encoding='utf-8') as f:
for line in f:
line = unidecode(line)
input_json = input_json + line
# submit request
r = requests.post(
"https://www.ncbi.nlm.nih.gov/research/pubtator-api/annotations/annotate/submit/Gene",
data=input_json.encode('utf-8'))
if r.status_code != 200:
print("[Error]: HTTP code " + str(r.status_code))
else:
SessionNumber = r.text
print("Thanks for your submission. The session number is : " + SessionNumber + "\n")
outputfile.write(SessionNumber + "\t" + submit_json_name + "\n")
if __name__ == "__main__":
pdf2json()
extract_text()
submit_json()