-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutf.py
More file actions
362 lines (319 loc) · 16.5 KB
/
utf.py
File metadata and controls
362 lines (319 loc) · 16.5 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# ******************************************************************************
# Copyright (c) 2025. All rights reserved.
# # This work is licensed under the Creative Commons Attribution 4.0
# International License. To view a copy of this license,
# visit # http://creativecommons.org/licenses/by/4.0/.
#
# Author: roximn <roximn148@gmail.com>
# ******************************************************************************
from shiny import reactive
from shiny.express import module, ui, render
import humanize
from collections import Counter
from pathlib import Path
import unicodedataplus as unicodedata
import plotly.express as px
from shiny import reactive
from shiny.express import render, ui
from shinywidgets import render_plotly
import pandas as pd
from .utils import CATEGORIES, BLOCKS, findBlock
# ******************************************************************************
@module
def modUtf(input, output, session):
# File Info ----------------------------------------------------------------
@reactive.calc
def infoHeader():
info = {'name': '<NO-SELECTION>', 'size': 0}
files = input.txtFile()
if files and len(files) >= 1:
fileData: dict = files[0]
info['name'] = fileData.get('name', '<???>')
info['size'] = fileData.get('size', 0)
header = f'File: {info["name"]}, '\
f'size: {humanize.naturalsize(info["size"], binary=True)}'
return header
# Data -------------------------------------------------------------------------
@reactive.calc
def charDf():
files = input.txtFile()
if files is None:
return None
if len(files) < 1:
return None
fileData: dict = files[0]
if 'datapath' not in fileData:
return None
counts = Counter()
try:
with Path(fileData['datapath']).open(mode='r', encoding='utf-8') as file:
while True:
chunk = file.read(16*1024)
if not chunk:
break
counts.update(chunk)
except Exception as e:
print(f"Error reading file: {e}")
return None
cdf = pd.DataFrame(counts.items(), columns=['char', 'count'])
cdf['code'] = cdf['char'].apply(ord)
cdf['unicode'] = cdf['char'].apply(lambda x: f'U+{ord(x):04X} {unicodedata.name(x, "NO-NAME")}')
cdf['category'] = cdf['char'].apply(lambda x: unicodedata.category(x))
cdf['block'] = cdf['code'].apply(lambda x: BLOCKS[findBlock(x)].name)
cdf['script'] = cdf['char'].apply(lambda x: unicodedata.script(x))
cdf.sort_values(by='code', ascending=True, inplace=True)
return cdf
# File Info --------------------------------------------------------------------
@reactive.calc
def infoBody():
df = charDf()
if df is not None:
nChars = len(df)
totalCount = df['count'].sum()
nBlocks = df['block'].unique().size
nScripts = df['script'].unique().size
nCategories = df['category'].unique().size
body = f'Selected file contains {humanize.apnumber(nChars)} character(s) '\
f'with total count of {humanize.intword(totalCount)} ({humanize.intcomma(totalCount)})\n'\
f'belonging to {humanize.apnumber(nBlocks)} unicode block(s) '\
f'spanning {humanize.apnumber(nScripts)} script(s) '\
f'with {humanize.apnumber(nCategories)} different categories.'
else:
body = "No processed data."
return body
# File Selection ---------------------------------------------------------------
with ui.layout_columns(col_widths=(4, 8), fillable=True):
with ui.card(class_='bg-light border-dark'):
ui.input_file("txtFile", "Choose a text file to upload:", multiple=False)
@render.express(inline=True)
def fileInfoUi():
with ui.card(class_='bg-light border-dark'):
ui.card_header(infoHeader())
infoBody()
# Panels -----------------------------------------------------------------------
with ui.navset_pill():
# Counts Panel -------------------------------------------------------------
with ui.nav_panel("Counts"):
# ----------------------------------------------------------------------
with ui.card(fill=True, class_='border-light'):
ui.card_header('Unicode Character Frequency (Least to Most Frequent)')
@render_plotly
def chartCounts():
df = charDf()
if df is None:
return None
fig = px.bar(df,
y='unicode', x='count',
orientation='h',
log_x=True,
text='count',
hover_data=['unicode', 'category'],
color='category',
labels={'unicode': 'Unicode',
'count': 'Frequency',
'category': 'Category'},
category_orders={'category': CATEGORIES},
color_discrete_sequence=px.colors.qualitative.Plotly)
fig.update_traces(texttemplate='%{text:,.0f}',
textposition='inside',
textangle=0)
fig.update_yaxes(categoryorder='total descending')
fig.update_layout(height=len(df)*24)
return fig
# Code Points Panel --------------------------------------------------------
with ui.nav_panel("Code Points"):
# ----------------------------------------------------------------------
with ui.card(fill=True, class_='border-light'):
ui.card_header('Unicode Character Frequency (Ascending Code Points)')
@render_plotly
def chartCodePoints():
df = charDf()
if df is None:
return None
fig = px.bar(df,
y='unicode', x='count',
orientation='h',
log_x=True,
text='count',
hover_data=['unicode', 'category'],
color='category',
labels={'unicode': 'Unicode',
'count': 'Frequency',
'category': 'Category'},
category_orders={'category': CATEGORIES},
color_discrete_sequence=px.colors.qualitative.Plotly)
fig.update_traces(texttemplate='%{text:,.0f}',
textposition='inside',
textangle=0)
fig.update_yaxes(categoryorder='category descending')
fig.update_layout(height=len(df)*24)
return fig
# Blocks panel -------------------------------------------------------------
with ui.nav_panel('Blocks'):
# ----------------------------------------------------------------------
with ui.card(fill=True, class_='border-light'):
ui.card_header('Block Counts')
@render_plotly
def chartBlockCounts():
df = charDf()
if df is None:
return None
blockCounts = df.groupby('block')['count'].sum().reset_index()
# Filter block names preserving the order of appearance in BLOCKS
BLOCK_NAMES = [b.name for b in BLOCKS]
blocks = blockCounts['block'].unique().tolist()
blocks = [bname for bname in BLOCK_NAMES if bname in blocks]
fig = px.bar(blockCounts,
x='block', y='count',
log_y=True,
text='count',
color='block',
labels={'count': 'Frequency',
'block': 'Block'},
category_orders={'block': blocks},
color_discrete_sequence=px.colors.qualitative.Alphabet)
fig.update_traces(texttemplate='%{text:,.0f}',
textposition='inside',
textangle=0)
fig.update_layout(height=500)
fig.update_xaxes(categoryorder='total descending')
return fig
# ----------------------------------------------------------------------
with ui.card(fill=True, class_='border-light'):
ui.card_header('Unicode Character Frequency wrt Blocks')
@render_plotly
def chartUnicodeCounts():
df = charDf()
if df is None:
return None
fig = px.bar(df,
y='unicode', x='count',
orientation='h',
log_x=True,
text='count',
hover_data=['unicode', 'block', 'category'],
color='block',
labels={'unicode': 'Unicode',
'count': 'Frequency',
'category': 'Category',
'block': 'Block'},
category_orders={'block': [b.name for b in BLOCKS]},
color_discrete_sequence=px.colors.qualitative.Alphabet)
fig.update_traces(texttemplate='%{text:,.0f}',
textposition='inside',
textangle=0)
fig.update_yaxes(categoryorder='category descending')
fig.update_layout(height=len(df)*24)
return fig
# Scripts Panel ------------------------------------------------------------
with ui.nav_panel("Scripts"):
# ----------------------------------------------------------------------
with ui.card(fill=True, class_='border-light'):
ui.card_header("Script Counts")
@render_plotly
def chartScriptCounts():
df = charDf()
if df is None:
return None
scriptCounts = df.groupby('script')['count'].sum().reset_index()
scripts = scriptCounts['script'].unique().tolist()
scripts.sort()
fig = px.bar(scriptCounts,
x='script', y='count',
log_y=True,
text='count',
color='script',
labels={'count': 'Frequency',
'script': 'Script'},
category_orders={'script': scripts},
color_discrete_sequence=px.colors.qualitative.Alphabet)
fig.update_traces(texttemplate='%{text:,.0f}',
textposition='inside',
textangle=0)
fig.update_layout(height=500)
fig.update_xaxes(categoryorder='total descending')
return fig
# ----------------------------------------------------------------------
with ui.card(fill=True, class_='border-light'):
ui.card_header("Unicode Character Frequency wrt Script")
@render_plotly
def chartScripts():
df = charDf()
if df is None:
return None
SCRIPTS: list[str] = (df['script']
.dropna()
.sort_values()
.unique()
.tolist())
fig = px.bar(df,
y='unicode', x='count',
orientation='h',
title='Unicode Character Frequency',
log_x=True,
text='count',
hover_data=['unicode', 'script', 'block', 'category'],
color='script',
labels={
'unicode': 'Unicode',
'count': 'Frequency',
'category': 'Category',
'block': 'Block',
'script': 'Script'},
category_orders={'script': SCRIPTS},
color_discrete_sequence=px.colors.qualitative.Alphabet)
fig.update_traces(texttemplate='%{text:,.0f}',
textposition='inside',
textangle=0)
fig.update_yaxes(categoryorder='category descending')
fig.update_layout(height=len(df)*24)
return fig
# Normalized CDF -----------------------------------------------------------
with ui.nav_panel("Normalized CFD"):
# ----------------------------------------------------------------------
with ui.card(fill=True, class_='border-light'):
ui.card_header('Normalized Cumulative Frequency Distribution')
@render_plotly
def chartEcdf():
df = charDf()
if df is None:
return None
df = df.sort_values(by='count', ascending=True)
# Calculate cumulative counts
df['CumCount'] = df['count'].cumsum()
total = df['count'].sum()
df['nCumFreq'] = df['CumCount'] / total
# Plot using Plotly Express with step-line
fig = px.line(df,
y='unicode', x='nCumFreq',
labels={'Element': 'Element',
'CumCount': 'Cumulative Count',
'nCumFreq': 'Normalized Cumulative Frequency'},
line_shape='vh')
fig.update_xaxes(range=[0, 1.1])
fig.update_layout(height=len(df)*24)
for percentile in [round(i * 0.1, 1) for i in range(0, 11, 2)]:
fig.add_hline(y=percentile*len(df)-1,
line=dict(color='green', dash='dot', width=2),
annotation_text=f'{percentile*100}%')
fig.add_vline(x=percentile,
line=dict(color='red', dash='dash', width=1),
annotation_text=f'{percentile*100}%')
return fig
# Table Panel --------------------------------------------------------------
with ui.nav_panel("Table"):
# ----------------------------------------------------------------------
with ui.card(fill=True, class_='border-light'):
@render.table(classes='table table-hover')
def tablePanel():
return charDf()
# Dataframe Panel ----------------------------------------------------------
with ui.nav_panel("Data frame"):
# ----------------------------------------------------------------------
with ui.card(fill=True, class_='border-light'):
@render.data_frame
def dataframePanel():
df = charDf()
if df is None:
return None
return render.DataTable(df, selection_mode="rows")