forked from ncbi-nlp/Ab3P
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabbrev_utils.py
More file actions
154 lines (135 loc) · 4.69 KB
/
Copy pathabbrev_utils.py
File metadata and controls
154 lines (135 loc) · 4.69 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
import csv
import ctypes
import logging
import multiprocessing
import os
import traceback
from ctypes import Structure, c_char_p, c_double, c_int
from typing import List
logger = logging.Logger(name=__name__)
class AbbrOut(Structure):
_fields_ = [
("sf", c_char_p),
("lf", c_char_p),
("strat", c_char_p),
("sf_offset", c_int),
("lf_offset", c_int),
("prec", c_double),
]
class AbbrExtractor:
def __init__(self):
self.libAb3PWrapper = ctypes.CDLL("libAb3PWrapper.so")
# Create Ab3PWrapper instance
create_ab3p = self.libAb3PWrapper.create_ab3p
create_ab3p.restype = ctypes.c_void_p
self.ab3p_instance = create_ab3p()
self.add_text = self.libAb3PWrapper.add_text
self.add_text.argtypes = [ctypes.c_void_p, ctypes.c_char_p]
self.get_abbrs = self.libAb3PWrapper.get_abbrs
self.get_abbrs.argtypes = [
ctypes.c_void_p,
ctypes.POINTER(AbbrOut),
ctypes.POINTER(ctypes.c_int),
]
self.get_abbrs.restype = None
def close(self):
# Destroy Ab3PWrapper instance
destroy_ab3p = self.libAb3PWrapper.destroy_ab3p
destroy_ab3p.argtypes = [ctypes.c_void_p]
destroy_ab3p(self.ab3p_instance)
def get_abbrs_from_line(self, text: str, max_ct=100) -> List[dict]:
# Add text
self.add_text(self.ab3p_instance, text.encode())
abbrs_count = ctypes.c_int(max_ct)
abbrs = (AbbrOut * abbrs_count.value)()
self.get_abbrs(self.ab3p_instance, abbrs, ctypes.byref(abbrs_count))
result = []
for i in range(abbrs_count.value):
try:
result.append(
{
"sf": abbrs[i].sf.decode(errors="ignore"),
"lf": abbrs[i].lf.decode(errors="ignore"),
"sf_offset": abbrs[i].sf_offset,
"lf_offset": abbrs[i].lf_offset,
"prec": abbrs[i].prec,
}
)
except IndexError:
pass
return result
def get_abbrs_from_pmc(abs_file: str, result_file: str, suffix=""):
logger.info(f"starting get_abbrs_from_pmc")
write_header = False if os.path.exists(result_file) else True
with open(result_file, "a", newline="") as csv_file:
f = csv.writer(csv_file)
if write_header:
f.writerow(["pmc_id", "sf", "lf", "sf_offset", "lf_offset", "prec"])
ae = AbbrExtractor()
with open(abs_file) as f2:
reader = csv.DictReader(f2)
csv.field_size_limit(5000000)
for idx, row in enumerate(reader):
abstract_id = row["pmc_id"]
logger.info(f"{idx=}, {abstract_id=}")
try:
results = ae.get_abbrs_from_line(row["content"])
except:
results = []
if not results:
continue
rows = [
[
abstract_id,
i["sf"],
i["lf"],
i["sf_offset"],
i["lf_offset"],
i["prec"],
]
for i in results
]
f.writerows(rows)
# ae.close()
def run_function_in_subprocess(func, timeout=300, *args, **kwargs):
def wrapper(queue, *args, **kwargs):
try:
result = func(*args, **kwargs)
queue.put((True, result))
except Exception as e:
error_message = traceback.format_exc()
queue.put((False, error_message))
queue = multiprocessing.Queue()
process = multiprocessing.Process(
target=wrapper, args=(queue, *args), kwargs=kwargs
)
process.start()
process.join(timeout)
if process.is_alive():
logger.error("Process timed out. Terminating process.")
process.terminate()
process.join()
return False, "Process timed out."
try:
success, result = queue.get(timeout=1)
except:
return False, "error"
if success:
logger.info("Function executed successfully.")
return True, result
else:
logger.error("Function failed with error:", result)
return False, result
if __name__ == "__main__":
source_file, target_file = (
f"source.csv",
f"result.csv",
)
logger.info(f"{source_file=}")
while True:
success, result = run_function_in_subprocess(
get_abbrs_from_pmc, 1800, source_file, target_file
)
logger.info(f"{result=}")
if success:
break