File size: 103,265 Bytes
d6814f1
 
 
 
 
 
 
 
 
 
c99406b
d6814f1
 
 
c99406b
1df6810
d6814f1
 
 
cf8b7b0
e41b021
1df6810
d6814f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79f2daf
d6814f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79f2daf
d3caf4c
 
d6814f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3caf4c
 
d6814f1
 
 
 
 
 
 
d3caf4c
 
d6814f1
d3caf4c
 
d6814f1
 
 
 
d3caf4c
 
d6814f1
 
 
 
 
 
 
d3caf4c
 
d6814f1
 
 
 
 
d3caf4c
 
 
d6814f1
 
 
 
 
 
 
d3caf4c
 
d6814f1
 
 
 
 
d3caf4c
 
d6814f1
 
 
 
 
 
d3caf4c
 
d6814f1
 
 
 
 
d3caf4c
 
d6814f1
 
 
 
 
d3caf4c
 
d6814f1
 
 
 
 
 
 
d3caf4c
 
d6814f1
 
 
 
 
 
 
d3caf4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6814f1
d3caf4c
 
 
 
 
 
 
 
 
 
 
 
 
5eb15fa
 
d6814f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5eb15fa
 
d6814f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c99406b
d6814f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29204f7
d6814f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59177aa
9a48a94
 
 
71c328b
 
d6814f1
 
71c328b
d3caf4c
d6814f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15955aa
d6814f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15955aa
 
 
 
 
d6814f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b763b1
d6814f1
 
4b763b1
d6814f1
 
 
 
 
4b763b1
d6814f1
 
 
 
4b763b1
d6814f1
 
 
 
 
 
4b763b1
 
 
 
d6814f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b763b1
 
d6814f1
 
 
 
 
 
4b763b1
d6814f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b763b1
 
d6814f1
4b763b1
 
d6814f1
cf8b7b0
d6814f1
79f2daf
d6814f1
59177aa
 
fe49aa3
d3caf4c
fe49aa3
 
 
 
 
59177aa
 
5eb15fa
 
 
 
 
 
59177aa
5eb15fa
59177aa
 
 
5eb15fa
 
59177aa
5eb15fa
59177aa
5eb15fa
 
 
 
59177aa
d6814f1
59177aa
fe49aa3
c99406b
fe49aa3
d6814f1
 
 
 
 
 
 
 
 
fe49aa3
 
 
4f228ef
5eb15fa
d6814f1
5eb15fa
d6814f1
5eb15fa
d6814f1
5eb15fa
 
 
fe49aa3
59177aa
c99406b
fe49aa3
d6814f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79f2daf
d6814f1
 
 
 
 
3f26784
d6814f1
 
 
1df6810
d6814f1
 
29204f7
d6814f1
 
29204f7
 
d6814f1
d3caf4c
d6814f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3caf4c
d6814f1
d3caf4c
d6814f1
 
 
d3caf4c
d6814f1
d3caf4c
d6814f1
d3caf4c
d6814f1
d3caf4c
d6814f1
d3caf4c
 
d6814f1
d3caf4c
d6814f1
 
59177aa
 
d3caf4c
 
d6814f1
d3caf4c
59177aa
d6814f1
c99406b
d3caf4c
c99406b
 
d6814f1
 
 
c99406b
 
 
59177aa
c99406b
59177aa
 
 
 
d3caf4c
59177aa
 
 
 
fe49aa3
d6814f1
d3caf4c
59177aa
d3caf4c
59177aa
 
 
 
 
d6814f1
59177aa
 
 
 
d6814f1
 
 
 
 
 
59177aa
 
 
d6814f1
 
59177aa
d6814f1
 
 
 
 
59177aa
 
d6814f1
 
 
 
 
fe49aa3
 
 
 
d6814f1
1df6810
d6814f1
 
a589da1
59177aa
8761807
d6814f1
8761807
29204f7
62d2b15
59177aa
62d2b15
59177aa
d6814f1
59177aa
 
 
eb59db6
d6814f1
 
 
 
 
29204f7
d6814f1
 
 
 
eb59db6
4f228ef
29204f7
d6814f1
 
 
 
d3caf4c
d6814f1
 
 
 
eb59db6
ef79581
 
d6814f1
 
 
 
 
 
 
 
 
 
 
 
59177aa
29204f7
 
e41b021
29204f7
d6814f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb59db6
29204f7
 
59177aa
d3caf4c
d6814f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29204f7
 
 
d6814f1
 
29204f7
 
d6814f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29204f7
d6814f1
ef79581
c492b2c
d6814f1
 
ef79581
29204f7
 
 
 
 
 
 
 
d6814f1
29204f7
 
 
d6814f1
 
 
29204f7
eb59db6
d6814f1
 
24cd13f
d6814f1
 
 
1e4825e
d6814f1
29204f7
d6814f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59177aa
4f228ef
29204f7
d6814f1
29204f7
d6814f1
 
1df6810
59177aa
d3caf4c
1df6810
 
d6814f1
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
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
import os
import json
import re
import logging
import requests
import markdown
import time
import io
import random
import hashlib
from datetime import datetime
from dataclasses import dataclass
from itertools import combinations, product
from typing import Iterator

import streamlit as st
import pandas as pd
import PyPDF2  # For handling PDF files
from collections import Counter

from openai import OpenAI  # OpenAI ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
from gradio_client import Client
from kaggle.api.kaggle_api_extended import KaggleApi
import tempfile
import glob
import shutil

# โ”€โ”€โ”€ ์ถ”๊ฐ€๋œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ(์ ˆ๋Œ€ ๋ˆ„๋ฝ ๊ธˆ์ง€) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
import pyarrow.parquet as pq
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity

# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Environment Variables / Constants โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

OPENAI_API_KEY   = os.getenv("OPENAI_API_KEY", "")
BRAVE_KEY        = os.getenv("SERPHOUSE_API_KEY", "")   # Brave Search API
KAGGLE_USERNAME  = os.getenv("KAGGLE_USERNAME", "")
KAGGLE_KEY       = os.getenv("KAGGLE_KEY", "")
KAGGLE_API_KEY   = KAGGLE_KEY

if not (KAGGLE_USERNAME and KAGGLE_KEY):
    raise RuntimeError("โš ๏ธ  KAGGLE_USERNAME๊ณผ KAGGLE_KEY ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ๋จผ์ € ์„ค์ •ํ•˜์„ธ์š”.")

os.environ["KAGGLE_USERNAME"] = KAGGLE_USERNAME
os.environ["KAGGLE_KEY"]      = KAGGLE_KEY

BRAVE_ENDPOINT   = "https://api.search.brave.com/res/v1/web/search"
IMAGE_API_URL    = "http://211.233.58.201:7896"  # ์˜ˆ์‹œ ์ด๋ฏธ์ง€ ์ƒ์„ฑ์šฉ API
MAX_TOKENS       = 7999

# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Logging โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s - %(levelname)s - %(message)s"
)

# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ ๊ตฐ์‚ฌ(๋ฐ€๋ฆฌํ„ฐ๋ฆฌ) ์ „์ˆ  ๋ฐ์ดํ„ฐ์…‹ ๋กœ๋“œ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
@st.cache_resource
def load_military_dataset():
    """
    mil.parquet (index, scenario_description, attack_reasoning, defense_reasoning)
    """
    path = os.path.join(os.path.dirname(__file__), "mil.parquet")
    if not os.path.exists(path):
        logging.warning("mil.parquet not found โ€“ military support disabled.")
        return None
    try:
        df = pq.read_table(path).to_pandas()
        return df
    except Exception as e:
        logging.error(f"Failed to read mil.parquet: {e}")
        return None

MIL_DF = load_military_dataset()

def is_military_query(text: str) -> bool:
    """๊ตฐ์‚ฌ/์ „์ˆ  ๊ด€๋ จ ํ‚ค์›Œ๋“œ๊ฐ€ ๋“ฑ์žฅํ•˜๋ฉด True ๋ฐ˜ํ™˜"""
    kw = [
        "๊ตฐ์‚ฌ", "์ „์ˆ ", "์ „ํˆฌ", "์ „์Ÿ", "์ž‘์ „", "๋ฌด๊ธฐ", "๋ณ‘๋ ฅ",
        "military", "tactic", "warfare", "battle", "operation"
    ]
    return any(k.lower() in text.lower() for k in kw)

def military_search(query: str, top_k: int = 3):
    """
    mil.parquet์˜ scenario_description ์—ด๊ณผ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„ ๋ถ„์„ํ•˜์—ฌ
    query์™€ ๊ฐ€์žฅ ์œ ์‚ฌํ•œ ์ƒ์œ„ ์‹œ๋‚˜๋ฆฌ์˜ค๋ฅผ ๋ฐ˜ํ™˜
    """
    if MIL_DF is None:
        return []
    try:
        corpus = MIL_DF["scenario_description"].tolist()
        vec = TfidfVectorizer().fit_transform([query] + corpus)
        sims = cosine_similarity(vec[0:1], vec[1:]).flatten()
        top_idx = sims.argsort()[-top_k:][::-1]
        return MIL_DF.iloc[top_idx][[
            "scenario_description",
            "attack_reasoning",
            "defense_reasoning"
        ]].to_dict("records")
    except Exception as e:
        logging.error(f"military_search error: {e}")
        return []

# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Kaggle Datasets โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
KAGGLE_DATASETS = {
    "general_business": {
        "ref": "mohammadgharaei77/largest-2000-global-companies",
        "title": "Largest 2000 Global Companies",
        "subtitle": "Comprehensive data about the world's largest companies",
        "url": "https://www.kaggle.com/datasets/mohammadgharaei77/largest-2000-global-companies",
        "keywords": ["business", "company", "corporation", "enterprise", "global", "๋น„์ฆˆ๋‹ˆ์Šค", "๊ธฐ์—…", "ํšŒ์‚ฌ", "๊ธ€๋กœ๋ฒŒ", "๊ธฐ์—…๊ฐ€์น˜"]
    },
    "global_development": {
        "ref": "michaelmatta0/global-development-indicators-2000-2020",
        "title": "Global Development Indicators (2000-2020)",
        "subtitle": "Economic and social indicators for countries worldwide",
        "url": "https://www.kaggle.com/datasets/michaelmatta0/global-development-indicators-2000-2020",
        "keywords": ["development", "economy", "global", "indicators", "social", "๊ฒฝ์ œ", "๋ฐœ์ „", "์ง€ํ‘œ", "์‚ฌํšŒ", "๊ตญ๊ฐ€", "๊ธ€๋กœ๋ฒŒ"]
    },
    "startup_ideas": {
        "ref": "rohitsahoo/100-startup-ideas",
        "title": "Startup Idea Generator Dataset",
        "subtitle": "A variety of startup ideas",
        "url": "https://www.kaggle.com/datasets/rohitsahoo/100-startup-ideas",
        "keywords": ["startup", "innovation", "business idea", "entrepreneurship", "์Šคํƒ€ํŠธ์—…", "์ฐฝ์—…", "ํ˜์‹ ", "์•„์ด๋””์–ด", "๊ธฐ์—…๊ฐ€"]
    },
    "legal_terms": {
        "ref": "gu05087/korean-legal-terms",
        "title": "Korean Legal Terms",
        "subtitle": "Database of Korean legal terminology",
        "url": "https://www.kaggle.com/datasets/gu05087/korean-legal-terms",
        "keywords": ["legal", "law", "terms", "korean", "legislation", "๋ฒ•๋ฅ ", "๋ฒ•์ ", "ํ•œ๊ตญ", "์šฉ์–ด", "๊ทœ์ œ"]
    },
    "billionaires": {
        "ref": "vincentcampanaro/forbes-worlds-billionaires-list-2024",
        "title": "Forbes World's Billionaires List 2024",
        "subtitle": "Comprehensive data on the world's wealthiest individuals",
        "url": "https://www.kaggle.com/datasets/vincentcampanaro/forbes-worlds-billionaires-list-2024",
        "keywords": ["billionaire", "wealth", "rich", "forbes", "finance", "๋ถ€์ž", "์–ต๋งŒ์žฅ์ž", "ํฌ๋ธŒ์Šค", "๋ถ€", "์žฌํ…Œํฌ"]
    },
    "financial_news": {
        "ref": "thedevastator/uncovering-financial-insights-with-the-reuters-2",
        "title": "Reuters Financial News Insights",
        "subtitle": "Financial news and market analysis from Reuters",
        "url": "https://www.kaggle.com/datasets/thedevastator/uncovering-financial-insights-with-the-reuters-2",
        "keywords": ["finance", "market", "stock", "investment", "news", "๊ธˆ์œต", "์‹œ์žฅ", "์ฃผ์‹", "ํˆฌ์ž", "๋‰ด์Šค"]
    },
    "ecommerce": {
        "ref": "oleksiimartusiuk/80000-products-e-commerce-data-clean",
        "title": "80,000 Products E-Commerce Data",
        "subtitle": "Clean dataset of e-commerce products information",
        "url": "https://www.kaggle.com/datasets/oleksiimartusiuk/80000-products-e-commerce-data-clean",
        "keywords": ["ecommerce", "product", "retail", "shopping", "online", "์ด์ปค๋จธ์Šค", "์ œํ’ˆ", "์†Œ๋งค", "์‡ผํ•‘", "์˜จ๋ผ์ธ"]
    },
    "world_development_indicators": {
        "ref": "georgejdinicola/world-bank-indicators",
        "title": "World Development Indicators",
        "subtitle": "Long-run socio-economic indicators for 200+ countries",
        "url": "https://www.kaggle.com/datasets/georgejdinicola/world-bank-indicators",
        "keywords": [
            "wdi", "macro", "economy", "gdp", "population",
            "๊ฐœ๋ฐœ์ง€ํ‘œ", "๊ฑฐ์‹œ๊ฒฝ์ œ", "์„ธ๊ณ„์€ํ–‰", "๊ฒฝ์ œ์ง€ํ‘œ", "์ธ๊ตฌ"
        ]
    },
    "commodity_prices": {
        "ref": "debashish311601/commodity-prices",
        "title": "Commodity Prices (2000-2023)",
        "subtitle": "Daily prices for crude oil, gold, grains, metals, etc.",
        "url": "https://www.kaggle.com/datasets/debashish311601/commodity-prices",
        "keywords": [
            "commodity", "oil", "gold", "raw material", "price",
            "์›์ž์žฌ", "์œ ๊ฐ€", "๊ธˆ", "๊ฐ€๊ฒฉ", "์‹œ์žฅ"
        ]
    },
    "world_trade": {
        "ref": "muhammadtalhaawan/world-export-and-import-dataset",
        "title": "World Export & Import Dataset",
        "subtitle": "34-year historical trade flows by country & product",
        "url": "https://www.kaggle.com/datasets/muhammadtalhaawan/world-export-and-import-dataset",
        "keywords": [
            "trade", "export", "import", "commerce", "flow",
            "๋ฌด์—ญ", "์ˆ˜์ถœ", "์ˆ˜์ž…", "๊ตญ์ œ๊ต์—ญ", "๊ด€์„ธ"
        ]
    },
    "us_business_reports": {
        "ref": "census/business-and-industry-reports",
        "title": "US Business & Industry Reports",
        "subtitle": "Key monthly economic indicators from the US Census Bureau",
        "url": "https://www.kaggle.com/datasets/census/business-and-industry-reports",
        "keywords": [
            "us", "economy", "retail sales", "construction", "manufacturing",
            "๋ฏธ๊ตญ", "๊ฒฝ์ œ์ง€ํ‘œ", "์†Œ๋งคํŒ๋งค", "์‚ฐ์—…์ƒ์‚ฐ", "๊ฑด์„ค"
        ]
    },
    "us_industrial_production": {
        "ref": "federalreserve/industrial-production-index",
        "title": "Industrial Production Index (US)",
        "subtitle": "Monthly Fed index for manufacturing, mining & utilities",
        "url": "https://www.kaggle.com/datasets/federalreserve/industrial-production-index",
        "keywords": [
            "industry", "production", "index", "fed", "us",
            "์‚ฐ์—…์ƒ์‚ฐ", "์ œ์กฐ์—…", "๋ฏธ๊ตญ", "๊ฒฝ๊ธฐ", "์ง€์ˆ˜"
        ]
    },
    "us_stock_market": {
        "ref": "borismarjanovic/price-volume-data-for-all-us-stocks-etfs",
        "title": "Huge Stock Market Dataset",
        "subtitle": "Historical prices & volumes for all US stocks and ETFs",
        "url": "https://www.kaggle.com/datasets/borismarjanovic/price-volume-data-for-all-us-stocks-etfs",
        "keywords": [
            "stock", "market", "finance", "equity", "price",
            "์ฃผ์‹", "๋ฏธ๊ตญ์ฆ์‹œ", "์‹œ์„ธ", "ETF", "๋ฐ์ดํ„ฐ"
        ]
    },
    "company_financials": {
        "ref": "rish59/financial-statements-of-major-companies2009-2023",
        "title": "Financial Statements of Major Companies (2009-2023)",
        "subtitle": "15-year income sheet & balance sheet data for global firms",
        "url": "https://www.kaggle.com/datasets/rish59/financial-statements-of-major-companies2009-2023",
        "keywords": [
            "financials", "income", "balance sheet", "cashflow",
            "์žฌ๋ฌด์ œํ‘œ", "๋งค์ถœ", "์ˆ˜์ต์„ฑ", "๊ธฐ์—…์žฌ๋ฌด", "ํฌํŠธํด๋ฆฌ์˜ค"
        ]
    },
    "startup_investments": {
        "ref": "justinas/startup-investments",
        "title": "Crunchbase Startup Investments",
        "subtitle": "Funding rounds & investor info for global startups",
        "url": "https://www.kaggle.com/datasets/justinas/startup-investments",
        "keywords": [
            "startup", "venture", "funding", "crunchbase",
            "ํˆฌ์ž", "VC", "์Šคํƒ€ํŠธ์—…", "๋ผ์šด๋“œ", "์‹ ๊ทœ์ง„์ž…"
        ]
    },
    "global_energy": {
        "ref": "atharvasoundankar/global-energy-consumption-2000-2024",
        "title": "Global Energy Consumption (2000-2024)",
        "subtitle": "Country-level energy usage by source & sector",
        "url": "https://www.kaggle.com/datasets/atharvasoundankar/global-energy-consumption-2000-2024",
        "keywords": [
            "energy", "consumption", "renewable", "oil", "utility",
            "์—๋„ˆ์ง€", "์†Œ๋น„", "์žฌ์ƒ์—๋„ˆ์ง€", "์ „๋ ฅ์ˆ˜์š”", "ํ™”์„์—ฐ๋ฃŒ"
        ]
    },
    "co2_emissions": {
        "ref": "ulrikthygepedersen/co2-emissions-by-country",
        "title": "COโ‚‚ Emissions by Country",
        "subtitle": "Annual COโ‚‚ emissions & per-capita data since 1960s",
        "url": "https://www.kaggle.com/datasets/ulrikthygepedersen/co2-emissions-by-country",
        "keywords": [
            "co2", "emission", "climate", "environment", "carbon",
            "ํƒ„์†Œ๋ฐฐ์ถœ", "๊ธฐํ›„๋ณ€ํ™”", "ํ™˜๊ฒฝ", "์˜จ์‹ค๊ฐ€์Šค", "์ง€์†๊ฐ€๋Šฅ"
        ]
    },
    "crop_climate": {
        "ref": "thedevastator/the-relationship-between-crop-production-and-cli",
        "title": "Crop Production & Climate Change",
        "subtitle": "Yield & area stats for wheat, corn, rice, soybean vs climate",
        "url": "https://www.kaggle.com/datasets/thedevastator/the-relationship-between-crop-production-and-cli",
        "keywords": [
            "agriculture", "crop", "climate", "yield", "food",
            "๋†์—…", "์ž‘๋ฌผ", "๊ธฐํ›„", "์ˆ˜ํ™•๋Ÿ‰", "์‹ํ’ˆ"
        ]
    },
    "esg_ratings": {
        "ref": "alistairking/public-company-esg-ratings-dataset",
        "title": "Public Company ESG Ratings",
        "subtitle": "Environment, Social & Governance scores for listed firms",
        "url": "https://www.kaggle.com/datasets/alistairking/public-company-esg-ratings-dataset",
        "keywords": [
            "esg", "sustainability", "governance", "csr",
            "ํ™˜๊ฒฝ", "์‚ฌํšŒ", "์ง€๋ฐฐ๊ตฌ์กฐ", "์ง€์†๊ฐ€๋Šฅ", "ํ‰๊ฐ€"
        ]
    },
    "global_health": {
        "ref": "malaiarasugraj/global-health-statistics",
        "title": "Global Health Statistics",
        "subtitle": "Comprehensive health indicators & disease prevalence by country",
        "url": "https://www.kaggle.com/datasets/malaiarasugraj/global-health-statistics",
        "keywords": [
            "health", "disease", "life expectancy", "WHO",
            "๋ณด๊ฑด", "์งˆ๋ณ‘", "๊ธฐ๋Œ€์ˆ˜๋ช…", "์˜๋ฃŒ", "๊ณต์ค‘๋ณด๊ฑด"
        ]
    },
    "housing_market": {
        "ref": "atharvasoundankar/global-housing-market-analysis-2015-2024",
        "title": "Global Housing Market Analysis (2015-2024)",
        "subtitle": "House price index, mortgage rates, rent data by country",
        "url": "https://www.kaggle.com/datasets/atharvasoundankar/global-housing-market-analysis-2015-2024",
        "keywords": [
            "housing", "real estate", "price index", "mortgage",
            "๋ถ€๋™์‚ฐ", "์ฃผํƒ๊ฐ€๊ฒฉ", "์ž„๋Œ€๋ฃŒ", "์‹œ์žฅ", "๊ธˆ๋ฆฌ"
        ]
    },
    "pharma_sales": {
        "ref": "milanzdravkovic/pharma-sales-data",
        "title": "Pharma Sales Data (2014-2019)",
        "subtitle": "600k sales records across 8 ATC drug categories",
        "url": "https://www.kaggle.com/datasets/milanzdravkovic/pharma-sales-data",
        "keywords": [
            "pharma", "sales", "drug", "healthcare", "medicine",
            "์ œ์•ฝ", "์˜์•ฝํ’ˆ", "๋งค์ถœ", "ํ—ฌ์Šค์ผ€์–ด", "์‹œ์žฅ"
        ]
    },
    "ev_sales": {
        "ref": "muhammadehsan000/global-electric-vehicle-sales-data-2010-2024",
        "title": "Global EV Sales Data (2010-2024)",
        "subtitle": "Electric vehicle unit sales by region & model year",
        "url": "https://www.kaggle.com/datasets/muhammadehsan000/global-electric-vehicle-sales-data-2010-2024",
        "keywords": [
            "ev", "electric vehicle", "automotive", "mobility",
            "์ „๊ธฐ์ฐจ", "ํŒ๋งค๋Ÿ‰", "์ž๋™์ฐจ์‚ฐ์—…", "์นœํ™˜๊ฒฝ๋ชจ๋นŒ๋ฆฌํ‹ฐ", "์‹œ์žฅ์„ฑ์žฅ"
        ]
    },
    "hr_attrition": {
        "ref": "pavansubhasht/ibm-hr-analytics-attrition-dataset",
        "title": "IBM HR Analytics: Attrition & Performance",
        "subtitle": "Employee demographics, satisfaction & attrition flags",
        "url": "https://www.kaggle.com/datasets/pavansubhasht/ibm-hr-analytics-attrition-dataset",
        "keywords": [
            "hr", "attrition", "employee", "people analytics",
            "์ธ์‚ฌ", "์ด์ง๋ฅ ", "์ง์›", "HR๋ถ„์„", "์กฐ์ง๊ด€๋ฆฌ"
        ]
    },
    "employee_satisfaction": {
        "ref": "redpen12/employees-satisfaction-analysis",
        "title": "Employee Satisfaction Survey Data",
        "subtitle": "Department-level survey scores on satisfaction & engagement",
        "url": "https://www.kaggle.com/datasets/redpen12/employees-satisfaction-analysis",
        "keywords": [
            "satisfaction", "engagement", "survey", "workplace",
            "์ง์›๋งŒ์กฑ๋„", "์กฐ์ง๋ฌธํ™”", "์„ค๋ฌธ", "๊ทผ๋ฌดํ™˜๊ฒฝ", "HR"
        ]
    },
    "world_bank_indicators": {
        "ref": "georgejdinicola/world-bank-indicators",
        "title": "World Bank Indicators by Topic (1960-Present)",
        "subtitle": "Macro-economic, ์‚ฌํšŒยท์ธ๊ตฌ ํ†ต๊ณ„ ๋“ฑ 200+๊ฐœ๊ตญ ์žฅ๊ธฐ ์‹œ๊ณ„์—ด ์ง€ํ‘œ",
        "url": "https://www.kaggle.com/datasets/georgejdinicola/world-bank-indicators",
        "keywords": ["world bank", "development", "economy", "global", "indicator", "์„ธ๊ณ„์€ํ–‰", "๊ฒฝ์ œ", "์ง€ํ‘œ", "๊ฐœ๋ฐœ", "๊ฑฐ์‹œ"]
    },
    "physical_chem_properties": {
        "ref": "ivanyakovlevg/physical-and-chemical-properties-of-substances",
        "title": "Physical & Chemical Properties of Substances",
        "subtitle": "8๋งŒ์—ฌ ํ™”ํ•ฉ๋ฌผ์˜ ๋ฌผ๋ฆฌยทํ™”ํ•™ ํŠน์„ฑ ๋ฐ ๋ถ„๋ฅ˜ ์ •๋ณด",
        "url": "https://www.kaggle.com/datasets/ivanyakovlevg/physical-and-chemical-properties-of-substances",
        "keywords": ["chemistry", "materials", "property", "substance", "ํ™”ํ•™", "๋ฌผ์„ฑ", "์†Œ์žฌ", "๋ฐ์ดํ„ฐ", "R&D"]
    },
    "global_weather_repository": {
        "ref": "nelgiriyewithana/global-weather-repository",
        "title": "Global Weather Repository",
        "subtitle": "์ „ ์„ธ๊ณ„ ๊ธฐ์ƒ ๊ด€์ธก์น˜(๊ธฐ์˜จยท๊ฐ•์ˆ˜ยทํ’์† ๋“ฑ) ์ผ๋ณ„ ์—…๋ฐ์ดํŠธ",
        "url": "https://www.kaggle.com/datasets/nelgiriyewithana/global-weather-repository",
        "keywords": ["weather", "climate", "meteorology", "global", "forecast", "๊ธฐ์ƒ", "๋‚ ์”จ", "๊ธฐํ›„", "๊ด€์ธก", "ํ™˜๊ฒฝ"]
    },
    "amazon_best_seller_softwares": {
        "ref": "kaverappa/amazon-best-seller-softwares",
        "title": "Amazon Best Seller โ€“ Software Category",
        "subtitle": "์•„๋งˆ์กด ์†Œํ”„ํŠธ์›จ์–ด ๋ฒ ์ŠคํŠธ์…€๋Ÿฌ ์ˆœ์œ„ ๋ฐ ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ",
        "url": "https://www.kaggle.com/datasets/kaverappa/amazon-best-seller-softwares",
        "keywords": ["amazon", "e-commerce", "software", "review", "ranking", "์•„๋งˆ์กด", "์ด์ปค๋จธ์Šค", "์†Œํ”„ํŠธ์›จ์–ด", "๋ฒ ์ŠคํŠธ์…€๋Ÿฌ", "๋ฆฌ๋ทฐ"]
    },
    "world_stock_prices": {
        "ref": "nelgiriyewithana/world-stock-prices-daily-updating",
        "title": "World Stock Prices (Daily Updating)",
        "subtitle": "30,000์—ฌ ๊ธ€๋กœ๋ฒŒ ์ƒ์žฅ์‚ฌ์˜ ์ผ๊ฐ„ ์ฃผ๊ฐ€ยท์‹œ์ดยท์„นํ„ฐ ์ •๋ณด ์‹ค์‹œ๊ฐ„ ๊ฐฑ์‹ ",
        "url": "https://www.kaggle.com/datasets/nelgiriyewithana/world-stock-prices-daily-updating",
        "keywords": ["stock", "finance", "market", "equity", "price", "๊ธ€๋กœ๋ฒŒ", "์ฃผ๊ฐ€", "๊ธˆ์œต", "์‹œ์žฅ", "ํˆฌ์ž"]
    }
}

