MrSimple07 commited on
Commit
4834e86
·
1 Parent(s): 11e130c

removed normalization

Browse files
Files changed (2) hide show
  1. documents_prep.py +1 -13
  2. utils.py +22 -41
documents_prep.py CHANGED
@@ -183,10 +183,7 @@ 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
- # 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,15 +443,6 @@ def load_table_documents(repo_id, hf_token, table_dir):
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
 
 
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
  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
 
utils.py CHANGED
@@ -173,26 +173,6 @@ def deduplicate_nodes(nodes):
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>", "", ""
@@ -200,21 +180,16 @@ def answer_question(question, query_engine, reranker, current_model, chunks_df=N
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,21 +202,27 @@ def answer_question(question, query_engine, reranker, current_model, chunks_df=N
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()
 
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>", "", ""
 
180
  try:
181
  start_time = time.time()
182
 
183
+ # DON'T normalize - use original query directly
184
+ retrieved_nodes = query_engine.retriever.retrieve(question)
185
+ log_message(f"user query: {question}")
 
 
 
 
 
186
 
187
  log_message(f"RETRIEVED: {len(retrieved_nodes)} nodes")
188
 
189
  unique_retrieved = deduplicate_nodes(retrieved_nodes)
190
  log_message(f"UNIQUE NODES: {len(unique_retrieved)} nodes")
191
 
192
+ # Check for connection types
193
  conn_types_retrieved = {}
194
  for node in unique_retrieved:
195
  if node.metadata.get('type') == 'table':
 
202
  for ct, cnt in sorted(conn_types_retrieved.items()):
203
  log_message(f" {ct}: {cnt} chunks")
204
 
205
+ # Check if target type was retrieved (keep original Cyrillic)
206
+ if 'С-25' in question: # Use Cyrillic
207
+ if 'С-25' in conn_types_retrieved:
208
+ log_message(f"✓ С-25 RETRIEVED: {conn_types_retrieved['С-25']} chunks")
 
 
 
 
209
  else:
210
+ log_message("✗ С-25 NOT RETRIEVED despite being in query!")
211
+
212
+ # Sample of retrieved tables
213
+ log_message("SAMPLE OF RETRIEVED TABLES:")
214
+ for i, node in enumerate(unique_retrieved[:10]):
215
+ if node.metadata.get('type') == 'table':
216
+ table_num = node.metadata.get('table_number', 'N/A')
217
+ table_title = node.metadata.get('table_title', 'N/A')
218
+ conn_type = node.metadata.get('connection_type', 'N/A')
219
+ doc_id = node.metadata.get('document_id', 'N/A')
220
+ log_message(f" [{i+1}] {doc_id} - Table {table_num} - Type: {conn_type}")
221
 
222
+ # Rerank
223
+ reranked_nodes = rerank_nodes(question, unique_retrieved, reranker, top_k=20)
224
 
225
+ # Direct query without formatting
226
  response = query_engine.query(question)
227
 
228
  end_time = time.time()