File size: 7,117 Bytes
2a949e8
 
5341324
 
 
 
 
 
 
 
 
 
 
 
 
 
2a949e8
5341324
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54241c8
5341324
 
 
 
 
 
 
 
 
 
 
 
2a949e8
5341324
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54241c8
5341324
 
54241c8
5341324
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54241c8
5341324
 
2a949e8
5341324
54241c8
5341324
 
2a949e8
 
5341324
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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 "<p>Please select at least one language</p>"
    
    # 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 "<p>Please select at least one search option</p>"
    
    # 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 "<p>No results found</p>"
    
    # Format results as HTML
    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;'>"
        
        # Source and Category
        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>"
        
        # Word Section
        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>"
        
        # Translation Section
        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>"
        
        # Expression Section
        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

# 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()