SUN_TZU_STRATEGIES = [
    {"๊ณ„": "๋งŒ์ฒœ๊ณผํ•ด", "์š”์•ฝ": "ํ‰๋ฒ”ํ•œ ์ฒ™, ๋ชฐ๋ž˜ ์ง„ํ–‰", "์กฐ๊ฑด": "์ƒ๋Œ€๊ฐ€ ์ง€์ผœ๋ณด๊ณ  ์žˆ์„ ๋•Œ", "ํ–‰๋™": "๋ฃจํ‹ดยทํ‰์˜จํ•จ ๊ณผ์‹œ", "๋ชฉ์ ": "๊ฒฝ๊ณ„ ๋ฌด๋ ฅํ™”", "์˜ˆ์‹œ": "๊ทœ์ œ๊ธฐ๊ด€ ๋ˆˆ์น˜ ๋ณด๋Š” ์‹ ์‚ฌ์—… ํŒŒ์ผ๋Ÿฟ"},
    {"๊ณ„": "์œ„์œ„๊ตฌ์กฐ", "์š”์•ฝ": "๋’คํ†ต์ˆ˜ ์น˜๋ฉด ํฌ์œ„ ํ’€๋ฆฐ๋‹ค", "์กฐ๊ฑด": "์šฐ๋ฆฌ ์ธก์ด ์••๋ฐ•๋ฐ›์„ ๋•Œ", "ํ–‰๋™": "์  ๋ณธ์ง„ ๊ธ‰์Šต", "๋ชฉ์ ": "์••๋ฐ• ํ•ด์†Œ", "์˜ˆ์‹œ": "๊ฒฝ์Ÿ์‚ฌ ํ•ต์‹ฌ ๊ณ ๊ฐ ๋บ๊ธฐ"},
    {"๊ณ„": "์ฐจ๋„์‚ด์ธ", "์š”์•ฝ": "๋‚ด ์† ๋”๋Ÿฝํžˆ์ง€ ๋งˆ", "์กฐ๊ฑด": "์ง์ ‘ ๊ณต๊ฒฉ ๋ถ€๋‹ด", "ํ–‰๋™": "์ œ3์ž ํ™œ์šฉ", "๋ชฉ์ ": "์ฑ…์ž„ ์ „๊ฐ€", "์˜ˆ์‹œ": "์–ธ๋ก ์„ ํ†ตํ•œ ๊ฒฝ์Ÿ์‚ฌ ๋น„ํŒ"},
    {"๊ณ„": "์ด์ผ๋Œ€์šฐ", "์š”์•ฝ": "์šฐ๋ฆฌ๊ฐ€ ์‰ฌ๋ฉด ์ ์ด ์ง€์นœ๋‹ค", "์กฐ๊ฑด": "์ƒ๋Œ€๊ฐ€ ๊ณผ๋กœ ์ค‘", "ํ–‰๋™": "๋ฒ„ํ‹ฐ๋ฉฐ ์ฒด๋ ฅ ๋ณด์กด", "๋ชฉ์ ": "์—ญ์ „ ํƒ€์ด๋ฐ ํ™•๋ณด", "์˜ˆ์‹œ": "ํ˜‘์ƒ ์ง€์—ฐ ํ›„ ํ—๊ฐ’ ์ธ์ˆ˜"},
    {"๊ณ„": "์ง„ํ™”ํƒ€๊ฒ", "์š”์•ฝ": "๋ถˆ๋‚  ๋•Œ ์ฃผ์›Œ ๋‹ด๊ธฐ", "์กฐ๊ฑด": "์‹œ์žฅ ํ˜ผ๋ž€ยท์œ„๊ธฐ", "ํ–‰๋™": "์ €๊ฐ€ ๋งค์ˆ˜", "๋ชฉ์ ": "์ €๋น„์šฉ ๊ณ ์ด์ต", "์˜ˆ์‹œ": "๊ธˆ์œต์œ„๊ธฐ ๋•Œ ์šฐ๋Ÿ‰์ž์‚ฐ ๋งค์ž…"},
    {"๊ณ„": "์„ฑ๋™๊ฒฉ์„œ", "์š”์•ฝ": "์†Œ์Œ์€ ์™ผ์ชฝ, ๊ณต๊ฒฉ์€ ์˜ค๋ฅธ์ชฝ", "์กฐ๊ฑด": "์ •๋ฉด ๋ฐฉ์–ด ๊ฒฌ๊ณ ", "ํ–‰๋™": "๊ฐ€์งœ ์‹ ํ˜ธ โ†’ ์šฐํšŒ", "๋ชฉ์ ": "๋ฐฉ์–ด ๋ถ„์‚ฐ", "์˜ˆ์‹œ": "์‹ ์ œํ’ˆ A ํ™๋ณด, ์‹ค์ œ๋Š” B ํ™•์žฅ"},
    {"๊ณ„": "๋ฌด์ค‘์ƒ์œ ", "์š”์•ฝ": "์—†๋Š” ๊ฒƒ๋„ ์žˆ๋Š” ์ฒ™", "์กฐ๊ฑด": "์ž์› ๋ถ€์กฑ", "ํ–‰๋™": "ํ—ˆ์„ธยท์—ฐ๋ง‰", "๋ชฉ์ ": "์ƒ๋Œ€ ํ˜ผ๋ž€", "์˜ˆ์‹œ": "์Šคํƒ€ํŠธ์—… ๊ณผ์žฅ ๋กœ๋“œ๋งต"},
    {"๊ณ„": "์•”๋„์ง„์ฐฝ", "์š”์•ฝ": "๋’ท๋ฌธ์œผ๋กœ ๋Œ์•„๊ฐ€๋ผ", "์กฐ๊ฑด": "์šฐํšŒ๋กœ ์กด์žฌ", "ํ–‰๋™": "๋น„๋ฐ€ ๋ฃจํŠธ ์นจํˆฌ", "๋ชฉ์ ": "ํ—ˆ๋ฅผ ์ฐŒ๋ฆ„", "์˜ˆ์‹œ": "๊ด€์„ธ ํ”ผํ•ด ์ œ3๊ตญ ์ƒ์‚ฐ"},
    {"๊ณ„": "๊ฒฉ์•ˆ๊ด€ํ™”", "์š”์•ฝ": "๋‚จ ์‹ธ์›€ ๊ตฌ๊ฒฝ", "์กฐ๊ฑด": "๋‘ ๊ฒฝ์Ÿ์ž ์ถฉ๋Œ", "ํ–‰๋™": "๊ด€๋ง", "๋ชฉ์ ": "๋‘˜ ๋‹ค ์†Œ๋ชจ", "์˜ˆ์‹œ": "ํ”Œ๋žซํผ ์ „์Ÿ ์ค‘ ์ค‘๋ฆฝ ์œ ์ง€"},
    {"๊ณ„": "์†Œ๋ฆฌ์žฅ๋„", "์š”์•ฝ": "์›ƒ์œผ๋ฉฐ ์นผ ์ˆจ๊ธฐ๊ธฐ", "์กฐ๊ฑด": "์นœ๋ฐ€ ๋ถ„์œ„๊ธฐ", "ํ–‰๋™": "์šฐํ˜ธ ์ œ์Šค์ฒ˜ ํ›„ ๊ธฐ์Šต", "๋ชฉ์ ": "๊ฒฝ๊ณ„ ๋ถ•๊ดด", "์˜ˆ์‹œ": "ํ•ฉ์ž‘ ํ›„ ํ•ต์‹ฌ ๊ธฐ์ˆ  ํƒˆ์ทจ"},
    {"๊ณ„": "์ด๋Œ€๋„๊ฐ•", "์š”์•ฝ": "๋œ ์ค‘์š”ํ•œ ๊ฑธ ๋‚ด์ค˜๋ผ", "์กฐ๊ฑด": "๋ญ”๊ฐ€ ์žƒ์—ˆ์„ ๋•Œ", "ํ–‰๋™": "๋ถ€์† ํฌ์ƒ", "๋ชฉ์ ": "ํ•ต์‹ฌ ๋ณดํ˜ธ", "์˜ˆ์‹œ": "์ œํ’ˆ ๋ผ์ธ ํ•˜๋‚˜ ๋‹จ์ข…"},
    {"๊ณ„": "์ˆœ์ˆ˜๊ฒฌ์–‘", "์š”์•ฝ": "๋ฐฉ์น˜๋œ ๊ฒƒ ์ฑ™๊ธฐ๊ธฐ", "์กฐ๊ฑด": "๊ฒฝ๊ณ„ ํ—ˆ์ˆ ", "ํ–‰๋™": "์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ์ˆ˜์ง‘", "๋ชฉ์ ": "๋ฌดํ˜ˆ ์ด๋“", "์˜ˆ์‹œ": "๊ณต๊ณต API ๋ฐ์ดํ„ฐ ๊ธ๊ธฐ"},
    {"๊ณ„": "ํƒ€์ดˆ๊ฒฝ์‚ฌ", "์š”์•ฝ": "ํ’€ ์ณ์„œ ๋ฑ€ ๋‚˜์˜จ๋‹ค", "์กฐ๊ฑด": "์ ์ด ์ˆจ์„ ๋•Œ", "ํ–‰๋™": "์ผ๋ถ€๋Ÿฌ ์†Œ๋ž€", "๋ชฉ์ ": "์œ„์น˜ ๋…ธ์ถœ", "์˜ˆ์‹œ": "์ด์‚ฌํšŒ ๋ฐ˜๋Œ€ํŒŒ ์˜์ค‘ ํŒŒ์•…"},
    {"๊ณ„": "์ฐจ์‹œํ™˜ํ˜ผ", "์š”์•ฝ": "์ฃฝ์€ ์นด๋“œ ์žฌํ™œ์šฉ", "์กฐ๊ฑด": "ํ๊ธฐ ์ž์›", "ํ–‰๋™": "๋ฆฌ๋ธŒ๋žœ๋”ฉ", "๋ชฉ์ ": "์ƒˆ ์ „๋ ฅ ํ™•๋ณด", "์˜ˆ์‹œ": "์‹คํŒจ ์•ฑ ์žฌ์ถœ์‹œ"},
    {"๊ณ„": "์กฐํ˜ธ์ด์‚ฐ", "์š”์•ฝ": "ํ˜ธ๋ž‘์ด ์‚ฐ ๋ฐ–์œผ๋กœ", "์กฐ๊ฑด": "๊ฐ•์  ๊ฑฐ์ ", "ํ–‰๋™": "์œ ์ธ ์ด๋™", "๋ชฉ์ ": "๋นˆ์ง‘ ๊ณต๋žต", "์˜ˆ์‹œ": "๊ฒฝ์Ÿ VC ํ–‰์‚ฌ ์œ ๋„ ํ›„ ๋”œ ์„ ์ "},
    {"๊ณ„": "์š•๊ธˆ๊ณ ์ข…", "์š”์•ฝ": "์žก์œผ๋ ค๋ฉด ๋†“์•„์ค˜๋ผ", "์กฐ๊ฑด": "์ธ์žฌยท์  ํฌํš", "ํ–‰๋™": "์ผ๋ถ€๋Ÿฌ ํ’€์–ด์คŒ", "๋ชฉ์ ": "์ €ํ•ญ ์•ฝํ™”", "์˜ˆ์‹œ": "ํ•ต์‹ฌ ์ธ์žฌ ์žฌ๊ณ„์•ฝ ์œ ๋„"},
    {"๊ณ„": "ํฌ์ „์ธ์˜ฅ", "์š”์•ฝ": "๋ฒฝ๋Œ ๋˜์ ธ ์˜ฅ ์–ป๊ธฐ", "์กฐ๊ฑด": "ํฐ ๋ณด์ƒ ํ•„์š”", "ํ–‰๋™": "์ž‘์€ ๋ฏธ๋ผ", "๋ชฉ์ ": "์ฐธ์—ฌ ์œ ๋„", "์˜ˆ์‹œ": "๋ฌด๋ฃŒ โ†’ ์œ ๋ฃŒ ์ „ํ™˜"},
    {"๊ณ„": "๊ธˆ์ ๊ธˆ์™•", "์š”์•ฝ": "๋„๋‘‘ ์žก์œผ๋ ค๋ฉด ๋‘๋ชฉ๋ถ€ํ„ฐ", "์กฐ๊ฑด": "์กฐ์ง ๋ณต์žก", "ํ–‰๋™": "์ˆ˜๋‡Œ ๊ณต๊ฒฉ", "๋ชฉ์ ": "์กฐ์ง ๋ถ•๊ดด", "์˜ˆ์‹œ": "์ตœ๋Œ€ ์ฃผ์ฃผ ์ง€๋ถ„ ๋งค์ž…"},
    {"๊ณ„": "๋ถ€์ €์ด์ง€", "์š”์•ฝ": "๊ฐ€๋งˆ ๋ฐ‘ ๋ถˆ ๋„๊ธฐ", "์กฐ๊ฑด": "์  ์˜์กด์„ฑ ์กด์žฌ", "ํ–‰๋™": "๋ณด๊ธ‰ ์ฐจ๋‹จ", "๋ชฉ์ ": "์ „๋ ฅ ๊ธ‰๊ฐ", "์˜ˆ์‹œ": "ํ•ต์‹ฌ ๊ณต๊ธ‰์—…์ฒด ์„ ์ "},
    {"๊ณ„": "ํ˜ผ์ˆ˜๋ชจ์–ด", "์š”์•ฝ": "๋ฌผ ํ๋ ค ๋†“๊ณ  ๋‚š์‹œ", "์กฐ๊ฑด": "ํŒ์„ธ ๋ถˆํˆฌ๋ช…", "ํ–‰๋™": "ํ˜ผํƒ ์œ ์ง€", "๋ชฉ์ ": "์–ด๋ถ€์ง€๋ฆฌ", "์˜ˆ์‹œ": "์ž…๋ฒ• ์ง€์—ฐ ๋กœ๋น„"},
    {"๊ณ„": "๊ธˆ์„ ํƒˆ๊ฐ", "์š”์•ฝ": "ํ—ˆ๋ฌผ ๋ฒ—๊ณ  ๋„๋ง", "์กฐ๊ฑด": "์ถ”์  ์‹ฌํ•จ", "ํ–‰๋™": "์™ธํ”ผ๋งŒ ๋‚จ๊น€", "๋ชฉ์ ": "์ถ”์  ๋ฌดํšจ", "์˜ˆ์‹œ": "๋ถ€์‹ค ์žํšŒ์‚ฌ ๋–ผ์–ด๋‚ด๊ธฐ"},
    {"๊ณ„": "๊ด€๋ฌธ์žก์ ", "์š”์•ฝ": "๋ฌธ ๋‹ซ๊ณ  ์žก์•„๋ผ", "์กฐ๊ฑด": "ํ‡ด๋กœ ์˜ˆ์ธก", "ํ–‰๋™": "์ถœ๊ตฌ ๋ด‰์‡„", "๋ชฉ์ ": "์™„์ „ ํฌํš", "์˜ˆ์‹œ": "๋ฝ์—… ์กฐํ•ญ์œผ๋กœ ์ง€๋ถ„ ๋งค์ง‘"},
    {"๊ณ„": "์›๊ต๊ทผ๊ณต", "์š”์•ฝ": "๋จผ ๋ฐ์™€ ์นœํ•ด์ง€๊ณ  ๊ฐ€๊นŒ์šด ๋ฐ ์นœ๋‹ค", "์กฐ๊ฑด": "๋‹ค๊ตญ ๊ฐ„ ๊ฒฝ์Ÿ", "ํ–‰๋™": "์›๊ฑฐ๋ฆฌ ๋™๋งน", "๋ชฉ์ ": "๋‹จ๊ณ„์  ํ™•์žฅ", "์˜ˆ์‹œ": "์›๊ฑฐ๋ฆฌ FTA ์ฒด๊ฒฐ ํ›„ ์ธ๊ทผ M&A"},
    {"๊ณ„": "๊ฐ€๋„๋ฒŒ๊ดต", "์š”์•ฝ": "๊ธธ ๋นŒ๋ ค ๊ณต๊ฒฉ", "์กฐ๊ฑด": "์ค‘๊ฐ„ ์„ธ๋ ฅ ์žฅ๋ฒฝ", "ํ–‰๋™": "ํ†ต๋กœ ๋ช…๋ถ„ โ†’ ์ œ์••", "๋ชฉ์ ": "์žฅ์•  ์ œ๊ฑฐ", "์˜ˆ์‹œ": "์ดํŒ ๋นŒ๋ฏธ ์‹œ์žฅ ์ง„์ž…"},
    {"๊ณ„": "ํˆฌ๋Ÿ‰ํ™˜์ฃผ", "์š”์•ฝ": "๋“ค๋ณด ๋ชฐ๋ž˜ ๋ฐ”๊ฟ”์น˜๊ธฐ", "์กฐ๊ฑด": "๊ฐ์‹œ ์กด์žฌ", "ํ–‰๋™": "๋‚ด๋ถ€ ๊ต์ฒด", "๋ชฉ์ ": "์ธ์‹ ์™œ๊ณก", "์˜ˆ์‹œ": "๋ฐฑ์—”๋“œ ๊ฐˆ์•„๋ผ์šฐ๊ธฐ"},
    {"๊ณ„": "์ง€์ƒ๋งค๊ดด", "์š”์•ฝ": "๋ฝ•๋‚˜๋ฌด ๊ฐ€๋ฆฌ์ผœ ํšŒ์ดˆ๋ฆฌ ์š•", "์กฐ๊ฑด": "์ง์ ‘ ๋น„ํŒ ๊ณค๋ž€", "ํ–‰๋™": "์ œ3์ž ์ง€์ ", "๋ชฉ์ ": "๋ฉ”์‹œ์ง€ ์ „๋‹ฌ", "์˜ˆ์‹œ": "์‹ฑํฌํƒฑํฌ ๋ณด๊ณ ์„œ ์••๋ฐ•"},
    {"๊ณ„": "๊ฐ€์น˜๋ถˆ์ „", "์š”์•ฝ": "๋ฐ”๋ณด ์—ฐ๊ธฐ", "์กฐ๊ฑด": "์ƒ๋Œ€ ์˜์‹ฌ ๋งŽ์Œ", "ํ–‰๋™": "์ผ๋ถ€๋Ÿฌ ํ—ˆ์ˆ ", "๋ชฉ์ ": "๋ฐฉ์‹ฌ ์œ ๋„", "์˜ˆ์‹œ": "์ €ํ‰๊ฐ€ ๊ฐ€์ด๋˜์Šค"},
    {"๊ณ„": "์ƒ์˜ฅ์ถ”์ œ", "์š”์•ฝ": "์‚ฌ๋‹ค๋ฆฌ ๊ฑท์–ด์ฐจ๊ธฐ", "์กฐ๊ฑด": "๊ธธ ์—ด์–ด์ค€ ๋’ค", "ํ–‰๋™": "ํ‡ด๋กœ ์ฐจ๋‹จ", "๋ชฉ์ ": "๊ณ ๋ฆฝ", "์˜ˆ์‹œ": "ํˆฌ์ž์ž ์ดˆ์ฒญ ํ›„ ์ •๋ณด ์ฐจ๋‹จ"},
    {"๊ณ„": "์ˆ˜์ƒ๊ฐœํ™”", "์š”์•ฝ": "๋‚˜๋ฌด์— ๊ฝƒ ํ•€ ์ฒ™", "์กฐ๊ฑด": "์‹ค๋ ฅ ๋ถ€์กฑ", "ํ–‰๋™": "์™ธํ˜• ๋ถ€ํ’€๋ฆผ", "๋ชฉ์ ": "์˜ํ–ฅ๋ ฅ ํ™•๋Œ€", "์˜ˆ์‹œ": "MOU ยท๊ณต๋™ ๋กœ๊ณ  ํ™๋ณด"},
    {"๊ณ„": "๋ฐ˜๊ฐ์œ„์ฃผ", "์š”์•ฝ": "์†๋‹˜์—์„œ ์ฃผ์ธ์œผ๋กœ", "์กฐ๊ฑด": "๋ถ€์ฐจ์  ์œ„์น˜", "ํ–‰๋™": "์ฃผ๋„๊ถŒ ์žฅ์•…", "๋ชฉ์ ": "์—ญ์ „ ์ง€ํœ˜", "์˜ˆ์‹œ": "ํ”Œ๋žซํผ ์ž…์ ์‚ฌ ์ž์ฒด ๋งˆ์ผ“"},
    {"๊ณ„": "๋ฏธ์ธ๊ณ„", "์š”์•ฝ": "๋งค๋ ฅ์œผ๋กœ ํŒ๋‹จ ํ๋ฆฌ๊ธฐ", "์กฐ๊ฑด": "์œ ํ˜น ๊ฐ€๋Šฅ", "ํ–‰๋™": "๊ฐ์ •ยท๋งค๋ ฅ ํ™œ์šฉ", "๋ชฉ์ ": "๊ฒฐ์ • ์™œ๊ณก", "์˜ˆ์‹œ": "์ง€์—ญ ํˆฌ์ž๋กœ ์ •์น˜์ธ ํ˜ธ๊ฐ ์–ป๊ธฐ"},
    {"๊ณ„": "๊ณต์„ฑ๊ณ„", "์š”์•ฝ": "ํ…… ๋นˆ ์„ฑ๋ฌธ ์—ด์–ด๋†“๊ธฐ", "์กฐ๊ฑด": "๋ณ‘๋ ฅ ๋ถ€์กฑ", "ํ–‰๋™": "๊ณผ๊ฐํžˆ ๊ณต๊ฐœ", "๋ชฉ์ ": "์ƒ๋Œ€ ์˜์‹ฌ", "์˜ˆ์‹œ": "๋‚ด๋ถ€์ž๋ฃŒ ์ „๋ฉด ๊ณต๊ฐœ"},
    {"๊ณ„": "๋ฐ˜๊ฐ„๊ณ„", "์š”์•ฝ": "๊ฐ€์งœ ์ŠคํŒŒ์ด ์—ญ์ด์šฉ", "์กฐ๊ฑด": "๋‚ด๋ถ€ ๋ถˆ์‹  ์š”์†Œ", "ํ–‰๋™": "๊ต๋ž€ ์ •๋ณด", "๋ชฉ์ ": "๋ถ„์—ด", "์˜ˆ์‹œ": "๊ฒฝ์Ÿ์‚ฌ์— ๊ฐ€์งœ ๋ฃจ๋จธ"},
    {"๊ณ„": "๊ณ ์œก๊ณ„", "์š”์•ฝ": "์‚ด ๋‚ด์ฃผ๊ณ  ๋ผˆ ์ทจํ•˜๊ธฐ", "์กฐ๊ฑด": "์‹ ๋ขฐ ์ƒ์‹ค", "ํ–‰๋™": "์Šค์Šค๋กœ ์†์‹ค", "๋ชฉ์ ": "์ง„์ •์„ฑ ์ฆ๋ช…", "์˜ˆ์‹œ": "CEO ๋ณด๋„ˆ์Šค ๋ฐ˜๋‚ฉ"},
    {"๊ณ„": "์—ฐํ™˜๊ณ„", "์š”์•ฝ": "์‚ฌ์Šฌ๋กœ ํ•œ๊บผ๋ฒˆ์—", "์กฐ๊ฑด": "๋ณต์ˆ˜ ๋Œ€์ƒ ๋‹ค์ˆ˜", "ํ–‰๋™": "์—ฐ๊ฒฐ ๋ฌถ๊ธฐ", "๋ชฉ์ ": "ํšจ์œจ ํƒ€๊ฒฉ", "์˜ˆ์‹œ": "ํŒจํ‚ค์ง€ ์ œ์žฌ์•ˆ"},
    {"๊ณ„": "์ฃผ์œ„์ƒ๊ณ„", "์š”์•ฝ": "๋„๋ง์ด ์ƒ์ฑ…", "์กฐ๊ฑด": "์Šน์‚ฐ ์—†์Œ", "ํ–‰๋™": "์ฆ‰์‹œ ํ›„ํ‡ด", "๋ชฉ์ ": "์†์‹ค ์ตœ์†Œยท์žฌ๊ธฐ", "์˜ˆ์‹œ": "์ ์ž ์‹œ์žฅ ์ฒ ์ˆ˜"}
]

