File size: 62,497 Bytes
338117f
 
 
 
 
42fdecf
338117f
 
 
42fdecf
338117f
 
 
42fdecf
 
338117f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42fdecf
 
 
338117f
 
42fdecf
 
338117f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19651ed
338117f
 
 
42fdecf
 
 
 
 
 
 
338117f
 
 
 
 
 
 
42fdecf
19651ed
338117f
 
42fdecf
338117f
42fdecf
338117f
 
42fdecf
 
 
338117f
42fdecf
338117f
42fdecf
 
 
 
 
338117f
42fdecf
338117f
42fdecf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
338117f
 
 
 
 
 
 
 
 
42fdecf
338117f
42fdecf
 
338117f
 
 
 
 
 
 
 
 
42fdecf
 
 
 
 
 
338117f
42fdecf
 
 
338117f
42fdecf
 
 
 
 
 
 
 
 
 
 
 
 
338117f
42fdecf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
338117f
42fdecf
 
 
 
 
 
 
 
 
 
 
338117f
 
 
42fdecf
 
 
 
 
 
 
 
 
 
 
 
338117f
 
 
 
 
 
 
42fdecf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
338117f
 
 
 
 
42fdecf
 
 
 
19651ed
338117f
 
 
 
42fdecf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
338117f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42fdecf
338117f
 
 
42fdecf
338117f
 
 
 
42fdecf
338117f
42fdecf
338117f
 
 
42fdecf
338117f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42fdecf
338117f
 
42fdecf
338117f
42fdecf
 
338117f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42fdecf
338117f
 
 
 
 
 
 
42fdecf
338117f
 
 
 
 
 
 
 
 
 
 
42fdecf
338117f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42fdecf
 
 
 
338117f
42fdecf
 
338117f
 
 
 
42fdecf
 
 
 
 
338117f
 
 
 
 
 
812a545
338117f
812a545
42fdecf
338117f
812a545
338117f
 
 
812a545
338117f
 
 
42fdecf
338117f
 
 
 
 
 
 
 
 
42fdecf
 
 
 
 
 
 
 
 
 
19651ed
42fdecf
 
 
19651ed
42fdecf
 
 
19651ed
338117f
 
 
 
 
9eaf386
338117f
 
 
 
 
 
42fdecf
9eaf386
338117f
 
 
 
 
 
 
 
 
42fdecf
338117f
 
19651ed
42fdecf
19651ed
338117f
 
 
42fdecf
338117f
42fdecf
338117f
 
 
 
 
 
 
 
 
42fdecf
338117f
42fdecf
338117f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42fdecf
338117f
 
 
 
 
 
42fdecf
338117f
 
 
 
 
 
 
 
42fdecf
338117f
 
42fdecf
 
338117f
42fdecf
338117f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
812a545
338117f
 
 
 
42fdecf
 
19651ed
42fdecf
19651ed
42fdecf
 
 
 
 
 
 
 
 
19651ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
338117f
 
 
 
b0a3482
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd3e2ea
19651ed
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
import gradio as gr
import pandas as pd
import numpy as np
import os
import traceback
from typing import Tuple, Dict, Any, Optional, List
import tempfile
import io
import datetime
import re

