File size: 27,857 Bytes
20e2df3
 
 
dbf2202
 
 
 
 
 
 
31aaa5b
 
 
 
dbf2202
31aaa5b
1af42bd
e014d5f
dbf2202
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20e2df3
 
81665dc
20e2df3
81665dc
20e2df3
 
 
 
 
 
 
 
 
 
 
 
 
81665dc
20e2df3
81665dc
20e2df3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81665dc
20e2df3
81665dc
20e2df3
 
 
 
 
 
 
 
 
 
 
 
 
 
81665dc
20e2df3
81665dc
20e2df3
 
3b42ed4
20e2df3
 
 
 
 
 
 
 
 
fce1e26
20e2df3
 
 
 
 
3b42ed4
 
20e2df3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b42ed4
20e2df3
e94b0ef
 
 
 
 
 
20e2df3
 
 
 
 
81665dc
20e2df3
 
 
 
 
 
3b42ed4
 
 
 
20e2df3
 
 
 
e94b0ef
 
 
20e2df3
 
 
 
 
 
 
 
 
 
 
 
 
81665dc
20e2df3
 
81665dc
 
 
 
 
 
 
 
 
 
 
46d5ba7
 
81665dc
 
 
 
 
 
 
46d5ba7
81665dc
 
 
 
 
 
 
 
46d5ba7
81665dc
 
 
 
 
 
20e2df3
 
81665dc
 
20e2df3
 
 
 
 
 
 
 
 
 
 
81665dc
20e2df3
 
 
 
 
 
 
 
 
 
 
 
81665dc
20e2df3
 
 
 
 
 
 
 
 
 
 
 
81665dc
20e2df3
 
 
 
 
 
 
 
 
 
 
 
81665dc
20e2df3
81665dc
20e2df3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b42ed4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92f29ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b42ed4
 
a19ab66
b52186e
 
 
 
a19ab66
 
 
 
b52186e
 
a19ab66
b52186e
 
 
a19ab66
b52186e
 
 
 
 
 
 
 
 
 
 
 
 
 
46d5ba7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a19ab66
46d5ba7
 
 
 
a19ab66
 
 
 
 
 
46d5ba7
 
 
 
 
635a691
 
 
 
 
 
 
 
 
46d5ba7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb60b0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
635a691
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c517770
635a691
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb60b0f
 
54f47d8
bb60b0f
 
 
 
 
 
 
54f47d8
bb60b0f
54f47d8
bb60b0f
54f47d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb60b0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54f47d8
bb60b0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0c7f01
7ad874f
 
 
 
0ab938c
 
7ad874f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0c7f01
7ad874f
0ab938c
 
 
 
 
 
 
7ad874f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ab938c
 
7ad874f
 
0ab938c
 
7ad874f
 
e0c7f01
 
 
 
 
 
 
 
 
 
 
 
 
7ad874f
 
 
 
 
 
 
 
e0c7f01
7ad874f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0c7f01
 
 
 
 
 
 
 
 
 
 
 
0ab938c
 
7ad874f
 
 
 
 
 
 
689111f
7ad874f
0ab938c
 
 
 
 
 
 
 
7ad874f
0ab938c
e1929e2
7ad874f
 
 
 
 
 
 
 
689111f
7ad874f
0ab938c
 
 
 
 
 
7ad874f
0ab938c
e1929e2
7ad874f
 
 
0ab938c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
689111f
0ab938c
 
 
 
e1929e2
0ab938c
 
 
 
7ad874f
 
 
 
 
 
 
 
 
f415f22
7ad874f
 
0ab938c
7ad874f
 
 
 
 
 
 
 
e0c7f01
 
 
 
 
7ad874f
 
 
 
f415f22
e0c7f01
7ad874f
 
0ab938c
7ad874f
 
 
 
 
 
e0c7f01
7ad874f
e0c7f01
7ad874f
e0c7f01
 
 
 
 
 
 
 
 
 
 
 
7ad874f
 
 
 
 
 
 
 
 
0ab938c
7ad874f
 
 
 
20e2df3
 
 
 
ad66bd8
20e2df3
 
 
 
 
 
 
 
81665dc
20e2df3
 
 
81665dc
20e2df3
 
 
fe9f23b
 
 
 
 
 
 
 
 
 
 
bb60b0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20e2df3
81665dc
 
20e2df3
 
 
81665dc
20e2df3
 
81665dc
20e2df3
 
 
92f29ca
 
 
 
 
 
 
49fcfcf
 
 
 
 
 
 
 
92f29ca
 
3b42ed4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60ad99d
 
e66554e
60ad99d
a19ab66
 
60ad99d
 
46d5ba7
 
 
 
a7e228d
46d5ba7
 
a7e228d
46d5ba7
 
20e2df3
81665dc
20e2df3
 
3b42ed4
e94b0ef
20e2df3
 
 
3b42ed4
81665dc
 
 
 
 
 
46d5ba7
 
 
81665dc
 
 
46d5ba7
20e2df3
 
 
 
b52186e
 
 
a19ab66
 
 
b52186e
a19ab66
 
