forked from Hydr0xy1/hackthon_aging_singularis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimrad_extractor.py
More file actions
451 lines (417 loc) · 18.8 KB
/
imrad_extractor.py
File metadata and controls
451 lines (417 loc) · 18.8 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# python >=3.8
# pip install spacy regex
# python -m spacy download en_core_web_sm
# # Optional (if using nltk instead of spaCy)
# pip install nltk
"""
imrad_extractor.py
Deterministic IMRaD extractor:
- Input: raw paper text (string)
- Output: JSON: nodes (Hypothesis/Experiment/Dataset/Analysis/Conclusion) + edges
- Strategy: section detection -> sentence segmentation -> deterministic regex/heuristics -> LLM fallback -> pattern learning
Author: ChatGPT (template)
"""
import re
import json
import uuid
import os
from collections import defaultdict, Counter
# Optional: spaCy for sentence segmentation
try:
import spacy
nlp = spacy.load("en_core_web_sm")
def sents_from_text(text):
return [sent.text.strip() for sent in nlp(text).sents if sent.text.strip()]
except Exception:
# fallback to simple newline/sentence splitter
import re
def sents_from_text(text):
# very simple: split on period/question/exclamation followed by space + capital letter
chunks = re.split(r'(?<=[\.\?\!])\s+(?=[A-Z0-9])', text)
return [c.strip() for c in chunks if c.strip()]
# -------------------------
# Config: deterministic cue phrases (can be expanded)
# -------------------------
CUE_PATTERNS = {
"Hypothesis": [
r"\bwe hypothesi[sz]e\b",
r"\bwe hypothes[ie]d\b",
r"\bwe propose\b",
r"\bit is hypothesized\b",
r"\bwe predict\b",
r"\bthis suggests\b.*hypoth",
r"\bhypothesi[sz]e\b",
],
"Experiment": [
r"\bwe (?:performed|conducted|carried out|did)\b",
r"\bwe treated\b",
r"\bwe injected\b",
r"\bwe administered\b",
r"\bwe used (?:an )?(?:assay|model|mouse|cell|cohort|experiment)\b",
r"\bmouse model\b",
r"\bin vitro\b",
r"\bin vivo\b",
r"\busing (?:the )?(?:protocol|method|assay)\b",
],
"Dataset": [
r"\bcohort\b",
r"\bTCGA\b",
r"\bPCAWG\b",
r"\b(n=|n =)\d+",
r"\bdata (?:from|obtained from|available at)\b",
r"\bGEO\b",
r"\b(whole[- ]genome|exome|WGS|WES|RNA-Seq|RNA Sequencing)\b",
],
"Analysis": [
r"\bwe (?:analyz|computed|calculated|modeled|fit)\b",
r"\bwe (?:used|applied) (?:regression|model|linear|xgboost|random forest|cox)\b",
r"\bcorrelat",
r"\bstatistical (?:analysis|test)\b",
r"\bp-value\b|\bp < 0\.",
r"\bwe trained\b",
],
"Conclusion": [
r"\bin conclusion\b",
r"\bwe conclude\b",
r"\bthese results (?:suggest|indicate|show)\b",
r"\bthis study (?:shows|demonstrates)\b",
r"\bsignificantly\b.*\b(implicat|associate|reduce|increase)\b",
]
}
# Patterns to detect section headings (IMRaD)
SECTION_HEADINGS = {
"INTRODUCTION": re.compile(r'^\s*(introduction|background)\s*$', re.I | re.M),
"METHODS": re.compile(r'^\s*(methods|materials and methods|methodology|experimental procedures)\s*$', re.I | re.M),
"RESULTS": re.compile(r'^\s*(results)\s*$', re.I | re.M),
"DISCUSSION": re.compile(r'^\s*(discussion|conclusions)\s*$', re.I | re.M),
"ABSTRACT": re.compile(r'^\s*(abstract)\s*$', re.I | re.M),
"CONCLUSION": re.compile(r'^\s*(conclusions|conclusion)\s*$', re.I | re.M)
}
# Simple mapping of sections to node relevance (heuristic)
SECTION_NODE_PRIOR = {
"ABSTRACT": ["Hypothesis", "Conclusion"],
"INTRODUCTION": ["Hypothesis"],
"METHODS": ["Experiment", "Dataset"],
"RESULTS": ["Experiment", "Analysis", "Dataset"],
"DISCUSSION": ["Conclusion", "Hypothesis"],
"CONCLUSION": ["Conclusion"]
}
# -------------------------
# Utilities
# -------------------------
def gen_id(prefix):
return f"{prefix}_{uuid.uuid4().hex[:8]}"
def match_patterns(text, patterns):
"""Return list of pattern matches (pattern, match_obj)"""
matches = []
for p in patterns:
m = re.search(p, text, flags=re.I)
if m:
matches.append((p, m))
return matches
# -------------------------
# Core extractor
# -------------------------
class IMRaDExtractor:
def __init__(self, patterns=CUE_PATTERNS, section_map=SECTION_HEADINGS,
node_prior=SECTION_NODE_PRIOR, pattern_store_path="learned_patterns.json"):
self.patterns = {k: [re.compile(p, re.I) for p in v] for k, v in patterns.items()}
self.section_map = section_map
self.node_prior = node_prior
self.pattern_store_path = pattern_store_path
self.learned = self._load_learned_patterns()
def _load_learned_patterns(self):
if os.path.exists(self.pattern_store_path):
try:
with open(self.pattern_store_path, "r", encoding="utf8") as fh:
return json.load(fh)
except Exception:
pass
return {k: [] for k in self.patterns.keys()}
def _save_learned_patterns(self):
with open(self.pattern_store_path, "w", encoding="utf8") as fh:
json.dump(self.learned, fh, indent=2, ensure_ascii=False)
def split_sections(self, text):
"""Split by headings if available. Returns list of (section_name, section_text)."""
# naive approach: find lines that look like headings
lines = text.splitlines()
sections = []
current_name = "BODY"
buffer = []
for ln in lines:
ln_stripped = ln.strip()
# if a heading line
found = None
for name, regex in self.section_map.items():
if regex.match(ln_stripped.lower() if isinstance(ln_stripped, str) else ln_stripped):
found = name
break
if found:
# push previous
if buffer:
sections.append((current_name, "\n".join(buffer).strip()))
current_name = found
buffer = []
else:
buffer.append(ln)
# final push
if buffer:
sections.append((current_name, "\n".join(buffer).strip()))
# normalize: if no headings found, return BODY as single section
if not sections:
return [("BODY", text)]
return sections
def assign_candidates_from_section(self, section_name, section_text):
"""Return list of (node_type, sentence, confidence, evidence) found in this section."""
sentences = sents_from_text(section_text)
candidates = []
for s in sentences:
found = False
# check learned patterns first
for node_type, learned_list in self.learned.items():
for lp in learned_list:
if re.search(lp, s, re.I):
candidates.append((node_type, s, 0.92, f"learned:{lp}"))
found = True
break
if found:
break
if found:
continue
# check core patterns, but also apply section prior weight
for node_type, pat_list in self.patterns.items():
for pat in pat_list:
if pat.search(s):
# base confidence
base_conf = 0.85
# boost if section matches prior
if node_type in self.node_prior.get(section_name, []):
base_conf += 0.08
candidates.append((node_type, s, round(min(0.99, base_conf), 2), f"pattern:{pat.pattern}"))
found = True
break
if found:
break
if not found:
# no deterministic match: candidate for LLM fallback if this sentence is important (length & punctuation heuristics)
# save as potential fallback
if len(s.split()) >= 6 and len(s) <= 400:
candidates.append(("FALLBACK", s, 0.10, "no_pattern"))
return candidates
def deterministic_extract(self, text):
"""Main deterministic pass over full text"""
sections = self.split_sections(text)
nodes = []
edges = []
for sec_name, sec_text in sections:
candidates = self.assign_candidates_from_section(sec_name, sec_text)
for node_type, sent, conf, evidence in candidates:
if node_type == "FALLBACK":
continue
node = {
"id": gen_id(node_type[:3].upper()),
"type": node_type,
"text": sent,
"section": sec_name,
"evidence": evidence,
"confidence": conf
}
nodes.append(node)
# build simple edges: experiments support analyses, analyses support conclusions, hypothesis->experiment if nearby
# naive heuristic: connect nodes by section ordering
nodes_by_section = defaultdict(list)
for n in nodes:
nodes_by_section[n["section"]].append(n)
# Link Hypothesis -> Experiment if in Introduction -> Methods/Results
for hyp in [n for n in nodes if n["type"] == "Hypothesis"]:
for exp in [n for n in nodes if n["type"] == "Experiment"]:
# only connect if different sections (heuristic)
if hyp["section"] != exp["section"]:
edges.append({
"start": hyp["id"], "end": exp["id"],
"type": "POSES_TEST",
"evidence": f"{hyp['evidence']} -> {exp['evidence']}",
"confidence": round(min(hyp["confidence"], exp["confidence"]), 2)
})
# Experiment -> Dataset / Analysis
for exp in [n for n in nodes if n["type"] == "Experiment"]:
for ds in [n for n in nodes if n["type"] == "Dataset"]:
edges.append({
"start": exp["id"], "end": ds["id"],
"type": "USES_DATASET",
"evidence": f"{exp['evidence']} -> {ds['evidence']}",
"confidence": round(min(exp["confidence"], ds["confidence"]), 2)
})
for an in [n for n in nodes if n["type"] == "Analysis"]:
edges.append({
"start": exp["id"], "end": an["id"],
"type": "GENERATES_ANALYSIS",
"evidence": f"{exp['evidence']} -> {an['evidence']}",
"confidence": round(min(exp["confidence"], an["confidence"]), 2)
})
# Analysis -> Conclusion
for an in [n for n in nodes if n["type"] == "Analysis"]:
for c in [n for n in nodes if n["type"] == "Conclusion"]:
edges.append({
"start": an["id"], "end": c["id"],
"type": "SUPPORTS_CONCLUSION",
"evidence": f"{an['evidence']} -> {c['evidence']}",
"confidence": round(min(an["confidence"], c["confidence"]), 2)
})
return {"nodes": nodes, "edges": edges, "sections": sections}
# -------------------------
# LLM fallback + pattern learning (very simple implementation)
# -------------------------
def llm_fallback(self, sentence, api_client=None, prompt_template=None):
"""
Call an LLM for classification into one of node types.
This function is a placeholder showing how to integrate with an LLM.
Provide api_client that wraps the LLM call and returns string label.
Example minimal api_client: lambda prompt: "Hypothesis"
"""
# If no external client provided, raise or return None
if api_client is None:
return None
# Example prompt:
prompt = (f"Classify the following sentence into one of: Hypothesis, Experiment, Dataset, Analysis, Conclusion, None.\n"
f"Sentence: '''{sentence}'''")
label = api_client(prompt) # user-supplied wrapper
# sanitize
label = (label or "").strip().split()[0]
if label not in ["Hypothesis", "Experiment", "Dataset", "Analysis", "Conclusion", "None"]:
label = "None"
return label
def expand_with_fallbacks(self, text, api_client=None, learn_new_patterns=True):
"""
Deterministic pass + fallback pass.
- Run deterministic_extract to get nodes.
- For sentences flagged as FALLBACK, call LLM (if provided) to classify.
- If LLM returns a label, add node. Also extract simple phrases to add to learned patterns.
"""
sections = self.split_sections(text)
primary = self.deterministic_extract(text)
nodes = primary["nodes"]
edges = primary["edges"]
fallback_sentences = []
# Identify fallback sentences
for sec_name, sec_text in sections:
sents = sents_from_text(sec_text)
# find sentences that weren't covered by deterministic patterns
for s in sents:
covered = False
for nt in nodes:
if nt["text"].strip() == s.strip():
covered = True
break
if not covered and len(s.split()) >= 6 and len(s) <= 500:
fallback_sentences.append((sec_name, s))
# call LLM for each fallback (if provided)
for sec_name, s in fallback_sentences:
label = None
if api_client:
label = self.llm_fallback(s, api_client=api_client)
if label and label != "None":
node = {
"id": gen_id(label[:3].upper()),
"type": label,
"text": s,
"section": sec_name,
"evidence": "llm_fallback",
"confidence": 0.6 # lower than deterministic
}
nodes.append(node)
# simple learning: extract short cue phrases and add to learned patterns
if learn_new_patterns:
phrase = self.simple_extract_cue(s)
if phrase:
# avoid duplicates
existing = set(self.learned.get(label, []))
if phrase not in existing:
self.learned[label].append(phrase)
# else: skip
# save learned patterns
if learn_new_patterns:
self._save_learned_patterns()
# Optionally rebuild edges (simple approach: reuse earlier edge builder)
# For simplicity, regenerate edges from full nodes list:
edges = self._build_edges_from_nodes(nodes)
return {"nodes": nodes, "edges": edges, "learned_patterns": self.learned}
def simple_extract_cue(self, sentence, max_len=6):
"""
Heuristic to extract a short cue phrase from a sentence for future deterministic matching.
Strategy: take first verb phrase or first 2-3 words containing 'we' or 'this' or 'suggest'.
More sophisticated extraction can use POS tagging.
"""
# look for "we <verb>" patterns
m = re.search(r'\bwe\s+([a-zA-Z-]+)', sentence, re.I)
if m:
cue = rf"\bwe\s+{re.escape(m.group(1))}\b"
return cue
# look for "this suggests" etc
m = re.search(r'\b(this|these)\s+[a-z]+\b', sentence, re.I)
if m:
return re.escape(m.group(0))
# fallback: take first 3 words as phrase
first_words = " ".join(sentence.split()[:3])
return re.escape(first_words)
def _build_edges_from_nodes(self, nodes):
edges = []
# same heuristics as deterministic_extract but using provided nodes list
for hyp in [n for n in nodes if n["type"] == "Hypothesis"]:
for exp in [n for n in nodes if n["type"] == "Experiment"]:
if hyp["section"] != exp["section"]:
edges.append({
"start": hyp["id"], "end": exp["id"], "type": "POSES_TEST",
"evidence": f"{hyp['evidence']} -> {exp['evidence']}", "confidence": round(min(hyp['confidence'], exp['confidence']), 2)
})
for exp in [n for n in nodes if n["type"] == "Experiment"]:
for ds in [n for n in nodes if n["type"] == "Dataset"]:
edges.append({
"start": exp["id"], "end": ds["id"], "type": "USES_DATASET",
"evidence": f"{exp['evidence']} -> {ds['evidence']}", "confidence": round(min(exp['confidence'], ds['confidence']), 2)
})
for an in [n for n in nodes if n["type"] == "Analysis"]:
edges.append({
"start": exp["id"], "end": an["id"], "type": "GENERATES_ANALYSIS",
"evidence": f"{exp['evidence']} -> {an['evidence']}", "confidence": round(min(exp['confidence'], an['confidence']), 2)
})
for an in [n for n in nodes if n["type"] == "Analysis"]:
for c in [n for n in nodes if n["type"] == "Conclusion"]:
edges.append({
"start": an["id"], "end": c["id"], "type": "SUPPORTS_CONCLUSION",
"evidence": f"{an['evidence']} -> {c['evidence']}", "confidence": round(min(an['confidence'], c['confidence']), 2)
})
return edges
# -------------------------
# Example usage
# -------------------------
if __name__ == "__main__":
# Example: minimal test text to show pipeline
sample_text = """
Abstract
Here we hypothesize that intermittent fasting reduces epigenetic age. We performed experiments in a mouse model.
Introduction
Epigenetic aging has been observed in many tissues. We propose that lifestyle can modulate epigenetic clocks.
Methods
We used a cohort of n=24 mice and measured methylation using Illumina arrays. We treated mice with daily fasting cycles.
Results
We observed a significant reduction of epigenetic age (p < 0.01). Our XGBoost model predicted chronological age with r=0.83.
Discussion
In conclusion, our data support the hypothesis that intermittent fasting reduces epigenetic age.
"""
extractor = IMRaDExtractor()
res_det = extractor.deterministic_extract(sample_text)
print("Deterministic nodes:")
print(json.dumps(res_det["nodes"], indent=2, ensure_ascii=False))
print("Deterministic edges:")
print(json.dumps(res_det["edges"], indent=2, ensure_ascii=False))
# Simulate fallback: we provide a trivial LLM wrapper that labels sentences containing "clock" as Analysis
def fake_llm(prompt):
if "clock" in prompt.lower():
return "Analysis"
if "fasting" in prompt.lower():
return "Hypothesis"
return "None"
res_full = extractor.expand_with_fallbacks(sample_text, api_client=fake_llm, learn_new_patterns=True)
print("\nAfter fallback + learning:")
print(json.dumps({"nodes": res_full["nodes"], "edges": res_full["edges"], "learned": extractor.learned}, indent=2, ensure_ascii=False))