class FeedbackTransformer:
    """
    A class to transform feedback data with delimited topic and sentiment columns
    into binary columns with prefixes T_, S_, and C_.
    """

    def __init__(self,
                 topic_prefix="TOPIC_",
                 sentiment_prefix="SENTIMENT_",
                 category_prefix="Categories:",
                 text_column="TEXT",
                 recommendation_column="Q4_Weiterempfehlung"):
        """
        Initialize the FeedbackTransformer with column specifications.
        """
        self.topic_prefix = topic_prefix
        self.sentiment_prefix = sentiment_prefix
        self.category_prefix = category_prefix
        self.text_column = text_column
        self.recommendation_column = recommendation_column
        self.data = None
        self.transformed_data = None
        self.topic_cols = []
        self.sentiment_cols = []
        self.category_cols = []
        self.unique_topics = set()
        self.unique_categories = set()
        self.unique_sentiments = set()
        self.topic_sentiment_mapping = {}  # Map topics to their sentiment values
        self.file_name = None
        self.original_filename = None
        self.selected_columns = []
        self.verbatim_column = None  # Store the verbatim/text column

    def load_data(self, file_obj):
        """
        Load data from the uploaded file object.
        """
        if file_obj is None:
            raise ValueError("No file uploaded")

        # Get file extension and store original filename
        file_name = file_obj if isinstance(file_obj, str) else (file_obj.name if hasattr(file_obj, 'name') else 'unknown')
        self.original_filename = os.path.splitext(os.path.basename(file_name))[0]
        _, file_ext = os.path.splitext(file_name)

        # Read the data based on file type
        try:
            if file_ext.lower() in ['.xlsx', '.xls']:
                self.data = pd.read_excel(file_obj)
            elif file_ext.lower() == '.csv':
                # Try comma delimiter first
                try:
                    self.data = pd.read_csv(file_obj, encoding='utf-8')
                except:
                    # If comma fails, try tab delimiter
                    self.data = pd.read_csv(file_obj, sep='\t', encoding='utf-8')
            else:
                # Default to tab-delimited
                self.data = pd.read_csv(file_obj, sep='\t', encoding='utf-8')
        except Exception as e:
            raise ValueError(f"Error reading file: {str(e)}")

        return len(self.data), len(self.data.columns)

    def identify_columns(self):
        """
        Identify topic, category, and sentiment columns in the data.
        """
        if self.data is None:
            raise ValueError("Data not loaded")

        # Extract columns based on prefixes
        self.topic_cols = [col for col in self.data.columns if "Topic:" in col]
        self.sentiment_cols = [col for col in self.data.columns if self.sentiment_prefix in col]
        self.category_cols = [col for col in self.data.columns if col.startswith(self.category_prefix)]

        # Try to identify verbatim/text column
        text_candidates = [col for col in self.data.columns if any(keyword in col.lower() for keyword in ['text', 'verbatim', 'comment', 'feedback'])]
        if text_candidates:
            self.verbatim_column = text_candidates[0]  # Use the first match
        elif self.text_column in self.data.columns:
            self.verbatim_column = self.text_column

        # If no columns found with specified prefixes, return all columns for manual selection
        all_cols = list(self.data.columns)

        return {
            'topic_cols': self.topic_cols,
            'sentiment_cols': self.sentiment_cols,
            'category_cols': self.category_cols,
            'all_columns': all_cols,
            'verbatim_column': self.verbatim_column
        }

    def extract_unique_topics_and_categories(self):
        """
        Extract all unique topics, categories, and sentiments from the respective columns.
        """
        self.unique_topics = set()
        self.unique_categories = set()
        self.unique_sentiments = set()
        self.topic_sentiment_mapping = {}

        # Extract from topic columns (delimited by |)
        for col in self.topic_cols:
            for value in self.data[col].dropna():
                if isinstance(value, str) and value.strip():
                    # Split by | delimiter and clean each topic
                    topics = [topic.strip() for topic in value.split('|') if topic.strip()]
                    self.unique_topics.update(topics)

        # Extract from category columns (delimited by |)
        for col in self.category_cols:
            for value in self.data[col].dropna():
                if isinstance(value, str) and value.strip():
                    # Split by | delimiter and clean each category
                    categories = [cat.strip() for cat in value.split('|') if cat.strip()]
                    self.unique_categories.update(categories)

        # Extract sentiments from sentiment columns and build topic-sentiment mapping
        for col in self.sentiment_cols:
            for idx, value in enumerate(self.data[col].dropna()):
                if isinstance(value, str) and value.strip():
                    # Split by | delimiter to get individual topic::sentiment pairs
                    pairs = [pair.strip() for pair in value.split('|') if pair.strip() and '::' in pair]
                    for pair in pairs:
                        if '::' in pair:
                            topic_part, sentiment_part = pair.split('::', 1)
                            topic = topic_part.strip()
                            sentiment = sentiment_part.strip()
                            if topic and sentiment:
                                self.unique_topics.add(topic)  # Add topic from sentiment data
                                self.unique_sentiments.add(sentiment)

                                # Store the mapping for later use
                                if idx not in self.topic_sentiment_mapping:
                                    self.topic_sentiment_mapping[idx] = {}
                                self.topic_sentiment_mapping[idx][topic] = sentiment

        return len(self.unique_topics), len(self.unique_categories), len(self.unique_sentiments)

    def set_selected_columns(self, selected_columns):
        """
        Set which original columns should be included in the output.
        """
        self.selected_columns = selected_columns if selected_columns else []

    def transform_data(self):
        """
        Transform the data into binary columns with T_, S_, and C_ prefixes.
        """
        if not self.unique_topics and not self.unique_categories:
            self.extract_unique_topics_and_categories()

        # Create output dataframe starting with feedback_id
        self.transformed_data = pd.DataFrame({'feedback_id': range(1, len(self.data) + 1)})

        # Add selected original columns first (right after feedback_id)
        for col in self.selected_columns:
            if col in self.data.columns:
                self.transformed_data[col] = self.data[col]

        # Add Verbatim sentiment columns
        self.transformed_data['Verbatim_Positive'] = 0
        self.transformed_data['Verbatim_Neutral'] = 0
        self.transformed_data['Verbatim_Negative'] = 0

        # Create binary topic columns with T_ prefix
        for topic in sorted(self.unique_topics):
            safe_topic_name = self._make_safe_column_name(topic)
            col_name = f"T_{safe_topic_name}"
            self.transformed_data[col_name] = 0

        # Create sentiment columns with S_ prefix (one per topic, containing actual sentiment values)
        for topic in sorted(self.unique_topics):
            safe_topic_name = self._make_safe_column_name(topic)
            col_name = f"S_{safe_topic_name}"
            self.transformed_data[col_name] = ""  # Initialize with empty strings

        # Create binary category columns with C_ prefix
        for category in sorted(self.unique_categories):
            safe_category_name = self._make_safe_column_name(category)
            col_name = f"C_{safe_category_name}"
            self.transformed_data[col_name] = 0

        # Fill in the data
        for idx, row in self.data.iterrows():
            # Process sentiment columns to determine which topics exist in ABSA column
            topics_in_absa = set()
            all_sentiments_in_row = set()  # Track all sentiments for verbatim columns

            for s_col in self.sentiment_cols:
                sentiment_value = row.get(s_col)
                if pd.notna(sentiment_value) and isinstance(sentiment_value, str) and sentiment_value.strip():
                    pairs = [pair.strip() for pair in sentiment_value.split('|') if pair.strip()]
                    for pair in pairs:
                        if '::' in pair:
                            topic_part, sentiment_part = pair.split('::', 1)
                            topic = topic_part.strip()
                            sentiment = sentiment_part.strip()

                            if topic and sentiment:
                                topics_in_absa.add(topic)
                                all_sentiments_in_row.add(sentiment.lower())  # Store in lowercase for matching

                                # Set the actual sentiment value (not 1/0)
                                safe_topic_name = self._make_safe_column_name(topic)
                                sentiment_col_name = f"S_{safe_topic_name}"
                                if sentiment_col_name in self.transformed_data.columns:
                                    self.transformed_data.loc[idx, sentiment_col_name] = sentiment

            # Set Verbatim sentiment columns based on sentiments found in ABSA
            if any(sentiment in all_sentiments_in_row for sentiment in ['positive', 'positiv']):
                self.transformed_data.loc[idx, 'Verbatim_Positive'] = 1
            if any(sentiment in all_sentiments_in_row for sentiment in ['neutral']):
                self.transformed_data.loc[idx, 'Verbatim_Neutral'] = 1
            if any(sentiment in all_sentiments_in_row for sentiment in ['negative', 'negativ']):
                self.transformed_data.loc[idx, 'Verbatim_Negative'] = 1

            # Set T_ columns to 1 if topic exists in ABSA column, 0 otherwise
            for topic in topics_in_absa:
                safe_topic_name = self._make_safe_column_name(topic)
                topic_col_name = f"T_{safe_topic_name}"
                if topic_col_name in self.transformed_data.columns:
                    self.transformed_data.loc[idx, topic_col_name] = 1

            # Process category columns
            categories_in_row = set()
            for c_col in self.category_cols:
                category_value = row.get(c_col)
                if pd.notna(category_value) and isinstance(category_value, str) and category_value.strip():
                    categories = [cat.strip() for cat in category_value.split('|') if cat.strip()]
                    categories_in_row.update(categories)

            # Set category binary values (always 1 if present in category column)
            for category in categories_in_row:
                safe_category_name = self._make_safe_column_name(category)
                category_col_name = f"C_{safe_category_name}"
                if category_col_name in self.transformed_data.columns:
                    self.transformed_data.loc[idx, category_col_name] = 1

        return self.transformed_data.shape

    def _make_safe_column_name(self, name):
        """
        Convert a name to a safe column name by removing/replacing problematic characters.
        """
        # Replace spaces and special characters with underscores
        safe_name = re.sub(r'[^\w]', '_', str(name))
        # Remove multiple consecutive underscores
        safe_name = re.sub(r'_+', '_', safe_name)
        # Remove leading/trailing underscores
        safe_name = safe_name.strip('_')
        return safe_name

    def analyze_data(self):
        """
        Analyze the transformed data to provide insights.
        """
        if self.transformed_data is None:
            raise ValueError("No transformed data to analyze")

        # Count different types of columns
        topic_cols = [col for col in self.transformed_data.columns if col.startswith('T_')]
        sentiment_cols = [col for col in self.transformed_data.columns if col.startswith('S_')]
        category_cols = [col for col in self.transformed_data.columns if col.startswith('C_')]
        verbatim_cols = ['Verbatim_Positive', 'Verbatim_Neutral', 'Verbatim_Negative']

        # Calculate statistics
        topic_stats = {}
        for col in topic_cols:
            topic_stats[col] = self.transformed_data[col].sum()

        # For sentiment columns, count non-empty values
        sentiment_stats = {}
        for col in sentiment_cols:
            sentiment_stats[col] = (self.transformed_data[col] != "").sum()

        category_stats = {}
        for col in category_cols:
            category_stats[col] = self.transformed_data[col].sum()

        # Verbatim sentiment statistics
        verbatim_stats = {}
        for col in verbatim_cols:
            if col in self.transformed_data.columns:
                verbatim_stats[col] = self.transformed_data[col].sum()

        # Sort by frequency
        sorted_topics = sorted(topic_stats.items(), key=lambda x: x[1], reverse=True)
        sorted_sentiments = sorted(sentiment_stats.items(), key=lambda x: x[1], reverse=True)
        sorted_categories = sorted(category_stats.items(), key=lambda x: x[1], reverse=True)
        sorted_verbatim = sorted(verbatim_stats.items(), key=lambda x: x[1], reverse=True)

        # Prepare analysis summary
        analysis_text = f"**Analysis Results**\n\n"
        analysis_text += f"Total feedbacks: {len(self.transformed_data)}\n"
        analysis_text += f"Selected original columns: {len(self.selected_columns)}\n"
        analysis_text += f"Verbatim sentiment columns: 3 (Positive, Neutral, Negative)\n"
        analysis_text += f"Topic columns (T_): {len(topic_cols)}\n"
        analysis_text += f"Sentiment columns (S_): {len(sentiment_cols)}\n"
        analysis_text += f"Category columns (C_): {len(category_cols)}\n"
        analysis_text += f"Verbatim column used: {self.verbatim_column}\n\n"

        if self.selected_columns:
            analysis_text += f"**Included Original Columns:** {', '.join(self.selected_columns)}\n\n"

        # Verbatim sentiment analysis
        if sorted_verbatim:
            analysis_text += "**Verbatim Sentiment Distribution:**\n"
            for verbatim_col, count in sorted_verbatim:
                percentage = (count / len(self.transformed_data)) * 100
                analysis_text += f"- {verbatim_col}: {count} occurrences ({percentage:.1f}%)\n"

        # Topic analysis
        if sorted_topics:
            analysis_text += "\n**Top 10 Most Frequent Topics (T_):**\n"
            for topic_col, count in sorted_topics[:10]:
                analysis_text += f"- {topic_col}: {count} occurrences\n"

        # Category analysis
        if sorted_categories:
            analysis_text += "\n**Top 10 Most Frequent Categories (C_):**\n"
            for category_col, count in sorted_categories[:10]:
                analysis_text += f"- {category_col}: {count} occurrences\n"

        # Sentiment analysis
        if sorted_sentiments:
            analysis_text += "\n**Top 10 Most Frequent Sentiments (S_):**\n"
            for sentiment_col, count in sorted_sentiments[:10]:
                analysis_text += f"- {sentiment_col}: {count} sentiment values\n"

        return analysis_text

    def save_transformed_data(self, output_format='xlsx'):
        """
        Save the transformed data and return the file path.
        """
        if self.transformed_data is None:
            raise ValueError("No transformed data to save")

        # Create filename with original filename prefix and timestamp
        timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
        prefix = self.original_filename if self.original_filename else 'transformed_feedback'

        if output_format == 'xlsx':
            filename = f"{prefix}_transformed_topics_{timestamp}.xlsx"
            temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.xlsx')
            self.transformed_data.to_excel(temp_file.name, index=False)
            temp_file.close()

            final_path = os.path.join(tempfile.gettempdir(), filename)
            if os.path.exists(final_path):
                os.remove(final_path)
            os.rename(temp_file.name, final_path)

        else:  # csv
            filename = f"{prefix}_binary_matrix_{timestamp}.csv"
            temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.csv')
            self.transformed_data.to_csv(temp_file.name, index=False)
            temp_file.close()

            final_path = os.path.join(tempfile.gettempdir(), filename)
            if os.path.exists(final_path):
                os.remove(final_path)
            os.rename(temp_file.name, final_path)

        if not os.path.exists(final_path):
            raise ValueError(f"Failed to create output file: {final_path}")

        return final_path