b52186e
 
 
20e2df3
81665dc
 
f20b78d
20e2df3
 
 
f20b78d
20e2df3
 
 
e94b0ef
635a691
 
 
 
 
 
 
 
 
 
20e2df3
e94b0ef
81665dc
54f47d8
20e2df3
bb60b0f
20e2df3
bb60b0f
 
20e2df3
54f47d8
20e2df3
 
 
7ad874f
493f7d2
 
 
 
 
 
 
 
 
 
 
 
 
e4f1664
 
635a691
e0c7f01
7ad874f
 
bb60b0f
0ab938c
bb60b0f
0ab938c
 
 
 
7ad874f
e0c7f01
7ad874f
 
 
20e2df3
 
 
10cc95e
20e2df3
 
 
 
 
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269

print("START")

import gradio as gr

# // ---------------------------------------------- // #

def f_ipcheck(request: gr.Request):
	try:
		import datetime
		import tzlocal
		tz1 = tzlocal.get_localzone()
		s1 = "{0}".format(tz1)
		
		d1 = {}
		d1["datetime"] = {}
		d1["datetime"][s1] = datetime.datetime.now().replace(tzinfo=tz1).strftime("%Y/%m/%dT%H:%M:%S%z")
		#d1["client.tcpip"] = dict(request.client)
		d1["client.http.header"] = dict(request.headers)
		
		import json
		result = json.dumps(d1, ensure_ascii=False, indent=2)
		pass
	except Exception as e:
		result = "ERROR: {0}".format(e)
		
		import traceback
		print(traceback.format_exc())
		pass
	
	return result
	pass

# // ---------------------------------------------- // #

def f_base64_encode(e_text1):
	try:
		s1 = e_text1
		import base64
		result = base64.b64encode(s1.encode()).decode()
		pass
	except Exception as e:
		result = "ERROR: {0}".format(e)
		
		import traceback
		print(traceback.format_exc())
		pass
	
	return result
	pass

def f_base64_decode(e_text1):
	try:
		s1 = e_text1
		import base64
		result = base64.b64decode(s1.encode()).decode()
		pass
	except Exception as e:
		result = "ERROR: {0}".format(e)
		
		import traceback
		print(traceback.format_exc())
		pass
	
	return result
	pass

# // ---------------------------------------------- // #

def f_json2jsonlines(e_text1):
	try:
		s1 = e_text1
		import json
		d1 = json.loads(s1)
		result = json.dumps(d1, ensure_ascii=False)
		pass
	except Exception as e:
		result = "ERROR: {0}".format(e)
		
		import traceback
		print(traceback.format_exc())
		pass
	
	return result
	pass

def f_jsonlines2json(e_text1):
	try:
		s1 = e_text1
		l1 = s1.splitlines()
		l2 = []
		index1 = 0
		for i1 in l1:
			s2 = None
			
			try:
				import json
				d1 = json.loads(i1)
				s2 = json.dumps(d1, ensure_ascii=False, indent=2)
				pass
			except Exception as e:
				s2 = "[LINE {1:,}] ERROR: {0}\n{2}\n".format(e, index1, i1)
				
				import traceback
				print(traceback.format_exc())
				pass
			
			index1 += 1
			
			l2.append(s2)
			pass
		
		result = "\n".join(l2)
		pass
	except Exception as e:
		result = "ERROR: {0}".format(e)
		
		import traceback
		print(traceback.format_exc())
		pass
	
	return result
	pass


# // ---------------------------------------------- // #

def f_sort(e_text1, e_radio1, e_checkbox1, e_checkbox2, e_checkbox3):
	try:
		b_sort2 = True
		
		if e_radio1 == "並べ替えしない":
			b_sort2 = False
			pass
		
		b_sort1 = False
		if e_radio1 == "降順":
			b_sort1 = True
			pass
		
		s1 = e_text1
		l1 = s1.splitlines()
		
		if e_checkbox2:
			l1 = map(lambda x: x.strip(), l1)
			pass
		
		if e_checkbox3:
			l1 = list(filter(lambda x: len(x.strip()) > 0, l1))
			pass
		
		if e_checkbox1:
			l1 = list(set(l1))
			pass
		
		if b_sort2:
			l1.sort(reverse=b_sort1)
			pass
		
		result = "\n".join(l1)
		pass
	except Exception as e:
		result = "ERROR: {0}".format(e)
		
		import traceback
		print(traceback.format_exc())
		pass
	
	return result
	pass


# // ---------------------------------------------- // #

def f_wordcount(e_text1):
	try:
		b_sort1 = False
		if e_radio1 == "降順":
			b_sort1 = True
			pass
		
		s1 = e_text1
		l1 = s1.splitlines()
		
		result = {}
		result["output1"] = "{0}".format(len(l1))
		result["output2"] = "{0}".format(len(s1))
		
		l2 = []
		for i1 in l1:
			s2 = "{0}".format(len(i1))
			l2.append(s2)
			pass
		
		result["output3"] = "\n".join(l2)
		pass
	except Exception as e:
		result = "ERROR: {0}".format(e)
		
		import traceback
		print(traceback.format_exc())
		pass
	
	return (result["output1"], result["output2"], result["output3"])
	pass


