-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument_processor.py
More file actions
243 lines (207 loc) · 8.78 KB
/
Copy pathdocument_processor.py
File metadata and controls
243 lines (207 loc) · 8.78 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
import os
import logging
import networkx as nx
import matplotlib.pyplot as plt
import plotly.graph_objects as go
from opensearchpy import OpenSearch
from typing import List, Dict, Any
from collections import Counter
import spacy
from dotenv import load_dotenv
import json
# Load environment variables
load_dotenv()
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class DocumentProcessor:
def __init__(self):
"""Initialize document processor with OpenSearch and NLP capabilities"""
try:
# Initialize OpenSearch client
self.opensearch = OpenSearch(
hosts=[{'host': os.getenv('OPENSEARCH_HOST', 'localhost'),
'port': int(os.getenv('OPENSEARCH_PORT', 9200))}],
http_auth=(os.getenv('OPENSEARCH_USER', 'admin'),
os.getenv('OPENSEARCH_PASSWORD', 'admin')),
use_ssl=True,
verify_certs=False,
ssl_show_warn=False
)
# Initialize spaCy for NLP
self.nlp = spacy.load("en_core_web_sm")
# Create index if it doesn't exist
self._create_index()
logger.info("Document processor initialized successfully")
except Exception as e:
logger.error(f"Error initializing document processor: {str(e)}")
raise
def _create_index(self):
"""Create OpenSearch index with appropriate mappings"""
index_name = "documents"
# Define index mapping
mapping = {
"mappings": {
"properties": {
"content": {"type": "text"},
"title": {"type": "text"},
"author": {"type": "text"},
"keywords": {"type": "keyword"},
"creation_date": {"type": "date", "format": "strict_date_optional_time||epoch_millis"},
"file_path": {"type": "keyword"},
"file_type": {"type": "keyword"},
"page_number": {"type": "integer"},
"embedding": {"type": "dense_vector", "dims": 384} # For semantic search
}
}
}
try:
if not self.opensearch.indices.exists(index_name):
self.opensearch.indices.create(index=index_name, body=mapping)
logger.info(f"Created index: {index_name}")
except Exception as e:
logger.error(f"Error creating index: {str(e)}")
def process_document(self, file_path: str) -> Dict[str, Any]:
"""Process a document and extract metadata, content, and keywords"""
try:
# Read the file content
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Process the content with spaCy
doc = self.nlp(content)
# Extract keywords (nouns and proper nouns)
keywords = [token.text.lower() for token in doc
if not token.is_stop and not token.is_punct
and token.pos_ in ['NOUN', 'PROPN']]
# Create document info
document_info = {
'content': content,
'file_path': file_path,
'file_type': os.path.splitext(file_path)[1].lower(),
'keywords': list(set(keywords)),
'title': os.path.basename(file_path),
'creation_date': None
}
# Index document in OpenSearch
self.opensearch.index(
index="documents",
body=document_info,
refresh=True
)
return document_info
except Exception as e:
logger.error(f"Error processing document: {str(e)}")
raise
def _index_document(self, document_info: Dict[str, Any]):
"""Index document in OpenSearch"""
try:
# Prepare document for indexing
for page in document_info['pages']:
doc = {
'content': page['content'],
'title': document_info.get('title', ''),
'author': document_info.get('author', ''),
'keywords': page['keywords'],
'entities': page['entities'],
'file_path': document_info['file_path'],
'file_type': document_info['file_type'],
'page_number': page['page_number'],
'creation_date': document_info.get('creation_date', '')
}
self.opensearch.index(
index="documents",
body=doc,
id=f"{document_info['file_path']}_{page['page_number']}"
)
logger.info(f"Indexed document: {document_info['file_path']}")
except Exception as e:
logger.error(f"Error indexing document: {str(e)}")
def generate_keyword_graph(self, file_path: str = None) -> Dict[str, Any]:
"""Generate keyword co-occurrence graph"""
try:
# Query OpenSearch for keywords
query = {"match_all": {}} if not file_path else {
"term": {"file_path": file_path}
}
response = self.opensearch.search(
index="documents",
body={"query": query},
size=1000
)
# Create graph
G = nx.Graph()
keyword_pairs = []
# Process keywords from search results
for hit in response['hits']['hits']:
keywords = hit['_source'].get('keywords', [])
for i, kw1 in enumerate(keywords):
for kw2 in keywords[i+1:]:
keyword_pairs.append(tuple(sorted([kw1, kw2])))
# Count co-occurrences
edge_weights = Counter(keyword_pairs)
# Add edges to graph
for (kw1, kw2), weight in edge_weights.items():
G.add_edge(kw1, kw2, weight=weight)
# Convert to simple format
nodes = [{"id": node, "label": node} for node in G.nodes()]
edges = [{"source": source, "target": target, "weight": G[source][target]["weight"]}
for source, target in G.edges()]
return {
"nodes": nodes,
"edges": edges
}
except Exception as e:
logger.error(f"Error generating keyword graph: {str(e)}")
return {"nodes": [], "edges": []}
def search_documents(self, query: str, file_path: str = None) -> List[Dict[str, Any]]:
"""Search documents using OpenSearch"""
try:
# Build search query
search_query = {
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": query,
"fields": ["content", "title", "keywords^2", "entities^2"]
}
}
]
}
},
"highlight": {
"fields": {
"content": {},
"title": {},
"keywords": {}
}
}
}
# Add file path filter if specified
if file_path:
search_query["query"]["bool"]["filter"] = [
{"term": {"file_path": file_path}}
]
response = self.opensearch.search(
index="documents",
body=search_query,
size=10
)
results = []
for hit in response['hits']['hits']:
source = hit['_source']
result = {
'title': source.get('title', ''),
'author': source.get('author', ''),
'file_path': source.get('file_path', ''),
'page_number': source.get('page_number', ''),
'score': hit['_score'],
'highlights': hit.get('highlight', {}),
'keywords': source.get('keywords', [])
}
results.append(result)
return results
except Exception as e:
logger.error(f"Error searching documents: {str(e)}")
return []