# Gradio interface functions
def get_column_selector(file_obj):
    """
    Get a combined column preview and selector interface.
    """
    try:
        if file_obj is None:
            return gr.CheckboxGroup(
                choices=[],
                value=[],
                label="πŸ“‹ Select Columns to Include",
                info="Upload a file first to see available columns"
            )

        # Read first few rows to get column names
        file_name = file_obj if isinstance(file_obj, str) else (file_obj.name if hasattr(file_obj, 'name') else 'unknown')
        _, file_ext = os.path.splitext(file_name)

        if file_ext.lower() in ['.xlsx', '.xls']:
            df = pd.read_excel(file_obj, nrows=5)
        elif file_ext.lower() == '.csv':
            try:
                df = pd.read_csv(file_obj, nrows=5)
            except:
                df = pd.read_csv(file_obj, sep='\t', nrows=5)
        else:
            df = pd.read_csv(file_obj, sep='\t', nrows=5)

        columns = list(df.columns)
        column_choices = [f"{i+1:2d}. {col}" for i, col in enumerate(columns)]

        return gr.CheckboxGroup(
            choices=column_choices,
            value=[],
            label=f"πŸ“‹ Select Columns to Include ({len(columns)} available)",
            info="Choose which original columns to include in the transformed file (in addition to feedback_id).",
            elem_classes=["column-selector"]
        )

    except Exception as e:
        return gr.CheckboxGroup(
            choices=[],
            value=[],
            label="πŸ“‹ Select Columns to Include",
            info=f"Error reading file: {str(e)}"
        )


def extract_column_names(selected_display_names):
    """
    Extract actual column names from the numbered display format.
    """
    if not selected_display_names:
        return []

    actual_names = []
    for display_name in selected_display_names:
        if '. ' in display_name:
            actual_name = display_name.split('. ', 1)[1]
            actual_names.append(actual_name)
        else:
            actual_names.append(display_name)

    return actual_names


def process_file(file_obj, topic_prefix, sentiment_prefix, category_prefix,
                text_column, recommendation_column, output_format, analyze_data, selected_columns):
    """
    Main processing function for Gradio interface.
    """
    try:
        # Extract actual column names from display format
        actual_column_names = extract_column_names(selected_columns)

        # Initialize transformer
        transformer = FeedbackTransformer(
            topic_prefix=topic_prefix,
            sentiment_prefix=sentiment_prefix,
            category_prefix=category_prefix,
            text_column=text_column,
            recommendation_column=recommendation_column
        )

        # Load data
        rows, cols = transformer.load_data(file_obj)
        status_msg = f"βœ… Loaded {rows} rows and {cols} columns\n"

        # Set selected columns for inclusion
        transformer.set_selected_columns(actual_column_names)
        status_msg += f"πŸ“‹ Selected {len(actual_column_names)} original columns for inclusion\n"
        if actual_column_names:
            status_msg += f"   Selected columns: {', '.join(actual_column_names)}\n"

        # Identify columns
        col_info = transformer.identify_columns()
        status_msg += f"\nπŸ“Š Found columns:\n"
        status_msg += f"- Topic columns: {len(col_info['topic_cols'])}\n"
        status_msg += f"- Sentiment columns: {len(col_info['sentiment_cols'])}\n"
        status_msg += f"- Category columns: {len(col_info['category_cols'])}\n"
        status_msg += f"- Verbatim column: {col_info['verbatim_column']}\n"

        # Extract unique topics, categories, and sentiments
        num_topics, num_categories, num_sentiments = transformer.extract_unique_topics_and_categories()
        status_msg += f"\n🎯 Found {num_topics} unique topics\n"
        status_msg += f"🏷️ Found {num_categories} unique categories\n"
        status_msg += f"πŸ’­ Found {num_sentiments} unique sentiments\n"

        # Transform data
        shape = transformer.transform_data()
        status_msg += f"\n✨ Transformed data shape: {shape[0]} rows Γ— {shape[1]} columns\n"
        status_msg += f"πŸ“Š Binary matrix created with T_, S_, C_ prefixes and Verbatim sentiment columns\n"
        status_msg += f"πŸ”§ T_ columns: 1 if topic present in ABSA column, 0 otherwise\n"
        status_msg += f"πŸ”§ S_ columns: contain actual sentiment values (not 1/0)\n"
        status_msg += f"πŸ”§ C_ columns: 1 if category assigned, 0 otherwise\n"
        status_msg += f"πŸ”§ Verbatim_Positive/Neutral/Negative: 1 if respective sentiment found in ABSA, 0 otherwise\n"

        # Analyze if requested
        analysis_result = ""
        if analyze_data:
            analysis_result = transformer.analyze_data()

        # Save transformed data
        output_file = transformer.save_transformed_data(output_format)
        status_msg += f"\nπŸ’Ύ File saved successfully: {os.path.basename(output_file)}\n"
        #status_msg += f"πŸ“₯ File download should start automatically\n"

        return status_msg, analysis_result, output_file

    except Exception as e:
        error_msg = f"❌ Error: {str(e)}\n\n{traceback.format_exc()}"
        return error_msg, "", None


