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