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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
|
|
|
|
7 |
|
8 |
app =flet_fastapi.app(main)
|
9 |
|
|
|
1 |
import flet as ft
|
2 |
import flet_fastapi
|
3 |
+
from flet import *
|
4 |
+
import requests
|
5 |
+
import json
|
6 |
+
import pandas as pd
|
7 |
+
import elasticsearch_serverless
|
8 |
+
import re
|
9 |
+
import os
|
10 |
+
|
11 |
+
def remove_arabic_diacritics(text):
|
12 |
+
diacritics_pattern = re.compile(r'[\u064B-\u065F\u0670\u06D6-\u06ED]')
|
13 |
+
|
14 |
+
no_diacritics_text = re.sub(diacritics_pattern, '', text)
|
15 |
+
|
16 |
+
return no_diacritics_text
|
17 |
+
|
18 |
+
diacritics = re.compile("""
|
19 |
+
ّ | # Tashdid
|
20 |
+
َ | # Fatha
|
21 |
+
ً | # Tanwin Fath
|
22 |
+
ُ | # Damma
|
23 |
+
ٌ | # Tanwin Damm
|
24 |
+
ِ | # Kasra
|
25 |
+
ٍ | # Tanwin Kasr
|
26 |
+
ْ | # Sukun
|
27 |
+
ـ # Tatwil/Kashida
|
28 |
+
""", re.VERBOSE)
|
29 |
+
|
30 |
+
def normalize_arabic(text):
|
31 |
+
text = diacritics.sub('', text)
|
32 |
+
text = text.replace('أ', 'ا')
|
33 |
+
text = text.replace('إ', 'ا')
|
34 |
+
text = text.replace('آ', 'ا')
|
35 |
+
text = text.replace('ة', 'ه')
|
36 |
+
text = text.replace('ى', 'ي')
|
37 |
+
return text
|
38 |
+
|
39 |
+
book_selected = False
|
40 |
+
|
41 |
+
first_run = 0
|
42 |
+
client = elasticsearch_serverless.Elasticsearch(
|
43 |
+
"https://e790c240926f48a78eec48ccb79ddcd1.us-central1.gcp.cloud.es.io:443",
|
44 |
+
api_key="MmY2VlpZOEJtVVZGLVJjNHpzRTM6akFlVmM2bHhSVmU1M25qRTIyZy1kUQ"
|
45 |
+
)
|
46 |
+
|
47 |
+
|
48 |
+
import pickle
|
49 |
+
with open('books_list.pkl', 'rb') as file:
|
50 |
+
books_list = pickle.load(file)
|
51 |
+
|
52 |
+
|
53 |
+
def e_search(query):
|
54 |
+
url_search = 'http://localhost:9202/books_jsons_01/_search'
|
55 |
+
|
56 |
+
query = remove_arabic_diacritics(query)
|
57 |
+
query = normalize_arabic(query)
|
58 |
+
|
59 |
+
j_query = {
|
60 |
+
"size": 500,
|
61 |
+
"query": {
|
62 |
+
"match": {
|
63 |
+
"Text": query
|
64 |
+
}
|
65 |
+
}
|
66 |
+
}
|
67 |
+
|
68 |
+
|
69 |
+
response_search = client.search(index="books_jsons_01", body=j_query)
|
70 |
+
|
71 |
+
unique_books = {}
|
72 |
+
for hit in response_search['hits']['hits']:
|
73 |
+
book = hit['_source']['Book']
|
74 |
+
page = hit['_source']['Page']
|
75 |
+
score = hit['_score']
|
76 |
+
if book not in unique_books:
|
77 |
+
unique_books[book] = {'Pages': {page: score}, 'Count': 1}
|
78 |
+
else:
|
79 |
+
if page not in unique_books[book]['Pages']:
|
80 |
+
unique_books[book]['Pages'][page] = score
|
81 |
+
unique_books[book]['Count'] += 1
|
82 |
+
|
83 |
+
book_data = []
|
84 |
+
for book, info in unique_books.items():
|
85 |
+
pages = sorted(info['Pages'].items())
|
86 |
+
book_data.append({'Book': book, 'Pages': [page for page, _ in pages], 'Scores': [score for _, score in pages], 'Count': info['Count']})
|
87 |
+
df = pd.DataFrame(book_data)
|
88 |
+
|
89 |
+
df = df.head(12)
|
90 |
+
print(j_query)
|
91 |
+
|
92 |
+
def get_top_two(row):
|
93 |
+
sorted_row = sorted(zip(row['Pages'], row['Scores']), key=lambda x: x[1], reverse=True)
|
94 |
+
return [page for page, score in sorted_row[:2]]
|
95 |
+
try:
|
96 |
+
df['Top Two Pages'] = df.apply(get_top_two, axis=1)
|
97 |
+
except:
|
98 |
+
pass
|
99 |
+
|
100 |
+
return df
|
101 |
+
|
102 |
|
103 |
async def main (page:ft.Page):
|
104 |
+
def e_search_book(query):
|
105 |
+
book_name = book_btn.text
|
106 |
+
print(book_name)
|
107 |
+
|
108 |
+
url_search = 'http://localhost:9202/books_jsons_01/_search'
|
109 |
+
|
110 |
+
query = remove_arabic_diacritics(query)
|
111 |
+
query = normalize_arabic(query)
|
112 |
+
j_query = {
|
113 |
+
"size": 10,
|
114 |
+
"query": {
|
115 |
+
"bool": {
|
116 |
+
"must": [
|
117 |
+
{
|
118 |
+
"match": {
|
119 |
+
"Text": query
|
120 |
+
}
|
121 |
+
},
|
122 |
+
{
|
123 |
+
"match": {
|
124 |
+
"Book": book_name
|
125 |
+
}
|
126 |
+
}
|
127 |
+
]
|
128 |
+
}
|
129 |
+
}
|
130 |
+
}
|
131 |
+
response_search = client.search(index="books_jsons_01", body=j_query)
|
132 |
+
|
133 |
+
data = []
|
134 |
+
for hit in response_search['hits']['hits']:
|
135 |
+
book = hit['_source']['Book']
|
136 |
+
page = hit['_source']['Page']
|
137 |
+
score = hit['_score']
|
138 |
+
text = hit['_source']['Text']
|
139 |
+
data.append({
|
140 |
+
"Book": book,
|
141 |
+
"Page": page,
|
142 |
+
"Score": score,
|
143 |
+
"Text": text
|
144 |
+
})
|
145 |
+
|
146 |
+
df = pd.DataFrame(data)
|
147 |
+
return df
|
148 |
+
|
149 |
+
def printer(e, name):
|
150 |
+
query_feild.value=name
|
151 |
+
page.update()
|
152 |
+
|
153 |
+
def query_feild_changed(string):
|
154 |
+
datatable_row.visible = False
|
155 |
+
listview.visible=True
|
156 |
+
|
157 |
+
query_list = books_list
|
158 |
+
|
159 |
+
list_items = {
|
160 |
+
name: ListTile(
|
161 |
+
title=Text(name),
|
162 |
+
leading=Icon(icons.ARROW_RIGHT_SHARP),
|
163 |
+
on_click=lambda e, name=name: printer(e, name)
|
164 |
+
)
|
165 |
+
for name in query_list
|
166 |
+
}
|
167 |
+
|
168 |
+
str_lower = normalize_arabic(string.control.value)
|
169 |
+
listview.controls = [
|
170 |
+
list_items.get(n) for n in query_list if str_lower in normalize_arabic(n)
|
171 |
+
] if str_lower else []
|
172 |
+
page.update()
|
173 |
+
|
174 |
+
def send_button(e):
|
175 |
+
|
176 |
+
global first_run
|
177 |
+
|
178 |
+
datatable_row.visible = True
|
179 |
+
listview.visible=False
|
180 |
+
|
181 |
+
|
182 |
+
if first_run >= 1 :
|
183 |
+
|
184 |
+
res_dt.columns.clear()
|
185 |
+
res_dt.rows.clear()
|
186 |
+
|
187 |
+
first_run=1
|
188 |
+
|
189 |
+
if not book_selected:
|
190 |
+
e_search_df = e_search(query_feild.value)
|
191 |
+
|
192 |
+
for i in range (len(e_search_df.columns)):
|
193 |
+
res_dt.columns.append(DataColumn(Text(e_search_df.columns[i]))) # Set column width here
|
194 |
+
|
195 |
+
for i in range (e_search_df.shape[0]):
|
196 |
+
res_dt.rows.append(DataRow(cells=[
|
197 |
+
DataCell(Text(e_search_df['Book'][i], width = 450)),
|
198 |
+
DataCell(Text(e_search_df['Pages'][i], width = 180)),
|
199 |
+
DataCell(Text(e_search_df['Scores'][i], width = 180)),
|
200 |
+
DataCell(Text(e_search_df['Count'][i], width = 120)),
|
201 |
+
DataCell(Text(e_search_df['Top Two Pages'][i], width = 200))
|
202 |
+
]))
|
203 |
+
|
204 |
+
|
205 |
+
else:
|
206 |
+
e_search_df = e_search_book(query_feild.value)
|
207 |
+
|
208 |
+
for i in range (len(e_search_df.columns)):
|
209 |
+
res_dt.columns.append(DataColumn(Text(e_search_df.columns[i]))) # Set column width here
|
210 |
+
|
211 |
+
for i in range ((e_search_df.shape[0])):
|
212 |
+
txt = e_search_df['Text'][i][:80].replace("\n", " ")
|
213 |
+
res_dt.rows.append(DataRow(cells=[
|
214 |
+
DataCell(Text(e_search_df['Book'][i], width = 450)),
|
215 |
+
DataCell(Text(e_search_df['Page'][i], width = 180)),
|
216 |
+
DataCell(Text(e_search_df['Score'][i], width = 180)),
|
217 |
+
DataCell(Row([Text(f"{txt}...", width = 400), IconButton(icon=icons.ARROW_RIGHT_OUTLINED, height = 50, on_click = show_book_text)]))
|
218 |
+
]))
|
219 |
+
|
220 |
+
|
221 |
+
|
222 |
+
|
223 |
+
page.update()
|
224 |
+
|
225 |
+
|
226 |
+
def book_btn_filter(e):
|
227 |
+
global book_selected
|
228 |
+
book_value = query_feild.value
|
229 |
+
|
230 |
+
if book_value in books_list:
|
231 |
+
book_btn.text = query_feild.value
|
232 |
+
book_btn.bgcolor=colors.GREEN
|
233 |
+
book_selected = True
|
234 |
+
|
235 |
+
else:
|
236 |
+
book_btn.text = "No Book Found"
|
237 |
+
book_btn.bgcolor=colors.CYAN
|
238 |
+
book_selected = False
|
239 |
+
|
240 |
+
page.update()
|
241 |
+
|
242 |
+
def show_book_text(e):
|
243 |
+
pass
|
244 |
+
|
245 |
+
res_dt=DataTable(
|
246 |
+
|
247 |
+
border=border.all(2, "blue"),
|
248 |
+
border_radius=10,
|
249 |
+
column_spacing=10,
|
250 |
+
#data_row_min_height = 80
|
251 |
+
)
|
252 |
+
datatable_row = Row([res_dt],alignment=MainAxisAlignment.CENTER)
|
253 |
+
|
254 |
+
query_feild=TextField(label="Inquiry",hint_text="Please write your inquiry", expand=True,
|
255 |
+
on_change=query_feild_changed
|
256 |
+
)
|
257 |
+
|
258 |
+
query_send=FloatingActionButton(icon=icons.SEND,
|
259 |
+
on_click=send_button
|
260 |
+
)
|
261 |
+
book_btn = ElevatedButton(text="Book Filter", height = 55, width= 180, icon=icons.FILTER,on_click = book_btn_filter,
|
262 |
+
bgcolor=colors.CYAN,
|
263 |
+
color=colors.WHITE,
|
264 |
+
style=ButtonStyle(shape=RoundedRectangleBorder(radius=10)),
|
265 |
+
)
|
266 |
+
listview = ListView(expand=1, spacing=10, padding=20)
|
267 |
+
|
268 |
+
Query_row = Row(controls=[query_feild,book_btn,query_send])
|
269 |
|
270 |
+
#Initial Invisable
|
271 |
+
datatable_row.visible = False
|
272 |
+
await page.add_async(Query_row,listview,datatable_row)
|
273 |
|
274 |
app =flet_fastapi.app(main)
|
275 |
|