# Create Gradio interface
with gr.Blocks(title="Binary Matrix Feedback Transformer", css="""
.column-selector .form-check {
    display: block !important;
    margin-bottom: 8px !important;
}
.column-selector .form-check-input {
    margin-right: 8px !important;
}
""") as demo:
    gr.Markdown("""
    # πŸ“Š Binary Matrix Feedback Transformer
    Transform feedback data with delimited topic and sentiment columns into binary matrix format.

    ### πŸ”§ Processing Logic:
    - **Verbatim_Positive/Neutral/Negative**: Set to 1 if respective sentiment is found in ABSA column, 0 otherwise
    - **T_ Columns**: Set to 1 if topic is present in ABSA column, 0 otherwise
    - **S_ Columns**: One column per topic (e.g., S_Allgemeine_Zufriedenheit) containing actual sentiment values
    - **C_ Columns**: Set to 1 if category is assigned, 0 otherwise

    ### πŸ“‹ Data Format Requirements:
    - **Topics**: Delimited by `|` (pipe) in "Topics:" columns (optional)
    - **Sentiments**: Format `Topic::Sentiment|Topic2::Sentiment2` in ABSA columns
    - **Categories**: Delimited by `|` (pipe) in "Categories:" columns

    ### πŸ†• Key Logic:
    - **Verbatim_** columns detect overall sentiment presence regardless of topic
    - **T_** columns based on ABSA column presence (topics that have sentiment data)
    - **S_** columns contain actual sentiment values (not binary 1/0)
    - No automatic column renaming for "Topic:" prefix
    """)

    with gr.Row():
        with gr.Column(scale=1):
            # File upload
            gr.Markdown("### πŸ“‹ 1. Source file upload")
            input_file = gr.File(
                label="Upload Input File",
                file_types=[".xlsx", ".xls", ".csv", ".txt"],
                type="filepath"
            )

            # Combined column selector
            gr.Markdown("### πŸ“‹ 2. Column Selection")
            column_selector = gr.CheckboxGroup(
                choices=[],
                value=[],
                label="Select Columns to Include",
                info="Upload a file first to see available columns"
            )

        with gr.Column(scale=1):
            # Configuration parameters
            gr.Markdown("### βš™οΈ 3. Configuration")

            topic_prefix = gr.Textbox(
                label="Topic Column Identifier",
                value="Topic:",
                info="Text to identify topic columns (for reference only)"
            )

            sentiment_prefix = gr.Textbox(
                label="Sentiment Column Prefix (ABSA)",
                value="ABSA:",
                info="Prefix to identify sentiment columns (format: Topic::Sentiment)"
            )

            category_prefix = gr.Textbox(
                label="Category Column Prefix",
                value="Categories:",
                info="Prefix to identify category columns"
            )

            text_column = gr.Textbox(
                label="Text/Verbatim Column Pattern",
                value="TEXT",
                info="Pattern to identify verbatim text column (for reference only)"
            )

            recommendation_column = gr.Textbox(
                label="Recommendation Column Name",
                value="Q4_Weiterempfehlung",
                info="Column containing recommendation scores (for reference only)"
            )

            output_format = gr.Radio(
                label="Output Format",
                choices=["xlsx", "csv"],
                value="xlsx"
            )

            analyze_checkbox = gr.Checkbox(
                label="Analyze transformed data",
                value=True
            )

    # Transform button
    transform_btn = gr.Button("πŸ”„ 4. Transform to Binary Matrix & Download", variant="primary", size="lg")

    # Output sections
    with gr.Row():
        with gr.Column():
            status_output = gr.Textbox(
                label="Processing Status",
                lines=12,
                interactive=False
            )

        with gr.Column():
            analysis_output = gr.Markdown(
                label="Data Analysis"
            )

    # Download section
    with gr.Row():
        with gr.Column():
            gr.Markdown("### πŸ“₯ Download Status")
            gr.Markdown("Please click on the link inside the output file size value to download the transformed file (the number value on the right hand side below). You may need to right click and select Save Link As (or something similar)")
            output_file = gr.File(
                label="Transformed Binary Matrix (Auto-Download)",
                interactive=False,
                visible=True
            )

    # Event handlers
    input_file.change(
        fn=get_column_selector,
        inputs=[input_file],
        outputs=[column_selector]
    )

    transform_btn.click(
        fn=process_file,
        inputs=[
            input_file,
            topic_prefix,
            sentiment_prefix,
            category_prefix,
            text_column,
            recommendation_column,
            output_format,
            analyze_checkbox,
            column_selector
        ],
        outputs=[status_output, analysis_output, output_file]
    )

    # Examples section
    gr.Markdown("""
    ### πŸ“ Example Transformations:

    **Input Data:**
    ```
    | feedback_id | ABSA: Sentiments | Categories: Issues |
    | 1 | Service::Negative|Quality::Positive | Issues|Support |
    ```

    **Output Binary Matrix:**
    ```
    | feedback_id | Verbatim_Positive | Verbatim_Neutral | Verbatim_Negative | T_Service | T_Quality | S_Service | S_Quality | C_Issues | C_Support |
    | 1 | 1 | 0 | 1 | 1 | 1 | Negative | Positive | 1 | 1 |
    ```

    ### πŸ’‘ Column Logic:
    - **Verbatim_Positive**: 1 if any "Positive"/"Positiv" sentiment found in ABSA
    - **Verbatim_Neutral**: 1 if any "Neutral" sentiment found in ABSA
    - **Verbatim_Negative**: 1 if any "Negative"/"Negativ" sentiment found in ABSA
    - **T_[topic_name]**: 1 if topic exists in ABSA column, 0 otherwise
    - **S_[topic_name]**: Actual sentiment value for that topic (e.g., "Positive", "Negative")
    - **C_[category_name]**: 1 if category is assigned, 0 otherwise
    - Safe column names (special characters replaced with underscores)

    ### πŸ” Key Changes Made:
    - **NEW**: Added Verbatim_Positive, Verbatim_Neutral, Verbatim_Negative columns
    - These columns are set to 1 if the respective sentiment is found anywhere in the ABSA column
    - Supports both English (Positive/Negative/Neutral) and German (Positiv/Negativ) sentiment detection
    - Removed automatic "Topic:" column renaming logic
    - T_ columns are now binary (1/0) based on topic existence in ABSA column
    - Topics are extracted from ABSA sentiment data for T_ column creation
    """)