# // ---------------------------------------------- // #

def f_hash(e_text1):
	result = {}
	
	s1 = e_text1
	
	try:
		import hashlib
		
		s2 = hashlib.md5(s1.encode()).hexdigest()
		pass
	except Exception as e:
		s2 = "ERROR: {0}".format(e)
		
		import traceback
		print(traceback.format_exc())
		pass
	result["output1"] = s2
	
	try:
		import hashlib
		
		s2 = hashlib.sha1(s1.encode()).hexdigest()
		pass
	except Exception as e:
		s2 = "ERROR: {0}".format(e)
		
		import traceback
		print(traceback.format_exc())
		pass
	result["output2"] = s2
	
	try:
		import hashlib
		
		s2 = hashlib.sha256(s1.encode()).hexdigest()
		pass
	except Exception as e:
		s2 = "ERROR: {0}".format(e)
		
		import traceback
		print(traceback.format_exc())
		pass
	result["output3"] = s2
	
	try:
		import hashlib
		
		s2 = hashlib.sha512(s1.encode()).hexdigest()
		pass
	except Exception as e:
		s2 = "ERROR: {0}".format(e)
		
		import traceback
		print(traceback.format_exc())
		pass
	result["output4"] = s2
	
	return (result["output1"], result["output2"], result["output3"], result["output4"])
	pass

# // ---------------------------------------------- // #

def f_generatepassword(e_number1, e_checkbox1, e_checkbox2, e_checkbox3, e_checkbox4, e_slider1):
	
	try:
		l2 = []
		
		import string
		
		n1 = int(e_number1)
		n2 = int(e_slider1)
		
		characters = ""
		
		if e_checkbox1:
			characters += string.digits
			pass
		
		if e_checkbox2:
			characters += string.ascii_lowercase
			pass
		
		if e_checkbox3:
			characters += string.ascii_uppercase
			pass
		
		if e_checkbox4:
			characters += "!#$%&()*+,-./:;<=>?@[]^_{|}~."
			pass
		
		import random
		for i1 in range(n2):
			s2 = ""
			if len(characters) > 0:
				l1 = [random.choice(characters) for i2 in range(n1)]
				s2 = "".join(l1)
				pass
			
			l2.append(s2)
			pass
		
		result = "\n".join(l2)
		pass
	except Exception as e:
		result = "ERROR: {0}".format(e)
		
		import traceback
		print(traceback.format_exc())
		pass
	
	return result
	pass

# // ---------------------------------------------- // #

def f_formathtml(e_text1):
	
	try:
		s1 = e_text1
		
		from lxml import etree, html
		tree1 = html.fromstring(s1)
		result = etree.tostring(tree1, encoding="utf-8", pretty_print=True).strip().decode()
		pass
	except Exception as e:
		result = "ERROR: {0}".format(e)
		
		import traceback
		print(traceback.format_exc())
		pass
	
	return result
	pass

# // ---------------------------------------------- // #

def f_formatxml(e_text1):
	
	try:
		s1 = e_text1.encode()
		
		from lxml import etree
		parser = etree.XMLParser(remove_blank_text=True)
		tree1 = etree.fromstring(s1, parser=parser)
		result = etree.tostring(tree1, encoding="utf-8", pretty_print=True, xml_declaration=True).strip().decode()
		pass
	except Exception as e:
		result = "ERROR: {0}".format(e)
		
		import traceback
		print(traceback.format_exc())
		pass
	
	return result
	pass

# // ---------------------------------------------- // #

def f_formatjavascript(e_text1):
	
	try:
		s1 = e_text1
		
		import jsbeautifier
		opt1 = jsbeautifier.default_options()
		opt1.indent_size = 4
		result = jsbeautifier.beautify(s1, opt1)
		pass
	except Exception as e:
		result = "ERROR: {0}".format(e)
		
		import traceback
		print(traceback.format_exc())
		pass
	
	return result
	pass

# // ---------------------------------------------- // #

def f_formatunicode(e_text1, e_radio1, e_checkbox1, e_checkbox2, e_checkbox3):
	
	try:
		s1 = e_text1
		
		b1 = e_checkbox1
		b2 = e_checkbox2
		b3 = e_checkbox3
		
		if e_radio1 == "全角に変換":
			import jaconv
			result = jaconv.h2z(s1, kana=b1, digit=b3, ascii=b2)
			pass
		else:
			import jaconv
			result = jaconv.z2h(s1, kana=b1, digit=b3, ascii=b2)
			pass
		pass
	except Exception as e:
		result = "ERROR: {0}".format(e)
		
		import traceback
		print(traceback.format_exc())
		pass
	
	return result
	pass

# // ---------------------------------------------- // #