physical_transformation_categories = {
    "์„ผ์„œ ๊ธฐ๋Šฅ": [
        "์‹œ๊ฐ ์„ผ์„œ", "์‹œ๊ฐ ๊ฐ์ง€", "์ฒญ๊ฐ ์„ผ์„œ", "์ฒญ๊ฐ ๊ฐ์ง€", "์ด‰๊ฐ ์„ผ์„œ", "์ด‰๊ฐ ๊ฐ์ง€",
        "๋ฏธ๊ฐ ์„ผ์„œ", "๋ฏธ๊ฐ ๊ฐ์ง€", "ํ›„๊ฐ ์„ผ์„œ", "ํ›„๊ฐ ๊ฐ์ง€", "์˜จ๋„ ์„ผ์„œ", "์˜จ๋„ ๊ฐ์ง€",
        "์Šต๋„ ์„ผ์„œ", "์Šต๋„ ๊ฐ์ง€", "์••๋ ฅ ์„ผ์„œ", "์••๋ ฅ ๊ฐ์ง€", "๊ฐ€์†๋„ ์„ผ์„œ", "๊ฐ€์†๋„ ๊ฐ์ง€",
        "ํšŒ์ „ ์„ผ์„œ", "ํšŒ์ „ ๊ฐ์ง€", "๊ทผ์ ‘ ์„ผ์„œ", "๊ทผ์ ‘ ๊ฐ์ง€", "์œ„์น˜ ์„ผ์„œ", "์œ„์น˜ ๊ฐ์ง€",
        "์šด๋™ ์„ผ์„œ", "์šด๋™ ๊ฐ์ง€", "๊ฐ€์Šค ์„ผ์„œ", "๊ฐ€์Šค ๊ฐ์ง€", "์ ์™ธ์„  ์„ผ์„œ", "์ ์™ธ์„  ๊ฐ์ง€",
        "์ž์™ธ์„  ์„ผ์„œ", "์ž์™ธ์„  ๊ฐ์ง€", "๋ฐฉ์‚ฌ์„  ์„ผ์„œ", "๋ฐฉ์‚ฌ์„  ๊ฐ์ง€", "์ž๊ธฐ์žฅ ์„ผ์„œ", "์ž๊ธฐ์žฅ ๊ฐ์ง€",
        "์ „๊ธฐ์žฅ ์„ผ์„œ", "์ „๊ธฐ์žฅ ๊ฐ์ง€", "ํ™”ํ•™๋ฌผ์งˆ ์„ผ์„œ", "ํ™”ํ•™๋ฌผ์งˆ ๊ฐ์ง€", "์ƒ์ฒด์‹ ํ˜ธ ์„ผ์„œ", "์ƒ์ฒด์‹ ํ˜ธ ๊ฐ์ง€",
        "์ง„๋™ ์„ผ์„œ", "์ง„๋™ ๊ฐ์ง€", "์†Œ์Œ ์„ผ์„œ", "์†Œ์Œ ๊ฐ์ง€", "๋น› ์„ธ๊ธฐ ์„ผ์„œ", "๋น› ์„ธ๊ธฐ ๊ฐ์ง€",
        "๋น› ํŒŒ์žฅ ์„ผ์„œ", "๋น› ํŒŒ์žฅ ๊ฐ์ง€", "๊ธฐ์šธ๊ธฐ ์„ผ์„œ", "๊ธฐ์šธ๊ธฐ ๊ฐ์ง€", "pH ์„ผ์„œ", "pH ๊ฐ์ง€",
        "์ „๋ฅ˜ ์„ผ์„œ", "์ „๋ฅ˜ ๊ฐ์ง€", "์ „์•• ์„ผ์„œ", "์ „์•• ๊ฐ์ง€", "์ด๋ฏธ์ง€ ์„ผ์„œ", "์ด๋ฏธ์ง€ ๊ฐ์ง€",
        "๊ฑฐ๋ฆฌ ์„ผ์„œ", "๊ฑฐ๋ฆฌ ๊ฐ์ง€", "๊นŠ์ด ์„ผ์„œ", "๊นŠ์ด ๊ฐ์ง€", "์ค‘๋ ฅ ์„ผ์„œ", "์ค‘๋ ฅ ๊ฐ์ง€",
        "์†๋„ ์„ผ์„œ", "์†๋„ ๊ฐ์ง€", "ํ๋ฆ„ ์„ผ์„œ", "ํ๋ฆ„ ๊ฐ์ง€", "์ˆ˜์œ„ ์„ผ์„œ", "์ˆ˜์œ„ ๊ฐ์ง€",
        "ํƒ๋„ ์„ผ์„œ", "ํƒ๋„ ๊ฐ์ง€", "์—ผ๋„ ์„ผ์„œ", "์—ผ๋„ ๊ฐ์ง€", "๊ธˆ์† ๊ฐ์ง€", "์••์ „ ์„ผ์„œ",
        "์••์ „ ๊ฐ์ง€", "๊ด‘์ „ ์„ผ์„œ", "๊ด‘์ „ ๊ฐ์ง€", "์—ด์ „๋Œ€ ์„ผ์„œ", "์—ด์ „๋Œ€ ๊ฐ์ง€", "ํ™€ ํšจ๊ณผ ์„ผ์„œ",
        "ํ™€ ํšจ๊ณผ ๊ฐ์ง€", "์ดˆ์ŒํŒŒ ์„ผ์„œ", "์ดˆ์ŒํŒŒ ๊ฐ์ง€", "๋ ˆ์ด๋” ์„ผ์„œ", "๋ ˆ์ด๋” ๊ฐ์ง€",
        "๋ผ์ด๋‹ค ์„ผ์„œ", "๋ผ์ด๋‹ค ๊ฐ์ง€", "ํ„ฐ์น˜ ์„ผ์„œ", "ํ„ฐ์น˜ ๊ฐ์ง€", "์ œ์Šค์ฒ˜ ์„ผ์„œ", "์ œ์Šค์ฒ˜ ๊ฐ์ง€",
        "์‹ฌ๋ฐ• ์„ผ์„œ", "์‹ฌ๋ฐ• ๊ฐ์ง€", "ํ˜ˆ์•• ์„ผ์„œ", "ํ˜ˆ์•• ๊ฐ์ง€"
    ],
    "ํฌ๊ธฐ์™€ ํ˜•ํƒœ ๋ณ€ํ™”": [
        "๋ถ€ํ”ผ ๋Š˜์–ด๋‚จ", "๋ถ€ํ”ผ ์ค„์–ด๋“ฆ", "๊ธธ์ด ๋Š˜์–ด๋‚จ", "๊ธธ์ด ์ค„์–ด๋“ฆ", "๋„ˆ๋น„ ๋Š˜์–ด๋‚จ", "๋„ˆ๋น„ ์ค„์–ด๋‚จ",
        "๋†’์ด ๋Š˜์–ด๋‚จ", "๋†’์ด ์ค„์–ด๋“ฆ", "๋ฐ€๋„ ๋ณ€ํ™”", "๋ฌด๊ฒŒ ์ฆ๊ฐ€", "๋ฌด๊ฒŒ ๊ฐ์†Œ", "๋ชจ์–‘ ๋ณ€ํ˜•",
        "์ƒํƒœ ๋ณ€ํ™”", "๋ถˆ๊ท ๋“ฑ ๋ณ€ํ˜•", "๋ณต์žกํ•œ ํ˜•ํƒœ ๋ณ€ํ˜•", "๋น„ํ‹€๋ฆผ", "๊ผฌ์ž„", "๋ถˆ๊ท ์ผํ•œ ํ™•์žฅ",
        "๋ถˆ๊ท ์ผํ•œ ์ถ•์†Œ", "๋ชจ์„œ๋ฆฌ ๋‘ฅ๊ธ€๊ฒŒ", "๋ชจ์„œ๋ฆฌ ๋‚ ์นด๋กญ๊ฒŒ", "๊นจ์ง", "๊ฐˆ๋ผ์ง", "์—ฌ๋Ÿฌ ์กฐ๊ฐ ๋‚˜๋ˆ ์ง",
        "๋ฌผ ์ €ํ•ญ", "๋จผ์ง€ ์ €ํ•ญ", "์ฐŒ๊ทธ๋Ÿฌ์ง", "๋ณต์›", "์ ‘ํž˜", "ํŽผ์ณ์ง", "์••์ฐฉ", "ํŒฝ์ฐฝ",
        "๋Š˜์–ด๋‚จ", "์ˆ˜์ถ•", "๊ตฌ๊ฒจ์ง", "ํ‰ํ‰ํ•ด์ง", "๋ญ‰๊ฐœ์ง", "๋‹จ๋‹จํ•ด์ง", "๋ง๋ฆผ", "ํŽด์ง",
        "๊บพ์ž„", "๊ตฌ๋ถ€๋Ÿฌ์ง"
    ],
    "ํ‘œ๋ฉด ๋ฐ ์™ธ๊ด€ ๋ณ€ํ™”": [
        "์ƒ‰์ƒ ๋ณ€ํ™”", "์งˆ๊ฐ ๋ณ€ํ™”", "ํˆฌ๋ช… ๋ณ€ํ™”", "๋ถˆํˆฌ๋ช… ๋ณ€ํ™”", "๋ฐ˜์ง์ž„ ๋ณ€ํ™”", "๋ฌด๊ด‘ ๋ณ€ํ™”",
        "๋น› ๋ฐ˜์‚ฌ ์ •๋„ ๋ณ€ํ™”", "๋ฌด๋Šฌ ๋ณ€ํ™”", "๊ฐ๋„์— ๋”ฐ๋ฅธ ์ƒ‰์ƒ ๋ณ€ํ™”", "๋น›์— ๋”ฐ๋ฅธ ์ƒ‰์ƒ ๋ณ€ํ™”",
        "์˜จ๋„์— ๋”ฐ๋ฅธ ์ƒ‰์ƒ ๋ณ€ํ™”", "ํ™€๋กœ๊ทธ๋žจ ํšจ๊ณผ", "ํ‘œ๋ฉด ๊ฐ๋„๋ณ„ ๋น› ๋ฐ˜์‚ฌ", "ํ‘œ๋ฉด ๋ชจ์–‘ ๋ณ€ํ˜•",
        "์ดˆ๋ฏธ์„ธ ํ‘œ๋ฉด ๊ตฌ์กฐ ๋ณ€ํ™”", "์ž๊ฐ€ ์„ธ์ • ํšจ๊ณผ", "์–ผ๋ฃฉ ์ƒ์„ฑ", "ํŒจํ„ด ์ƒ์„ฑ", "ํ๋ฆผ ๋ณ€ํ™”",
        "์„ ๋ช…ํ•จ ๋ณ€ํ™”", "๊ด‘ํƒ ๋ณ€ํ™”", "์œค๊ธฐ ๋ณ€ํ™”", "์ƒ‰์กฐ ๋ณ€ํ™”", "์ฑ„๋„ ๋ณ€ํ™”", "๋ฐœ๊ด‘",
        "ํ˜•๊ด‘", "๋น› ์‚ฐ๋ž€ ํšจ๊ณผ", "๋น› ํก์ˆ˜ ๋ณ€ํ™”", "๋ฐ˜ํˆฌ๋ช… ํšจ๊ณผ", "๊ทธ๋ฆผ์ž ํšจ๊ณผ ๋ณ€ํ™”",
        "์ž์™ธ์„  ๋ฐ˜์‘ ๋ณ€ํ™”", "์•ผ๊ด‘ ํšจ๊ณผ"
    ],
    "๋ฌผ์งˆ์˜ ์ƒํƒœ ๋ณ€ํ™”": [
        "๊ณ ์ฒด ์ „ํ™˜", "์•ก์ฒด ์ „ํ™˜", "๊ธฐ์ฒด ์ „ํ™˜", "๊ฒฐ์ •ํ™”", "์šฉํ•ด", "์‚ฐํ™”", "๋ถ€์‹",
        "๋”ฑ๋”ฑํ•ด์ง", "๋ถ€๋“œ๋Ÿฌ์›Œ์ง", "ํŠน์ˆ˜ ์ƒํƒœ ์ „ํ™˜", "๋ฌด์ •ํ˜• ์ „ํ™˜", "๊ฒฐ์ •ํ˜• ์ „ํ™˜", "์„ฑ๋ถ„ ๋ถ„๋ฆฌ",
        "๋ฏธ์„ธ ์ž…์ž ํ˜•์„ฑ", "๋ฏธ์„ธ ์ž…์ž ๋ถ„ํ•ด", "์ ค ํ˜•์„ฑ", "์ ค ํ’€์–ด์ง", "์ค€์•ˆ์ • ์ƒํƒœ ๋ณ€ํ™”",
        "๋ถ„์ž ์ž๊ฐ€ ์ •๋ ฌ", "๋ถ„์ž ์ž๊ฐ€ ๋ถ„ํ•ด", "์ƒํƒœ๋ณ€ํ™” ์ง€์—ฐ ํ˜„์ƒ", "๋…น์Œ", "๊ตณ์Œ",
        "์ฆ๋ฐœ", "์‘์ถ•", "์Šนํ™”", "์ฆ์ฐฉ", "์นจ์ „", "๋ถ€์œ ", "๋ถ„์‚ฐ", "์‘์ง‘",
        "๊ฑด์กฐ", "์Šต์œค", "ํŒฝ์œค", "์ˆ˜์ถ•", "๋™๊ฒฐ", "ํ•ด๋™", "ํ’ํ™”", "์นจ์‹",
        "์ถฉ์ „", "๋ฐฉ์ „", "๊ฒฐํ•ฉ", "๋ถ„๋ฆฌ", "๋ฐœํšจ", "๋ถ€ํŒจ"
    ],
    "์›€์ง์ž„ ํŠน์„ฑ ๋ณ€ํ™”": [
        "๊ฐ€์†", "๊ฐ์†", "์ผ์ • ์†๋„ ์œ ์ง€", "์ง„๋™", "์ง„๋™ ๊ฐ์†Œ", "๋ถ€๋”ชํž˜", "ํŠ•๊น€",
        "ํšŒ์ „ ์†๋„ ์ฆ๊ฐ€", "ํšŒ์ „ ์†๋„ ๊ฐ์†Œ", "ํšŒ์ „ ๋ฐฉํ–ฅ ๋ณ€ํ™”", "๋ถˆ๊ทœ์น™ ์›€์ง์ž„", "๋ฉˆ์ท„๋‹ค", "๋ฏธ๋„๋Ÿฌ์ง€๋Š” ํ˜„์ƒ",
        "๊ณต์ง„", "๋ฐ˜๊ณต์ง„", "์œ ์ฒด ์† ์ €ํ•ญ ๋ณ€ํ™”", "์œ ์ฒด ์† ์–‘๋ ฅ ๋ณ€ํ™”", "์›€์ง์ž„ ์ €ํ•ญ ๋ณ€ํ™”",
        "๋ณตํ•ฉ ์ง„๋™ ์›€์ง์ž„", "ํŠน์ˆ˜ ์œ ์ฒด ์† ์›€์ง์ž„", "ํšŒ์ „-์ด๋™ ์—ฐ๊ณ„ ์›€์ง์ž„", "๊ด€์„ฑ ์ •์ง€",
        "์ถฉ๊ฒฉ ํก์ˆ˜", "์ถฉ๊ฒฉ ์ „๋‹ฌ", "์šด๋™๋Ÿ‰ ๋ณด์กด", "๋งˆ์ฐฐ๋ ฅ ๋ณ€ํ™”", "๊ด€์„ฑ ํƒˆ์ถœ", "๋ถˆ์•ˆ์ • ๊ท ํ˜•",
        "๋™์  ์•ˆ์ •์„ฑ", "ํ”๋“ค๋ฆผ ๊ฐ์‡ ", "๊ฒฝ๋กœ ์˜ˆ์ธก์„ฑ", "ํšŒํ”ผ ์›€์ง์ž„"
    ],
    "๊ตฌ์กฐ์  ๋ณ€ํ™”": [
        "๋ถ€ํ’ˆ ์ถ”๊ฐ€", "๋ถ€ํ’ˆ ์ œ๊ฑฐ", "์กฐ๋ฆฝ", "๋ถ„ํ•ด", "์ ‘๊ธฐ", "ํŽด๊ธฐ", "๋ณ€ํ˜•", "์›์ƒ๋ณต๊ตฌ",
        "์ตœ์  ๊ตฌ์กฐ ๋ณ€ํ™”", "์ž๊ฐ€ ์žฌ๋ฐฐ์—ด", "์ž์—ฐ ํŒจํ„ด ํ˜•์„ฑ", "์ž์—ฐ ํŒจํ„ด ์†Œ๋ฉธ", "๊ทœ์น™์  ํŒจํ„ด ๋ณ€ํ™”",
        "๋ชจ๋“ˆ์‹ ๋ณ€ํ˜•", "๋ณต์žก์„ฑ ์ฆ๊ฐ€ ๊ตฌ์กฐ", "์›๋ž˜ ๋ชจ์–‘ ๊ธฐ์–ต ํšจ๊ณผ", "์‹œ๊ฐ„์— ๋”ฐ๋ฅธ ํ˜•ํƒœ ๋ณ€ํ™”",
        "๋ถ€๋ถ„ ์ œ๊ฑฐ", "๋ถ€๋ถ„ ๊ต์ฒด", "๊ฒฐํ•ฉ", "๋ถ„๋ฆฌ", "๋ถ„ํ• ", "ํ†ตํ•ฉ", "์ค‘์ฒฉ", "๊ฒน์นจ",
        "๋‚ด๋ถ€ ๊ตฌ์กฐ ๋ณ€ํ™”", "์™ธ๋ถ€ ๊ตฌ์กฐ ๋ณ€ํ™”", "์ค‘์‹ฌ์ถ• ์ด๋™", "๊ท ํ˜•์  ๋ณ€ํ™”", "๊ณ„์ธต ๊ตฌ์กฐ ๋ณ€ํ™”",
        "์ง€์ง€ ๊ตฌ์กฐ ๋ณ€ํ™”", "์‘๋ ฅ ๋ถ„์‚ฐ ๊ตฌ์กฐ", "์ถฉ๊ฒฉ ํก์ˆ˜ ๊ตฌ์กฐ", "๊ทธ๋ฆฌ๋“œ ๊ตฌ์กฐ ๋ณ€ํ™”", "๋งคํŠธ๋ฆญ์Šค ๊ตฌ์กฐ ๋ณ€ํ™”",
        "์ƒํ˜ธ ์—ฐ๊ฒฐ์„ฑ ๋ณ€ํ™”"
    ],
    "๊ณต๊ฐ„ ์ด๋™": [
        "์•ž ์ด๋™", "๋’ค ์ด๋™", "์ขŒ ์ด๋™", "์šฐ ์ด๋™", "์œ„ ์ด๋™", "์•„๋ž˜ ์ด๋™",
        "์„ธ๋กœ์ถ• ํšŒ์ „(๊ณ ๊ฐœ ๋„๋•์ž„)", "๊ฐ€๋กœ์ถ• ํšŒ์ „(๊ณ ๊ฐœ ์ “๊ธฐ)", "๊ธธ์ด์ถ• ํšŒ์ „(์˜†์œผ๋กœ ๊ธฐ์šธ์ž„)", "์› ์šด๋™",
        "๋‚˜์„ ํ˜• ์ด๋™", "๊ด€์„ฑ์— ์˜ํ•œ ๋ฏธ๋„๋Ÿฌ์ง", "ํšŒ์ „์ถ• ๋ณ€ํ™”", "๋ถˆ๊ทœ์น™ ํšŒ์ „", "ํ”๋“ค๋ฆผ ์šด๋™",
        "ํฌ๋ฌผ์„  ์ด๋™", "๋ฌด์ค‘๋ ฅ ๋ถ€์œ ", "์ˆ˜๋ฉด ์œ„ ๋ถ€์œ ", "์ ํ”„", "๋„์•ฝ", "์Šฌ๋ผ์ด๋”ฉ", "๋กค๋ง",
        "์ž์œ  ๋‚™ํ•˜", "์™•๋ณต ์šด๋™", "ํƒ„์„ฑ ํŠ•๊น€", "๊ด€ํ†ต", "ํšŒํ”ผ ์›€์ง์ž„", "์ง€๊ทธ์žฌ๊ทธ ์ด๋™", "์Šค์œ™ ์šด๋™"
    ],
    "์‹œ๊ฐ„ ๊ด€๋ จ ๋ณ€ํ™”": [
        "๋…ธํ™”", "ํ’ํ™”", "๋งˆ๋ชจ", "๋ถ€์‹", "์ƒ‰ ๋ฐ”๋žจ", "๋ณ€์ƒ‰", "์†์ƒ", "ํšŒ๋ณต",
        "์ˆ˜๋ช… ์ฃผ๊ธฐ ๋ณ€ํ™”", "์‚ฌ์šฉ์ž ์ƒํ˜ธ์ž‘์šฉ์— ๋”ฐ๋ฅธ ์ ์‘", "ํ•™์Šต ๊ธฐ๋ฐ˜ ํ˜•ํƒœ ์ตœ์ ํ™”", "์‹œ๊ฐ„์— ๋”ฐ๋ฅธ ๋ฌผ์„ฑ ๋ณ€ํ™”",
        "์ง‘๋‹จ ๊ธฐ์–ต ํšจ๊ณผ", "๋ฌธํ™”์  ์˜๋ฏธ ๋ณ€ํ™”", "์ง€์—ฐ ๋ฐ˜์‘", "์ด์ „ ์ƒํƒœ ์˜์กด ๋ณ€ํ™”", "์ ์ง„์  ์‹œ๊ฐ„ ๋ณ€ํ™”",
        "์ง„ํ™”์  ๋ณ€ํ™”", "์ฃผ๊ธฐ์  ์žฌ์ƒ", "๊ณ„์ ˆ ๋ณ€ํ™” ์ ์‘", "์ƒ์ฒด๋ฆฌ๋“ฌ ๋ณ€ํ™”", "์ƒ์•  ์ฃผ๊ธฐ ๋‹จ๊ณ„",
        "์„ฑ์žฅ", "ํ‡ดํ™”", "์ž๊ฐ€ ๋ณต๊ตฌ", "์ž๊ฐ€ ์žฌ์ƒ", "์ž์—ฐ ์ˆœํ™˜ ์ ์‘", "์ง€์†์„ฑ", "์ผ์‹œ์„ฑ",
        "๊ธฐ์–ต ํšจ๊ณผ", "์ง€์—ฐ๋œ ์ž‘์šฉ", "๋ˆ„์  ํšจ๊ณผ"
    ],
    "๋น›๊ณผ ์‹œ๊ฐ ํšจ๊ณผ": [
        "๋ฐœ๊ด‘", "์†Œ๋“ฑ", "๋น› ํˆฌ๊ณผ", "๋น› ์ฐจ๋‹จ", "๋น› ์‚ฐ๋ž€", "๋น› ์ง‘์ค‘", "์ƒ‰์ƒ ์ŠคํŽ™ํŠธ๋Ÿผ ๋ณ€ํ™”",
        "๋น› ํšŒ์ ˆ", "๋น› ๊ฐ„์„ญ", "ํ™€๋กœ๊ทธ๋žจ ์ƒ์„ฑ", "๋ ˆ์ด์ € ํšจ๊ณผ", "๋น› ํŽธ๊ด‘", "ํ˜•๊ด‘", "์ธ๊ด‘",
        "์ž์™ธ์„  ๋ฐœ๊ด‘", "์ ์™ธ์„  ๋ฐœ๊ด‘", "๊ด‘ํ•™์  ์ฐฉ์‹œ", "๋น› ๊ตด์ ˆ", "๊ทธ๋ฆผ์ž ์ƒ์„ฑ", "๊ทธ๋ฆผ์ž ์ œ๊ฑฐ",
        "์ƒ‰์ˆ˜์ฐจ ํšจ๊ณผ", "๋ฌด์ง€๊ฐœ ํšจ๊ณผ", "๊ธ€๋กœ์šฐ ํšจ๊ณผ", "ํ”Œ๋ž˜์‹œ ํšจ๊ณผ", "์กฐ๋ช… ํŒจํ„ด", "๋น” ํšจ๊ณผ",
        "๊ด‘ ํ•„ํ„ฐ ํšจ๊ณผ", "๋น›์˜ ๋ฐฉํ–ฅ์„ฑ ๋ณ€ํ™”", "ํˆฌ์˜ ํšจ๊ณผ", "๋น› ๊ฐ์ง€", "๋น› ๋ฐ˜์‘", "๊ด‘๋„ ๋ณ€ํ™”"
    ],
    "์†Œ๋ฆฌ์™€ ์ง„๋™ ํšจ๊ณผ": [
        "์†Œ๋ฆฌ ๋ฐœ์ƒ", "์†Œ๋ฆฌ ์†Œ๋ฉธ", "์Œ ๋†’๋‚ฎ์ด ๋ณ€ํ™”", "์Œ๋Ÿ‰ ๋ณ€ํ™”", "์Œ์ƒ‰ ๋ณ€ํ™”", "๊ณต๋ช…",
        "๋ฐ˜๊ณต๋ช…", "์Œํ–ฅ ์ง„๋™", "์ดˆ์ŒํŒŒ ๋ฐœ์ƒ", "์ €์ŒํŒŒ ๋ฐœ์ƒ", "์†Œ๋ฆฌ ์ง‘์ค‘", "์†Œ๋ฆฌ ๋ถ„์‚ฐ",
        "์Œํ–ฅ ๋ฐ˜์‚ฌ", "์Œํ–ฅ ํก์ˆ˜", "์Œํ–ฅ ๋„ํ”Œ๋Ÿฌ ํšจ๊ณผ", "์ŒํŒŒ ๊ฐ„์„ญ", "์Œํ–ฅ ๊ณต์ง„", "์ง„๋™ ํŒจํ„ด ๋ณ€ํ™”",
        "ํƒ€์•… ํšจ๊ณผ", "์Œํ–ฅ ํ”ผ๋“œ๋ฐฑ", "์Œํ–ฅ ์ฐจํ", "์Œํ–ฅ ์ฆํญ", "์†Œ๋ฆฌ ์ง€ํ–ฅ์„ฑ", "์†Œ๋ฆฌ ์™œ๊ณก",
        "๋น„ํŠธ ์ƒ์„ฑ", "๋ฐฐ์Œ ์ƒ์„ฑ", "์ฃผํŒŒ์ˆ˜ ๋ณ€์กฐ", "์Œํ–ฅ ์ถฉ๊ฒฉํŒŒ", "์Œํ–ฅ ํ•„ํ„ฐ๋ง"
    ],
    "์—ด ๊ด€๋ จ ๋ณ€ํ™”": [
        "์˜จ๋„ ์ƒ์Šน", "์˜จ๋„ ํ•˜๊ฐ•", "์—ด ํŒฝ์ฐฝ", "์—ด ์ˆ˜์ถ•", "์—ด ์ „๋‹ฌ", "์—ด ์ฐจ๋‹จ", "์••๋ ฅ ์ƒ์Šน",
        "์••๋ ฅ ํ•˜๊ฐ•", "์—ด ๋ณ€ํ™”์— ๋”ฐ๋ฅธ ์žํ™”", "์—”ํŠธ๋กœํ”ผ ๋ณ€ํ™”", "์—ด์ „๊ธฐ ํšจ๊ณผ", "์ž๊ธฐ์žฅ์— ์˜ํ•œ ์—ด ๋ณ€ํ™”",
        "์ƒํƒœ ๋ณ€ํ™” ์ค‘ ์—ด ์ €์žฅ", "์ƒํƒœ ๋ณ€ํ™” ์ค‘ ์—ด ๋ฐฉ์ถœ", "์—ด ์ŠคํŠธ๋ ˆ์Šค ๋ฐœ์ƒ", "์—ด ์ŠคํŠธ๋ ˆ์Šค ํ•ด์†Œ",
        "๊ธ‰๊ฒฉํ•œ ์˜จ๋„ ๋ณ€ํ™” ์˜ํ–ฅ", "๋ณต์‚ฌ ๋ƒ‰๊ฐ", "๋ณต์‚ฌ ๊ฐ€์—ด", "๋ฐœ์—ด", "ํก์—ด", "์—ด ๋ถ„ํฌ ๋ณ€ํ™”",
        "์—ด ๋ฐ˜์‚ฌ", "์—ด ํก์ˆ˜", "๋ƒ‰๊ฐ ์‘์ถ•", "์—ด ํ™œ์„ฑํ™”", "์—ด ๋ณ€์ƒ‰", "์—ด ํŒฝ์ฐฝ ๊ณ„์ˆ˜ ๋ณ€ํ™”",
        "์—ด ์•ˆ์ •์„ฑ ๋ณ€ํ™”", "๋‚ด์—ด์„ฑ", "๋‚ดํ•œ์„ฑ", "์ž๊ฐ€ ๋ฐœ์—ด", "์—ด์  ํ‰ํ˜•", "์—ด์  ๋ถˆ๊ท ํ˜•",
        "์—ด์  ๋ณ€ํ˜•", "์—ด ๋ถ„์‚ฐ", "์—ด ์ง‘์ค‘"
    ],
    "์ „๊ธฐ ๋ฐ ์ž๊ธฐ ๋ณ€ํ™”": [
        "์ž์„ฑ ์ƒ์„ฑ", "์ž์„ฑ ์†Œ๋ฉธ", "์ „ํ•˜๋Ÿ‰ ์ฆ๊ฐ€", "์ „ํ•˜๋Ÿ‰ ๊ฐ์†Œ", "์ „๊ธฐ์žฅ ์ƒ์„ฑ", "์ „๊ธฐ์žฅ ์†Œ๋ฉธ",
        "์ž๊ธฐ์žฅ ์ƒ์„ฑ", "์ž๊ธฐ์žฅ ์†Œ๋ฉธ", "์ดˆ์ „๋„ ์ƒํƒœ ์ „ํ™˜", "๊ฐ•์œ ์ „์ฒด ํŠน์„ฑ ๋ณ€ํ™”", "์–‘์ž ์ƒํƒœ ๋ณ€ํ™”",
        "ํ”Œ๋ผ์ฆˆ๋งˆ ํ˜•์„ฑ", "ํ”Œ๋ผ์ฆˆ๋งˆ ์†Œ๋ฉธ", "์Šคํ•€ํŒŒ ์ „๋‹ฌ", "๋น›์— ์˜ํ•œ ์ „๊ธฐ ๋ฐœ์ƒ", "์••๋ ฅ์— ์˜ํ•œ ์ „๊ธฐ ๋ฐœ์ƒ",
        "์ž๊ธฐ์žฅ ๋‚ด ์ „๋ฅ˜ ๋ณ€ํ™”", "์ „๊ธฐ ์ €ํ•ญ ๋ณ€ํ™”", "์ „๊ธฐ ์ „๋„์„ฑ ๋ณ€ํ™”", "์ •์ „๊ธฐ ๋ฐœ์ƒ", "์ •์ „๊ธฐ ๋ฐฉ์ „",
        "์ „์ž๊ธฐ ์œ ๋„", "์ „์ž๊ธฐํŒŒ ๋ฐฉ์ถœ", "์ „์ž๊ธฐํŒŒ ํก์ˆ˜", "์ „๊ธฐ ์šฉ๋Ÿ‰ ๋ณ€ํ™”", "์ž๊ธฐ ์ด๋ ฅ ํ˜„์ƒ",
        "์ „๊ธฐ์  ๋ถ„๊ทน", "์ „์ž ํ๋ฆ„ ๋ฐฉํ–ฅ ๋ณ€ํ™”", "์ „๊ธฐ์  ๊ณต๋ช…", "์ „๊ธฐ์  ์ฐจํ", "์ „๊ธฐ์  ๋…ธ์ถœ",
        "์ž๊ธฐ ์ฐจํ", "์ž๊ธฐ ๋…ธ์ถœ", "์ž๊ธฐ์žฅ ์ •๋ ฌ"
    ],
    "ํ™”ํ•™์  ๋ณ€ํ™”": [
        "ํ‘œ๋ฉด ์ฝ”ํŒ… ๋ณ€ํ™”", "๋ฌผ์งˆ ์„ฑ๋ถ„ ๋ณ€ํ™”", "ํ™”ํ•™ ๋ฐ˜์‘ ๋ณ€ํ™”", "์ด‰๋งค ์ž‘์šฉ ์‹œ์ž‘/์ค‘๋‹จ",
        "๋น›์— ์˜ํ•œ ํ™”ํ•™ ๋ฐ˜์‘", "์ „๊ธฐ์— ์˜ํ•œ ํ™”ํ•™ ๋ฐ˜์‘", "๋‹จ๋ถ„์ž๋ง‰ ํ˜•์„ฑ", "๋ถ„์ž ์ˆ˜์ค€ ๊ตฌ์กฐ ๋ณ€ํ™”",
        "์ƒ์ฒด ๋ชจ๋ฐฉ ํ‘œ๋ฉด ๋ณ€ํ™”", "ํ™˜๊ฒฝ ๋ฐ˜์‘ํ˜• ๋ฌผ์งˆ ๋ณ€ํ™”", "์ฃผ๊ธฐ์  ํ™”ํ•™ ๋ฐ˜์‘", "์‚ฐํ™”", "ํ™˜์›",
        "๊ณ ๋ถ„์žํ™”", "๋ฌผ ๋ถ„ํ•ด", "ํ™”ํ•ฉ", "๋ฐฉ์‚ฌ์„  ์˜ํ–ฅ", "์‚ฐ-์—ผ๊ธฐ ๋ฐ˜์‘", "์ค‘ํ™” ๋ฐ˜์‘",
        "์ด์˜จํ™”", "ํ™”ํ•™์  ํก์ฐฉ/ํƒˆ์ฐฉ", "์ด‰๋งค ํšจ์œจ ๋ณ€ํ™”", "ํšจ์†Œ ํ™œ์„ฑ ๋ณ€ํ™”", "๋ฐœ์ƒ‰ ๋ฐ˜์‘",
        "pH ๋ณ€ํ™”", "ํ™”ํ•™์  ํ‰ํ˜• ์ด๋™", "๊ฒฐํ•ฉ ํ˜•์„ฑ/๋ถ„ํ•ด", "์šฉํ•ด๋„ ๋ณ€ํ™”"
    ],
    "์ƒ๋ฌผํ•™์  ๋ณ€ํ™”": [
        "์„ฑ์žฅ/์œ„์ถ•", "์„ธํฌ ๋ถ„์—ด/์‚ฌ๋ฉธ", "์ƒ๋ฌผ ๋ฐœ๊ด‘", "์‹ ์ง„๋Œ€์‚ฌ ๋ณ€ํ™”", "๋ฉด์—ญ ๋ฐ˜์‘",
        "ํ˜ธ๋ฅด๋ชฌ ๋ถ„๋น„", "์‹ ๊ฒฝ ๋ฐ˜์‘", "์œ ์ „์  ๋ฐœํ˜„", "์ ์‘/์ง„ํ™”", "์ƒ์ฒด๋ฆฌ๋“ฌ ๋ณ€ํ™”",
        "์žฌ์ƒ/์น˜์œ ", "๋…ธํ™”/์„ฑ์ˆ™", "์ƒ์ฒด ๋ชจ๋ฐฉ ๋ณ€ํ™”", "๋ฐ”์ด์˜คํ•„๋ฆ„ ํ˜•์„ฑ", "์ƒ๋ฌผํ•™์  ๋ถ„ํ•ด",
        "ํšจ์†Œ ํ™œ์„ฑํ™”/๋น„ํ™œ์„ฑํ™”", "์ƒ๋ฌผํ•™์  ์‹ ํ˜ธ ์ „๋‹ฌ", "์ŠคํŠธ๋ ˆ์Šค ๋ฐ˜์‘", "์ฒด์˜จ ์กฐ์ ˆ", "์ƒ๋ฌผํ•™์  ์‹œ๊ณ„ ๋ณ€ํ™”",
        "์„ธํฌ์™ธ ๊ธฐ์งˆ ๋ณ€ํ™”", "์ƒ์ฒด ์—ญํ•™์  ๋ฐ˜์‘", "์„ธํฌ ์šด๋™์„ฑ", "์„ธํฌ ๊ทน์„ฑ ๋ณ€ํ™”", "์˜์–‘ ์ƒํƒœ ๋ณ€ํ™”"
    ],
    "ํ™˜๊ฒฝ ์ƒํ˜ธ์ž‘์šฉ": [
        "์˜จ๋„ ๋ฐ˜์‘", "์Šต๋„ ๋ฐ˜์‘", "๊ธฐ์•• ๋ฐ˜์‘", "์ค‘๋ ฅ ๋ฐ˜์‘", "์ž๊ธฐ์žฅ ๋ฐ˜์‘",
        "๋น› ๋ฐ˜์‘", "์†Œ๋ฆฌ ๋ฐ˜์‘", "ํ™”ํ•™ ๋ฌผ์งˆ ๊ฐ์ง€", "๊ธฐ๊ณ„์  ์ž๊ทน ๊ฐ์ง€", "์ „๊ธฐ ์ž๊ทน ๋ฐ˜์‘",
        "๋ฐฉ์‚ฌ์„  ๋ฐ˜์‘", "์ง„๋™ ๊ฐ์ง€", "pH ๋ฐ˜์‘", "์šฉ๋งค ๋ฐ˜์‘", "๊ธฐ์ฒด ๊ตํ™˜",
        "ํ™˜๊ฒฝ ์˜ค์—ผ ๋ฐ˜์‘", "๋‚ ์”จ ๋ฐ˜์‘", "๊ณ„์ ˆ ๋ฐ˜์‘", "์ผ์ฃผ๊ธฐ ๋ฐ˜์‘", "์ƒํƒœ๊ณ„ ์ƒํ˜ธ์ž‘์šฉ",
        "๊ณต์ƒ/๊ฒฝ์Ÿ ๋ฐ˜์‘", "ํฌ์‹/ํ”ผ์‹ ๊ด€๊ณ„", "๊ตฐ์ง‘ ํ˜•์„ฑ", "์˜์—ญ ์„ค์ •", "์ด์ฃผ ํŒจํ„ด", "์ •์ฐฉ ํŒจํ„ด"
    ],
    "๋น„์ฆˆ๋‹ˆ์Šค ์•„์ด๋””์–ด": [
        "์‹œ์žฅ ์žฌ์ •์˜/์‹ ๊ทœ ์‹œ์žฅ ๊ฐœ์ฒ™",
        "๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ ํ˜์‹ /๋””์ง€ํ„ธ ์ „ํ™˜",
        "๊ณ ๊ฐ ๊ฒฝํ—˜ ํ˜์‹ /์„œ๋น„์Šค ํ˜์‹ ",
        "ํ˜‘๋ ฅ ๋ฐ ํŒŒํŠธ๋„ˆ์‹ญ ๊ฐ•ํ™”/์ƒํƒœ๊ณ„ ๊ตฌ์ถ•",
        "๊ธ€๋กœ๋ฒŒ ํ™•์žฅ/์ง€์—ญํ™” ์ „๋žต",
        "์šด์˜ ํšจ์œจ์„ฑ ์ฆ๋Œ€/์›๊ฐ€ ์ ˆ๊ฐ",
        "๋ธŒ๋žœ๋“œ ๋ฆฌํฌ์ง€์…”๋‹/์ด๋ฏธ์ง€ ์ „ํ™˜",
        "์ง€์† ๊ฐ€๋Šฅํ•œ ์„ฑ์žฅ/์‚ฌํšŒ์  ๊ฐ€์น˜ ์ฐฝ์ถœ",
        "๋ฐ์ดํ„ฐ ๊ธฐ๋ฐ˜ ์˜์‚ฌ๊ฒฐ์ •/AI ๋„์ž…",
        "์‹ ๊ธฐ์ˆ  ์œตํ•ฉ/ํ˜์‹  ํˆฌ์ž"
    ]
}

