MrSimple07 commited on
Commit
11e130c
·
1 Parent(s): 9c9aff4

added the new function to replace latin crylic c25

Browse files
Files changed (2) hide show
  1. documents_prep.py +13 -2
  2. utils.py +43 -22
documents_prep.py CHANGED
@@ -183,7 +183,10 @@ def format_table_header(doc_id, table_identifier, table_num, table_title, sectio
183
  type_match = re.search(r'[СУUTC]-?\d+(?:-\d+)?', table_title)
184
  if type_match:
185
  connection_type = type_match.group(0)
186
- content += f"ТИП СОЕДИНЕНИЯ: {connection_type}\n"
 
 
 
187
 
188
  if table_num and table_num != table_identifier:
189
  content += f"НОМЕР ТАБЛИЦЫ: {table_num}\n"
@@ -443,6 +446,15 @@ def load_table_documents(repo_id, hf_token, table_dir):
443
  log_message(f"Error loading {file_path}: {e}")
444
 
445
  log_message(f"✓ Loaded {len(all_chunks)} table chunks")
 
 
 
 
 
 
 
 
 
446
  return all_chunks
447
 
448
 
@@ -494,7 +506,6 @@ def load_image_documents(repo_id, hf_token, image_dir):
494
  return documents
495
 
496
  def load_all_documents(repo_id, hf_token, json_dir, table_dir, image_dir):
497
- """Main loader - combines all document types"""
498
  log_message("="*60)
499
  log_message("STARTING DOCUMENT LOADING")
500
  log_message("="*60)
 
183
  type_match = re.search(r'[СУUTC]-?\d+(?:-\d+)?', table_title)
184
  if type_match:
185
  connection_type = type_match.group(0)
186
+ # NORMALIZE: Convert Cyrillic to Latin for consistency
187
+ connection_type_normalized = connection_type.replace('С', 'C').replace('У', 'U').replace('Т', 'T')
188
+ # Show BOTH in content for searchability
189
+ content += f"ТИП СОЕДИНЕНИЯ: {connection_type} ({connection_type_normalized})\n"
190
 
191
  if table_num and table_num != table_identifier:
192
  content += f"НОМЕР ТАБЛИЦЫ: {table_num}\n"
 
446
  log_message(f"Error loading {file_path}: {e}")
447
 
448
  log_message(f"✓ Loaded {len(all_chunks)} table chunks")
449
+
450
+ log_message("="*60)
451
+ log_message("CONNECTION TYPE ENCODING CHECK:")
452
+ for chunk in all_chunks[:50]: # Check first 50
453
+ conn_type = chunk.metadata.get('connection_type', '')
454
+ if 'C' in conn_type or 'С' in conn_type:
455
+ # Show both representations
456
+ log_message(f" Original: '{conn_type}' | Bytes: {conn_type.encode('utf-8')}")
457
+ log_message("="*60)
458
  return all_chunks
459
 
460
 
 
506
  return documents
507
 
508
  def load_all_documents(repo_id, hf_token, json_dir, table_dir, image_dir):
 
509
  log_message("="*60)
510
  log_message("STARTING DOCUMENT LOADING")
511
  log_message("="*60)
utils.py CHANGED
@@ -173,20 +173,48 @@ def deduplicate_nodes(nodes):
173
  return unique_nodes
174
 
175
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
176
  def answer_question(question, query_engine, reranker, current_model, chunks_df=None):
177
  if query_engine is None:
178
  return "<div style='background-color: #e53e3e; color: white; padding: 20px; border-radius: 10px;'>Система не инициализирована</div>", "", ""
179
 
180
  try:
181
  start_time = time.time()
182
- retrieved_nodes = query_engine.retriever.retrieve(question)
183
- log_message(f"user query: {question}")
 
 
 
 
 
 
 
 
184
  log_message(f"RETRIEVED: {len(retrieved_nodes)} nodes")
185
 
186
  unique_retrieved = deduplicate_nodes(retrieved_nodes)
187
  log_message(f"UNIQUE NODES: {len(unique_retrieved)} nodes")
188
 
189
- # NEW: Check for connection types in retrieved nodes
190
  conn_types_retrieved = {}
191
  for node in unique_retrieved:
192
  if node.metadata.get('type') == 'table':
@@ -199,28 +227,21 @@ def answer_question(question, query_engine, reranker, current_model, chunks_df=N
199
  for ct, cnt in sorted(conn_types_retrieved.items()):
200
  log_message(f" {ct}: {cnt} chunks")
201
 
202
- # Check if С-25 was retrieved
203
- if 'С-25' in question:
204
- if 'С-25' in conn_types_retrieved:
205
- log_message(f"✓ С-25 RETRIEVED: {conn_types_retrieved['С-25']} chunks")
 
 
 
 
206
  else:
207
- log_message("✗ С-25 NOT RETRIEVED despite being in query!")
208
-
209
- # Log sample of retrieved tables
210
- log_message("SAMPLE OF RETRIEVED TABLES:")
211
- for i, node in enumerate(unique_retrieved[:10]):
212
- if node.metadata.get('type') == 'table':
213
- table_num = node.metadata.get('table_number', 'N/A')
214
- table_title = node.metadata.get('table_title', 'N/A')
215
- conn_type = node.metadata.get('connection_type', 'N/A')
216
- doc_id = node.metadata.get('document_id', 'N/A')
217
- log_message(f" [{i+1}] {doc_id} - Table {table_num} - Type: {conn_type}")
218
-
219
- # Rerank
220
- reranked_nodes = rerank_nodes(question, unique_retrieved, reranker, top_k=20)
221
 
 
 
222
 
223
- # Direct query without formatting
224
  response = query_engine.query(question)
225
 
226
  end_time = time.time()
 
173
  return unique_nodes
174
 
175
 
176
+ def normalize_query(query):
177
+ """Normalize Cyrillic connection types to Latin in queries"""
178
+ import re
179
+
180
+ # Find all connection type patterns
181
+ pattern = r'[СУUTC]-?\d+(?:-\d+)?'
182
+
183
+ def replace_func(match):
184
+ conn_type = match.group(0)
185
+ # Convert Cyrillic to Latin
186
+ normalized = conn_type.replace('С', 'C').replace('У', 'U').replace('Т', 'T')
187
+ return normalized
188
+
189
+ normalized_query = re.sub(pattern, replace_func, query)
190
+
191
+ if normalized_query != query:
192
+ log_message(f"Query normalized: '{query}' → '{normalized_query}'")
193
+
194
+ return normalized_query
195
+
196
  def answer_question(question, query_engine, reranker, current_model, chunks_df=None):
197
  if query_engine is None:
198
  return "<div style='background-color: #e53e3e; color: white; padding: 20px; border-radius: 10px;'>Система не инициализирована</div>", "", ""
199
 
200
  try:
201
  start_time = time.time()
202
+
203
+ # NORMALIZE query for better matching
204
+ normalized_question = normalize_query(question)
205
+
206
+ # Use normalized query for retrieval
207
+ retrieved_nodes = query_engine.retriever.retrieve(normalized_question)
208
+ log_message(f"Original query: {question}")
209
+ if normalized_question != question:
210
+ log_message(f"Normalized query: {normalized_question}")
211
+
212
  log_message(f"RETRIEVED: {len(retrieved_nodes)} nodes")
213
 
214
  unique_retrieved = deduplicate_nodes(retrieved_nodes)
215
  log_message(f"UNIQUE NODES: {len(unique_retrieved)} nodes")
216
 
217
+ # Check for connection types - now check for NORMALIZED version
218
  conn_types_retrieved = {}
219
  for node in unique_retrieved:
220
  if node.metadata.get('type') == 'table':
 
227
  for ct, cnt in sorted(conn_types_retrieved.items()):
228
  log_message(f" {ct}: {cnt} chunks")
229
 
230
+ # Check for the target type (normalized)
231
+ target_type = normalize_query("С-25") # Will become "C-25" or "C25"
232
+ log_message(f"Checking for target connection type: {target_type}")
233
+ if any(t in question for t in ['С-25', 'C-25', 'C25']):
234
+ found_types = [ct for ct in conn_types_retrieved.keys()
235
+ if 'C25' in ct or 'C-25' in ct]
236
+ if found_types:
237
+ log_message(f"✓ C-25 variants RETRIEVED: {found_types}")
238
  else:
239
+ log_message("✗ C-25 NOT RETRIEVED despite being in query!")
 
 
 
 
 
 
 
 
 
 
 
 
 
240
 
241
+ # Rest continues with normalized_question
242
+ reranked_nodes = rerank_nodes(normalized_question, unique_retrieved, reranker, top_k=20)
243
 
244
+ # Use ORIGINAL question for final response (user sees their original)
245
  response = query_engine.query(question)
246
 
247
  end_time = time.time()