def f_formattext_en(e_text1):
	result = {}
	result["output1"] = ""
	result["output2"] = ""
	
	try:
		s1 = e_text1
		s1 = " ".join(s1.split())
		
		import nltk
		from nltk.tokenize import sent_tokenize, word_tokenize
		
		nltk.data.path.append("/opt/tmp_nltk")
		nltk.download("punkt")
		
		l1 = []
		for token in sent_tokenize(s1):
			l1.append(token.strip())
			pass
		
		s2 = "\n".join(l1)
		
		import urllib.parse
		s3 = "https://translate.google.co.jp/?sl=auto&text={0}".format(urllib.parse.quote_plus(s2[:3000]))
		
		result["output1"] = s2
		result["output2"] = s3
		pass
	except Exception as e:
		result["output1"] = "ERROR: {0}".format(e)
		
		import traceback
		print(traceback.format_exc())
		pass
	
	return (result["output1"], result["output2"])
	pass

# // ---------------------------------------------- // #

def f_formattext_ja(e_text1, e_checkbox1):
	
	try:
		s1 = e_text1
		
		if e_checkbox1:
			import re
			ss1 = re.split(r"\r|\n|\r\n", s1)
			s1 = "".join(ss1)
			pass
		
		import bunkai
		from bunkai import Bunkai
		bunkai = Bunkai()
		
		l1 = []
		
		import re
		l2 = re.split(r"\r|\n|\r\n|。", s1)
		for i2 in l2:
			i2 = i2.strip()
			for token in bunkai(i2):
				l1.append(token.strip())
				print("TOKEN: {0}".format(len(l1)))
				pass
			pass
		
		result = "\n".join(l1)
		pass
	except Exception as e:
		result = "ERROR: {0}".format(e)
		
		import traceback
		print(traceback.format_exc())
		pass
	
	return result
	pass

# // ---------------------------------------------- // #

def f_janome(e_text1):
	result = {}
	result["output1"] = ""
	result["output2"] = ""
	try:
		b_sort1 = False
		if e_radio1 == "降順":
			b_sort1 = True
			pass
		
		s1 = e_text1
		s1 = " ".join(s1.split())[:10000]
		
		l2 = []
		l3 = []
		
		from janome.tokenizer import Tokenizer
		tokenizer = Tokenizer()
		for token in tokenizer.tokenize(s1):
			d1 = {}
			d1["token"] = "{0}".format(token.surface)
			d1["label"] = "{0}".format(token.part_of_speech.split(",")[0])
			
			s3 = "{0}".format(token.reading)
			
			if s3 == "*":
				s3 = d1["token"]
				pass
			
			d1["reading"] = s3
			
			import json
			s2 = json.dumps(d1, ensure_ascii=False)
			
			l2.append(s2)
			l3.append(d1["token"])
			pass
		
		result["output1"] = "\n".join(l2)
		result["output2"] = " ".join(l3)
		pass
	except Exception as e:
		result["output1"] = "ERROR: {0}".format(e)
		
		import traceback
		print(traceback.format_exc())
		pass
	
	return (result["output1"], result["output2"])
	pass

# // ---------------------------------------------- // #

def f_gensim(e_text1, e_checkbox1):
	result = {}
	result["output1"] = ""
	result["output2"] = ""
	try:
		
		s1 = e_text1
		
		documents = []
		
		list_labels = []
		list1 = []
		index1 = 0
		for i1 in s1.splitlines()[:1000]:
			s1 = i1.strip()
			
			if e_checkbox1:
				ss1 = s1.split()
				s2 = ss1[0]
				s1 = " ".join(ss1[1:])
				pass
			else:
				s2 = "{0:08}".format(index1)
				s1 = " ".join(s1.split())
				pass
			
			d1, s1 = f_janome(s1)
			
			import gensim
			document1 = gensim.models.doc2vec.TaggedDocument(words=s1.split(), tags=[s2])
			documents.append(document1)
			
			list1.append(s1)
			list_labels.append(s2)
			
			index1 += 1
			pass
		
		import gensim
		model = gensim.models.doc2vec.Doc2Vec(documents=documents, dm=1, vector_size=512, window=10, min_count=10, workers=64, epochs=100, compute_loss=True)
		
		vectors = []
		for i1 in range(len(list1)):
			s2 = list_labels[i1]
			s1 = model.docvecs[s2]
			vectors.append(s1)
			pass
		
		list_text1 = []
		list_vector1 = []
		
		if True:
			for i1 in range(len(list1)):
				import io
				buf = io.StringIO()
				
				import numpy
				numpy.savetxt(buf, vectors[i1])
				
				s2 = "\t".join(buf.getvalue().split())
				d1 = {}
				d1["text"] = list1[i1]
				d1["vector"] = s2
				
				import json
				s3 = json.dumps(d1, ensure_ascii=False)
				list_vector1.append(s3)
				
				s4 = "{0}\t{1}".format(list_labels[i1], s2)
				list_text1.append(s4)
				pass
			pass
		
		if len(list_vector1) > 0:
			result["output1"] = "\n".join(list_vector1)
			pass
		
		if len(list_text1) > 0:
			result["output2"] = "\n".join(list_text1)
			pass
		pass
	except Exception as e:
		result["output1"] = "ERROR: {0}".format(e)
		
		import traceback
		print(traceback.format_exc())
		pass
	
	return (result["output1"], result["output2"])
	pass