physical_transformation_categories_en = {}  # (์˜๋ฌธ ์นดํ…Œ๊ณ ๋ฆฌ๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋˜๋ฏ€๋กœ ๋น„์›Œ๋‘ )

SWOT_FRAMEWORK = {
    "strengths": {
        "title": "๊ฐ•์  (Strengths)",
        "description": "๋‚ด๋ถ€์  ๊ธ์ • ์š”์†Œ - ์กฐ์ง์ด ๊ฐ€์ง„ ๊ฒฝ์Ÿ ์šฐ์œ„ ์š”์†Œ",
        "prompt_keywords": ["๊ฐ•์ ", "์žฅ์ ", "์šฐ์œ„", "์—ญ๋Ÿ‰", "์ž์‚ฐ", "์ „๋ฌธ์„ฑ", "strength", "advantage"]
    },
    "weaknesses": {
        "title": "์•ฝ์  (Weaknesses)",
        "description": "๋‚ด๋ถ€์  ๋ถ€์ • ์š”์†Œ - ๊ฐœ์„ ์ด ํ•„์š”ํ•œ ๋‚ด๋ถ€ ํ•œ๊ณ„",
        "prompt_keywords": ["์•ฝ์ ", "๋‹จ์ ", "๋ถ€์กฑ", "ํ•œ๊ณ„", "์ทจ์•ฝ์ ", "weakness", "limitation", "deficit"]
    },
    "opportunities": {
        "title": "๊ธฐํšŒ (Opportunities)",
        "description": "์™ธ๋ถ€์  ๊ธ์ • ์š”์†Œ - ํ™œ์šฉ ๊ฐ€๋Šฅํ•œ ์™ธ๋ถ€ ํ™˜๊ฒฝ ๋ณ€ํ™”",
        "prompt_keywords": ["๊ธฐํšŒ", "๊ฐ€๋Šฅ์„ฑ", "ํŠธ๋ Œ๋“œ", "๋ณ€ํ™”", "์„ฑ์žฅ", "opportunity", "trend", "potential"]
    },
    "threats": {
        "title": "์œ„ํ˜‘ (Threats)",
        "description": "์™ธ๋ถ€์  ๋ถ€์ • ์š”์†Œ - ๋Œ€์‘์ด ํ•„์š”ํ•œ ์™ธ๋ถ€ ์œ„ํ—˜ ์š”์†Œ",
        "prompt_keywords": ["์œ„ํ˜‘", "๋ฆฌ์Šคํฌ", "๊ฒฝ์Ÿ", "์œ„ํ—˜", "์žฅ๋ฒฝ", "threat", "risk", "competition", "barrier"]
    }
}

