|
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: |
|
|
|
conn = sqlite3.connect('asawal_amqran.db') |
|
cursor = conn.cursor() |
|
|
|
|
|
conditions = [] |
|
search_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 "<p>Please select at least one language</p>" |
|
|
|
|
|
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 "<p>Please select at least one search option</p>" |
|
|
|
|
|
query = f"SELECT * FROM lexie WHERE {' OR '.join(conditions)}" |
|
params = [] |
|
|
|
|
|
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}") |
|
|
|
|
|
cursor.execute(query, params) |
|
results = cursor.fetchall() |
|
conn.close() |
|
|
|
if not results: |
|
return "<p>No results found</p>" |
|
|
|
|
|
html_output = "<div style='font-family: Arial, sans-serif;'>" |
|
|
|
for result in results: |
|
result_dict = dict(zip([desc[0] for desc in cursor.description], result)) |
|
|
|
html_output += "<div style='border: 1px solid #ccc; margin: 10px; padding: 15px; position: relative;'>" |
|
|
|
|
|
if result_dict['source']: |
|
html_output += f"<div style='text-align: center; font-style: italic;'>{html.escape(result_dict['source'])}</div>" |
|
if result_dict['category']: |
|
html_output += f"<div style='position: absolute; top: 10px; right: 10px; font-weight: bold;'>{html.escape(result_dict['category'])}</div>" |
|
|
|
|
|
html_output += "<h3>Word</h3><ul>" |
|
for field, label in [ |
|
('word', 'Word'), |
|
('latin', 'Latin'), |
|
('construct', 'Construct'), |
|
('plural', 'Plural'), |
|
('acc', 'Accusative'), |
|
('accneg', 'Negative Accusative'), |
|
('inacc', 'Inaccusative'), |
|
('variante', 'Variant'), |
|
('feminine', 'Feminine'), |
|
('fem_construct', 'Feminine Construct'), |
|
('fem_plural', 'Feminine Plural'), |
|
('fem_plural_construct', 'Feminine Plural Construct') |
|
]: |
|
if result_dict[field]: |
|
html_output += f"<li><strong>{label}:</strong> {html.escape(result_dict[field])}</li>" |
|
html_output += "</ul>" |
|
|
|
|
|
html_output += "<h3>Translations</h3><ul>" |
|
if result_dict['french']: |
|
html_output += f"<li><strong>French:</strong> {html.escape(result_dict['french'])}</li>" |
|
if result_dict['arabic']: |
|
html_output += f"<li><strong>Arabic:</strong> {html.escape(result_dict['arabic'])}</li>" |
|
if result_dict['mean_ar']: |
|
html_output += f"<li><strong>Arabic Meaning:</strong> {html.escape(result_dict['mean_ar'])}</li>" |
|
html_output += "</ul>" |
|
|
|
|
|
html_output += "<h3>Expressions</h3><ul>" |
|
for field, label in [ |
|
('exp_zgh', 'Amazigh Expression'), |
|
('exp_fra', 'French Expression'), |
|
('exp_ara', 'Arabic Expression') |
|
]: |
|
if result_dict[field]: |
|
html_output += f"<li><strong>{label}:</strong> {html.escape(result_dict[field])}</li>" |
|
html_output += "</ul>" |
|
|
|
html_output += "</div>" |
|
|
|
html_output += "</div>" |
|
return html_output |
|
|
|
|
|
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") |
|
|
|
|
|
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() |