# // ---------------------------------------------- // #

global_google_encoder = None

def f_googlesentenceencoder(e_text1, e_checkbox1):
	result = {}
	result["output1"] = ""
	result["output2"] = ""
	try:
		
		s1 = e_text1
		
		list_labels = []
		list1 = []
		index1 = 0
		for i1 in s1.splitlines()[:1000]:
			s1 = i1.strip()
			
			if e_checkbox1:
				ss1 = s1.split()
				s2 = ss1[0]
				s1 = " ".join(ss1[1:])
				pass
			else:
				s2 = "{0:08}".format(index1)
				s1 = " ".join(s1.split())
				pass
			
			list1.append(s1)
			list_labels.append(s2)
			
			index1 += 1
			pass
		
		global global_google_encoder
		if global_google_encoder is None:
			import tensorflow_hub
			global_google_encoder = tensorflow_hub.load("https://tfhub.dev/google/universal-sentence-encoder/4")
			pass
		
		import tensorflow
		import tensorflow_text
		vectors = None
		
		with tensorflow.device("/CPU:0"):
			vectors = global_google_encoder(list1)
			pass
		
		list_text1 = []
		list_vector1 = []
		
		for i1 in range(len(list1)):
			import io
			buf = io.StringIO()
			
			import numpy
			numpy.savetxt(buf, vectors[i1])
			
			s2 = "\t".join(buf.getvalue().split())
			d1 = {}
			d1["text"] = list1[i1]
			d1["vector"] = s2
			
			import json
			s3 = json.dumps(d1, ensure_ascii=False)
			list_vector1.append(s3)
			
			s4 = "{0}\t{1}".format(list_labels[i1], s2)
			list_text1.append(s4)
			pass
		
		if len(list_vector1) > 0:
			result["output1"] = "\n".join(list_vector1)
			pass
		
		if len(list_text1) > 0:
			result["output2"] = "\n".join(list_text1)
			pass
		pass
	except Exception as e:
		result["output1"] = "ERROR: {0}".format(e)
		
		import traceback
		print(traceback.format_exc())
		pass
	
	return (result["output1"], result["output2"])
	pass
# // ---------------------------------------------- // #