# Launch the app
if __name__ == "__main__":
    demo.launch(import gradio as gr
import pandas as pd
import numpy as np
import os
import traceback
from typing import Tuple, Dict, Any, Optional, List
import tempfile
import io
import datetime
import re

class FeedbackTransformer:
    """
    A class to transform feedback data with delimited topic and sentiment columns
    into binary columns with prefixes T_, S_, and C_.
    """

    def __init__(self,
                 topic_prefix="TOPIC_",
                 sentiment_prefix="SENTIMENT_",
                 category_prefix="Categories:",
                 text_column="TEXT",
                 recommendation_column="Q4_Weiterempfehlung"):
        """
        Initialize the FeedbackTransformer with column specifications.
        """
        self.topic_prefix = topic_prefix
        self.sentiment_prefix = sentiment_prefix
        self.category_prefix = category_prefix
        self.text_column = text_column
        self.recommendation_column = recommendation_column
        self.data = None
        self.transformed_data = None
        self.topic_cols = []
        self.sentiment_cols = []
        self.category_cols = []
        self.unique_topics = set()
        self.unique_categories = set()
        self.unique_sentiments = set()
        self.topic_sentiment_mapping = {}  # Map topics to their sentiment values
        self.file_name = None
        self.original_filename = None
        self.selected_columns = []
        self.verbatim_column = None  # Store the verbatim/text column
        self.dynamic_topic_prefix = None  # Store dynamically extracted topic prefix

    def load_data(self, file_obj):
        """
        Load data from the uploaded file object.
        """
        if file_obj is None:
            raise ValueError("No file uploaded")

        # Get file extension and store original filename
        file_name = file_obj if isinstance(file_obj, str) else (file_obj.name if hasattr(file_obj, 'name') else 'unknown')
        self.original_filename = os.path.splitext(os.path.basename(file_name))[0]
        _, file_ext = os.path.splitext(file_name)

        # Read the data based on file type
        try:
            if file_ext.lower() in ['.xlsx', '.xls']:
                self.data = pd.read_excel(file_obj)
            elif file_ext.lower() == '.csv':
                # Try comma delimiter first
                try:
                    self.data = pd.read_csv(file_obj, encoding='utf-8')
                except:
                    # If comma fails, try tab delimiter
                    self.data = pd.read_csv(file_obj, sep='\t', encoding='utf-8')
            else:
                # Default to tab-delimited
                self.data = pd.read_csv(file_obj, sep='\t', encoding='utf-8')
        except Exception as e:
            raise ValueError(f"Error reading file: {str(e)}")

        return len(self.data), len(self.data.columns)

    def extract_topic_prefix_from_category(self):
        """
        Extract the topic prefix from a column containing "Category:"
        by finding text between "Category:" and "("
        """
        # Look for columns containing "Category:"
        category_pattern_cols = [col for col in self.data.columns if "Category:" in col]

        if category_pattern_cols:
            # Use the first matching column
            category_col = category_pattern_cols[0]

            # Try to extract from column name first
            match = re.search(r'Category:\s*([^(]+)\s*\(', category_col)
            if match:
                extracted_prefix = match.group(1).strip() + ":"
                self.dynamic_topic_prefix = extracted_prefix
                return extracted_prefix

            # If not found in column name, try to extract from column values
            for value in self.data[category_col].dropna():
                if isinstance(value, str):
                    match = re.search(r'Category:\s*([^(]+)\s*\(', value)
                    if match:
                        extracted_prefix = match.group(1).strip() + ":"
                        self.dynamic_topic_prefix = extracted_prefix
                        return extracted_prefix

        # If no match found, return None
        return None

    def identify_columns(self):
        """
        Identify topic, category, and sentiment columns in the data.
        """
        if self.data is None:
            raise ValueError("Data not loaded")

        # First try to extract topic prefix dynamically
        extracted_prefix = self.extract_topic_prefix_from_category()

        # Use dynamic prefix if found, otherwise use the provided topic_prefix
        topic_identifier = extracted_prefix if extracted_prefix else self.topic_prefix

        # Log the prefix being used
        print(f"Using topic prefix: '{topic_identifier}'")

        # Extract columns based on prefixes
        # For topic columns, use the dynamic or provided prefix
        if topic_identifier:
            self.topic_cols = [col for col in self.data.columns if topic_identifier in col]
        else:
            self.topic_cols = [col for col in self.data.columns if "Topic:" in col]

        self.sentiment_cols = [col for col in self.data.columns if self.sentiment_prefix in col]
        self.category_cols = [col for col in self.data.columns if col.startswith(self.category_prefix)]

        # Try to identify verbatim/text column
        text_candidates = [col for col in self.data.columns if any(keyword in col.lower() for keyword in ['text', 'verbatim', 'comment', 'feedback'])]
        if text_candidates:
            self.verbatim_column = text_candidates[0]  # Use the first match
        elif self.text_column in self.data.columns:
            self.verbatim_column = self.text_column

        # If no columns found with specified prefixes, return all columns for manual selection
        all_cols = list(self.data.columns)

        return {
            'topic_cols': self.topic_cols,
            'sentiment_cols': self.sentiment_cols,
            'category_cols': self.category_cols,
            'all_columns': all_cols,
            'verbatim_column': self.verbatim_column,
            'dynamic_topic_prefix': self.dynamic_topic_prefix
        }

    def extract_unique_topics_and_categories(self):
        """
        Extract all unique topics, categories, and sentiments from the respective columns.
        """
        self.unique_topics = set()
        self.unique_categories = set()
        self.unique_sentiments = set()
        self.topic_sentiment_mapping = {}

        # Extract from topic columns (delimited by |)
        for col in self.topic_cols:
            for value in self.data[col].dropna():
                if isinstance(value, str) and value.strip():
                    # Split by | delimiter and clean each topic
                    topics = [topic.strip() for topic in value.split('|') if topic.strip()]
                    self.unique_topics.update(topics)

        # Extract from category columns (delimited by |)
        for col in self.category_cols:
            for value in self.data[col].dropna():
                if isinstance(value, str) and value.strip():
                    # Split by | delimiter and clean each category
                    categories = [cat.strip() for cat in value.split('|') if cat.strip()]
                    self.unique_categories.update(categories)

        # Extract sentiments from sentiment columns and build topic-sentiment mapping
        for col in self.sentiment_cols:
            for idx, value in enumerate(self.data[col].dropna()):
                if isinstance(value, str) and value.strip():
                    # Split by | delimiter to get individual topic::sentiment pairs
                    pairs = [pair.strip() for pair in value.split('|') if pair.strip() and '::' in pair]
                    for pair in pairs:
                        if '::' in pair:
                            topic_part, sentiment_part = pair.split('::', 1)
                            topic = topic_part.strip()
                            sentiment = sentiment_part.strip()
                            if topic and sentiment:
                                self.unique_topics.add(topic)  # Add topic from sentiment data
                                self.unique_sentiments.add(sentiment)

                                # Store the mapping for later use
                                if idx not in self.topic_sentiment_mapping:
                                    self.topic_sentiment_mapping[idx] = {}
                                self.topic_sentiment_mapping[idx][topic] = sentiment

        return len(self.unique_topics), len(self.unique_categories), len(self.unique_sentiments)

    def set_selected_columns(self, selected_columns):
        """
        Set which original columns should be included in the output.
        """
        self.selected_columns = selected_columns if selected_columns else []

    def transform_data(self):
        """
        Transform the data into binary columns with T_, S_, and C_ prefixes.
        """
        if not self.unique_topics and not self.unique_categories:
            self.extract_unique_topics_and_categories()

        # Create output dataframe starting with feedback_id
        self.transformed_data = pd.DataFrame({'feedback_id': range(1, len(self.data) + 1)})

        # Add selected original columns first (right after feedback_id)
        for col in self.selected_columns:
            if col in self.data.columns:
                self.transformed_data[col] = self.data[col]

        # Add Verbatim sentiment columns
        self.transformed_data['Verbatim_Positive'] = 0
        self.transformed_data['Verbatim_Neutral'] = 0
        self.transformed_data['Verbatim_Negative'] = 0

        # Create binary topic columns with T_ prefix
        for topic in sorted(self.unique_topics):
            safe_topic_name = self._make_safe_column_name(topic)
            col_name = f"T_{safe_topic_name}"
            self.transformed_data[col_name] = 0

        # Create sentiment columns with S_ prefix (one per topic, containing actual sentiment values)
        for topic in sorted(self.unique_topics):
            safe_topic_name = self._make_safe_column_name(topic)
            col_name = f"S_{safe_topic_name}"
            self.transformed_data[col_name] = ""  # Initialize with empty strings

        # Create binary category columns with C_ prefix
        for category in sorted(self.unique_categories):
            safe_category_name = self._make_safe_column_name(category)
            col_name = f"C_{safe_category_name}"
            self.transformed_data[col_name] = 0

        # Fill in the data
        for idx, row in self.data.iterrows():
            # Process sentiment columns to determine which topics exist in ABSA column
            topics_in_absa = set()
            all_sentiments_in_row = set()  # Track all sentiments for verbatim columns

            for s_col in self.sentiment_cols:
                sentiment_value = row.get(s_col)
                if pd.notna(sentiment_value) and isinstance(sentiment_value, str) and sentiment_value.strip():
                    pairs = [pair.strip() for pair in sentiment_value.split('|') if pair.strip()]
                    for pair in pairs:
                        if '::' in pair:
                            topic_part, sentiment_part = pair.split('::', 1)
                            topic = topic_part.strip()
                            sentiment = sentiment_part.strip()

                            if topic and sentiment:
                                topics_in_absa.add(topic)
                                all_sentiments_in_row.add(sentiment.lower())  # Store in lowercase for matching

                                # Set the actual sentiment value (not 1/0)
                                safe_topic_name = self._make_safe_column_name(topic)
                                sentiment_col_name = f"S_{safe_topic_name}"
                                if sentiment_col_name in self.transformed_data.columns:
                                    self.transformed_data.loc[idx, sentiment_col_name] = sentiment

            # Set Verbatim sentiment columns based on sentiments found in ABSA
            if any(sentiment in all_sentiments_in_row for sentiment in ['positive', 'positiv']):
                self.transformed_data.loc[idx, 'Verbatim_Positive'] = 1
            if any(sentiment in all_sentiments_in_row for sentiment in ['neutral']):
                self.transformed_data.loc[idx, 'Verbatim_Neutral'] = 1
            if any(sentiment in all_sentiments_in_row for sentiment in ['negative', 'negativ']):
                self.transformed_data.loc[idx, 'Verbatim_Negative'] = 1

            # Set T_ columns to 1 if topic exists in ABSA column, 0 otherwise
            for topic in topics_in_absa:
                safe_topic_name = self._make_safe_column_name(topic)
                topic_col_name = f"T_{safe_topic_name}"
                if topic_col_name in self.transformed_data.columns:
                    self.transformed_data.loc[idx, topic_col_name] = 1

            # Process category columns
            categories_in_row = set()
            for c_col in self.category_cols:
                category_value = row.get(c_col)
                if pd.notna(category_value) and isinstance(category_value, str) and category_value.strip():
                    categories = [cat.strip() for cat in category_value.split('|') if cat.strip()]
                    categories_in_row.update(categories)

            # Set category binary values (always 1 if present in category column)
            for category in categories_in_row:
                safe_category_name = self._make_safe_column_name(category)
                category_col_name = f"C_{safe_category_name}"
                if category_col_name in self.transformed_data.columns:
                    self.transformed_data.loc[idx, category_col_name] = 1

        return self.transformed_data.shape

    def _make_safe_column_name(self, name):
        """
        Convert a name to a safe column name by removing/replacing problematic characters.
        """
        # Replace spaces and special characters with underscores
        safe_name = re.sub(r'[^\w]', '_', str(name))
        # Remove multiple consecutive underscores
        safe_name = re.sub(r'_+', '_', safe_name)
        # Remove leading/trailing underscores
        safe_name = safe_name.strip('_')
        return safe_name

    def analyze_data(self):
        """
        Analyze the transformed data to provide insights.
        """
        if self.transformed_data is None:
            raise ValueError("No transformed data to analyze")

        # Count different types of columns
        topic_cols = [col for col in self.transformed_data.columns if col.startswith('T_')]
        sentiment_cols = [col for col in self.transformed_data.columns if col.startswith('S_')]
        category_cols = [col for col in self.transformed_data.columns if col.startswith('C_')]
        verbatim_cols = ['Verbatim_Positive', 'Verbatim_Neutral', 'Verbatim_Negative']

        # Calculate statistics
        topic_stats = {}
        for col in topic_cols:
            topic_stats[col] = self.transformed_data[col].sum()

        # For sentiment columns, count non-empty values
        sentiment_stats = {}
        for col in sentiment_cols:
            sentiment_stats[col] = (self.transformed_data[col] != "").sum()

        category_stats = {}
        for col in category_cols:
            category_stats[col] = self.transformed_data[col].sum()

        # Verbatim sentiment statistics
        verbatim_stats = {}
        for col in verbatim_cols:
            if col in self.transformed_data.columns:
                verbatim_stats[col] = self.transformed_data[col].sum()

        # Sort by frequency
        sorted_topics = sorted(topic_stats.items(), key=lambda x: x[1], reverse=True)
        sorted_sentiments = sorted(sentiment_stats.items(), key=lambda x: x[1], reverse=True)
        sorted_categories = sorted(category_stats.items(), key=lambda x: x[1], reverse=True)
        sorted_verbatim = sorted(verbatim_stats.items(), key=lambda x: x[1], reverse=True)

        # Prepare analysis summary
        analysis_text = f"**Analysis Results**\n\n"
        analysis_text += f"Total feedbacks: {len(self.transformed_data)}\n"
        analysis_text += f"Selected original columns: {len(self.selected_columns)}\n"
        analysis_text += f"Verbatim sentiment columns: 3 (Positive, Neutral, Negative)\n"
        analysis_text += f"Topic columns (T_): {len(topic_cols)}\n"
        analysis_text += f"Sentiment columns (S_): {len(sentiment_cols)}\n"
        analysis_text += f"Category columns (C_): {len(category_cols)}\n"
        analysis_text += f"Verbatim column used: {self.verbatim_column}\n"

        # Add dynamic topic prefix info
        if self.dynamic_topic_prefix:
            analysis_text += f"Dynamic topic prefix extracted: '{self.dynamic_topic_prefix}'\n\n"
        else:
            analysis_text += f"Topic prefix used: '{self.topic_prefix}'\n\n"

        if self.selected_columns:
            analysis_text += f"**Included Original Columns:** {', '.join(self.selected_columns)}\n\n"

        # Verbatim sentiment analysis
        if sorted_verbatim:
            analysis_text += "**Verbatim Sentiment Distribution:**\n"
            for verbatim_col, count in sorted_verbatim:
                percentage = (count / len(self.transformed_data)) * 100
                analysis_text += f"- {verbatim_col}: {count} occurrences ({percentage:.1f}%)\n"

        # Topic analysis
        if sorted_topics:
            analysis_text += "\n**Top 10 Most Frequent Topics (T_):**\n"
            for topic_col, count in sorted_topics[:10]:
                analysis_text += f"- {topic_col}: {count} occurrences\n"

        # Category analysis
        if sorted_categories:
            analysis_text += "\n**Top 10 Most Frequent Categories (C_):**\n"
            for category_col, count in sorted_categories[:10]:
                analysis_text += f"- {category_col}: {count} occurrences\n"

        # Sentiment analysis
        if sorted_sentiments:
            analysis_text += "\n**Top 10 Most Frequent Sentiments (S_):**\n"
            for sentiment_col, count in sorted_sentiments[:10]:
                analysis_text += f"- {sentiment_col}: {count} sentiment values\n"

        return analysis_text

    def save_transformed_data(self, output_format='xlsx'):
        """
        Save the transformed data and return the file path.
        """
        if self.transformed_data is None:
            raise ValueError("No transformed data to save")

        # Create filename with original filename prefix and timestamp
        timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
        prefix = self.original_filename if self.original_filename else 'transformed_feedback'

        if output_format == 'xlsx':
            filename = f"{prefix}_transformed_topics_{timestamp}.xlsx"
            temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.xlsx')
            self.transformed_data.to_excel(temp_file.name, index=False)
            temp_file.close()

            final_path = os.path.join(tempfile.gettempdir(), filename)
            if os.path.exists(final_path):
                os.remove(final_path)
            os.rename(temp_file.name, final_path)

        else:  # csv
            filename = f"{prefix}_binary_matrix_{timestamp}.csv"
            temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.csv')
            self.transformed_data.to_csv(temp_file.name, index=False)
            temp_file.close()

            final_path = os.path.join(tempfile.gettempdir(), filename)
            if os.path.exists(final_path):
                os.remove(final_path)
            os.rename(temp_file.name, final_path)

        if not os.path.exists(final_path):
            raise ValueError(f"Failed to create output file: {final_path}")

        return final_path


# Gradio interface functions
def get_column_selector(file_obj):
    """
    Get a combined column preview and selector interface.
    """
    try:
        if file_obj is None:
            return gr.CheckboxGroup(
                choices=[],
                value=[],
                label="πŸ“‹ Select Columns to Include",
                info="Upload a file first to see available columns"
            )

        # Read first few rows to get column names
        file_name = file_obj if isinstance(file_obj, str) else (file_obj.name if hasattr(file_obj, 'name') else 'unknown')
        _, file_ext = os.path.splitext(file_name)

        if file_ext.lower() in ['.xlsx', '.xls']:
            df = pd.read_excel(file_obj, nrows=5)
        elif file_ext.lower() == '.csv':
            try:
                df = pd.read_csv(file_obj, nrows=5)
            except:
                df = pd.read_csv(file_obj, sep='\t', nrows=5)
        else:
            df = pd.read_csv(file_obj, sep='\t', nrows=5)

        columns = list(df.columns)
        column_choices = [f"{i+1:2d}. {col}" for i, col in enumerate(columns)]

        return gr.CheckboxGroup(
            choices=column_choices,
            value=[],
            label=f"πŸ“‹ Select Columns to Include ({len(columns)} available)",
            info="Choose which original columns to include in the transformed file (in addition to feedback_id).",
            elem_classes=["column-selector"]
        )

    except Exception as e:
        return gr.CheckboxGroup(
            choices=[],
            value=[],
            label="πŸ“‹ Select Columns to Include",
            info=f"Error reading file: {str(e)}"
        )


def extract_column_names(selected_display_names):
    """
    Extract actual column names from the numbered display format.
    """
    if not selected_display_names:
        return []

    actual_names = []
    for display_name in selected_display_names:
        if '. ' in display_name:
            actual_name = display_name.split('. ', 1)[1]
            actual_names.append(actual_name)
        else:
            actual_names.append(display_name)

    return actual_names


def process_file(file_obj, topic_prefix, sentiment_prefix, category_prefix,
                text_column, recommendation_column, output_format, analyze_data, selected_columns):
    """
    Main processing function for Gradio interface.
    """
    try:
        # Extract actual column names from display format
        actual_column_names = extract_column_names(selected_columns)

        # Initialize transformer
        transformer = FeedbackTransformer(
            topic_prefix=topic_prefix,
            sentiment_prefix=sentiment_prefix,
            category_prefix=category_prefix,
            text_column=text_column,
            recommendation_column=recommendation_column
        )

        # Load data
        rows, cols = transformer.load_data(file_obj)
        status_msg = f"βœ… Loaded {rows} rows and {cols} columns\n"

        # Set selected columns for inclusion
        transformer.set_selected_columns(actual_column_names)
        status_msg += f"πŸ“‹ Selected {len(actual_column_names)} original columns for inclusion\n"
        if actual_column_names:
            status_msg += f"   Selected columns: {', '.join(actual_column_names)}\n"

        # Identify columns
        col_info = transformer.identify_columns()
        status_msg += f"\nπŸ“Š Found columns:\n"
        status_msg += f"- Topic columns: {len(col_info['topic_cols'])}\n"
        status_msg += f"- Sentiment columns: {len(col_info['sentiment_cols'])}\n"
        status_msg += f"- Category columns: {len(col_info['category_cols'])}\n"
        status_msg += f"- Verbatim column: {col_info['verbatim_column']}\n"

        # Add dynamic topic prefix info
        if col_info.get('dynamic_topic_prefix'):
            status_msg += f"- Dynamic topic prefix extracted: '{col_info['dynamic_topic_prefix']}'\n"

        # Extract unique topics, categories, and sentiments
        num_topics, num_categories, num_sentiments = transformer.extract_unique_topics_and_categories()
        status_msg += f"\n🎯 Found {num_topics} unique topics\n"
        status_msg += f"🏷️ Found {num_categories} unique categories\n"
        status_msg += f"πŸ’­ Found {num_sentiments} unique sentiments\n"

        # Transform data
        shape = transformer.transform_data()
        status_msg += f"\n✨ Transformed data shape: {shape[0]} rows Γ— {shape[1]} columns\n"
        status_msg += f"πŸ“Š Binary matrix created with T_, S_, C_ prefixes and Verbatim sentiment columns\n"
        status_msg += f"πŸ”§ T_ columns: 1 if topic present in ABSA column, 0 otherwise\n"
        status_msg += f"πŸ”§ S_ columns: contain actual sentiment values (not 1/0)\n"
        status_msg += f"πŸ”§ C_ columns: 1 if category assigned, 0 otherwise\n"
        status_msg += f"πŸ”§ Verbatim_Positive/Neutral/Negative: 1 if respective sentiment found in ABSA, 0 otherwise\n"

        # Analyze if requested
        analysis_result = ""
        if analyze_data:
            analysis_result = transformer.analyze_data()

        # Save transformed data
        output_file = transformer.save_transformed_data(output_format)
        status_msg += f"\nπŸ’Ύ File saved successfully: {os.path.basename(output_file)}\n"
        #status_msg += f"πŸ“₯ File download should start automatically\n"

        return status_msg, analysis_result, output_file

    except Exception as e:
        error_msg = f"❌ Error: {str(e)}\n\n{traceback.format_exc()}"
        return error_msg, "", None


# Create Gradio interface
with gr.Blocks(title="Binary Matrix Feedback Transformer", css="""
.column-selector .form-check {
    display: block !important;
    margin-bottom: 8px !important;
}
.column-selector .form-check-input {
    margin-right: 8px !important;
}
""") as demo:
    gr.Markdown("""
    # πŸ“Š Binary Matrix Feedback Transformer
    Transform feedback data with delimited topic and sentiment columns into binary matrix format.

    ### πŸ”§ Processing Logic:
    - **Automatic Topic Prefix Detection**: Extracts topic prefix from columns containing "Category:" by finding text between "Category:" and "("
    - **Verbatim_Positive/Neutral/Negative**: Set to 1 if respective sentiment is found in ABSA column, 0 otherwise
    - **T_ Columns**: Set to 1 if topic is present in ABSA column, 0 otherwise
    - **S_ Columns**: One column per topic (e.g., S_Allgemeine_Zufriedenheit) containing actual sentiment values
    - **C_ Columns**: Set to 1 if category is assigned, 0 otherwise

    ### πŸ“‹ Data Format Requirements:
    - **Topics**: Delimited by `|` (pipe) in columns identified by dynamic or manual prefix
    - **Sentiments**: Format `Topic::Sentiment|Topic2::Sentiment2` in ABSA columns
    - **Categories**: Delimited by `|` (pipe) in "Categories:" columns

    ### πŸ†• Key Features:
    - **Dynamic Topic Prefix Extraction**: Automatically extracts topic prefix from "Category:" columns
    - **Verbatim_** columns detect overall sentiment presence regardless of topic
    - **T_** columns based on ABSA column presence (topics that have sentiment data)
    - **S_** columns contain actual sentiment values (not binary 1/0)
    """)

    with gr.Row():
        with gr.Column(scale=1):
            # File upload
            gr.Markdown("### πŸ“‹ 1. Source file upload")
            input_file = gr.File(
                label="Upload Input File",
                file_types=[".xlsx", ".xls", ".csv", ".txt"],
                type="filepath"
            )

            # Combined column selector
            gr.Markdown("### πŸ“‹ 2. Column Selection")
            column_selector = gr.CheckboxGroup(
                choices=[],
                value=[],
                label="Select Columns to Include",
                info="Upload a file first to see available columns"
            )

        with gr.Column(scale=1):
            # Configuration parameters
            gr.Markdown("### βš™οΈ 3. Configuration")

            topic_prefix = gr.Textbox(
                label="Topic Column Identifier (Fallback)",
                value="Topic:",
                info="Fallback identifier if dynamic extraction from Category: column fails"
            )

            sentiment_prefix = gr.Textbox(
                label="Sentiment Column Prefix (ABSA)",
                value="ABSA:",
                info="Prefix to identify sentiment columns (format: Topic::Sentiment)"
            )

            category_prefix = gr.Textbox(
                label="Category Column Prefix",
                value="Categories:",
                info="Prefix to identify category columns"
            )

            text_column = gr.Textbox(
                label="Text/Verbatim Column Pattern",
                value="TEXT",
                info="Pattern to identify verbatim text column (for reference only)"
            )

            recommendation_column = gr.Textbox(
                label="Recommendation Column Name",
                value="Q4_Weiterempfehlung",
                info="Column containing recommendation scores (for reference only)"
            )

            output_format = gr.Radio(
                label="Output Format",
                choices=["xlsx", "csv"],
                value="xlsx"
            )

            analyze_checkbox = gr.Checkbox(
                label="Analyze transformed data",
                value=True
            )

    # Transform button
    transform_btn = gr.Button("πŸ”„ 4. Transform to Binary Matrix & Download", variant="primary", size="lg")

    # Output sections
    with gr.Row():
        with gr.Column():
            status_output = gr.Textbox(
                label="Processing Status",
                lines=12,
                interactive=False
            )

        with gr.Column():
            analysis_output = gr.Markdown(
                label="Data Analysis"
            )

    # Download section
    with gr.Row():
        with gr.Column():
            gr.Markdown("### πŸ“₯ Download Status")
            gr.Markdown("Please click on the link inside the output file size value to download the transformed file (the number value on the right hand side below). You may need to right click and select Save Link As (or something similar)")
            output_file = gr.File(
                label="Transformed Binary Matrix (Auto-Download)",
                interactive=False,
                visible=True
            )

    # Event handlers
    input_file.change(
        fn=get_column_selector,
        inputs=[input_file],
        outputs=[column_selector]
    )

    transform_btn.click(
        fn=process_file,
        inputs=[
            input_file,
            topic_prefix,
            sentiment_prefix,
            category_prefix,
            text_column,
            recommendation_column,
            output_format,
            analyze_checkbox,
            column_selector
        ],
        outputs=[status_output, analysis_output, output_file]
    )

    # Examples section
    gr.Markdown("""
    ### πŸ“ Example Transformations:

    **Input Data with Dynamic Topic Extraction:**
    ```
    | Column: "Category: Service (ABC)" | ABSA: Sentiments | Categories: Issues |
    | 1 | Service::Negative|Quality::Positive | Issues|Support |
    ```

    **System will:**
    1. Extract "Service:" from "Category: Service (ABC)" column
    2. Use "Service:" to identify topic columns instead of "Topic:"

    **Output Binary Matrix:**
    ```
    | feedback_id | Verbatim_Positive | Verbatim_Neutral | Verbatim_Negative | T_Service | T_Quality | S_Service | S_Quality | C_Issues | C_Support |
    | 1 | 1 | 0 | 1 | 1 | 1 | Negative | Positive | 1 | 1 |
    ```

    ### πŸ’‘ Dynamic Topic Prefix Logic:
    - Searches for columns containing "Category:"
    - Extracts text between "Category:" and "(" (e.g., "Service" from "Category: Service (ABC)")
    - Adds ":" to create the topic prefix (e.g., "Service:")
    - Uses this prefix to identify topic columns
    - Falls back to manual "Topic Column Identifier" if extraction fails

    ### πŸ” Key Changes in This Version:
    - **NEW**: Automatic extraction of topic prefix from Category columns
    - Dynamically identifies topic columns based on extracted prefix
    - Maintains all other functionality (Verbatim columns, T_, S_, C_ logic)
    - Provides fallback to manual topic prefix if extraction fails
    """)

# Launch the app
if __name__ == "__main__":
    demo.launch()