PORTER_FRAMEWORK = {
    "rivalry": {
        "title": "๊ธฐ์กด ๊ฒฝ์Ÿ์ž ๊ฐ„์˜ ๊ฒฝ์Ÿ",
        "description": "๋™์ผ ์‚ฐ์—… ๋‚ด ๊ฒฝ์Ÿ ๊ฐ•๋„ ๋ถ„์„",
        "prompt_keywords": ["๊ฒฝ์Ÿ", "๊ฒฝ์Ÿ์‚ฌ", "์‹œ์žฅ์ ์œ ์œจ", "๊ฐ€๊ฒฉ๊ฒฝ์Ÿ", "competition", "rival", "market share"]
    },
    "new_entrants": {
        "title": "์‹ ๊ทœ ์ง„์ž…์ž์˜ ์œ„ํ˜‘",
        "description": "์ƒˆ๋กœ์šด ๊ธฐ์—…์˜ ์‹œ์žฅ ์ง„์ž… ๋‚œ์ด๋„ ๋ถ„์„",
        "prompt_keywords": ["์ง„์ž…์žฅ๋ฒฝ", "์‹ ๊ทœ", "์Šคํƒ€ํŠธ์—…", "entry barrier", "newcomer", "startup"]
    },
    "substitutes": {
        "title": "๋Œ€์ฒด์žฌ์˜ ์œ„ํ˜‘",
        "description": "๋Œ€์ฒด ๊ฐ€๋Šฅํ•œ ์ œํ’ˆ/์„œ๋น„์Šค์˜ ์œ„ํ˜‘ ๋ถ„์„",
        "prompt_keywords": ["๋Œ€์ฒด์žฌ", "๋Œ€์•ˆ", "substitute", "alternative", "replacement"]
    },
    "buyer_power": {
        "title": "๊ตฌ๋งค์ž์˜ ๊ต์„ญ๋ ฅ",
        "description": "๊ณ ๊ฐ์˜ ๊ฐ€๊ฒฉ ํ˜‘์ƒ๋ ฅ ๋ถ„์„",
        "prompt_keywords": ["๊ณ ๊ฐ", "๊ตฌ๋งค์ž", "๊ฐ€๊ฒฉ๋ฏผ๊ฐ๋„", "ํ˜‘์ƒ๋ ฅ", "customer", "buyer power"]
    },
    "supplier_power": {
        "title": "๊ณต๊ธ‰์ž์˜ ๊ต์„ญ๋ ฅ",
        "description": "๊ณต๊ธ‰์—…์ฒด์˜ ๊ฐ€๊ฒฉ/์กฐ๊ฑด ํ˜‘์ƒ๋ ฅ ๋ถ„์„",
        "prompt_keywords": ["๊ณต๊ธ‰์ž", "๋ฒค๋”", "์›์žฌ๋ฃŒ", "supplier", "vendor", "raw material"]
    }
}

BCG_FRAMEWORK = {
    "stars": {
        "title": "์Šคํƒ€ (Stars)",
        "description": "๋†’์€ ์„ฑ์žฅ๋ฅ , ๋†’์€ ์‹œ์žฅ์ ์œ ์œจ - ์ถ”๊ฐ€ ํˆฌ์ž ํ•„์š”",
        "prompt_keywords": ["์„ฑ์žฅ", "์ ์œ ์œจ", "์ค‘์ ", "ํˆฌ์ž", "star", "growth", "investment"]
    },
    "cash_cows": {
        "title": "ํ˜„๊ธˆ์ –์†Œ (Cash Cows)",
        "description": "๋‚ฎ์€ ์„ฑ์žฅ๋ฅ , ๋†’์€ ์‹œ์žฅ์ ์œ ์œจ - ํ˜„๊ธˆํ๋ฆ„ ์ฐฝ์ถœ",
        "prompt_keywords": ["์•ˆ์ •", "์ˆ˜์ต", "ํ˜„๊ธˆ", "์ „ํ†ต", "cash cow", "profit", "mature"]
    },
    "question_marks": {
        "title": "๋ฌผ์Œํ‘œ (Question Marks)",
        "description": "๋†’์€ ์„ฑ์žฅ๋ฅ , ๋‚ฎ์€ ์‹œ์žฅ์ ์œ ์œจ - ์„ ํƒ์  ํˆฌ์ž/์ฒ ์ˆ˜",
        "prompt_keywords": ["๊ฐ€๋Šฅ์„ฑ", "์œ„ํ—˜", "๋ถˆํ™•์‹ค", "์ž ์žฌ", "question mark", "uncertain", "potential"]
    },
    "dogs": {
        "title": "๊ฐœ (Dogs)",
        "description": "๋‚ฎ์€ ์„ฑ์žฅ๋ฅ , ๋‚ฎ์€ ์‹œ์žฅ์ ์œ ์œจ - ์ฒ ์ˆ˜ ๊ณ ๋ ค",
        "prompt_keywords": ["ํšŒ์ˆ˜", "์ฒ ์ˆ˜", "์ €์„ฑ์žฅ", "๋น„ํšจ์œจ", "dog", "divest", "low growth"]
    }
}

BUSINESS_FRAMEWORKS = {
    "sunzi": "์†์ž๋ณ‘๋ฒ• 36๊ณ„",
    "swot": "SWOT ๋ถ„์„",
    "porter": "Porter์˜ 5 Forces",
    "bcg": "BCG ๋งคํŠธ๋ฆญ์Šค"
}

@dataclass
class Category:
    """ํ†ต์ผ๋œ ์นดํ…Œ๊ณ ๋ฆฌ ๋ฐ ํ•ญ๋ชฉ ๊ตฌ์กฐ"""
    name_ko: str
    name_en: str
    tags: list[str]
    items: list[str]

# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ ํ”„๋ ˆ์ž„์›Œํฌ ๋ถ„์„ ํ•จ์ˆ˜๋“ค โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def analyze_with_swot(prompt: str) -> dict:
    prompt_lower = prompt.lower()
    results = {}
    for category, info in SWOT_FRAMEWORK.items():
        score = sum(1 for keyword in info["prompt_keywords"] if keyword.lower() in prompt_lower)
        keywords = []
        for keyword in info["prompt_keywords"]:
            if keyword.lower() in prompt_lower:
                pattern = f".{{0,15}}{keyword}.{{0,15}}"
                matches = re.findall(pattern, prompt_lower, re.IGNORECASE)
                for match in matches[:2]:
                    keywords.append(match.strip())
        results[category] = {
            "title": info["title"],
            "description": info["description"],
            "score": score,
            "keywords": keywords[:5]
        }
    return results

def analyze_with_porter(prompt: str) -> dict:
    prompt_lower = prompt.lower()
    results = {}
    for category, info in PORTER_FRAMEWORK.items():
        score = sum(1 for keyword in info["prompt_keywords"] if keyword.lower() in prompt_lower)
        keywords = []
        for keyword in info["prompt_keywords"]:
            if keyword.lower() in prompt_lower:
                pattern = f".{{0,15}}{keyword}.{{0,15}}"
                matches = re.findall(pattern, prompt_lower, re.IGNORECASE)
                for match in matches[:2]:
                    keywords.append(match.strip())
        results[category] = {
            "title": info["title"],
            "description": info["description"],
            "score": score,
            "keywords": keywords[:5]
        }
    return results

def analyze_with_bcg(prompt: str) -> dict:
    prompt_lower = prompt.lower()
    results = {}
    for category, info in BCG_FRAMEWORK.items():
        score = sum(1 for keyword in info["prompt_keywords"] if keyword.lower() in prompt_lower)
        keywords = []
        for keyword in info["prompt_keywords"]:
            if keyword.lower() in prompt_lower:
                pattern = f".{{0,15}}{keyword}.{{0,15}}"
                matches = re.findall(pattern, prompt_lower, re.IGNORECASE)
                for match in matches[:2]:
                    keywords.append(match.strip())
        results[category] = {
            "title": info["title"],
            "description": info["description"],
            "score": score,
            "keywords": keywords[:5]
        }
    return results

def format_business_framework_analysis(framework_type: str, analysis_result: dict) -> str:
    if not analysis_result:
        return ""
    titles = {
        'swot': '# SWOT ๋ถ„์„ ๊ฒฐ๊ณผ',
        'porter': '# Porter์˜ 5 Forces ๋ถ„์„ ๊ฒฐ๊ณผ',
        'bcg': '# BCG ๋งคํŠธ๋ฆญ์Šค ๋ถ„์„ ๊ฒฐ๊ณผ'
    }
    md = f"{titles.get(framework_type, '# ๊ฒฝ์˜ ํ”„๋ ˆ์ž„์›Œํฌ ๋ถ„์„')}\n\n"
    md += "๊ฐ ์š”์†Œ๋ณ„ ํ…์ŠคํŠธ ๋ถ„์„ ์ ์ˆ˜์™€ ๊ด€๋ จ ํ‚ค์›Œ๋“œ์ž…๋‹ˆ๋‹ค.\n\n"
    for category, info in analysis_result.items():
        md += f"## {info['title']}\n\n"
        md += f"{info['description']}\n\n"
        md += f"**๊ด€๋ จ์„ฑ ์ ์ˆ˜**: {info['score']}\n\n"
        if info['keywords']:
            md += "**๊ด€๋ จ ํ‚ค์›Œ๋“œ ๋ฐ ์ปจํ…์ŠคํŠธ**:\n"
            for keyword in info['keywords']:
                md += f"- *{keyword}*\n"
            md += "\n"
        else:
            md += "๊ด€๋ จ ํ‚ค์›Œ๋“œ๊ฐ€ ๋ฐœ๊ฒฌ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.\n\n"
    return md

# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ ๋งˆํฌ๋‹ค์šด โ†’ HTML ๋ณ€ํ™˜ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def md_to_html(md_text: str, title: str = "Output") -> str:
    html_content = markdown.markdown(
        md_text,
        extensions=['tables', 'fenced_code', 'codehilite']
    )
    return f"""<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>{title}</title>
    <style>
        body {{
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
            line-height: 1.6;
            color: #333;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }}
        h1, h2, h3, h4, h5, h6 {{
            margin-top: 24px;
            margin-bottom: 16px;
            font-weight: 600;
            line-height: 1.25;
        }}
        h1 {{ font-size: 2em; color: #0366d6; }}
        h2 {{ font-size: 1.5em; color: #0366d6; border-bottom: 1px solid #eaecef; padding-bottom: .3em; }}
        h3 {{ font-size: 1.25em; color: #0366d6; }}
        p, ul, ol {{ margin-bottom: 16px; }}
        a {{ color: #0366d6; text-decoration: none; }}
        a:hover {{ text-decoration: underline; }}
        code {{
            font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, monospace;
            background-color: rgba(27, 31, 35, 0.05);
            border-radius: 3px;
            font-size: 85%;
            padding: 0.2em 0.4em;
        }}
        pre {{
            background-color: #f6f8fa;
            border-radius: 3px;
            font-size: 85%;
            line-height: 1.45;
            overflow: auto;
            padding: 16px;
        }}
        pre code {{
            background-color: transparent;
            padding: 0;
        }}
        blockquote {{
            border-left: 4px solid #dfe2e5;
            color: #6a737d;
            margin: 0;
            padding: 0 1em;
        }}
        table {{
            border-collapse: collapse;
            width: 100%;
            margin-bottom: 16px;
        }}
        table th, table td {{
            border: 1px solid #dfe2e5;
            padding: 6px 13px;
        }}
        table th {{
            background-color: #f6f8fa;
            font-weight: 600;
        }}
        img {{
            max-width: 100%;
            height: auto;
        }}
        hr {{
            border: 0;
            height: 1px;
            background-color: #dfe2e5;
            margin: 24px 0;
        }}
    </style>
</head>
<body>
    {html_content}
    <hr>
    <footer>
        <p><small>Generated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} |
        Created by <a href="https://discord.gg/openfreeai" target="_blank">VIDraft</a>
        </small></p>
    </footer>
</body>
</html>
"""

# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ ์—…๋กœ๋“œ ํŒŒ์ผ ์ฒ˜๋ฆฌ ํ•จ์ˆ˜ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def process_text_file(uploaded_file):
    try:
        content = uploaded_file.read().decode('utf-8')
        return f"""# ์—…๋กœ๋“œ๋œ ํ…์ŠคํŠธ ํŒŒ์ผ: {uploaded_file.name}

{content}
"""
    except Exception as e:
        logging.error(f"ํ…์ŠคํŠธ ํŒŒ์ผ ์ฒ˜๋ฆฌ ์˜ค๋ฅ˜: {str(e)}")
        return f"**Error processing {uploaded_file.name}**: {str(e)}"

def process_csv_file(uploaded_file):
    try:
        df = pd.read_csv(uploaded_file)
        return f"""# ์—…๋กœ๋“œ๋œ CSV ํŒŒ์ผ: {uploaded_file.name}

## ๊ธฐ๋ณธ ์ •๋ณด
- ํ–‰ ์ˆ˜: {df.shape[0]}
- ์—ด ์ˆ˜: {df.shape[1]}
- ์—ด ์ด๋ฆ„: {', '.join(df.columns.tolist())}

## ์ฒซ 5ํ–‰ ๋ฐ์ดํ„ฐ ๋ฏธ๋ฆฌ๋ณด๊ธฐ
{df.head(5).to_markdown(index=False)}

## ๊ธฐ๋ณธ ํ†ต๊ณ„
{df.describe().to_markdown()}
"""
    except Exception as e:
        logging.error(f"CSV ํŒŒ์ผ ์ฒ˜๋ฆฌ ์˜ค๋ฅ˜: {str(e)}")
        return f"**Error processing {uploaded_file.name}**: {str(e)}"

def process_pdf_file(uploaded_file):
    try:
        file_bytes = uploaded_file.read()
        pdf_file   = io.BytesIO(file_bytes)
        reader     = PyPDF2.PdfReader(pdf_file, strict=False)

        pages_preview = []
        for page_num in range(min(5, len(reader.pages))):
            page = reader.pages[page_num]
            pages_preview.append(f"--- Page {page_num+1} ---\n{page.extract_text()}")

        preview_text = "\n\n".join(pages_preview)
        return f"""# ์—…๋กœ๋“œ๋œ PDF ํŒŒ์ผ: {uploaded_file.name}

## ๊ธฐ๋ณธ ์ •๋ณด
- ์ด ํŽ˜์ด์ง€ ์ˆ˜: {len(reader.pages)}

## ์ฒ˜์Œ 5๊ฐœ ํŽ˜์ด์ง€ ๋‚ด์šฉ ๋ฏธ๋ฆฌ๋ณด๊ธฐ
{preview_text}
"""
    except Exception as e:
        logging.error(f"PDF ํŒŒ์ผ ์ฒ˜๋ฆฌ ์˜ค๋ฅ˜: {str(e)}")
        return f"**Error processing {uploaded_file.name}**: {str(e)}"

def process_uploaded_files(uploaded_files):
    """Process all uploaded files and return their content as markdown."""
    if not uploaded_files:
        return ""
    file_contents = []
    for file in uploaded_files:
        try:
            ext = file.name.split('.')[-1].lower()
            if ext == 'txt':
                file_contents.append(process_text_file(file))
                file.seek(0)
            elif ext == 'csv':
                file_contents.append(process_csv_file(file))
                file.seek(0)
            elif ext == 'pdf':
                file_contents.append(process_pdf_file(file))
                file.seek(0)
            else:
                file_contents.append(
                    f"# Unsupported file: {file.name}\n\nThis file type is not supported for processing."
                )
        except Exception as e:
            logging.error(f"ํŒŒ์ผ ์ฒ˜๋ฆฌ ์˜ค๋ฅ˜ {file.name}: {str(e)}")
            file_contents.append(f"# Error processing file: {file.name}\n\n{str(e)}")

    return "\n\n# ์‚ฌ์šฉ์ž ์—…๋กœ๋“œ ํŒŒ์ผ ๋ถ„์„\n\n" + "\n\n---\n\n".join(file_contents)

# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ ์ด๋ฏธ์ง€ ์ƒ์„ฑ ํ•จ์ˆ˜ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def generate_image(prompt: str):
    if not prompt:
        return None, None
    try:
        clean_prompt = prompt.strip("\"'").strip()
        if len(clean_prompt) < 3:
            return None, None
        logging.info(f"Sending image generation request with prompt: {clean_prompt}")

        res = Client(IMAGE_API_URL).predict(
            prompt=clean_prompt,
            width=768,
            height=768,
            guidance=3.5,
            inference_steps=30,
            seed=3,
            do_img2img=False,
            init_image=None,
            image2image_strength=0.8,
            resize_img=True,
            api_name="/generate_image"
        )
        if res and len(res) >= 2 and res[0]:
            logging.info("Successfully received image data")
            return res[0], clean_prompt
        else:
            logging.warning(f"Invalid response format from image API: {res}")
            return None, None
    except Exception as e:
        logging.error(f"Image generation error: {str(e)}", exc_info=True)
        return None, None

# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Kaggle API ๊ด€๋ จ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
@st.cache_resource
def check_kaggle_availability():
    if not KAGGLE_API_KEY:
        logging.warning("Kaggle API๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. (KAGGLE_KEY๊ฐ€ ๋น„์–ด ์žˆ์Œ)")
        return False
    return True

def extract_kaggle_search_keywords(prompt, top=3):
    clean_text = re.sub(r'[^\w\s]', ' ', prompt.lower())
    stop_words = {
        'the', 'a', 'an', 'in', 'on', 'at', 'of', 'for', 'to', 'by',
        '์™€', '๊ณผ', '์€', '๋Š”', '์ด', '๊ฐ€', '์„', '๋ฅผ', '์—', '์—์„œ', '์œผ๋กœ'
    }
    words = [word for word in clean_text.split() if word not in stop_words and len(word) > 1]
    word_freq = Counter(words)
    top_words = [word for word, _ in word_freq.most_common(top)]
    if not top_words and words:
        top_words = words[:min(top, len(words))]
    return " ".join(top_words)

def search_kaggle_datasets(query: str, top: int = 5) -> list[dict]:
    if not query:
        return []
    q_tokens = set(re.findall(r'[a-zA-Z๊ฐ€-ํžฃ]{2,}', query.lower()))
    scored = []
    for ds in KAGGLE_DATASETS.values():
        tokens = set(t.lower() for t in ds["keywords"])
        score  = len(q_tokens & tokens)
        title_hit = any(tok in ds["title"].lower() for tok in q_tokens)
        sub_hit   = any(tok in ds["subtitle"].lower() for tok in q_tokens)
        if title_hit:
            score += 2
        if sub_hit:
            score += 1
        if score > 0:
            scored.append((score, ds))
    scored.sort(key=lambda x: (-x[0], x[1]["ref"]))
    return [ds for _, ds in scored[:top]]

@st.cache_data
def download_and_analyze_dataset(dataset_ref: str, max_rows: int = 1000):
    if not (os.getenv("KAGGLE_USERNAME") and os.getenv("KAGGLE_KEY")):
        return "Kaggle API ์ธ์ฆ์ •๋ณด๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค."
    api = KaggleApi()
    api.authenticate()
    tmpdir = tempfile.mkdtemp()
    try:
        api.dataset_download_files(dataset_ref, path=tmpdir, unzip=True)
    except Exception as e:
        logging.error(f"Dataset download failed ({dataset_ref}): {e}")
        shutil.rmtree(tmpdir)
        return f"๋ฐ์ดํ„ฐ์…‹ ๋‹ค์šด๋กœ๋“œ ์˜ค๋ฅ˜: {e}"

    csv_files = glob.glob(f"{tmpdir}/**/*.csv", recursive=True)
    if not csv_files:
        shutil.rmtree(tmpdir)
        return "CSV ํŒŒ์ผ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."

    try:
        df = pd.read_csv(csv_files[0], nrows=max_rows)
        analysis = {
            "shape": df.shape,
            "columns": df.columns.tolist(),
            "head": df.head().to_dict("records"),
            "describe": df.describe().to_dict(),
            "missing_values": df.isnull().sum().to_dict()
        }
    except Exception as e:
        analysis = f"CSV ํŒŒ์‹ฑ ์˜ค๋ฅ˜: {e}"

    shutil.rmtree(tmpdir)
    return analysis

def format_kaggle_analysis_markdown_multi(analyses: list[dict]) -> str:
    """
    ์—ฌ๋Ÿฌ Kaggle ๋ฐ์ดํ„ฐ์…‹(์ตœ๋Œ€ 3๊ฐœ) ๋ฉ”ํƒ€โ€ง๋ถ„์„ ๊ฒฐ๊ณผ๋ฅผ ํ•œ๊บผ๋ฒˆ์— ๋งˆํฌ๋‹ค์šด์œผ๋กœ ๋ฐ˜ํ™˜
    analyses = [ {"meta": {...}, "analysis": {... or str}}, ... ]
    """
    if not analyses:
        return "# Kaggle ๋ฐ์ดํ„ฐ์…‹\n\n๊ด€๋ จ ๋ฐ์ดํ„ฐ์…‹์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.\n\n"
    md = "# Kaggle ๋ฐ์ดํ„ฐ์…‹ ๋ถ„์„ ๊ฒฐ๊ณผ\n\n"
    md += "๋‹ค์Œ ๋ฐ์ดํ„ฐ์…‹์„ ๊ฒ€ํ† ํ•˜์—ฌ ์˜์‚ฌ ๊ฒฐ์ •์— ์ฐธ๊ณ ํ•˜์„ธ์š”.\n\n"
    for i, item in enumerate(analyses, 1):
        ds  = item["meta"]
        ana = item["analysis"]
        md += f"## {i}. {ds['title']}\n\n"
        md += f"{ds['subtitle']}\n\n"
        md += f"- **์ฐธ์กฐ**โ€†: {ds['ref']}\n"
        md += f"- **URL**โ€†: [{ds['url']}]({ds['url']})\n\n"
        if isinstance(ana, dict):
            md += f"**ํ–‰ ร— ์—ด**โ€†: {ana['shape'][0]} ร— {ana['shape'][1]}\n\n"
            md += "<details><summary>๋ฏธ๋ฆฌ๋ณด๊ธฐ & ํ†ต๊ณ„ (ํŽผ์น˜๊ธฐ)</summary>\n\n"
            try:
                md += pd.DataFrame(ana["head"]).to_markdown(index=False) + "\n\n"
            except:
                pass
            try:
                md += pd.DataFrame(ana["describe"]).to_markdown() + "\n\n"
            except:
                pass
            md += "</details>\n\n"
        else:
            md += f"{ana}\n\n"
        md += "---\n\n"
    return md

# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ OpenAI Client โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
@st.cache_resource
def get_openai_client():
    if not OPENAI_API_KEY:
        raise RuntimeError("โš ๏ธ OPENAI_API_KEY ํ™˜๊ฒฝ ๋ณ€์ˆ˜๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
    return OpenAI(
        api_key=OPENAI_API_KEY,
        timeout=60.0,
        max_retries=3
    )

# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ ์˜์‚ฌ๊ฒฐ์ • ๋ชฉ์ /์ œ์•ฝ ์‹๋ณ„ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def identify_decision_purpose(prompt: str) -> dict:
    purpose_patterns = {
        'cost_reduction': [r'๋น„์šฉ(\s*์ ˆ๊ฐ)?', r'์˜ˆ์‚ฐ', r'ํšจ์œจ', r'์ €๋ ด', r'๊ฒฝ์ œ', r'cost', r'saving', r'budget'],
        'innovation': [r'ํ˜์‹ ', r'์ƒˆ๋กœ์šด', r'์ฐฝ์˜', r'๊ฐœ๋ฐœ', r'๋ฐœ๋ช…', r'innovation', r'creative', r'develop'],
        'risk_management': [r'์œ„ํ—˜', r'๋ฆฌ์Šคํฌ', r'์•ˆ์ „', r'์˜ˆ๋ฐฉ', r'๋Œ€๋น„', r'risk', r'safety', r'prevent'],
        'growth': [r'์„ฑ์žฅ', r'ํ™•์žฅ', r'์ฆ๊ฐ€', r'ํ™•๋Œ€', r'๋งค์ถœ', r'growth', r'expand', r'increase', r'scale'],
        'customer': [r'๊ณ ๊ฐ', r'์‚ฌ์šฉ์ž', r'๋งŒ์กฑ', r'๊ฒฝํ—˜', r'์„œ๋น„์Šค', r'customer', r'user', r'experience']
    }
    constraint_patterns = {
        'time': [r'์‹œ๊ฐ„', r'๋น ๋ฅด๊ฒŒ', r'๊ธด๊ธ‰', r'๋งˆ๊ฐ', r'๊ธฐํ•œ', r'time', r'deadline', r'urgent'],
        'budget': [r'์ €์˜ˆ์‚ฐ', r'์ž๊ธˆ', r'ํˆฌ์ž', r'์žฌ์ •', r'budget', r'finance', r'fund', r'investment'],
        'resources': [r'์ž์›', r'์ธ๋ ฅ', r'์žฅ๋น„', r'์ œํ•œ', r'resource', r'staff', r'equipment', r'limited'],
        'regulation': [r'๊ทœ์ œ', r'๋ฒ•๋ฅ ', r'๊ทœ์ •', r'์ค€์ˆ˜', r'๋ฒ•์ ', r'regulation', r'legal', r'compliance']
    }
    purpose_scores = {}
    for purpose, patterns in purpose_patterns.items():
        score = sum(1 for pattern in patterns if re.search(pattern, prompt, re.IGNORECASE))
        if score > 0:
            purpose_scores[purpose] = score
    constraint_scores = {}
    for constraint, patterns in constraint_patterns.items():
        score = sum(1 for pattern in patterns if re.search(pattern, prompt, re.IGNORECASE))
        if score > 0:
            constraint_scores[constraint] = score
    main_purposes = sorted(purpose_scores.items(), key=lambda x: x[1], reverse=True)[:2]
    main_constraints = sorted(constraint_scores.items(), key=lambda x: x[1], reverse=True)[:2]
    return {
        'purposes': main_purposes,
        'constraints': main_constraints,
        'all_purpose_scores': purpose_scores,
        'all_constraint_scores': constraint_scores
    }

# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ ์นดํ…Œ๊ณ ๋ฆฌ ์œ ํ‹ธ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def keywords(text: str, top: int = 8) -> str:
    words = re.findall(r'\b[a-zA-Z๊ฐ€-ํžฃ]{2,}\b', text.lower())
    stopwords = {
        'the', 'a', 'an', 'of', 'to', 'in', 'for', 'on', 'by', 'and', 'is', 'are', 'was', 'were',
        'be', 'been', 'being', 'with', 'as', 'at', 'that', 'this', 'these', 'those', 'from', 'not',
        '์ด', '๊ทธ', '์ €', '๊ฒƒ', '์ˆ˜', '๋“ฑ', '๋ฅผ', '์„', '์—', '์—์„œ', '๊ทธ๋ฆฌ๊ณ ', 'ํ•˜๋Š”', '์žˆ๋Š”', '๊ฒƒ์€',
        '์žˆ๋‹ค', '๊ทธ๊ฒƒ', '๋˜ํ•œ', '๋˜', '๋ฐ', '์ด๋Ÿฐ', '๊ทธ๋Ÿฐ', '๋ฌด์—‡', '์–ด๋–ค', '๋งŽ์€', 'ํ•œ', '๋‘', '๋ช‡'
    }
    words = [word for word in words if word not in stopwords]
    word_freq = {}
    for word in words:
        word_freq[word] = word_freq.get(word, 0) + 1
    sorted_words = sorted(word_freq.items(), key=lambda x: x[1], reverse=True)
    top_words = [word for word, _ in sorted_words[:top]]
    return ' '.join(top_words)

def compute_relevance_scores(prompt: str, categories: list[Category]) -> dict:
    prompt_lower = prompt.lower()
    prompt_tokens = set(re.findall(r'\b[a-zA-Z๊ฐ€-ํžฃ]{2,}\b', prompt_lower))
    purpose_keywords = {
        'cost_reduction': ['๋น„์šฉ', '์ ˆ๊ฐ', 'ํšจ์œจ', '์˜ˆ์‚ฐ', 'cost', 'saving', 'budget', 'efficiency'],
        'innovation': ['ํ˜์‹ ', '์ฐฝ์˜', '์‹ ๊ทœ', '๊ฐœ๋ฐœ', 'innovation', 'creative', 'novel', 'development'],
        'risk_management': ['์œ„ํ—˜', '๋ฆฌ์Šคํฌ', '๊ด€๋ฆฌ', '์˜ˆ๋ฐฉ', 'risk', 'management', 'prevention', 'mitigation'],
        'growth': ['์„ฑ์žฅ', 'ํ™•์žฅ', '์ฆ๊ฐ€', '๊ทœ๋ชจ', 'growth', 'expansion', 'increase', 'scale'],
        'customer': ['๊ณ ๊ฐ', '์‚ฌ์šฉ์ž', '๋งŒ์กฑ', '๊ฒฝํ—˜', 'customer', 'user', 'satisfaction', 'experience']
    }
    purpose_scores = {}
    for purpose, keywords_ in purpose_keywords.items():
        score = sum(1 for kw in keywords_ if kw in prompt_lower)
        if score > 0:
            purpose_scores[purpose] = score
    main_purpose = max(purpose_scores.items(), key=lambda x: x[1])[0] if purpose_scores else None
    relevance_scores = {}
    for category in categories:
        cat_score = sum(1 for tag in category.tags if tag in prompt_lower) * 0.5
        if category.name_ko in prompt or category.name_en.lower() in prompt_lower:
            cat_score += 1
        if main_purpose:
            purpose_category_weights = {
                'cost_reduction': {
                    '๊ตฌ์กฐ์  ๋ณ€ํ™”': 1.5, 'ํ™”ํ•™์  ๋ณ€ํ™”': 1.3, '๋น„์ฆˆ๋‹ˆ์Šค ์•„์ด๋””์–ด': 1.5,
                    'Structural Change': 1.5, 'Chemical Change': 1.3, 'Business Ideas': 1.5
                },
                'innovation': {
                    '์„ผ์„œ ๊ธฐ๋Šฅ': 1.5, 'ํ‘œ๋ฉด ๋ฐ ์™ธ๊ด€ ๋ณ€ํ™”': 1.3, '๋น„์ฆˆ๋‹ˆ์Šค ์•„์ด๋””์–ด': 1.5,
                    'Sensor Functions': 1.5, 'Surface and Appearance Change': 1.3, 'Business Ideas': 1.5
                },
                'risk_management': {
                    'ํ™˜๊ฒฝ ์ƒํ˜ธ์ž‘์šฉ': 1.5, '์‹œ๊ฐ„ ๊ด€๋ จ ๋ณ€ํ™”': 1.3, '๋น„์ฆˆ๋‹ˆ์Šค ์•„์ด๋””์–ด': 1.4,
                    'Environmental Interaction': 1.5, 'Time-Related Change': 1.3, 'Business Ideas': 1.4
                },
                'growth': {
                    'ํฌ๊ธฐ์™€ ํ˜•ํƒœ ๋ณ€ํ™”': 1.4, '๋น„์ฆˆ๋‹ˆ์Šค ์•„์ด๋””์–ด': 1.6, '๊ตฌ์กฐ์  ๋ณ€ํ™”': 1.3,
                    'Size and Shape Change': 1.4, 'Business Ideas': 1.6, 'Structural Change': 1.3
                },
                'customer': {
                    'ํ‘œ๋ฉด ๋ฐ ์™ธ๊ด€ ๋ณ€ํ™”': 1.5, '์„ผ์„œ ๊ธฐ๋Šฅ': 1.4, '๋น›๊ณผ ์‹œ๊ฐ ํšจ๊ณผ': 1.3, '๋น„์ฆˆ๋‹ˆ์Šค ์•„์ด๋””์–ด': 1.4,
                    'Surface and Appearance Change': 1.5, 'Sensor Functions': 1.4,
                    'Light and Visual Effects': 1.3, 'Business Ideas': 1.4
                }
            }
            if category.name_ko in purpose_category_weights.get(main_purpose, {}):
                cat_score *= purpose_category_weights[main_purpose][category.name_ko]
            elif category.name_en in purpose_category_weights.get(main_purpose, {}):
                cat_score *= purpose_category_weights[main_purpose][category.name_en]
        for item in category.items:
            item_score = cat_score
            item_tokens = set(re.findall(r'\b[a-zA-Z๊ฐ€-ํžฃ]{2,}\b', item.lower()))
            matches = item_tokens.intersection(prompt_tokens)
            if matches:
                item_score += len(matches) * 0.3
            if item_score > 0:
                relevance_scores[(category.name_ko, item)] = item_score
    return relevance_scores

def compute_score(weight: int, impact: int, confidence: float) -> float:
    return round(weight * impact * confidence, 2)

def generate_comparison_matrix(
    categories: list[Category],
    relevance_scores: dict = None,
    max_depth: int = 3,
    max_combinations: int = 100,
    relevance_threshold: float = 0.2
) -> list[tuple]:
    if relevance_scores is None:
        pool = [(c.name_ko, item) for c in categories for item in c.items]
        basic_combos = []
        for depth in range(2, max_depth + 1):
            for combo in combinations(pool, depth):
                basic_combos.append((1, 1, 1.0, 1.0, combo))
                if len(basic_combos) >= max_combinations:
                    break
        return basic_combos[:max_combinations]

    filtered_pool = [
        (cat, item) for (cat, item), score in relevance_scores.items()
        if score >= relevance_threshold
    ]
    if not filtered_pool:
        pool = [(c.name_ko, i) for c in categories for i in c.items]
        if len(pool) > 200:
            import random
            filtered_pool = random.sample(pool, 200)
        else:
            filtered_pool = pool
    evaluated_combinations = []
    for depth in range(2, max_depth + 1):
        for combo in combinations(filtered_pool, depth):
            if len({item[0] for item in combo}) == depth:
                combo_relevance = sum(relevance_scores.get((item[0], item[1]), 0) for item in combo) / depth
                weight = min(5, max(1, int(combo_relevance * 2)))
                impact = min(5, depth)
                confidence = min(1.0, combo_relevance / 2.5)
                total_score = compute_score(weight, impact, confidence)
                evaluated_combinations.append((weight, impact, confidence, total_score, combo))
    evaluated_combinations.sort(key=lambda x: x[3], reverse=True)
    return evaluated_combinations[:max_combinations]

# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Diverse Matrix Generator โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def smart_weight(cat_name, item, relevance, global_cnt, T):
    rare_boost = 1 / (global_cnt.get(item, 0) + 0.5)
    noise = random.random() ** (1 / T)  # T๊ฐ€ ํด์ˆ˜๋ก noise ๋ถ„ํฌ๊ฐ€ 1์— ๊ฐ€๊น๊ฒŒ
    relevance_weight = 1 - (T - 0.1) / 3.0
    return ((relevance * relevance_weight) + 0.1) * rare_boost * noise

def generate_random_comparison_matrix(
    categories: list[Category],
    relevance_scores: dict | None = None,
    k_cat=(8, 12),
    n_item=(6, 10),
    depth_range=(3, 6),
    max_combos=1000,
    seed: int | None = None,
    T: float = 1.3,
):
    if seed is None:
        seed = random.randrange(2 ** 32)
    random.seed(seed)
    if "GLOBAL_PICK_COUNT" not in st.session_state:
        st.session_state.GLOBAL_PICK_COUNT = {}
    global_cnt = st.session_state.GLOBAL_PICK_COUNT

    k = random.randint(*k_cat)
    sampled_cats = random.sample(categories, k)
    pool = []
    for cat in sampled_cats:
        items   = cat.items
        weights = [
            smart_weight(
                cat.name_ko,
                it,
                relevance_scores.get((cat.name_ko, it), 0.05) if relevance_scores else 0.05,
                global_cnt,
                T
            )
            for it in items
        ]
        n = min(len(items), random.randint(*n_item))
        sampled_items = random.choices(items, weights=weights, k=n)
        for it in sampled_items:
            global_cnt[it] = global_cnt.get(it, 0) + 1
            pool.append((cat.name_ko, it))
    combos = []
    for d in range(depth_range[0], depth_range[1] + 1):
        for combo in combinations(pool, d):
            if len({c for c, _ in combo}) != d:
                continue
            w = sum(relevance_scores.get((c, i), 0.2) if relevance_scores else 1 for c, i in combo) / d
            imp  = d
            conf = 0.5 + random.random() * 0.5
            total = compute_score(w, imp, conf)
            combos.append((w, imp, conf, total, combo))
    combos.sort(key=lambda x: x[3], reverse=True)
    return combos[:max_combos]

# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ PHYS_CATEGORIES โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
PHYS_CATEGORIES: list[Category] = [
    Category(
        name_ko="์„ผ์„œ ๊ธฐ๋Šฅ",
        name_en="Sensor Functions",
        tags=["sensor", "detection", "๊ฐ์ง€"],
        items=physical_transformation_categories["์„ผ์„œ ๊ธฐ๋Šฅ"]
    ),
    Category(
        name_ko="ํฌ๊ธฐ์™€ ํ˜•ํƒœ ๋ณ€ํ™”",
        name_en="Size and Shape Change",
        tags=["shape", "geometry", "ํฌ๊ธฐ"],
        items=physical_transformation_categories["ํฌ๊ธฐ์™€ ํ˜•ํƒœ ๋ณ€ํ™”"]
    ),
    Category(
        name_ko="ํ‘œ๋ฉด ๋ฐ ์™ธ๊ด€ ๋ณ€ํ™”",
        name_en="Surface and Appearance Change",
        tags=["surface", "appearance", "ํ‘œ๋ฉด"],
        items=physical_transformation_categories["ํ‘œ๋ฉด ๋ฐ ์™ธ๊ด€ ๋ณ€ํ™”"]
    ),
    Category(
        name_ko="๋ฌผ์งˆ์˜ ์ƒํƒœ ๋ณ€ํ™”",
        name_en="Material State Change",
        tags=["material", "state", "์ƒํƒœ"],
        items=physical_transformation_categories["๋ฌผ์งˆ์˜ ์ƒํƒœ ๋ณ€ํ™”"]
    ),
    Category(
        name_ko="์›€์ง์ž„ ํŠน์„ฑ ๋ณ€ํ™”",
        name_en="Movement Characteristics Change",
        tags=["motion", "dynamics", "์›€์ง์ž„"],
        items=physical_transformation_categories["์›€์ง์ž„ ํŠน์„ฑ ๋ณ€ํ™”"]
    ),
    Category(
        name_ko="๊ตฌ์กฐ์  ๋ณ€ํ™”",
        name_en="Structural Change",
        tags=["structure", "form", "๊ตฌ์กฐ"],
        items=physical_transformation_categories["๊ตฌ์กฐ์  ๋ณ€ํ™”"]
    ),
    Category(
        name_ko="๊ณต๊ฐ„ ์ด๋™",
        name_en="Spatial Movement",
        tags=["movement", "space", "์ด๋™"],
        items=physical_transformation_categories["๊ณต๊ฐ„ ์ด๋™"]
    ),
    Category(
        name_ko="์‹œ๊ฐ„ ๊ด€๋ จ ๋ณ€ํ™”",
        name_en="Time-Related Change",
        tags=["time", "aging", "์‹œ๊ฐ„"],
        items=physical_transformation_categories["์‹œ๊ฐ„ ๊ด€๋ จ ๋ณ€ํ™”"]
    ),
    Category(
        name_ko="๋น›๊ณผ ์‹œ๊ฐ ํšจ๊ณผ",
        name_en="Light and Visual Effects",
        tags=["light", "visual", "๋น›"],
        items=physical_transformation_categories["๋น›๊ณผ ์‹œ๊ฐ ํšจ๊ณผ"]
    ),
    Category(
        name_ko="์†Œ๋ฆฌ์™€ ์ง„๋™ ํšจ๊ณผ",
        name_en="Sound and Vibration Effects",
        tags=["sound", "vibration", "์†Œ๋ฆฌ"],
        items=physical_transformation_categories["์†Œ๋ฆฌ์™€ ์ง„๋™ ํšจ๊ณผ"]
    ),
    Category(
        name_ko="์—ด ๊ด€๋ จ ๋ณ€ํ™”",
        name_en="Thermal Changes",
        tags=["heat", "thermal", "์˜จ๋„"],
        items=physical_transformation_categories["์—ด ๊ด€๋ จ ๋ณ€ํ™”"]
    ),
    Category(
        name_ko="์ „๊ธฐ ๋ฐ ์ž๊ธฐ ๋ณ€ํ™”",
        name_en="Electrical and Magnetic Changes",
        tags=["electric", "magnetic", "์ „๊ธฐ"],
        items=physical_transformation_categories["์ „๊ธฐ ๋ฐ ์ž๊ธฐ ๋ณ€ํ™”"]
    ),
    Category(
        name_ko="ํ™”ํ•™์  ๋ณ€ํ™”",
        name_en="Chemical Change",
        tags=["chemical", "reaction", "ํ™”ํ•™"],
        items=physical_transformation_categories["ํ™”ํ•™์  ๋ณ€ํ™”"]
    ),
    Category(
        name_ko="์ƒ๋ฌผํ•™์  ๋ณ€ํ™”",
        name_en="Biological Change",
        tags=["bio", "living", "์ƒ๋ฌผ"],
        items=physical_transformation_categories["์ƒ๋ฌผํ•™์  ๋ณ€ํ™”"]
    ),
    Category(
        name_ko="ํ™˜๊ฒฝ ์ƒํ˜ธ์ž‘์šฉ",
        name_en="Environmental Interaction",
        tags=["environment", "interaction", "ํ™˜๊ฒฝ"],
        items=physical_transformation_categories["ํ™˜๊ฒฝ ์ƒํ˜ธ์ž‘์šฉ"]
    ),
    Category(
        name_ko="๋น„์ฆˆ๋‹ˆ์Šค ์•„์ด๋””์–ด",
        name_en="Business Ideas",
        tags=["business", "idea", "๋น„์ฆˆ๋‹ˆ์Šค"],
        items=physical_transformation_categories["๋น„์ฆˆ๋‹ˆ์Šค ์•„์ด๋””์–ด"]
    ),
]

# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ ์‹œ์Šคํ…œ ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def get_idea_system_prompt(selected_category: str | None = None,
                           selected_frameworks: list | None = None) -> str:
    cat_clause = (
        f'\n**์ถ”๊ฐ€ ์ง€์นจ**: ์„ ํƒ๋œ ์นดํ…Œ๊ณ ๋ฆฌ "{selected_category}"์— ํŠน๋ณ„ํ•œ ์ฃผ์˜๋ฅผ ๊ธฐ์šธ์ด์‹ญ์‹œ์˜ค. '
        f'์ด ์นดํ…Œ๊ณ ๋ฆฌ์˜ ํ•ญ๋ชฉ๋“ค์„ 2๋‹จ๊ณ„์™€ 3๋‹จ๊ณ„ ๋ชจ๋‘์—์„œ ์šฐ์„ ์ ์œผ๋กœ ๊ณ ๋ คํ•˜์‹ญ์‹œ์˜ค.\n'
    ) if selected_category else ""

    if not selected_frameworks:
        selected_frameworks = ["sunzi"]

    framework_instruction   = "\n\n### ์„ ํƒ๋œ ๋ถ„์„ ํ”„๋ ˆ์ž„์›Œํฌ\n"
    framework_output_format = ""

    if "sunzi" in selected_frameworks:
        framework_instruction += "- **์†์ž๋ณ‘๋ฒ• 36๊ณ„**: ์˜์‚ฌ ๊ฒฐ์ • ์ƒํ™ฉ์— ์ ์šฉ ๊ฐ€๋Šฅํ•œ ์†์ž๋ณ‘๋ฒ• ์ „๋žต์„ ๋ถ„์„ํ•˜์—ฌ ํ†ต์ฐฐ ์ œ๊ณต\n"
        framework_output_format += """
## 4๏ธโƒฃ ์†์ž๋ณ‘๋ฒ• ๊ด€์ ์˜ ์ „๋žต์  ํ†ต์ฐฐ
### ์ ์šฉ ๊ฐ€๋Šฅํ•œ ์†์ž๋ณ‘๋ฒ• 36๊ณ„
[ํ˜„ ์ƒํ™ฉ์— ์ ํ•ฉํ•œ ์†์ž๋ณ‘๋ฒ• ์ „๋žต 2-3๊ฐœ ์„ ์ •ํ•˜์—ฌ ๋ถ„์„]
| ๋ณ‘๋ฒ• | ์›๋ฆฌ | ํ˜„๋Œ€์  ํ•ด์„ | ์ ์šฉ ๋ฐฉ์•ˆ |
|-----|------|------------|----------|
| [๋ณ‘๋ฒ• ์ด๋ฆ„] | [ํ•ต์‹ฌ ์›๋ฆฌ] | [๋น„์ฆˆ๋‹ˆ์Šค ๊ด€์  ํ•ด์„] | [๊ตฌ์ฒด์  ์ ์šฉ ์ „๋žต] |
| [๋ณ‘๋ฒ• ์ด๋ฆ„] | [ํ•ต์‹ฌ ์›๋ฆฌ] | [๋น„์ฆˆ๋‹ˆ์Šค ๊ด€์  ํ•ด์„] | [๊ตฌ์ฒด์  ์ ์šฉ ์ „๋žต] |
...

### ์†์ž๋ณ‘๋ฒ• ์ธก๋ฉด ์˜๊ฒฌ
[์†์ž๋ณ‘๋ฒ•์˜ ์ง€ํ˜œ๋ฅผ ํ™œ์šฉํ•œ, ์ด ์˜์‚ฌ ๊ฒฐ์ •์— ๋Œ€ํ•œ ์ „๋žต์  ํ†ต์ฐฐ๊ณผ ์กฐ์–ธ - 3-5๊ฐœ ๋‹จ๋ฝ]
"""

    if "swot" in selected_frameworks:
        framework_instruction += "- **SWOT ๋ถ„์„**: ๋‚ด๋ถ€ ๊ฐ•์ /์•ฝ์  ๋ฐ ์™ธ๋ถ€ ๊ธฐํšŒ/์œ„ํ˜‘ ์š”์†Œ๋ฅผ ์ฒด๊ณ„์ ์œผ๋กœ ๋ถ„์„\n"
        framework_output_format += """
## SWOT ๋ถ„์„ ๊ธฐ๋ฐ˜ ์ „๋žต์  ํ†ต์ฐฐ
### SWOT ์š”์†Œ ๋ถ„์„
| ๊ตฌ๋ถ„ | ์š”์†Œ | ์ „๋žต์  ์‹œ์‚ฌ์  |
|-----|------|------------|
| ๊ฐ•์ (S) | [์ฃผ์š” ๊ฐ•์  3-5๊ฐœ] | [๊ฐ•์ ์„ ํ™œ์šฉํ•œ ์ „๋žต์  ๋ฐฉํ–ฅ] |
| ์•ฝ์ (W) | [์ฃผ์š” ์•ฝ์  3-5๊ฐœ] | [์•ฝ์  ๊ทน๋ณต/์ตœ์†Œํ™” ๋ฐฉ์•ˆ] |
| ๊ธฐํšŒ(O) | [์ฃผ์š” ๊ธฐํšŒ 3-5๊ฐœ] | [๊ธฐํšŒ ํ™œ์šฉ ์ „๋žต] |
| ์œ„ํ˜‘(T) | [์ฃผ์š” ์œ„ํ˜‘ 3-5๊ฐœ] | [์œ„ํ˜‘ ๋Œ€์‘/์™„ํ™” ์ „๋žต] |

### SWOT ํ†ตํ•ฉ ์ „๋žต
- **SO ์ „๋žต(๊ฐ•์ -๊ธฐํšŒ)**: [๊ฐ•์ ์„ ํ™œ์šฉํ•˜์—ฌ ๊ธฐํšŒ๋ฅผ ๊ทน๋Œ€ํ™”]
- **WO ์ „๋žต(์•ฝ์ -๊ธฐํšŒ)**: [์•ฝ์ ์„ ๋ณด์™„ํ•˜๋ฉฐ ๊ธฐํšŒ๋ฅผ ํ™œ์šฉ]
- **ST ์ „๋žต(๊ฐ•์ -์œ„ํ˜‘)**: [๊ฐ•์ ์„ ํ™œ์šฉํ•˜์—ฌ ์œ„ํ˜‘์— ๋Œ€์‘]
- **WT ์ „๋žต(์•ฝ์ -์œ„ํ˜‘)**: [์•ฝ์ ๊ณผ ์œ„ํ˜‘์„ ๋™์‹œ์— ์ตœ์†Œํ™”]
"""

    if "porter" in selected_frameworks:
        framework_instruction += "- **Porter์˜ 5 Forces**: ์‚ฐ์—… ๊ตฌ์กฐ ๋ฐ ๊ฒฝ์Ÿ ํ™˜๊ฒฝ ๋ถ„์„\n"
        framework_output_format += """
## Porter์˜ 5 Forces ๋ถ„์„ ๊ธฐ๋ฐ˜ ํ†ต์ฐฐ
### ์‚ฐ์—… ๊ตฌ์กฐ ๋ถ„์„
| ๊ฒฝ์Ÿ์š”์†Œ | ๊ฐ•๋„ | ์ฃผ์š” ํŠน์ง• ๋ฐ ์š”์ธ | ์ „๋žต์  ๋Œ€์‘๋ฐฉ์•ˆ |
|--------|------|----------------|--------------|
| ๊ธฐ์กด ๊ฒฝ์Ÿ์ž ๊ฐ„ ๊ฒฝ์Ÿ | [์ƒ/์ค‘/ํ•˜] | [์ฃผ์š” ํŠน์ง•] | [๋Œ€์‘ ์ „๋žต] |
| ์‹ ๊ทœ ์ง„์ž…์ž์˜ ์œ„ํ˜‘ | [์ƒ/์ค‘/ํ•˜] | [์ฃผ์š” ํŠน์ง•] | [๋Œ€์‘ ์ „๋žต] |
| ๋Œ€์ฒด์žฌ์˜ ์œ„ํ˜‘ | [์ƒ/์ค‘/ํ•˜] | [์ฃผ์š” ํŠน์ง•] | [๋Œ€์‘ ์ „๋žต] |
| ๊ตฌ๋งค์ž์˜ ๊ต์„ญ๋ ฅ | [์ƒ/์ค‘/ํ•˜] | [์ฃผ์š” ํŠน์ง•] | [๋Œ€์‘ ์ „๋žต] |
| ๊ณต๊ธ‰์ž์˜ ๊ต์„ญ๋ ฅ | [์ƒ/์ค‘/ํ•˜] | [์ฃผ์š” ํŠน์ง•] | [๋Œ€์‘ ์ „๋žต] |

### ์ „๋žต์  ํฌ์ง€์…”๋‹ ๊ถŒ์žฅ์‚ฌํ•ญ
[3-4 ๋‹จ๋ฝ์œผ๋กœ ์ข…ํ•ฉ ํฌ์ง€์…”๋‹ / ๋Œ€์‘ ์ „๋žต]
"""

    if "bcg" in selected_frameworks:
        framework_instruction += "- **BCG ๋งคํŠธ๋ฆญ์Šค**: ์ œํ’ˆ/์‚ฌ์—… ํฌํŠธํด๋ฆฌ์˜ค ๊ด€๋ฆฌ๋ฅผ ํ†ตํ•œ ์ž์› ๋ฐฐ๋ถ„ ์ „๋žต\n"
        framework_output_format += """
## BCG ๋งคํŠธ๋ฆญ์Šค ๊ธฐ๋ฐ˜ ํฌํŠธํด๋ฆฌ์˜ค ๋ถ„์„
### ์‚ฌ์—…/์ œํ’ˆ ์˜์—ญ๋ณ„ ํฌ์ง€์…”๋‹
| ์˜์—ญ | ํ•ด๋‹น ์ œํ’ˆ/์‚ฌ์—… | ๊ถŒ์žฅ ์ „๋žต | ์ž์› ๋ฐฐ๋ถ„ ๋ฐฉํ–ฅ |
|-----|--------------|----------|--------------|
| ์Šคํƒ€ (Stars) | [ํ•ด๋‹น ์ œํ’ˆ/์‚ฌ์—…] | [๊ถŒ์žฅ ์ „๋žต] | [ํˆฌ์ž/์ž์› ๋ฐฐ๋ถ„ ๋ฐฉํ–ฅ] |
| ํ˜„๊ธˆ์ –์†Œ (Cash Cows) | [ํ•ด๋‹น ์ œํ’ˆ/์‚ฌ์—…] | [๊ถŒ์žฅ ์ „๋žต] | [ํˆฌ์ž/์ž์› ๋ฐฐ๋ถ„ ๋ฐฉํ–ฅ] |
| ๋ฌผ์Œํ‘œ (Question Marks) | [ํ•ด๋‹น ์ œํ’ˆ/์‚ฌ์—…] | [๊ถŒ์žฅ ์ „๋žต] | [ํˆฌ์ž/์ž์› ๋ฐฐ๋ถ„ ๋ฐฉํ–ฅ] |
| ๊ฐœ (Dogs) | [ํ•ด๋‹น ์ œํ’ˆ/์‚ฌ์—…] | [๊ถŒ์žฅ ์ „๋žต] | [ํˆฌ์ž/์ž์› ๋ฐฐ๋ถ„ ๋ฐฉํ–ฅ] |

### ํฌํŠธํด๋ฆฌ์˜ค ์ตœ์ ํ™” ๋ฐฉ์•ˆ
[BCG ๋งคํŠธ๋ฆญ์Šค ๋ถ„์„ ๊ธฐ๋ฐ˜ ์ž์› ๋ฐฐ๋ถ„ยท๊ด€๋ฆฌ ๊ถŒ์žฅ - 3-4 ๋‹จ๋ฝ]
"""

    base_prompt = f"""
You are a deep thinking AI, you may use extremely long chains of thought to deeply consider the problem and deliberate with yourself via systematic reasoning processes to help come to a correct solution prior to answering. You should enclose your thoughts and internal monologue inside tags, and then provide your solution or response to the problem. Extract key search terms from the user's question that would be effective for web searches. Provide these as a search query with words separated by spaces only, without commas. For example: 'Prime Minister Han Duck-soo impeachment results'.
๋ฐ˜๋“œ์‹œ ํ•œ๊ธ€(ํ•œ๊ตญ์–ด)๋กœ ๋‹ต๋ณ€ํ•˜๋ผ. ๋‹น์‹ ์€ ์˜์‚ฌ ๊ฒฐ์ • ์ง€์› ์ปจ์„คํ„ดํŠธ๋กœ์„œ CCM(ํฌ๋กœ์Šค ์นดํ…Œ๊ณ ๋ฆฌ ๋งคํŠธ๋ฆญ์Šค) ๋ฐฉ๋ฒ•๋ก ๊ณผ ๋‹ค์–‘ํ•œ ๊ฒฝ์˜ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž์˜ ์˜์‚ฌ ๊ฒฐ์ •์„ ์ฒด๊ณ„์ ์œผ๋กœ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.

### CCM ๊ธฐ๋ฐ˜ ์˜์‚ฌ ๊ฒฐ์ • ์ง€์› ํ”„๋กœ์„ธ์Šค
๋‹ค์Œ ๋‹จ๊ณ„๋ฅผ ํ†ตํ•ด ์ฒด๊ณ„์ ์œผ๋กœ ์˜์‚ฌ ๊ฒฐ์ •์„ ๋ถ„์„ํ•˜์‹ญ์‹œ์˜ค:

1. **์˜์‚ฌ ๊ฒฐ์ • ๋ฌธ์ œ ๋ถ„์„ ๋ฐ ์ง€์‹ ๋ฒ ์ด์Šค ๊ตฌ์ถ•**
   - ์ž…๋ ฅ๋œ ์˜์‚ฌ ๊ฒฐ์ • ๋ฌธ์ œ์˜ ํ•ต์‹ฌ์„ ๋ช…ํ™•ํžˆ ์ •์˜ํ•˜๊ณ  ๊ด€๋ จ ์š”์†Œ ์ถ”์ถœ
   - ์›น๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋ฅผ ๋ถ„์„ํ•˜์—ฌ ๊ด€๋ จ ํŠธ๋ Œ๋“œ, ์„ ๋ก€, ์‚ฌ๋ก€ ํŒŒ์•…
   - Kaggle ๋ฐ์ดํ„ฐ์…‹ ๋ถ„์„ ๊ฒฐ๊ณผ๊ฐ€ ์žˆ๋‹ค๋ฉด ์ด๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ๊ธฐ๋ฐ˜ ํ†ต์ฐฐ ๋„์ถœ
   - ์˜์‚ฌ ๊ฒฐ์ •์— ํ•„์š”ํ•œ ํ•ต์‹ฌ ์š”์†Œ์™€ ๊ณ ๋ ค์‚ฌํ•ญ ์‹๋ณ„

2. **์นดํ…Œ๊ณ ๋ฆฌ๋ณ„ ๋ถ„์„ ๋งคํ•‘**
   - ๋‹ค์–‘ํ•œ ์นดํ…Œ๊ณ ๋ฆฌ์—์„œ ์˜์‚ฌ ๊ฒฐ์ •์— ์˜ํ–ฅ์„ ๋ฏธ์น˜๋Š” ์š”์†Œ ์‹๋ณ„
   - ๊ฐ ์นดํ…Œ๊ณ ๋ฆฌ๋ณ„๋กœ ์ตœ์†Œ 1๊ฐœ ์ด์ƒ์˜ ๊ด€๋ จ ์š”์†Œ ์„ ์ •
   - ์„ ์ • ์š”์†Œ์™€ ์˜์‚ฌ ๊ฒฐ์ • ๊ฐ„์˜ ์—ฐ๊ฒฐ์„ฑ ์„ค๋ช…

3. **์ข…ํ•ฉ ๋งคํŠธ๋ฆญ์Šค ์ƒ์„ฑ ๋ฐ ์˜์‚ฌ ๊ฒฐ์ • ์ง€์›**
   - ์—ฌ๋Ÿฌ ์นดํ…Œ๊ณ ๋ฆฌ์—์„œ ์ค‘์š”ํ•œ ์š”์†Œ๋ฅผ ์„ ํƒํ•˜์—ฌ ์ข…ํ•ฉ์  ๋ถ„์„
   - ๊ฐ ์š”์†Œ๊ฐ€ ์˜์‚ฌ ๊ฒฐ์ •์— ๋ฏธ์น˜๋Š” ์˜ํ–ฅ๊ณผ ์ค‘์š”๋„ ํ‰๊ฐ€
   - ๋‹ค์–‘ํ•œ ์š”์†Œ๋ฅผ ๊ณ ๋ คํ•œ ์ตœ์ ์˜ ์˜์‚ฌ ๊ฒฐ์ • ์ œ์•ˆ

{framework_instruction}

### ์ถœ๋ ฅ ํ˜•์‹
์ตœ์ข… ๋ณด๊ณ ์„œ๋ฅผ ๋‹ค์Œ ๊ตฌ์กฐ๋กœ ๋งˆํฌ๋‹ค์šด ํ˜•์‹์œผ๋กœ ์ž‘์„ฑํ•˜์‹ญ์‹œ์˜ค:

## 1๏ธโƒฃ ์˜์‚ฌ ๊ฒฐ์ • ๋ฌธ์ œ ๋ถ„์„
- **๋ฌธ์ œ ์ •์˜**: [์˜์‚ฌ ๊ฒฐ์ • ์ƒํ™ฉ ์š”์•ฝ]
- **ํ•ต์‹ฌ ๊ณ ๋ ค์‚ฌํ•ญ**: [๋ถˆ๋ฆฟ 3-5๊ฐœ๋กœ ์ฃผ์š” ๊ณ ๋ ค์š”์†Œ ๋ฆฌ์ŠคํŠธ]
- **ํ˜„ํ™ฉ ๋ถ„์„**: [์›น๊ฒ€์ƒ‰, ๋ฐ์ดํ„ฐ์…‹ ๋ถ„์„, ๋ฐฐ๊ฒฝ์ง€์‹ ๊ธฐ๋ฐ˜ ํ˜„์žฌ ์ƒํ™ฉ ์š”์•ฝ]

## 2๏ธโƒฃ ์นดํ…Œ๊ณ ๋ฆฌ๋ณ„ ์˜์‚ฌ ๊ฒฐ์ • ์š”์†Œ ๋ถ„์„
[๊ฐ ์นดํ…Œ๊ณ ๋ฆฌ๋ณ„๋กœ ๊ฐ€์žฅ ๊ด€๋ จ์žˆ๋Š” ์š”์†Œ๋“ค์„ ํ‘œ ํ˜•์‹์œผ๋กœ ์ •๋ฆฌ]
| ์นดํ…Œ๊ณ ๋ฆฌ | ๊ด€๋ จ ์š”์†Œ | ์˜์‚ฌ ๊ฒฐ์ •๊ณผ์˜ ์—ฐ๊ด€์„ฑ |
|---------|----------|-------------------|
| [์นดํ…Œ๊ณ ๋ฆฌ1] | [์„ ์ • ์š”์†Œ] | [์˜์‚ฌ ๊ฒฐ์ •๊ณผ์˜ ์—ฐ๊ด€์„ฑ ์„ค๋ช…] |
| [์นดํ…Œ๊ณ ๋ฆฌ2] | [์„ ์ • ์š”์†Œ] | [์˜์‚ฌ ๊ฒฐ์ •๊ณผ์˜ ์—ฐ๊ด€์„ฑ ์„ค๋ช…] |
... (๊ด€๋ จ ์นดํ…Œ๊ณ ๋ฆฌ์— ๋Œ€ํ•ด ์ž‘์„ฑ)

## 3๏ธโƒฃ ์ข…ํ•ฉ์  ์˜์‚ฌ ๊ฒฐ์ • ๋ถ„์„
### ์ฃผ์š” ๊ฒฐ์ • ์š”์†Œ ๋ฐ ์˜ํ–ฅ ํ‰๊ฐ€
[๋‹ค์–‘ํ•œ ์นดํ…Œ๊ณ ๋ฆฌ๋ฅผ ๊ณ ๋ คํ•œ ์ข…ํ•ฉ์  ์ ‘๊ทผ]
| ์ฃผ์š” ์š”์†Œ | ๊ธ์ •์  ์˜ํ–ฅ | ๋ถ€์ •์  ์˜ํ–ฅ | ์ค‘์š”๋„ |
|----------|------------|------------|--------|
| [์š”์†Œ1] | [๊ธ์ •์  ์˜ํ–ฅ ์„ค๋ช…] | [๋ถ€์ •์  ์˜ํ–ฅ ์„ค๋ช…] | [์ƒ/์ค‘/ํ•˜] |
| [์š”์†Œ2] | [๊ธ์ •์  ์˜ํ–ฅ ์„ค๋ช…] | [๋ถ€์ •์  ์˜ํ–ฅ ์„ค๋ช…] | [์ƒ/์ค‘/ํ•˜] |
...

### ์˜์‚ฌ ๊ฒฐ์ • ๋Œ€์•ˆ ๋ถ„์„
| ๋Œ€์•ˆ | ์žฅ์  | ๋‹จ์  | ์ข…ํ•ฉ ํ‰๊ฐ€ |
|-----|------|------|----------|
| [๋Œ€์•ˆ1] | [์žฅ์  ๋ฆฌ์ŠคํŠธ] | [๋‹จ์  ๋ฆฌ์ŠคํŠธ] | [์ „๋ฐ˜์  ํ‰๊ฐ€] |
| [๋Œ€์•ˆ2] | [์žฅ์  ๋ฆฌ์ŠคํŠธ] | [๋‹จ์  ๋ฆฌ์ŠคํŠธ] | [์ „๋ฐ˜์  ํ‰๊ฐ€] |
...

{framework_output_format}

## 5๏ธโƒฃ ๊ถŒ์žฅ ์˜์‚ฌ ๊ฒฐ์ • ๋ฐ ์‹คํ–‰ ๊ณ„ํš (3๊ฐœ ์•ˆ)

### Plan&nbsp;A&nbsp;: [๊ถŒ์žฅ ๊ฒฐ์ • ์ œ๋ชฉ&nbsp;A]
- **๊ฐœ์š”** : 2โ€’3์ค„ ํ•ต์‹ฌ ์š”์•ฝ  
- **WHY โ€“ ์ „๋žต์  ๊ทผ๊ฑฐ** : ๋ฐ์ดํ„ฐ/ํ”„๋ ˆ์ž„์›Œํฌ ์ธ์šฉ 2โ€’3๊ฐœ  
- **WHAT โ€“ SMART ๋ชฉํ‘œ** : ๊ตฌ์ฒดยท์ธก์ •ยท๋‹ฌ์„ฑยท๊ด€๋ จยท๊ธฐํ•œ  
- **HOW โ€“ ์‹คํ–‰ ๋กœ๋“œ๋งต** : ๋‹จ๊ณ„๋ณ„ ์ผ์ • + RACI ์š”์•ฝํ‘œ  
- **RISK & MITIGATION** : ์ฃผ์š” ๋ฆฌ์Šคํฌ ร— ๋Œ€์‘ ์ „๋žต  
- **KPI & ๋ชจ๋‹ˆํ„ฐ๋ง** : ์„ ํ–‰ยทํ›„ํ–‰ KPI, ์ธก์ • ์ฃผ๊ธฐ  
- **TIME IMPACT** : ๋‹จ๊ธฐ / ์ค‘๊ธฐ / ์žฅ๊ธฐ ์˜ํ–ฅ  
- **Plan-B Trigger** : ์žฌ์ ๊ฒ€ยทํ”ผ๋ฒ— ์กฐ๊ฑด  

### Plan&nbsp;B&nbsp;: [๊ถŒ์žฅ ๊ฒฐ์ • ์ œ๋ชฉ&nbsp;B]
- **๊ฐœ์š”** : 2โ€’3์ค„ ํ•ต์‹ฌ ์š”์•ฝ  
- **WHY โ€“ ์ „๋žต์  ๊ทผ๊ฑฐ** : ๋ฐ์ดํ„ฐ/ํ”„๋ ˆ์ž„์›Œํฌ ์ธ์šฉ 2โ€’3๊ฐœ  
- **WHAT โ€“ SMART ๋ชฉํ‘œ** : ๊ตฌ์ฒดยท์ธก์ •ยท๋‹ฌ์„ฑยท๊ด€๋ จยท๊ธฐํ•œ  
- **HOW โ€“ ์‹คํ–‰ ๋กœ๋“œ๋งต** : ๋‹จ๊ณ„๋ณ„ ์ผ์ • + RACI ์š”์•ฝํ‘œ  
- **RISK & MITIGATION** : ์ฃผ์š” ๋ฆฌ์Šคํฌ ร— ๋Œ€์‘ ์ „๋žต  
- **KPI & ๋ชจ๋‹ˆํ„ฐ๋ง** : ์„ ํ–‰ยทํ›„ํ–‰ KPI, ์ธก์ • ์ฃผ๊ธฐ  
- **TIME IMPACT** : ๋‹จ๊ธฐ / ์ค‘๊ธฐ / ์žฅ๊ธฐ ์˜ํ–ฅ  
- **Plan-B Trigger** : ์žฌ์ ๊ฒ€ยทํ”ผ๋ฒ— ์กฐ๊ฑด  

### Plan&nbsp;C&nbsp;: [๊ถŒ์žฅ ๊ฒฐ์ • ์ œ๋ชฉ&nbsp;C]
- **๊ฐœ์š”** : 2โ€’3์ค„ ํ•ต์‹ฌ ์š”์•ฝ  
- **WHY โ€“ ์ „๋žต์  ๊ทผ๊ฑฐ** : ๋ฐ์ดํ„ฐ/ํ”„๋ ˆ์ž„์›Œํฌ ์ธ์šฉ 2โ€’3๊ฐœ  
- **WHAT โ€“ SMART ๋ชฉํ‘œ** : ๊ตฌ์ฒดยท์ธก์ •ยท๋‹ฌ์„ฑยท๊ด€๋ จยท๊ธฐํ•œ  
- **HOW โ€“ ์‹คํ–‰ ๋กœ๋“œ๋งต** : ๋‹จ๊ณ„๋ณ„ ์ผ์ • + RACI ์š”์•ฝํ‘œ  
- **RISK & MITIGATION** : ์ฃผ์š” ๋ฆฌ์Šคํฌ ร— ๋Œ€์‘ ์ „๋žต  
- **KPI & ๋ชจ๋‹ˆํ„ฐ๋ง** : ์„ ํ–‰ยทํ›„ํ–‰ KPI, ์ธก์ • ์ฃผ๊ธฐ  
- **TIME IMPACT** : ๋‹จ๊ธฐ / ์ค‘๊ธฐ / ์žฅ๊ธฐ ์˜ํ–ฅ  
- **Plan-B Trigger** : ์žฌ์ ๊ฒ€ยทํ”ผ๋ฒ— ์กฐ๊ฑด  

## 6๏ธโƒฃ ์ฐธ๊ณ  ์ •๋ณด
### ๋ฐ์ดํ„ฐ ์ถœ์ฒ˜
- **Kaggle ๋ฐ์ดํ„ฐ์…‹**: [๋ถ„์„์— ์‚ฌ์šฉ๋œ Kaggle ๋ฐ์ดํ„ฐ์…‹ ์ •๋ณด]
- **์›น ๊ฒ€์ƒ‰ ์ •๋ณด**: [์ฃผ์š” ์ฐธ๊ณ ํ•œ ์›น ์ •๋ณด ์ถœ์ฒ˜ 3-5๊ฐœ]

### ์ด๋ฏธ์ง€ ํ”„๋กฌํ”„ํŠธ
[์˜์‚ฌ ๊ฒฐ์ •์˜ ํ•ต์‹ฌ์„ ์‹œ๊ฐํ™”ํ•  ์ˆ˜ ์žˆ๋Š” ์˜๋ฌธ ์ด๋ฏธ์ง€ ํ”„๋กฌํ”„ํŠธ 1์ค„]

{cat_clause}
Step-by-step ์‚ฌ๊ณ  ๊ณผ์ •์„ ๋”ฐ๋ฅด๋˜, ์ถœ๋ ฅ์—๋Š” ์ตœ์ข… ๋ณด๊ณ ์„œ๋งŒ ํ‘œ์‹œํ•˜์‹ญ์‹œ์˜ค. ์ฃผ์š” ์˜์‚ฌ ๊ฒฐ์ • ์š”์†Œ์™€ ๋Œ€์•ˆ์€ ๋‹จ์ˆœํ•œ ๊ฐœ์š”๊ฐ€ ์•„๋‹Œ, ๊ตฌ์ฒด์ ์ด๊ณ  ์‹ค์งˆ์ ์ธ ๋ถ„์„๊ณผ ํ•จ๊ป˜ ์ œ์‹œํ•˜์‹ญ์‹œ์˜ค. ์›น ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ์™€ Kaggle ๋ฐ์ดํ„ฐ์…‹ ๋ถ„์„์€ ์ฐธ๊ณ  ์ •๋ณด์— ๋ฐ˜๋“œ์‹œ ํฌํ•จํ•˜์—ฌ ์˜์‚ฌ ๊ฒฐ์ •์˜ ๊ทผ๊ฑฐ๋กœ ํ™œ์šฉํ•˜์‹ญ์‹œ์˜ค.
"""
    return base_prompt.strip()

# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Brave Search API โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
@st.cache_data(ttl=3600)
def brave_search(query: str, count: int = 20):
    if not BRAVE_KEY:
        raise RuntimeError("โš ๏ธ SERPHOUSE_API_KEY (Brave API Key) ํ™˜๊ฒฝ ๋ณ€์ˆ˜๊ฐ€ ๋น„์–ด์žˆ์Šต๋‹ˆ๋‹ค.")
    headers = {
        "Accept": "application/json",
        "Accept-Encoding": "gzip",
        "X-Subscription-Token": BRAVE_KEY
    }
    params = {"q": query, "count": str(count)}
    for attempt in range(3):
        try:
            r = requests.get(BRAVE_ENDPOINT, headers=headers, params=params, timeout=15)
            r.raise_for_status()
            data = r.json()
            raw = data.get("web", {}).get("results") or data.get("results", [])
            if not raw:
                raise ValueError("No search results found.")
            arts = []
            for i, res in enumerate(raw[:count], 1):
                url = res.get("url", res.get("link", ""))
                host = re.sub(r"https?://(www\.)?", "", url).split("/")[0]
                arts.append({
                    "index": i,
                    "title": res.get("title", "No title"),
                    "link": url,
                    "snippet": res.get("description", res.get("text", "No snippet")),
                    "displayed_link": host
                })
            return arts
        except Exception as e:
            logging.error(f"Brave search failure (attempt {attempt+1}/3): {e}")
            time.sleep(1)
    return []