def f_tsne(e_text1, e_checkbox1, e_radio1, e_radio2, e_checkbox2):
	result = {}
	result["output1"] = e_text1
	result["output2"] = None
	result["output3"] = ""
	result["output4"] = ""
	result["output5"] = None
	
	try:
		s1 = e_text1
		
		n_dimension = 3
		
		if e_radio1 == "3D":
			n_dimension = 3
			pass
		else:
			n_dimension = 2
			pass
		
		list_vectors = []
		list_labels = []
		list_annotations = []
		
		list1 = []
		index1 = 0
		for i1 in s1.splitlines():
			s1 = "{0:08} {1}".format(index1, i1.strip())
			list1.append(s1)
			index1 += 1
			pass
		
		import random
		random.shuffle(list1)
		
		index1 = 0
		for i1 in list1[:1000]:
			i1 = list1[index1].strip()
			
			if len(i1) == 0:
				continue
				pass
			
			ss1 = i1.split()
			
			s1 = None
			s2 = None
			
			if e_checkbox1:
				s1 = ss1[1]
				s2 = " ".join(ss1[2:])
				pass
			else:
				s1 = ss1[0]
				s2 = " ".join(ss1[1:])
				pass
			
			d2 = {
				"showarrow": False,
				"x": 0,
				"y": 0,
				"z": 0,
				"text": s1,
				"xanchor": "left",
				"xshift": 10,
				"opacity": 0.7
				
			}
			
			
			import io
			buf = io.StringIO(s2)
			
			import numpy
			l1 = numpy.loadtxt(buf, delimiter=" ")
			
			list_labels.append(s1)
			list_vectors.append(l1)
			list_annotations.append(d2)
			
			index1 += 1
			pass
		
		import numpy
		X = numpy.array(list_vectors)
		
		tsne = None
		
		if e_radio2 == "TSNE":
			from sklearn.manifold import TSNE
			tsne = TSNE(
				n_components=n_dimension,
				init='random',
				method='barnes_hut',
				n_iter=1000,
				verbose=2
			).fit_transform(X)
			pass
		else:
			import umap
			from scipy.sparse.csgraph import connected_components
			
			tsne = umap.UMAP(
				n_components=n_dimension,
				init='random',
				verbose=2
			).fit_transform(X)
			pass
		
		
		index1 = 0
		for i in tsne:
			list_annotations[index1]["x"] = i[0]
			list_annotations[index1]["y"] = i[1]
			if len(i) == 3:
				list_annotations[index1]["z"] = i[2]
				pass
			index1 += 1
			pass

		
		list_vectors2 = []
		
		if n_dimension == 3:
			x = tsne[:,0]
			y = tsne[:,1]
			z = tsne[:,2]
			
			l1 = []
			for i1 in range(len(x)):
				s1 = "{0}\t{1:.3f}\t{2:.3f}\t{3:.3f}".format(list_labels[i1], x[i1], y[i1], z[i1])
				l1.append(s1)
				
				l2 = []
				l2.append(x[i1].astype(int))
				l2.append(y[i1].astype(int))
				l2.append(z[i1].astype(int))
				
				list_vectors2.append(l2)
				
				pass
			l1.sort()
			l1.insert(0, "\t".join(["line", "x", "y", "z"]))
			result["output3"] = "\n".join(l1)
			pass
		else:
			x = tsne[:,0]
			y = tsne[:,1]
			
			l1 = []
			for i1 in range(len(x)):
				s1 = "{0}\t{1:.3f}\t{2:.3f}".format(list_labels[i1], x[i1], y[i1])
				l1.append(s1)
				
				l2 = []
				l2.append(x[i1].astype(int))
				l2.append(y[i1].astype(int))
				list_vectors2.append(l2)
				
				pass
			l1.sort()
			l1.insert(0, "\t".join(["line", "x", "y"]))
			result["output3"] = "\n".join(l1)
			pass
		
		X = numpy.array(list_vectors2)
		
		from sklearn.cluster import KMeans
		
		list_squarederrors = []
		for i1 in range(1, 32):
			model = KMeans(n_clusters=i1, init="k-means++", n_init="auto")
			model.fit(X)
			list_squarederrors.append(model.inertia_)
			pass
		
		n_clusters = 32
		n1 = (list_squarederrors[0] - list_squarederrors[-1]) * 0.06
		n2 = (list_squarederrors[0] - list_squarederrors[-1]) * 0.5
		
		index1 = 1
		for i1 in list_squarederrors:
			if n1 > i1:
				n_clusters = index1
				break
				pass
			index1 += 1
			pass
		
		import matplotlib.pyplot as plt
		plt.cla()
		plt.clf()
		plt.plot(range(1, 32), list_squarederrors, marker='o')
		
		plt.vlines(n_clusters, 0, list_squarederrors[0], color="r", linestyles="dotted")
		plt.text(n_clusters, n2, "{0}".format(n_clusters), fontsize=32, verticalalignment="bottom", horizontalalignment="center")
		
		plt.xlabel("number of clusters")
		plt.ylabel("sum of squared errors")
		plt.subplots_adjust(left=0.16, right=0.95, bottom=0.12, top=0.95)
		
		result["output5"] = plt
		
		model = KMeans(n_clusters=n_clusters, init="k-means++", n_init="auto")
		model.fit(X)
		clusters = model.predict(X)
		
		list_cluster1 = []
		index1 = 0
		for i1 in clusters:
			s1 = "{0}\t{1}".format(list_labels[index1], i1)
			list_cluster1.append(s1)
			index1 += 1
			pass
		list_cluster1.sort()
		list_cluster1.insert(0, "\t".join(["line", "cluster"]))
		result["output4"] = "\n".join(list_cluster1)
		
		
		from sklearn import preprocessing
		import plotly.graph_objects as go
		trace1 = None
		if n_dimension == 3:
			trace1 = go.Scatter3d(
				x=tsne[:,0],
				y=tsne[:,1],
				z=tsne[:,2],
				text=list_labels,
				mode="markers",
				textfont_size=8,
				marker=dict(
					sizemode="diameter",
					color = preprocessing.LabelEncoder().fit_transform(clusters),
					colorscale = "Portland",
					line=dict(color="rgb(255, 255, 255)"),
					opacity=0.9,
					size=2
				)
			)
			pass
		else:
			mode = "markers"
			if e_checkbox2:
				mode = "markers+text"
				pass
				
			trace1 = go.Scatter(
				x=tsne[:,0],
				y=tsne[:,1],
				text=list_labels,
				textfont_size=8,
				mode=mode,
				marker=dict(
					sizemode="diameter",
					color = preprocessing.LabelEncoder().fit_transform(clusters),
					colorscale = "Portland",
					line=dict(color="rgb(255, 255, 255)"),
					opacity=0.9,
					size=10
				)
			)
			
			pass
		
		data=[trace1]
		layout=None
		
		if e_checkbox2 and n_dimension == 3:
			layout={
				"scene": {
					"annotations": list_annotations
				}
			}
			pass
		
		fig = go.Figure(data=data, layout=layout)
		
		result["output2"] = fig
		pass
	except Exception as e:
		result["output1"] = "ERROR: {0}".format(e)
		
		import traceback
		print(traceback.format_exc())
		pass
	
	return (result["output1"], result["output2"], result["output3"], result["output4"], result["output5"])
	pass

# // ---------------------------------------------- // #

