import gradio as gr import sqlite3 import html from typing import List, Dict def search_dictionary(search_term: str, language: str, exact_match: bool, word_match: bool, contains: bool, starts_with: bool, ends_with: bool) -> str: if not search_term or search_term.isspace(): return "
Please enter a search term
" conn = sqlite3.connect('asawal_amqran.db') cursor = conn.cursor() # Simplify - just use the search term as is normalized_search = search_term.lower() search_columns = { "Amazigh": ["word", "latin", "construct", "plural", "acc", "accneg", "inacc", "variante", "feminine", "fem_construct", "fem_plural", "fem_plural_construct", "exp_zgh"], "Arabic": ["arabic", "exp_ara", "mean_ar"], "French": ["french", "exp_fra"] }.get(language, []) if not search_columns: return "Please select a language
" if not any([exact_match, word_match, contains, starts_with, ends_with]): return "Please select at least one search option
" priority_results = [] seen_word_ids = set() # Priority 1: Exact Match if exact_match: conditions = [f"LOWER({col}) = ?" for col in search_columns] query = f"SELECT * FROM lexie WHERE {' OR '.join(conditions)}" params = [normalized_search] * len(search_columns) cursor.execute(query, params) column_names = [desc[0] for desc in cursor.description] word_id_idx = column_names.index('word_id') if 'word_id' in column_names else -1 for row in cursor.fetchall(): if word_id_idx != -1: word_id = row[word_id_idx] if word_id not in seen_word_ids: seen_word_ids.add(word_id) priority_results.append((1, row)) # Priority 2: Exact Word Match if word_match: conditions = [] for col in search_columns: conditions.extend([ f"LOWER({col}) = ?", f"LOWER({col}) LIKE ? AND LOWER({col}) NOT LIKE ?", f"LOWER({col}) LIKE ? AND LOWER({col}) NOT LIKE ?", f"LOWER({col}) LIKE ? AND LOWER({col}) NOT LIKE ?" ]) query = f"SELECT * FROM lexie WHERE {' OR '.join(conditions)}" params = [] for _ in search_columns: params.extend([ normalized_search, f"{normalized_search} %", f"%{normalized_search}%", f"% {normalized_search}", f"%{normalized_search}%", f"% {normalized_search} %", f"%{normalized_search}%" ]) cursor.execute(query, params) column_names = [desc[0] for desc in cursor.description] word_id_idx = column_names.index('word_id') if 'word_id' in column_names else -1 for row in cursor.fetchall(): if word_id_idx != -1: word_id = row[word_id_idx] if word_id not in seen_word_ids: seen_word_ids.add(word_id) priority_results.append((2, row)) # Priority 3: Contains if contains: conditions = [f"LOWER({col}) LIKE ?" for col in search_columns] query = f"SELECT * FROM lexie WHERE {' OR '.join(conditions)}" params = [f"%{normalized_search}%"] * len(search_columns) cursor.execute(query, params) column_names = [desc[0] for desc in cursor.description] word_id_idx = column_names.index('word_id') if 'word_id' in column_names else -1 for row in cursor.fetchall(): if word_id_idx != -1: word_id = row[word_id_idx] if word_id not in seen_word_ids: seen_word_ids.add(word_id) priority_results.append((3, row)) # Priority 4: Starts With if starts_with: conditions = [f"LOWER({col}) LIKE ?" for col in search_columns] query = f"SELECT * FROM lexie WHERE {' OR '.join(conditions)}" params = [f"{normalized_search}%"] * len(search_columns) cursor.execute(query, params) column_names = [desc[0] for desc in cursor.description] word_id_idx = column_names.index('word_id') if 'word_id' in column_names else -1 for row in cursor.fetchall(): if word_id_idx != -1: word_id = row[word_id_idx] if word_id not in seen_word_ids: seen_word_ids.add(word_id) priority_results.append((4, row)) # Priority 5: Ends With if ends_with: conditions = [f"LOWER({col}) LIKE ?" for col in search_columns] query = f"SELECT * FROM lexie WHERE {' OR '.join(conditions)}" params = [f"%{normalized_search}"] * len(search_columns) cursor.execute(query, params) column_names = [desc[0] for desc in cursor.description] word_id_idx = column_names.index('word_id') if 'word_id' in column_names else -1 for row in cursor.fetchall(): if word_id_idx != -1: word_id = row[word_id_idx] if word_id not in seen_word_ids: seen_word_ids.add(word_id) priority_results.append((5, row)) conn.close() if not priority_results: return "No results found
" # Sort by priority priority_results.sort(key=lambda x: x[0]) results = [row for priority, row in priority_results] # Format results as HTML html_output = "No data found
" html_output += "