def mock_results(query: str) -> str:
    ts = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    return (
        f"# Fallback Search Content (Generated: {ts})\n\n"
        f"The web search API request failed. Please generate the ideas based on '{query}' using general knowledge.\n\n"
        f"You may consider aspects such as:\n\n"
        f"- Basic concept or definition of {query}\n"
        f"- Common facts or challenges\n"
        f"- Potential categories from transformation list\n\n"
        f"Note: This is fallback text, not real-time data.\n\n"
    )

def do_web_search(query: str) -> str:
    try:
        arts = brave_search(query, 20)
        if not arts:
            logging.warning("No search results from Brave. Using fallback.")
            return mock_results(query)
        hdr = "# Web Search Results\nUse the information below...\n\n"
        body = "\n".join(
            f"### Result {a['index']}: {a['title']}\n\n{a['snippet']}\n\n**Source**: [{a['displayed_link']}]({a['link']})\n\n---\n"
            for a in arts
        )
        return hdr + body
    except Exception as e:
        logging.error(f"Web search process failed: {str(e)}")
        return mock_results(query)

# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Streamlit ๋ฉ”์ธ ์•ฑ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def idea_generator_app():
    st.title("Ilรบvatar(์ผ๋ฃจ๋ฐ”ํƒ€๋ฅด) : Decision Support AI")
    st.caption("'์ผ๋ฃจ๋ฐ”ํƒ€๋ฅด'๋Š” ๋น…๋ฐ์ดํ„ฐ๋ฅผ ์ž์œจ์ ์œผ๋กœ ์ˆ˜์ง‘ยท๋ถ„์„ํ•˜์—ฌ 12์–ต ๊ฐœ ์ด์ƒ์˜ ๋ณตํ•ฉ ์˜์‚ฌ๊ฒฐ์ • ๋ณ€์ˆ˜๋ฅผ ์‹ค์‹œ๊ฐ„ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ, ์ „๋žต์  ํ†ต์ฐฐ์„ ๋„์ถœํ•˜๋Š” ์ดˆ์ง€๋Šฅํ˜• ์˜์‚ฌ๊ฒฐ์ • ์‹œ์Šคํ…œ์ž…๋‹ˆ๋‹ค.")

    default_vals = {
        "ai_model": "gpt-4.1-mini",
        "messages": [],
        "auto_save": True,
        "generate_image": True,
        "web_search_enabled": True,
        "kaggle_enabled": True,
        "selected_frameworks": ["sunzi"],
        "GLOBAL_PICK_COUNT": {},
        "_skip_dup_idx": None
    }
    for k, v in default_vals.items():
        if k not in st.session_state:
            st.session_state[k] = v

    sb = st.sidebar
    st.session_state.temp = sb.slider(
        "Diversity temperature", 0.1, 3.0, 1.3, 0.1,
        help="0.1 = ์—ฐ๊ด€์„ฑ ์œ„์ฃผ, 3.0 = ๋งค์šฐ ๋†’์€ ๋‹ค์–‘์„ฑ"
    )

    sb.title("Decision Support Settings")
    sb.toggle("Auto Save", key="auto_save")
    sb.toggle("Auto Image Generation", key="generate_image")

    st.session_state.web_search_enabled = sb.toggle(
        "Use Web Search", value=st.session_state.web_search_enabled
    )
    st.session_state.kaggle_enabled = sb.toggle(
        "Use Kaggle Datasets", value=st.session_state.kaggle_enabled
    )

    if st.session_state.web_search_enabled:
        sb.info("โœ… Web search results will be integrated.")
    if st.session_state.kaggle_enabled:
        if KAGGLE_KEY:
            sb.info("โœ… Kaggle datasets will be analyzed.")
        else:
            sb.error("โš ๏ธ KAGGLE_KEY not set. Kaggle integration disabled.")
            st.session_state.kaggle_enabled = False

    # ๋ถ„์„ ํ”„๋ ˆ์ž„์›Œํฌ ์„ ํƒ
    sb.subheader("๋ถ„์„ ํ”„๋ ˆ์ž„์›Œํฌ ์„ค์ •")
    selected_frameworks = sb.multiselect(
        "์‚ฌ์šฉํ•  ๊ฒฝ์˜ ํ”„๋ ˆ์ž„์›Œํฌ ์„ ํƒ",
        options=list(BUSINESS_FRAMEWORKS.keys()),
        default=st.session_state.selected_frameworks,
        format_func=lambda x: BUSINESS_FRAMEWORKS[x]
    )
    st.session_state.selected_frameworks = selected_frameworks or ["sunzi"]

    # ์˜ˆ์‹œ ํ† ํ”ฝ
    example_topics = {
        "example1": "์Šค๋งˆํŠธํ™ˆ ํ™˜๊ฒฝ์—์„œ ์‚ฌ์šฉ์ž ๊ฒฝํ—˜์„ ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ๋Š” ์ƒˆ๋กœ์šด ๊ฐ€์ „์ œํ’ˆ ๋””์ž์ธ ์˜์‚ฌ๊ฒฐ์ •",
        "example2": "์นœํ™˜๊ฒฝ ์—๋„ˆ์ง€ ๋ถ„์•ผ ์ง„์ถœ์„ ์œ„ํ•œ ์ตœ์  ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ ์„ ํƒ ์˜์‚ฌ๊ฒฐ์ •",
        "example3": "2030๋…„ ์˜๋ฃŒ ํ—ฌ์Šค์ผ€์–ด ์‚ฐ์—…์˜ ๊ธฐ์ˆ  ๋ฐœ์ „ ๋ฐฉํ–ฅ๊ณผ ํˆฌ์ž ์ „๋žต ์˜์‚ฌ๊ฒฐ์ •"
    }
    sb.subheader("Example Decision Topics")
    c1, c2, c3 = sb.columns(3)
    if c1.button("์ œํ’ˆ ๋””์ž์ธ ์˜์‚ฌ๊ฒฐ์ •", key="ex1"):
        process_example(example_topics["example1"])
    if c2.button("์‹ ์‚ฌ์—… ์ง„์ถœ ์ „๋žต", key="ex2"):
        process_example(example_topics["example2"])
    if c3.button("์‚ฐ์—… ๋ฏธ๋ž˜ ์ „๋ง", key="ex3"):
        process_example(example_topics["example3"])

    # ์ตœ๊ทผ ๊ฒฐ๊ณผ ๋‹ค์šด๋กœ๋“œ
    latest_ideas = next(
        (m["content"] for m in reversed(st.session_state.messages)
         if m["role"] == "assistant" and m["content"].strip()),
        None
    )
    if latest_ideas:
        title_match = re.search(r"# (.*?)(\n|$)", latest_ideas)
        title = (title_match.group(1) if title_match else "ideas").strip()
        sb.subheader("Download Latest Ideas")
        d1, d2 = sb.columns(2)
        d1.download_button("Download as Markdown", latest_ideas,
                           file_name=f"{title}.md", mime="text/markdown")
        d2.download_button("Download as HTML", md_to_html(latest_ideas, title),
                           file_name=f"{title}.html", mime="text/html")

    # ๋Œ€ํ™” ํžˆ์Šคํ† ๋ฆฌ ์—…๋กœ๋“œ/๋‹ค์šด๋กœ๋“œ
    up = sb.file_uploader("Load Conversation History (.json)",
                          type=["json"], key="json_uploader")
    if up:
        try:
            st.session_state.messages = json.load(up)
            sb.success("Conversation history loaded successfully")
        except Exception as e:
            sb.error(f"Failed to load: {e}")

    if sb.button("Download Conversation as JSON"):
        sb.download_button(
            "Save JSON",
            data=json.dumps(st.session_state.messages, ensure_ascii=False, indent=2),
            file_name="chat_history.json",
            mime="application/json"
        )

    # ํŒŒ์ผ ์—…๋กœ๋“œ
    st.subheader("File Upload (Optional)")
    uploaded_files = st.file_uploader(
        "Upload files to reference in the idea generation (txt, csv, pdf)",
        type=["txt", "csv", "pdf"],
        accept_multiple_files=True,
        key="file_uploader"
    )
    if uploaded_files:
        st.success(f"{len(uploaded_files)} files uploaded.")
        with st.expander("Preview Uploaded Files", expanded=False):
            for idx, file in enumerate(uploaded_files):
                st.write(f"**File Name:** {file.name}")
                ext = file.name.split('.')[-1].lower()
                try:
                    if ext == 'txt':
                        preview = file.read(1000).decode('utf-8', errors='ignore')
                        file.seek(0)
                        st.text_area("Preview", preview + ("..." if len(preview) >= 1000 else ""), height=150)
                    elif ext == 'csv':
                        df = pd.read_csv(file)
                        file.seek(0)
                        st.dataframe(df.head(5))
                    elif ext == 'pdf':
                        reader = PyPDF2.PdfReader(io.BytesIO(file.read()), strict=False)
                        file.seek(0)
                        pg_txt = reader.pages[0].extract_text() if reader.pages else "(No text)"
                        st.text_area("Preview", (pg_txt[:500] + "...") if pg_txt else "(No text)", height=150)
                except Exception as e:
                    st.error(f"Preview failed: {e}")
                if idx < len(uploaded_files) - 1:
                    st.divider()

    # ์ด๋ฏธ ๋ Œ๋”๋œ ๋ฉ”์‹œ์ง€(์ค‘๋ณต ๋ฐฉ์ง€)
    skip_idx = st.session_state.get("_skip_dup_idx")
    for i, m in enumerate(st.session_state.messages):
        if skip_idx is not None and i == skip_idx:
            continue
        with st.chat_message(m["role"]):
            st.markdown(m["content"])
            if "image" in m:
                st.image(m["image"], caption=m.get("image_caption", ""))
    st.session_state["_skip_dup_idx"] = None

    # ์ฑ„ํŒ… ์ž…๋ ฅ
    prompt = st.chat_input("์˜์‚ฌ ๊ฒฐ์ •์— ๋„์›€์ด ํ•„์š”ํ•œ ์ƒํ™ฉ์ด๋‚˜ ๋ฌธ์ œ๋ฅผ ์„ค๋ช…ํ•ด ์ฃผ์„ธ์š”.")
    if prompt:
        process_input(prompt, uploaded_files)
    sb.markdown("---")
    sb.markdown("Created by [VIDraft](https://discord.gg/openfreeai)")


def process_example(topic):
    process_input(topic, [])

def process_input(prompt: str, uploaded_files):
    if not any(m["role"] == "user" and m["content"] == prompt for m in st.session_state.messages):
        st.session_state.messages.append({"role": "user", "content": prompt})
    with st.chat_message("user"):
        st.markdown(prompt)

    for i in range(len(st.session_state.messages) - 1):
        if (st.session_state.messages[i]["role"] == "user"
            and st.session_state.messages[i]["content"] == prompt
            and st.session_state.messages[i + 1]["role"] == "assistant"):
            return

    with st.chat_message("assistant"):
        status = st.status("Preparing to generate ideasโ€ฆ")
        stream_placeholder = st.empty()
        full_response = ""

        try:
            client = get_openai_client()
            status.update(label="Initializing modelโ€ฆ")

            selected_cat        = st.session_state.get("category_focus", None)
            selected_frameworks = st.session_state.get("selected_frameworks", ["sunzi"])

            sys_prompt = get_idea_system_prompt(
                selected_category=selected_cat,
                selected_frameworks=selected_frameworks
            )

            def category_context(sel):
                if sel:
                    return json.dumps({sel: physical_transformation_categories[sel]}, ensure_ascii=False)
                return "ALL_CATEGORIES: " + ", ".join(physical_transformation_categories.keys())

            use_web_search = st.session_state.web_search_enabled
            use_kaggle     = st.session_state.kaggle_enabled
            has_uploaded   = bool(uploaded_files)

            search_content  = None
            kaggle_content  = None
            file_content    = None

            # โ‘  ์›น๊ฒ€์ƒ‰
            if use_web_search:
                status.update(label="Searching the webโ€ฆ")
                with st.spinner("Searchingโ€ฆ"):
                    search_content = do_web_search(keywords(prompt, top=5))

            # โ‘ก Kaggle
            if use_kaggle and check_kaggle_availability():
                status.update(label="Kaggle ๋ฐ์ดํ„ฐ์…‹ ๋ถ„์„ ์ค‘โ€ฆ")
                with st.spinner("Searching Kaggleโ€ฆ"):
                    kaggle_kw = extract_kaggle_search_keywords(prompt)
                    try:
                        datasets = search_kaggle_datasets(kaggle_kw)
                    except Exception as e:
                        logging.warning(f"search_kaggle_datasets ์˜ค๋ฅ˜ ๋ฌด์‹œ: {e}")
                        datasets = []
                    analyses = []
                    if datasets:
                        status.update(label="Downloading & analysing datasetsโ€ฆ")
                        for ds in datasets:
                            try:
                                ana = download_and_analyze_dataset(ds["ref"])
                            except Exception as e:
                                logging.error(f"Kaggle ๋ถ„์„ ์˜ค๋ฅ˜({ds['ref']}) : {e}")
                                ana = f"๋ฐ์ดํ„ฐ์…‹ ๋ถ„์„ ์˜ค๋ฅ˜: {e}"
                            analyses.append({"meta": ds, "analysis": ana})
                    if analyses:
                        kaggle_content = format_kaggle_analysis_markdown_multi(analyses)

            # โ‘ข ํŒŒ์ผ ์—…๋กœ๋“œ
            if has_uploaded:
                status.update(label="Reading uploaded filesโ€ฆ")
                with st.spinner("Processing filesโ€ฆ"):
                    file_content = process_uploaded_files(uploaded_files)

            # โ‘ฃ Military Tactics Dataset (์‹ ๊ทœ ์ถ”๊ฐ€)
            mil_content = None
            if is_military_query(prompt):
                status.update(label="Searching military tactics datasetโ€ฆ")
                with st.spinner("Loading military insightsโ€ฆ"):
                    mil_rows = military_search(prompt)
                if mil_rows:
                    mil_content = "# Military Tactics Dataset Reference\n\n"
                    for i, row in enumerate(mil_rows, 1):
                        mil_content += (
                            f"### Case {i}\n"
                            f"**Scenario:** {row['scenario_description']}\n\n"
                            f"**Attack Reasoning:** {row['attack_reasoning']}\n\n"
                            f"**Defense Reasoning:** {row['defense_reasoning']}\n\n---\n"
                        )

            user_content = prompt
            if search_content:
                user_content += "\n\n" + search_content
            if kaggle_content:
                user_content += "\n\n" + kaggle_content
            if file_content:
                user_content += "\n\n" + file_content
            if mil_content:
                user_content += "\n\n" + mil_content

            # ๋‚ด๋ถ€ ๋ถ„์„
            status.update(label="์˜์‚ฌ ๊ฒฐ์ • ๋ฌธ์ œ ๋ถ„์„ ์ค‘โ€ฆ")
            decision_purpose = identify_decision_purpose(prompt)
            relevance_scores = compute_relevance_scores(prompt, PHYS_CATEGORIES)

            status.update(label="์˜์‚ฌ ๊ฒฐ์ • ๋งคํŠธ๋ฆญ์Šค ์ƒ์„ฑ ์ค‘โ€ฆ")
            T = st.session_state.temp
            k_cat_range  = (4, 8) if T < 1.0 else (6, 10) if T < 2.0 else (8, 12)
            n_item_range = (2, 4) if T < 1.0 else (3, 6) if T < 2.0 else (4, 8)
            depth_range  = (2, 3) if T < 1.0 else (2, 5) if T < 2.0 else (2, 6)
            combos = generate_random_comparison_matrix(
                PHYS_CATEGORIES,
                relevance_scores,
                k_cat=k_cat_range,
                n_item=n_item_range,
                depth_range=depth_range,
                seed=hash(prompt) & 0xFFFFFFFF,
                T=T,
            )

            combos_table = "| ์กฐํ•ฉ | ๊ฐ€์ค‘์น˜ | ์˜ํ–ฅ๋„ | ์‹ ๋ขฐ๋„ | ์ด์  |\n|------|--------|--------|--------|-----|\n"
            for w, imp, conf, tot, cmb in combos:
                combo_str = " + ".join(f"{c[0]}-{c[1]}" for c in cmb)
                combos_table += f"| {combo_str} | {w} | {imp} | {conf:.1f} | {tot} |\n"

            purpose_info = "\n\n## ์˜์‚ฌ ๊ฒฐ์ • ๋ชฉ์  ๋ถ„์„\n"
            if decision_purpose['purposes']:
                purpose_info += "### ์ฃผ์š” ๋ชฉ์ \n"
                for p, s in decision_purpose['purposes']:
                    purpose_info += f"- **{p}** (๊ด€๋ จ์„ฑ: {s})\n"
            if decision_purpose['constraints']:
                purpose_info += "\n### ์ฃผ์š” ์ œ์•ฝ ์กฐ๊ฑด\n"
                for c, s in decision_purpose['constraints']:
                    purpose_info += f"- **{c}** (๊ด€๋ จ์„ฑ: {s})\n"

            framework_contents = []
            if "swot" in selected_frameworks:
                swot_res = analyze_with_swot(prompt)
                framework_contents.append(format_business_framework_analysis("swot", swot_res))
            if "porter" in selected_frameworks:
                porter_res = analyze_with_porter(prompt)
                framework_contents.append(format_business_framework_analysis("porter", porter_res))
            if "bcg" in selected_frameworks:
                bcg_res = analyze_with_bcg(prompt)
                framework_contents.append(format_business_framework_analysis("bcg", bcg_res))

            if framework_contents:
                user_content += "\n\n## ๊ฒฝ์˜ ํ”„๋ ˆ์ž„์›Œํฌ ๋ถ„์„ ๊ฒฐ๊ณผ\n\n" + "\n\n".join(framework_contents)

            user_content += f"\n\n## ์˜์‚ฌ ๊ฒฐ์ • ๋งคํŠธ๋ฆญ์Šค ๋ถ„์„{purpose_info}\n{combos_table}"

            status.update(label="Generating ideasโ€ฆ")
            api_messages = [
                {"role": "system", "content": sys_prompt},
                {"role": "system", "name": "category_db", "content": category_context(selected_cat)},
                {"role": "user",   "content": user_content},
            ]
            stream = client.chat.completions.create(
                model="gpt-4.1-mini",
                messages=api_messages,
                temperature=1,
                max_tokens=MAX_TOKENS,
                top_p=1,
                stream=True
            )

            for chunk in stream:
                if chunk.choices and chunk.choices[0].delta.content:
                    full_response += chunk.choices[0].delta.content
                    stream_placeholder.markdown(full_response + "โ–Œ")

            stream_placeholder.markdown(full_response)
            status.update(label="Ideas created!", state="complete")

            # ์ด๋ฏธ์ง€ ์ƒ์„ฑ
            img_data = img_caption = None
            if st.session_state.generate_image and full_response:
                match = re.search(r"###\s*์ด๋ฏธ์ง€\s*ํ”„๋กฌํ”„ํŠธ\s*\n+([^\n]+)", full_response, re.I)
                if not match:
                    match = re.search(r"Image\s+Prompt\s*[:\-]\s*([^\n]+)", full_response, re.I)
                if match:
                    raw_prompt = re.sub(r'[\r\n"\'\\]', " ", match.group(1)).strip()
                    with st.spinner("์•„์ด๋””์–ด ์ด๋ฏธ์ง€ ์ƒ์„ฑ ์ค‘โ€ฆ"):
                        img_data, img_caption = generate_image(raw_prompt)
                    if img_data:
                        st.image(img_data, caption=f"์•„์ด๋””์–ด ์‹œ๊ฐํ™” โ€“ {img_caption}")

            answer_msg = {"role": "assistant", "content": full_response}
            if img_data:
                answer_msg["image"]         = img_data
                answer_msg["image_caption"] = img_caption
            st.session_state["_skip_dup_idx"] = len(st.session_state.messages)
            st.session_state.messages.append(answer_msg)

            # ๋‹ค์šด๋กœ๋“œ ๋ฒ„ํŠผ
            st.subheader("Download This Output")
            col_md, col_html = st.columns(2)
            col_md.download_button(
                "Markdown",
                data=full_response,
                file_name=f"{prompt[:30]}.md",
                mime="text/markdown"
            )
            col_html.download_button(
                "HTML",
                data=md_to_html(full_response, prompt[:30]),
                file_name=f"{prompt[:30]}.html",
                mime="text/html"
            )

            if st.session_state.auto_save:
                fn = f"chat_history_auto_{datetime.now():%Y%m%d_%H%M%S}.json"
                with open(fn, "w", encoding="utf-8") as fp:
                    json.dump(st.session_state.messages, fp, ensure_ascii=False, indent=2)

        except Exception as e:
            logging.error("process_input error", exc_info=True)
            st.error(f"โš ๏ธ ์ž‘์—… ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {e}")
            st.session_state.messages.append(
                {"role": "assistant", "content": f"โš ๏ธ ์˜ค๋ฅ˜: {e}"}
            )

def main():
    idea_generator_app()

if __name__ == "__main__":
    main()