import gradio as gr import sqlite3 from typing import List, Dict import html def search_dictionary(search_term: str, lang_amazigh: bool, lang_arabic: bool, lang_french: bool, exact_match: bool, word_match: bool, contains: bool, starts_with: bool, ends_with: bool) -> str: # Connect to database conn = sqlite3.connect('asawal_amqran.db') cursor = conn.cursor() # Build WHERE clause based on search options and languages conditions = [] search_columns = [] # Language-specific columns if lang_amazigh: search_columns.extend([ "word", "latin", "construct", "plural", "acc", "accneg", "inacc", "variante", "feminine", "fem_construct", "fem_plural", "fem_plural_construct", "exp_zgh" ]) if lang_arabic: search_columns.extend(["arabic", "exp_ara", "mean_ar"]) if lang_french: search_columns.extend(["french", "exp_fra"]) if not search_columns: return "

Please select at least one language

" # Build search conditions based on selected options if exact_match or word_match: exact_conditions = [f"{col} = ?" for col in search_columns] if exact_match: conditions.append(f"({' OR '.join(exact_conditions)})") if word_match: word_conditions = [f"{col} LIKE ? OR {col} LIKE ?" for col in search_columns] conditions.append(f"({' OR '.join(word_conditions)})") if contains: contains_conditions = [f"{col} LIKE ?" for col in search_columns] conditions.append(f"({' OR '.join(contains_conditions)})") if starts_with: starts_conditions = [f"{col} LIKE ?" for col in search_columns] conditions.append(f"({' OR '.join(starts_conditions)})") if ends_with: ends_conditions = [f"{col} LIKE ?" for col in search_columns] conditions.append(f"({' OR '.join(ends_conditions)})") if not conditions: return "

Please select at least one search option

" # Construct query query = f"SELECT * FROM lexie WHERE {' OR '.join(conditions)}" params = [] # Add parameters based on search types for col in search_columns: if exact_match or word_match: if exact_match: params.append(search_term) if word_match: params.extend([f"{search_term} %", f"% {search_term}%"]) if contains: params.append(f"%{search_term}%") if starts_with: params.append(f"{search_term}%") if ends_with: params.append(f"%{search_term}") # Execute query cursor.execute(query, params) results = cursor.fetchall() conn.close() if not results: return "

No results found

" # Format results as HTML html_output = "
" for result in results: result_dict = dict(zip([desc[0] for desc in cursor.description], result)) html_output += "
" # Source and Category if result_dict['source']: html_output += f"
{html.escape(result_dict['source'])}
" if result_dict['category']: html_output += f"
{html.escape(result_dict['category'])}
" # Word Section html_output += "

Word

" # Translation Section html_output += "

Translations

" # Expression Section html_output += "

Expressions

" html_output += "
" html_output += "
" return html_output # Create Gradio interface with gr.Blocks(title="Dictionary Search") as demo: gr.Markdown("# Dictionary Search") with gr.Row(): with gr.Column(scale=1): search_input = gr.Textbox(label="Search Term", placeholder="Enter search term...") search_button = gr.Button("Search") gr.Markdown("### Language Options") amazigh = gr.Checkbox(label="Amazigh", value=True) arabic = gr.Checkbox(label="Arabic", value=True) french = gr.Checkbox(label="French", value=True) gr.Markdown("### Search Options") exact_match = gr.Checkbox(label="Exact Match (whole cell)", value=True) word_match = gr.Checkbox(label="Exact Word Match (within cell)", value=False) contains = gr.Checkbox(label="Contains", value=True) starts_with = gr.Checkbox(label="Starts With", value=False) ends_with = gr.Checkbox(label="Ends With", value=False) with gr.Column(scale=3): output = gr.HTML(label="Results") # Connect search button and enter key search_input.submit( search_dictionary, inputs=[search_input, amazigh, arabic, french, exact_match, word_match, contains, starts_with, ends_with], outputs=output ) search_button.click( search_dictionary, inputs=[search_input, amazigh, arabic, french, exact_match, word_match, contains, starts_with, ends_with], outputs=output ) demo.launch()