@@ -22,6 +22,8 @@ function trans(messageName, placeholders = []) {
22
22
return message ;
23
23
}
24
24
25
+ console . log ( "Available functions in messenger.messages:" , Object . keys ( messenger . messages ) ) ;
26
+
25
27
26
28
function initialize ( ) {
27
29
console . log ( "Initializing..." ) ;
@@ -332,13 +334,16 @@ function selectMessage(messageId) {
332
334
}
333
335
334
336
function getMessageTags ( messageId ) {
335
- return messenger . messages . get ( messageId ) . then ( ( message ) => message . tags || [ ] ) ;
337
+ return messenger . messages . get ( messageId ) . then ( ( message ) => {
338
+ console . log ( `Retrieved tags for message ${ messageId } :` , message . tags ) ;
339
+ return message . tags || [ ] ;
340
+ } ) ;
336
341
}
337
342
343
+
344
+ // learnTagFromMail Funktion
338
345
function learnTagFromMail ( messageId , tagName , isPositive ) {
339
- console . log (
340
- `Training with message ID: ${ messageId } and tag: ${ tagName } `
341
- ) ;
346
+ console . log ( `Training with message ID: ${ messageId } and tag: ${ tagName } ` ) ;
342
347
343
348
const tagKey = tagNameToKeyMap [ tagName ] ;
344
349
@@ -350,8 +355,7 @@ function learnTagFromMail(messageId, tagName, isPositive) {
350
355
messenger . storage . local
351
356
. get ( [ "bayesData" , "tagOnTraining" ] )
352
357
. then ( ( result ) => {
353
- const tagOnTraining =
354
- result . tagOnTraining !== undefined ? result . tagOnTraining : true ;
358
+ const tagOnTraining = result . tagOnTraining !== undefined ? result . tagOnTraining : true ;
355
359
bayesData = result . bayesData || { } ;
356
360
357
361
bayesData [ tagName ] = bayesData [ tagName ] || {
@@ -378,16 +382,10 @@ function learnTagFromMail(messageId, tagName, isPositive) {
378
382
379
383
const uniqueTokens = Object . keys ( tokenCounts ) ;
380
384
381
- const probabilityData = calculateBayesProbability (
382
- tokens ,
383
- bayesData [ tagName ]
384
- ) ;
385
+ const probabilityData = calculateBayesProbability ( tokens , bayesData [ tagName ] ) ;
385
386
const probabilityBefore = probabilityData . probability ;
386
387
387
- const knownTokenData = getKnownTokenPercentage (
388
- tokens ,
389
- bayesData [ tagName ] . tokenList
390
- ) ;
388
+ const knownTokenData = getKnownTokenPercentage ( tokens , bayesData [ tagName ] . tokenList ) ;
391
389
392
390
if ( knownTokenData . knownPercentage === 100 ) {
393
391
console . log ( "Aborting: mail already trained (all tokens known)." ) ;
@@ -399,18 +397,14 @@ function learnTagFromMail(messageId, tagName, isPositive) {
399
397
probabilityBefore > 0.9999 &&
400
398
knownTokenData . knownPercentage >= 90
401
399
) {
402
- console . log (
403
- "Overfitting stop: Probability already > 99.99% and over 90% tokens known."
404
- ) ;
400
+ console . log ( "Overfitting stop: Probability already > 99.99% and over 90% tokens known." ) ;
405
401
return ;
406
402
} else if (
407
403
! isPositive &&
408
404
probabilityBefore < 0.0001 &&
409
405
knownTokenData . knownPercentage >= 90
410
406
) {
411
- console . log (
412
- "Overfitting stop: Probability already < 0.01% and over 90% tokens known."
413
- ) ;
407
+ console . log ( "Overfitting stop: Probability already < 0.01% and over 90% tokens known." ) ;
414
408
return ;
415
409
}
416
410
@@ -431,8 +425,7 @@ function learnTagFromMail(messageId, tagName, isPositive) {
431
425
bayesData [ tagName ] . tokenList [ token ] [ 5 ] += 1 ; // negativeTrainCount
432
426
}
433
427
434
- bayesData [ tagName ] . tokenList [ token ] [ 6 ] =
435
- bayesData [ tagName ] . trainingCount ;
428
+ bayesData [ tagName ] . tokenList [ token ] [ 6 ] = bayesData [ tagName ] . trainingCount ;
436
429
} ) ;
437
430
438
431
uniqueTokens . forEach ( ( token ) => {
@@ -455,63 +448,34 @@ function learnTagFromMail(messageId, tagName, isPositive) {
455
448
if ( tagOnTraining ) {
456
449
getMessageTags ( messageId )
457
450
. then ( ( currentTags ) => {
451
+ let updatedTags ;
458
452
if ( isPositive ) {
459
- const updatedTags = Array . from (
460
- new Set ( [ ...currentTags , tagKey ] )
461
- ) ;
462
- messenger . messages
463
- . update ( messageId , {
464
- tags : updatedTags ,
465
- } )
466
- . then ( ( ) => {
467
- console . log ( `Tag "${ tagName } " added to the email.` ) ;
468
- } )
469
- . catch ( ( error ) => {
470
- console . error ( `Error adding tag "${ tagName } ":` , error ) ;
471
- } ) ;
453
+ updatedTags = Array . from ( new Set ( [ ...currentTags , tagKey ] ) ) ;
472
454
} else {
473
- const updatedTags = currentTags . filter (
474
- ( key ) => key !== tagKey
475
- ) ;
476
- messenger . messages
477
- . update ( messageId , {
478
- tags : updatedTags ,
479
- } )
480
- . then ( ( ) => {
481
- console . log ( `Tag "${ tagName } " removed from the email.` ) ;
482
- } )
483
- . catch ( ( error ) => {
484
- console . error (
485
- `Error removing tag "${ tagName } ":` ,
486
- error
487
- ) ;
488
- } ) ;
455
+ updatedTags = currentTags . filter ( ( key ) => key !== tagKey ) ;
489
456
}
457
+
458
+ messenger . messages
459
+ . update ( messageId , { tags : updatedTags } )
460
+ . then ( ( ) => {
461
+ console . log ( `Tag "${ tagName } " ${ isPositive ? "added to" : "removed from" } the email.` ) ;
462
+ } )
463
+ . catch ( ( error ) => {
464
+ console . error ( `Error updating tag "${ tagName } ":` , error ) ;
465
+ } ) ;
490
466
} )
491
467
. catch ( ( error ) => {
492
- console . error (
493
- `Error retrieving tags for message ${ messageId } :` ,
494
- error
495
- ) ;
468
+ console . error ( `Error retrieving tags for message ${ messageId } :` , error ) ;
496
469
} ) ;
497
470
}
498
471
499
472
messenger . storage . local
500
473
. set ( { bayesData } )
501
474
. then ( ( ) => {
502
- const probabilityAfter = calculateBayesProbability (
503
- tokens ,
504
- bayesData [ tagName ]
505
- ) . probability ;
506
- const trainingResult = isPositive
507
- ? `as ${ tagName } `
508
- : `as NOT ${ tagName } ` ;
475
+ const probabilityAfter = calculateBayesProbability ( tokens , bayesData [ tagName ] ) . probability ;
476
+ const trainingResult = isPositive ? `als ${ tagName } ` : `als NICHT ${ tagName } ` ;
509
477
console . log (
510
- `Mail trained ${ trainingResult } . (Before -> After Training: ${ (
511
- probabilityBefore * 100
512
- ) . toFixed ( 2 ) } % -> ${ (
513
- probabilityAfter * 100
514
- ) . toFixed ( 2 ) } %)`
478
+ `Mail wurde ${ trainingResult } trainiert. (Vorher -> Nachher: ${ ( probabilityBefore * 100 ) . toFixed ( 2 ) } % -> ${ ( probabilityAfter * 100 ) . toFixed ( 2 ) } %)`
515
479
) ;
516
480
} )
517
481
. catch ( ( error ) => {
@@ -527,6 +491,11 @@ function learnTagFromMail(messageId, tagName, isPositive) {
527
491
} ) ;
528
492
}
529
493
494
+
495
+
496
+
497
+
498
+ // classifyEmail Funktion
530
499
function classifyEmail ( messageId ) {
531
500
console . log ( `Classifying message with ID: ${ messageId } ` ) ;
532
501
getEmailContent ( messageId )
@@ -538,14 +507,9 @@ function classifyEmail(messageId) {
538
507
selectedTags . forEach ( ( tagName ) => {
539
508
const tagKey = tagNameToKeyMap [ tagName ] ;
540
509
if ( tagKey && bayesData [ tagName ] ) {
541
- const probabilityData = calculateBayesProbability (
542
- tokens ,
543
- bayesData [ tagName ]
544
- ) ;
510
+ const probabilityData = calculateBayesProbability ( tokens , bayesData [ tagName ] ) ;
545
511
const probability = probabilityData . probability ;
546
- console . log (
547
- `Probability for ${ tagName } : ${ ( probability * 100 ) . toFixed ( 2 ) } %`
548
- ) ;
512
+ console . log ( `Probability for ${ tagName } : ${ ( probability * 100 ) . toFixed ( 2 ) } %` ) ;
549
513
550
514
if ( probability >= threshold ) {
551
515
tagsToAdd . push ( tagKey ) ;
@@ -571,15 +535,11 @@ function classifyEmail(messageId) {
571
535
. update ( messageId , { tags : updatedTags } )
572
536
. then ( ( ) => {
573
537
if ( tagsToAdd . length > 0 ) {
574
- const addedTagNames = tagsToAdd . map (
575
- ( key ) => tagKeyToNameMap [ key ]
576
- ) ;
538
+ const addedTagNames = tagsToAdd . map ( ( key ) => tagKeyToNameMap [ key ] ) ;
577
539
console . log ( "Tags added:" , addedTagNames ) ;
578
540
}
579
541
if ( tagsToRemove . length > 0 ) {
580
- const removedTagNames = tagsToRemove . map (
581
- ( key ) => tagKeyToNameMap [ key ]
582
- ) ;
542
+ const removedTagNames = tagsToRemove . map ( ( key ) => tagKeyToNameMap [ key ] ) ;
583
543
console . log ( "Tags removed:" , removedTagNames ) ;
584
544
}
585
545
} )
@@ -591,17 +551,16 @@ function classifyEmail(messageId) {
591
551
}
592
552
} )
593
553
. catch ( ( error ) => {
594
- console . error (
595
- `Error retrieving tags for message ${ messageId } :` ,
596
- error
597
- ) ;
554
+ console . error ( `Error retrieving tags for message ${ messageId } :` , error ) ;
598
555
} ) ;
599
556
} )
600
557
. catch ( ( error ) => {
601
558
console . error ( "Error classifying email:" , error ) ;
602
559
} ) ;
603
560
}
604
561
562
+
563
+
605
564
function classifyNewEmail ( messageId ) {
606
565
getEmailContent ( messageId )
607
566
. then ( ( content ) => {
@@ -611,14 +570,9 @@ function classifyNewEmail(messageId) {
611
570
selectedTags . forEach ( ( tagName ) => {
612
571
const tagKey = tagNameToKeyMap [ tagName ] ;
613
572
if ( tagKey && bayesData [ tagName ] ) {
614
- const probabilityData = calculateBayesProbability (
615
- tokens ,
616
- bayesData [ tagName ]
617
- ) ;
573
+ const probabilityData = calculateBayesProbability ( tokens , bayesData [ tagName ] ) ;
618
574
const probability = probabilityData . probability ;
619
- console . log (
620
- `Probability for ${ tagName } : ${ ( probability * 100 ) . toFixed ( 2 ) } %`
621
- ) ;
575
+ console . log ( `Probability for ${ tagName } : ${ ( probability * 100 ) . toFixed ( 2 ) } %` ) ;
622
576
623
577
if ( probability >= threshold ) {
624
578
tagsToAdd . push ( tagKey ) ;
@@ -629,9 +583,7 @@ function classifyNewEmail(messageId) {
629
583
if ( tagsToAdd . length > 0 ) {
630
584
getMessageTags ( messageId )
631
585
. then ( ( currentTags ) => {
632
- const updatedTags = Array . from (
633
- new Set ( [ ...currentTags , ...tagsToAdd ] )
634
- ) ;
586
+ const updatedTags = Array . from ( new Set ( [ ...currentTags , ...tagsToAdd ] ) ) ;
635
587
messenger . messages
636
588
. update ( messageId , { tags : updatedTags } )
637
589
. then ( ( ) => {
@@ -645,10 +597,7 @@ function classifyNewEmail(messageId) {
645
597
} ) ;
646
598
} )
647
599
. catch ( ( error ) => {
648
- console . error (
649
- `Error retrieving tags for message ${ messageId } :` ,
650
- error
651
- ) ;
600
+ console . error ( `Error retrieving tags for message ${ messageId } :` , error ) ;
652
601
} ) ;
653
602
} else {
654
603
console . log ( "No tags added." ) ;
@@ -659,6 +608,11 @@ function classifyNewEmail(messageId) {
659
608
} ) ;
660
609
}
661
610
611
+
612
+
613
+
614
+
615
+
662
616
function onNewMailReceived ( folder , messages ) {
663
617
const accountId = folder . accountId ;
664
618
if ( selectedAccounts . includes ( accountId ) ) {
0 commit comments