with gr.Blocks() as sample01:
	gr.Markdown("aokitools")
	
	with gr.Tab("パスワード生成"):
		e_number1 = gr.Number(value=16, minimum=1, maximum=1024, label="桁数")
		
		e_checkbox1 = gr.Checkbox(value=True, label="数字を含める")
		e_checkbox2 = gr.Checkbox(value=True, label="小文字を含める")
		e_checkbox3 = gr.Checkbox(value=True, label="大文字を含める")
		e_checkbox4 = gr.Checkbox(value=False, label="記号を含める")
		
		e_slider1 = gr.Slider(value=100, minimum=100, maximum=10000, step=100, label="生成する数", interactive=True)
		
		e_text1 = gr.TextArea(show_label=True, label="TEXT")
		
		with gr.Row():
			e_button1 = gr.Button("生成")
			e_button1.click(f_generatepassword, inputs=[e_number1, e_checkbox1, e_checkbox2, e_checkbox3, e_checkbox4, e_slider1], outputs=[e_text1])
			pass
		pass
	
	with gr.Tab("IP確認"):
		e_text1 = gr.TextArea(show_label=True, label="TEXT")
		
		with gr.Row():
			e_button1 = gr.Button("確認")
			e_button1.click(f_ipcheck, inputs=[], outputs=[e_text1])
			pass
		
		sample01.load(f_ipcheck, inputs=[], outputs=[e_text1])
		pass
	
	with gr.Tab("hash"):
		e_text1 = gr.TextArea(show_label=True, label="TEXT")
		
		e_text2 = gr.Textbox(show_label=True, label="MD5")
		e_text3 = gr.Textbox(show_label=True, label="SHA1")
		e_text4 = gr.Textbox(show_label=True, label="SHA256")
		e_text5 = gr.Textbox(show_label=True, label="SHA512")
		
		with gr.Row():
			e_button1 = gr.Button("hashを計算")
			e_button1.click(f_hash, inputs=[e_text1], outputs=[e_text2, e_text3, e_text4, e_text5])
			pass
		pass
	
	
	with gr.Tab("base64"):
		e_text1 = gr.TextArea(show_label=True, label="TEXT")
		e_text2 = gr.TextArea(show_label=True, label="BASE64")
		
		with gr.Row():
			e_button1 = gr.Button("エンコード text >> base64")
			e_button1.click(f_base64_encode, inputs=[e_text1], outputs=[e_text2])
			
			e_button1 = gr.Button("デコード base64 >> text")
			e_button1.click(f_base64_decode, inputs=[e_text2], outputs=[e_text1])
			pass
		pass
	
	
	with gr.Tab("JSON整形"):
		e_text1 = gr.TextArea(show_label=True, label="JSON LINES")
		e_text2 = gr.TextArea(show_label=True, label="JSON")
		
		with gr.Row():
			e_button1 = gr.Button("JSON に変換")
			e_button1.click(f_jsonlines2json, inputs=[e_text1], outputs=[e_text2])
			
			e_button1 = gr.Button("JSON LINES に変換")
			e_button1.click(f_json2jsonlines, inputs=[e_text2], outputs=[e_text1])
			pass
		pass
	
	with gr.Tab("JavaScript整形"):
		e_text1 = gr.TextArea(show_label=True, label="JavaScript")
		
		with gr.Row():
			e_button1 = gr.Button("JavaScript整形")
			e_button1.click(f_formatjavascript, inputs=[e_text1], outputs=[e_text1])
			pass
		
		gr.Markdown(
			"""
			---
			1. JavaScript実行ツール
			- [https://jsfiddle.net/](https://jsfiddle.net/)
			""")
		
		pass
	
	with gr.Tab("HTML整形"):
		e_text1 = gr.TextArea(show_label=True, label="HTML")
		
		with gr.Row():
			e_button1 = gr.Button("HTML整形")
			e_button1.click(f_formathtml, inputs=[e_text1], outputs=[e_text1])
			pass
		pass
	
	with gr.Tab("XML整形"):
		e_text1 = gr.TextArea(show_label=True, label="XML")
		
		with gr.Row():
			e_button1 = gr.Button("XML整形")
			e_button1.click(f_formatxml, inputs=[e_text1], outputs=[e_text1])
			pass
		pass
	
	with gr.Tab("日本語整形"):
		e_text1 = gr.TextArea(show_label=True, label="TEXT")
		e_checkbox1 = gr.Checkbox(value=True, label="改行を除去して整形")
		
		e_button1 = gr.Button("日本語整形")
		e_button1.click(f_formattext_ja, inputs=[e_text1, e_checkbox1], outputs=[e_text1])
		pass
	
	with gr.Tab("英語整形"):
		e_text1 = gr.TextArea(show_label=True, label="TEXT")
		e_button1 = gr.Button("英語整形")
		e_text2 = gr.Textbox(show_label=True, label="Google Translate")
		#e_button2 = gr.Button("翻訳")
		
		e_button1.click(f_formattext_en, inputs=[e_text1], outputs=[e_text1, e_text2])
		#e_button2.click(fn=None, inputs=[e_text2], js="(e_text2) => {window.open(e_text2, '_blank', 'noreferrer')}")
		pass
	
	with gr.Tab("並べ替え/重複排除"):
		e_text1 = gr.TextArea(show_label=True, label="TEXT")
		e_checkbox1 = gr.Checkbox(label="重複排除")
		e_checkbox2 = gr.Checkbox(label="前後の空白を除去")
		e_checkbox3 = gr.Checkbox(label="空行除去")
		e_radio1 = gr.Radio(["並べ替えしない", "昇順", "降順"], value="並べ替えしない", label="sort order")
		
		with gr.Row():
			e_button1 = gr.Button("並べ替え")
			e_button1.click(f_sort, inputs=[e_text1, e_radio1, e_checkbox1, e_checkbox2, e_checkbox3], outputs=[e_text1])
			pass
		pass
	
	
	with gr.Tab("文字数カウント"):
		e_text1 = gr.TextArea(show_label=True, label="TEXT")
		e_text2 = gr.Textbox(show_label=True, label="行数")
		e_text3 = gr.Textbox(show_label=True, label="全体の文字数(改行コードを含む)")
		e_text4 = gr.Textbox(show_label=True, label="1行ごとの文字数(改行コードを除く)")
		
		with gr.Row():
			e_button1 = gr.Button("文字数カウント")
			e_button1.click(f_wordcount, inputs=[e_text1], outputs=[e_text2, e_text3, e_text4])
			pass
		pass
	
	
	with gr.Tab("全角半角変換"):
		e_text1 = gr.TextArea(show_label=True, label="TEXT")
		e_radio1 = gr.Radio(["半角に変換", "全角に変換"], value="全角に変換", label="type")
		e_checkbox1 = gr.Checkbox(value=True, label="カタカナを変換")
		e_checkbox2 = gr.Checkbox(value=True, label="アルファベットを変換")
		e_checkbox3 = gr.Checkbox(value=True, label="数字を変換")
		
		e_button1 = gr.Button("変換")
		e_button1.click(f_formatunicode, inputs=[e_text1, e_radio1, e_checkbox1, e_checkbox2, e_checkbox3], outputs=[e_text1])
		pass
	
	
	with gr.Tab("形態素解析"):
		e_text1 = gr.TextArea(show_label=True, label="TEXT")
		e_text2 = gr.TextArea(show_label=True, label="JSON")
		e_text3 = gr.TextArea(show_label=True, label="分かち書き")
		
		with gr.Row():
			e_button1 = gr.Button("形態素解析")
			e_button1.click(f_janome, inputs=[e_text1], outputs=[e_text2, e_text3])
			pass
		pass
	
	with gr.Tab("Doc2Vec"):
		e_text1 = gr.TextArea(show_label=True, label="TEXT")
		e_checkbox1 = gr.Checkbox(label="1カラム目をラベルとして設定")
		
		e_button1 = gr.Button("変換")
		
		e_text2 = gr.TextArea(show_label=True, label="JSON LINES")
		e_text3 = gr.TextArea(show_label=True, label="ベクトル")
		
		e_button1.click(f_gensim, inputs=[e_text1, e_checkbox1], outputs=[e_text2, e_text3])
		pass
	
	with gr.Tab("GoogleSentenceEncoder"):
		e_text1 = gr.TextArea(show_label=True, label="TEXT")
		e_checkbox1 = gr.Checkbox(label="1カラム目をラベルとして設定")
		
		e_button1 = gr.Button("変換")
		
		e_text2 = gr.TextArea(show_label=True, label="JSON LINES")
		e_text3 = gr.TextArea(show_label=True, label="ベクトル")
		
		e_button1.click(f_googlesentenceencoder, inputs=[e_text1, e_checkbox1], outputs=[e_text2, e_text3])
		pass
	
	
	with gr.Tab("次元削減"):
		s1 = ""
		
		if False:
			import os
			path1 = "{0}/dataset/sample01.txt".format(os.path.dirname(__file__))
			f1 = open(path1, "r", encoding="utf-8")
			l1 = f1.read().split(r"(\r\n|\r|\n)+")
			f1.close()
			
			import random
			random.shuffle(l1)
			s1 = "\n".join(l1[:300])
			pass
		
		e_text1 = gr.TextArea(s1, show_label=True, label="入力ベクトル(空白区切り,31行以上~1000行まで)")
		e_checkbox1 = gr.Checkbox(value=True, label="1カラム目をラベルとして設定")
		e_checkbox2 = gr.Checkbox(label="ラベルをプロットする")
		e_radio1 = gr.Radio(["2D", "3D"], value="2D", label="dimension")
		e_radio2 = gr.Radio(["TSNE", "UMAP"], value="TSNE", label="algorithm")
		
		e_button1 = gr.Button("プロット")
		
		e_plot1 = gr.Plot(show_label=True, label="プロット")
		e_plot2 = gr.Plot(show_label=True, label="クラスタ数の推定")
		e_text2 = gr.TextArea(show_label=True, label="次元削減後のベクトル")
		e_text3 = gr.TextArea(show_label=True, label="クラスタリング結果")
		
		e_button1.click(f_tsne, inputs=[e_text1, e_checkbox1, e_radio1, e_radio2, e_checkbox2], outputs=[e_text1, e_plot1, e_text2, e_text3, e_plot2])
		pass
	
	
	pass

sample01.queue()
sample01.launch(share=False)

# // ---------------------------------------------- // #

print("END")