-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructured_extractor.py
More file actions
167 lines (138 loc) · 6.2 KB
/
structured_extractor.py
File metadata and controls
167 lines (138 loc) · 6.2 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
import re
from typing import Dict, List, Tuple
from pdf_analyzer import PDFAnalyzer
class StructuredDataExtractor:
def __init__(self, analyzer: PDFAnalyzer):
self.analyzer = analyzer
def extract_structured_data(self, text: str) -> Dict:
"""Extract structured data from text using spaCy"""
if not self.analyzer.nlp:
return self._extract_fallback_data(text)
doc = self.analyzer.nlp(text)
return {
"sentences": self._extract_sentences(doc),
"entities": self._extract_entities(doc),
"key_concepts": self._extract_key_concepts(doc),
"relationships": self._extract_relationships(doc),
"facts": self._extract_facts(doc),
"metadata": self._extract_metadata(doc)
}
def _extract_sentences(self, doc) -> List[str]:
"""Extract meaningful sentences"""
sentences = []
for sent in doc.sents:
text = sent.text.strip()
if len(text) > 20 and not text.startswith(('Figure', 'Table', 'Chapter')):
sentences.append(text)
return sentences
def _extract_entities(self, doc) -> List[Tuple[str, str]]:
"""Extract named entities with their types"""
entities = []
for ent in doc.ents:
if ent.label_ in ['PERSON', 'ORG', 'GPE', 'PRODUCT', 'EVENT', 'DATE', 'QUANTITY']:
entities.append((ent.text, ent.label_))
return entities
def _extract_key_concepts(self, doc) -> List[str]:
"""Extract key concepts and noun phrases"""
concepts = []
# Noun chunks (complete noun phrases)
for chunk in doc.noun_chunks:
if len(chunk.text) > 3 and chunk.text.lower() not in ['the text', 'this document']:
concepts.append(chunk.text.lower())
# Important nouns and adjectives
for token in doc:
if (token.pos_ in ['NOUN', 'PROPN', 'ADJ'] and
not token.is_stop and
len(token.text) > 3 and
token.text.lower() not in ['text', 'page', 'chapter', 'section']):
concepts.append(token.text.lower())
# Remove duplicates and return most frequent
concept_counts = {}
for concept in concepts:
concept_counts[concept] = concept_counts.get(concept, 0) + 1
sorted_concepts = sorted(concept_counts.items(), key=lambda x: x[1], reverse=True)
return [concept for concept, count in sorted_concepts[:20]]
def _extract_relationships(self, doc) -> List[Dict]:
"""Extract semantic relationships between concepts"""
relationships = []
# Subject-verb-object relationships
for token in doc:
if token.dep_ == "nsubj" and token.head.pos_ == "VERB":
subject = token.text
verb = token.head.text
# Find object
for child in token.head.children:
if child.dep_ in ["dobj", "pobj"]:
obj = child.text
relationships.append({
"type": "action",
"subject": subject,
"action": verb,
"object": obj
})
# Adjective-noun relationships
for token in doc:
if token.pos_ == "ADJ":
for child in token.children:
if child.pos_ == "NOUN":
relationships.append({
"type": "attribute",
"concept": child.text,
"attribute": token.text
})
return relationships
def _extract_facts(self, doc) -> List[str]:
"""Extract factual statements"""
facts = []
for sent in doc.sents:
# Look for factual patterns
text = sent.text.lower()
# Contains numbers or dates
if re.search(r'\d+', text):
facts.append(sent.text)
# Contains comparison words
elif any(word in text for word in ['more', 'less', 'better', 'worse', 'higher', 'lower']):
facts.append(sent.text)
# Contains cause-effect words
elif any(word in text for word in ['causes', 'leads to', 'results in', 'affects']):
facts.append(sent.text)
return facts[:10] # Limit to top 10 facts
def _extract_metadata(self, doc) -> Dict:
"""Extract document metadata"""
return {
"total_sentences": len(list(doc.sents)),
"total_tokens": len(doc),
"language": doc.lang_,
"key_topics": self._extract_topics(doc)
}
def _extract_topics(self, doc) -> List[str]:
"""Extract main topics from the document"""
topics = []
# Find most frequent noun phrases
noun_phrases = {}
for chunk in doc.noun_chunks:
phrase = chunk.text.lower()
if len(phrase) > 5:
noun_phrases[phrase] = noun_phrases.get(phrase, 0) + 1
# Return top topics
sorted_topics = sorted(noun_phrases.items(), key=lambda x: x[1], reverse=True)
return [topic for topic, count in sorted_topics[:5]]
def _extract_fallback_data(self, text: str) -> Dict:
"""Fallback extraction when spaCy is not available"""
sentences = re.split(r'[.!?]+', text)
sentences = [s.strip() for s in sentences if len(s.strip()) > 20]
words = text.lower().split()
concepts = [w for w in words if len(w) > 4 and w.isalpha()]
return {
"sentences": sentences,
"entities": [],
"key_concepts": concepts[:10],
"relationships": [],
"facts": sentences[:5],
"metadata": {
"total_sentences": len(sentences),
"total_tokens": len(words),
"language": "unknown",
"key_topics": concepts[:5]
}
}