-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScoring.lua
More file actions
960 lines (836 loc) · 30.7 KB
/
Scoring.lua
File metadata and controls
960 lines (836 loc) · 30.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
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
local addonName, BBT = ...
BBT = BBT or _G.BigBotTracker or {}
_G.BigBotTracker = BBT
BBT.Scoring = BBT.Scoring or {}
local Scoring = BBT.Scoring
local Util = BBT.Util
local FEATURE_VERSION = 4
local LOG_2 = math.log(2)
local function log2(value)
if value <= 0 then
return 0
end
return math.log(value) / LOG_2
end
local function bucketInterval(seconds, binSize)
binSize = binSize or 10
return math.floor((seconds + (binSize / 2)) / binSize) * binSize
end
local function sortedCopy(values)
local copy = {}
for _, value in ipairs(values or {}) do
copy[#copy + 1] = tonumber(value) or 0
end
table.sort(copy)
return copy
end
local function calculateAverage(values)
if #values == 0 then
return 0
end
local total = 0
for _, value in ipairs(values) do
total = total + value
end
return total / #values
end
local function calculateVariance(values, average)
if #values <= 1 then
return 0
end
local total = 0
for _, value in ipairs(values) do
local delta = value - average
total = total + (delta * delta)
end
return total / (#values - 1)
end
local function calculatePercentile(values, percentile)
values = sortedCopy(values)
if #values == 0 then
return 0
end
if #values == 1 then
return values[1]
end
local rank = (percentile / 100) * (#values - 1) + 1
local lower = math.floor(rank)
local upper = math.ceil(rank)
if lower == upper then
return values[lower]
end
local fraction = rank - lower
return values[lower] + ((values[upper] - values[lower]) * fraction)
end
local function calculateMedian(values)
return calculatePercentile(values, 50)
end
local function calculateRobustStats(values)
values = values or {}
if #values == 0 then
return {
median = 0,
mad = 0,
iqr = 0,
robustCoefficientVariation = 1,
}
end
local median = calculateMedian(values)
local deviations = {}
for _, value in ipairs(values) do
deviations[#deviations + 1] = math.abs(value - median)
end
local mad = calculateMedian(deviations)
local q1 = calculatePercentile(values, 25)
local q3 = calculatePercentile(values, 75)
local scaledMad = mad * 1.4826
local robustCoefficientVariation = median > 0 and (scaledMad / median) or 1
return {
median = median,
mad = mad,
iqr = math.max(0, q3 - q1),
robustCoefficientVariation = robustCoefficientVariation,
}
end
local function calculateEntropyFromCounts(counts, total)
if total <= 0 then
return 1
end
local bucketCount = 0
local entropy = 0
for _, count in pairs(counts or {}) do
if count > 0 then
bucketCount = bucketCount + 1
local p = count / total
entropy = entropy - (p * log2(p))
end
end
if bucketCount <= 1 then
return 0
end
return entropy / log2(bucketCount)
end
local function buildBucketSummary(intervalRecords, binSize)
local counts = {}
local total = 0
for _, record in ipairs(intervalRecords or {}) do
local bucket = record.b or bucketInterval(record.s or 0, binSize)
counts[bucket] = (counts[bucket] or 0) + 1
total = total + 1
end
local buckets = {}
for bucket, count in pairs(counts) do
buckets[#buckets + 1] = {
bucket = bucket,
count = count,
percent = total > 0 and (count / total * 100) or 0,
}
end
table.sort(buckets, function(left, right)
if left.count == right.count then
return left.bucket < right.bucket
end
return left.count > right.count
end)
return buckets, counts, total
end
local function calculateRollingEntropy(intervalRecords, windowSize, binSize)
local count = #intervalRecords
if count < 2 then
return 1
end
local startIndex = math.max(1, count - windowSize + 1)
local counts = {}
local total = 0
for index = startIndex, count do
local record = intervalRecords[index]
local bucket = record.b or bucketInterval(record.s or 0, binSize)
counts[bucket] = (counts[bucket] or 0) + 1
total = total + 1
end
return calculateEntropyFromCounts(counts, total)
end
local function buildCadencePhases(intervalRecords, minPhaseLength)
local phases = {}
minPhaseLength = minPhaseLength or 3
local currentBucket
local phaseCount = 0
local phaseFirstTime
local phaseLastTime
local function flush()
if currentBucket and phaseCount >= minPhaseLength then
phases[#phases + 1] = {
bucket = currentBucket,
count = phaseCount,
startTime = phaseFirstTime,
endTime = phaseLastTime,
duration = math.max(0, (phaseLastTime or 0) - (phaseFirstTime or 0)),
}
end
end
for _, record in ipairs(intervalRecords or {}) do
local bucket = record.b
if bucket == currentBucket then
phaseCount = phaseCount + 1
phaseLastTime = record.t
else
flush()
currentBucket = bucket
phaseCount = 1
phaseFirstTime = (record.t or 0) - (record.s or 0)
phaseLastTime = record.t
end
end
flush()
local switches = 0
local lastBucket
local totalDuration = 0
for _, phase in ipairs(phases) do
if lastBucket and lastBucket ~= phase.bucket then
switches = switches + 1
end
totalDuration = totalDuration + (phase.duration or 0)
lastBucket = phase.bucket
end
return phases, switches, totalDuration
end
local function buildWindowSummary(intervalRecords, windowSeconds, binSize, anchorTime)
local records = {}
local values = {}
anchorTime = anchorTime or 0
for _, record in ipairs(intervalRecords or {}) do
if anchorTime <= 0 or anchorTime - (record.t or 0) <= windowSeconds then
records[#records + 1] = record
values[#values + 1] = record.s or 0
end
end
local buckets, counts, intervalCount = buildBucketSummary(records, binSize)
local robust = calculateRobustStats(values)
local messageCount = intervalCount > 0 and intervalCount + 1 or 0
return {
seconds = windowSeconds,
intervalCount = intervalCount,
messageCount = messageCount,
entropy = calculateEntropyFromCounts(counts, intervalCount),
topBucket = buckets[1] and buckets[1].bucket or 0,
topBucketPercent = buckets[1] and buckets[1].percent or 0,
medianInterval = robust.median,
robustCoefficientVariation = robust.robustCoefficientVariation,
postsPerHour = windowSeconds > 0 and (messageCount / (windowSeconds / 3600)) or 0,
}
end
local function classifyCadence(timing, behavior)
local intervalCount = timing.intervalCount or 0
local buckets = timing.dominantBuckets or {}
local topBucket = buckets[1]
local topBucketPercent = topBucket and (topBucket.percent or 0) or 0
local rollingEntropy = timing.lowestRollingEntropy or 1
local globalEntropy = timing.globalEntropy or 1
local robustCv = timing.robustCoefficientVariation or 1
local cadenceSwitches = timing.cadenceSwitchCount or 0
local phaseCount = #(timing.cadencePhases or {})
local averageInterval = timing.averageInterval or 0
local medianInterval = timing.medianInterval or 0
local hasGapOutliers = medianInterval > 0 and averageInterval > (medianInterval * 1.5)
if intervalCount < 3 then
return "Sparse"
end
if cadenceSwitches > 0 and phaseCount >= 2 then
return "Mixed Cadence"
end
if
topBucketPercent >= 95
and rollingEntropy <= 0.25
and globalEntropy <= 0.25
and robustCv <= 0.16
and not hasGapOutliers
then
return "Fixed Cadence"
end
if topBucketPercent >= 75 and rollingEntropy <= 0.25 and robustCv <= 0.16 then
return "Dominant Active-Run Cadence"
end
if
(topBucketPercent >= 60 and rollingEntropy <= 0.45 and robustCv <= 0.35)
or (topBucketPercent >= 35 and robustCv <= 0.18)
then
return "Jittered Cadence"
end
if
(behavior and (behavior.burstCount or 0) > 0)
and (behavior.activeSpan or 0) <= 600
and topBucketPercent < 55
then
return "Burst Pattern"
end
return "Variable"
end
local function addWeightedReason(reasons, weight, text)
if text and text ~= "" then
reasons[#reasons + 1] = {
weight = weight or 0,
text = text,
}
end
end
local function finalizeReasons(reasons)
table.sort(reasons, function(left, right)
if left.weight == right.weight then
return left.text < right.text
end
return left.weight > right.weight
end)
local finalized = {}
for index = 1, math.min(5, #reasons) do
finalized[index] = reasons[index].text
end
return finalized
end
local function calculateNetworkContext(network)
network = network or {}
local peerCount = network.peerCount or 0
local summary = network.summary or {}
local score = math.min(12, peerCount * 4)
if peerCount >= 2 then
if (summary.averageRollingEntropy or 1) <= 0.40 then
score = score + 8
end
if (summary.averageTemplateReusePercent or 0) >= 60 then
score = score + 8
end
if (summary.messageCount or 0) >= 20 then
score = score + 6
end
if (summary.cadenceSwitchCount or 0) > 0 then
score = score + 3
end
if (summary.averagePostsPerHour or 0) >= 10 then
score = score + 3
end
end
local confidence = network.confidence or 0
if peerCount > 0 then
confidence = math.max(confidence, math.min(peerCount / 3, 1) * 30)
end
return {
score = Util.Clamp(score, 0, 44),
confidence = Util.Clamp(confidence, 0, 100),
}
end
local function getRecencyFactor(candidate)
local lastSeen = candidate.lastSeen or Util.GetNow()
local age = math.max(0, Util.GetNow() - lastSeen)
local gracePeriod = 7 * 86400
local fullDecayWindow = 53 * 86400
if age <= gracePeriod then
return 1
end
local decayProgress = math.min(1, (age - gracePeriod) / fullDecayWindow)
return math.max(0.55, 1 - (decayProgress * 0.45))
end
local function countEvidenceFamilies(familyScores)
local count = 0
if (familyScores.timing or 0) >= 8 then
count = count + 1
end
if (familyScores.content or 0) >= 8 then
count = count + 1
end
if (familyScores.activity or 0) >= 6 then
count = count + 1
end
if (familyScores.persistence or 0) >= 4 then
count = count + 1
end
if (familyScores.baseline or 0) >= 5 then
count = count + 1
end
return count
end
local function calculateConfidence(candidate, familyCount)
local messageCount = candidate.totalMessages or 0
local timing = candidate.timing or {}
local intervalCount = timing.intervalCount or 0
local dayCount = Util.CountMap(candidate.daysSeen)
local activeWindows = 0
for _, summary in pairs(timing.windowSummaries or {}) do
if (summary.messageCount or 0) >= 3 then
activeWindows = activeWindows + 1
end
end
local confidence = 0
confidence = confidence + math.min(messageCount / 18, 1) * 30
confidence = confidence + math.min(intervalCount / 12, 1) * 28
confidence = confidence + math.min(activeWindows / 3, 1) * 14
confidence = confidence + math.min(dayCount / 3, 1) * 16
confidence = confidence + math.min((familyCount or 0) / 3, 1) * 12
return Util.Clamp(confidence, 0, 100)
end
local function countActiveWindows(timing)
local activeWindows = 0
for _, summary in pairs((timing or {}).windowSummaries or {}) do
if (summary.messageCount or 0) >= 3 then
activeWindows = activeWindows + 1
end
end
return activeWindows
end
local function getStatusCapReasons(candidate, familyCount)
local reasons = {}
local timing = candidate.timing or {}
local baseline = candidate.baseline or {}
local messageCount = candidate.totalMessages or 0
if messageCount < 6 then
reasons[#reasons + 1] = "Too few local messages"
elseif messageCount < 12 then
reasons[#reasons + 1] = "Limited local history"
end
if (timing.intervalCount or #(timing.intervals or {})) < 3 then
reasons[#reasons + 1] = "Too few timing samples"
end
if (familyCount or 0) <= 1 then
reasons[#reasons + 1] = "Only one signal type"
end
if (baseline.sampleCount or 0) > 0 and (baseline.sampleCount or 0) < 50 then
reasons[#reasons + 1] = "Baseline still warming up"
end
return reasons
end
local function getStatus(score, localEvidence, familyCount, familyScores, candidate, hasMeaningfulEvidence)
if not hasMeaningfulEvidence then
return "Observing"
end
local messageCount = candidate.totalMessages or 0
local timing = candidate.timing or {}
local intervalCount = timing.intervalCount or #(timing.intervals or {})
if messageCount < 6 or intervalCount < 3 then
return "Observing"
end
if messageCount < 12 or familyCount <= 1 then
return "Early Pattern"
end
local hasContent = (familyScores.content or 0) >= 8
local hasTimingOrActivity = (familyScores.timing or 0) >= 8 or (familyScores.activity or 0) >= 6
local activeRuns = #(timing.cadencePhases or {})
local enoughVolume = (messageCount >= 18 and intervalCount >= 10)
or activeRuns >= 2
or countActiveWindows(timing) >= 2
if
score >= 85
and localEvidence >= 70
and familyCount >= 3
and hasContent
and hasTimingOrActivity
and enoughVolume
then
return "Very Strong Pattern"
end
if score >= 70 and localEvidence >= 55 and familyCount >= 2 then
return "Strong Pattern"
end
if score >= 45 and localEvidence >= 35 and familyCount >= 1 then
return "Repeated Pattern"
end
if score >= 20 then
return "Early Pattern"
end
return "Observing"
end
local function scoreTiming(candidate, reasons)
local timing = candidate.timing or {}
local intervalCount = timing.intervalCount or 0
if intervalCount < 4 then
return 0
end
local score = 0
local topBucket = timing.dominantBuckets and timing.dominantBuckets[1]
local topBucketPercent = topBucket and (topBucket.percent or 0) or 0
local rollingEntropy = timing.lowestRollingEntropy or 1
local robustCv = timing.robustCoefficientVariation or 1
local cadenceClass = timing.cadenceClass or "Variable"
if cadenceClass == "Fixed Cadence" then
score = score + 15
addWeightedReason(reasons, 15, "Intervals are almost entirely concentrated around one fixed posting cadence.")
elseif cadenceClass == "Dominant Active-Run Cadence" or cadenceClass == "Dominant Cadence" then
score = score + 10
addWeightedReason(reasons, 10, "Most active intervals cluster around one posting cadence with occasional gaps.")
elseif cadenceClass == "Jittered Cadence" then
score = score + 11
addWeightedReason(reasons, 11, "Timing looks jittered but still cadence-bound.")
elseif cadenceClass == "Mixed Cadence" or cadenceClass == "Mixed Regular" then
score = score + 12
addWeightedReason(reasons, 12, "Cadence changed between stable posting schedules.")
end
if topBucketPercent >= 75 then
score = score + 8
addWeightedReason(
reasons,
8,
string.format("%d%% of intervals fall near %ds.", math.floor(topBucketPercent + 0.5), topBucket.bucket or 0)
)
elseif topBucketPercent >= 60 then
score = score + 5
addWeightedReason(reasons, 5, string.format("%d%% of intervals share one cadence.", topBucketPercent))
end
if rollingEntropy <= 0.20 then
score = score + 7
addWeightedReason(reasons, 7, "Recent posting windows have very low interval entropy.")
elseif rollingEntropy <= 0.40 then
score = score + 4
end
if robustCv <= 0.12 then
score = score + 5
addWeightedReason(
reasons,
5,
string.format(
"Median interval %ds with very low robust variation.",
math.floor((timing.medianInterval or 0) + 0.5)
)
)
elseif robustCv <= 0.25 then
score = score + 3
end
if (timing.cadencePhaseDuration or 0) >= 600 then
score = score + 3
end
local cap = (cadenceClass == "Dominant Active-Run Cadence" or cadenceClass == "Dominant Cadence") and 32 or 35
return Util.Clamp(score, 0, cap)
end
local function scoreContent(candidate, reasons)
local content = candidate.content or {}
local messageCount = candidate.totalMessages or 0
if messageCount < 3 then
return 0
end
local score = 0
local templateReuse = content.templateReusePercent or 0
local shingleReuse = content.shingleReusePercent or 0
local nearDuplicateRate = messageCount > 0 and ((content.nearDuplicateCount or 0) / messageCount * 100) or 0
local intentRepeat = content.dominantIntentPercent or 0
if templateReuse >= 80 then
score = score + 13
addWeightedReason(
reasons,
13,
string.format("%d%% of local messages reuse the same text pattern.", templateReuse)
)
elseif templateReuse >= 60 then
score = score + 9
addWeightedReason(reasons, 9, "Most messages reuse the same exact template.")
elseif templateReuse >= 40 then
score = score + 5
end
if shingleReuse >= 75 then
score = score + 10
addWeightedReason(reasons, 10, "Similar ad wording clusters even when text is rearranged.")
elseif shingleReuse >= 55 then
score = score + 6
end
if nearDuplicateRate >= 60 then
score = score + 5
addWeightedReason(
reasons,
5,
string.format("%d near-duplicate messages observed.", content.nearDuplicateCount or 0)
)
elseif nearDuplicateRate >= 30 then
score = score + 3
end
if intentRepeat >= 75 and (content.adIntentTotal or 0) >= 3 then
score = score + 4
end
return Util.Clamp(score, 0, 30)
end
local function scoreActivity(candidate, reasons)
local behavior = candidate.behavior or {}
local timing = candidate.timing or {}
local score = 0
local rate = behavior.postsPerHour or 0
for _, summary in pairs(timing.windowSummaries or {}) do
rate = math.max(rate, summary.postsPerHour or 0)
end
if rate >= 60 then
score = score + 10
addWeightedReason(reasons, 10, string.format("Peak active-window rate was %.1f posts/hour.", rate))
elseif rate >= 30 then
score = score + 7
addWeightedReason(reasons, 7, string.format("Posting rate was %.1f posts/hour.", rate))
elseif rate >= 15 then
score = score + 4
end
if (behavior.burstCount or 0) >= 2 then
score = score + 6
addWeightedReason(reasons, 6, string.format("%d burst windows detected.", behavior.burstCount or 0))
elseif (behavior.burstCount or 0) == 1 then
score = score + 3
end
if (behavior.longestActiveSpan or 0) >= 1800 then
score = score + 4
end
return Util.Clamp(score, 0, 20)
end
local function scorePersistence(candidate, reasons)
local score = 0
local dayCount = Util.CountMap(candidate.daysSeen)
local behavior = candidate.behavior or {}
if dayCount >= 3 then
score = score + 6
addWeightedReason(reasons, 6, string.format("Observed on %d separate days.", dayCount))
elseif dayCount >= 2 then
score = score + 4
end
if (behavior.activeSpan or 0) >= 3600 then
score = score + 4
addWeightedReason(reasons, 4, "Posting pattern spans more than an hour.")
elseif (behavior.activeSpan or 0) >= 1800 then
score = score + 2
end
return Util.Clamp(score, 0, 10)
end
local function scoreBaseline(candidate, reasons)
local baseline = candidate.baseline or {}
local sampleCount = baseline.sampleCount or 0
if sampleCount < 50 then
return 0
end
local score = 0
local ratePercentile = baseline.postsPerHourPercentile or 0
local regularityPercentile = baseline.regularityPercentile or 0
local reusePercentile = baseline.templateReusePercentile or 0
if regularityPercentile >= 95 then
score = score + 6
addWeightedReason(reasons, 6, "Timing regularity is above the 95th percentile for the local channel baseline.")
elseif regularityPercentile >= 90 then
score = score + 4
end
if ratePercentile >= 95 then
score = score + 5
addWeightedReason(reasons, 5, "Posting rate is unusually high for the local channel baseline.")
elseif ratePercentile >= 90 then
score = score + 3
end
if reusePercentile >= 95 then
score = score + 4
elseif reusePercentile >= 90 then
score = score + 2
end
local cap = sampleCount < 200 and 5 or 15
return Util.Clamp(score, 0, cap)
end
function Scoring.BucketInterval(seconds, binSize)
return bucketInterval(seconds, binSize)
end
function Scoring.CalculateEntropy(counts, total)
return calculateEntropyFromCounts(counts, total)
end
function Scoring.CalculateRobustStats(values)
return calculateRobustStats(values)
end
function Scoring.UpdateMetrics(candidate, settings)
settings = settings or {}
local timingSettings = settings.timing or {}
local intervalBin = timingSettings.intervalBin or 10
local intervals = candidate.timing and candidate.timing.intervals or {}
local intervalValues = {}
candidate.featureVersion = FEATURE_VERSION
candidate.timing = candidate.timing or {}
candidate.content = candidate.content or {}
candidate.behavior = candidate.behavior or {}
for _, record in ipairs(intervals) do
intervalValues[#intervalValues + 1] = record.s or 0
record.b = record.b or bucketInterval(record.s or 0, intervalBin)
end
local average = calculateAverage(intervalValues)
local variance = calculateVariance(intervalValues, average)
local robust = calculateRobustStats(intervalValues)
local coefficientVariation = average > 0 and (math.sqrt(variance) / average) or 1
local buckets, bucketCounts, intervalCount = buildBucketSummary(intervals, intervalBin)
local globalEntropy = calculateEntropyFromCounts(bucketCounts, intervalCount)
local rolling = {}
local rollingWindows = timingSettings.rollingWindows or { 5, 10, 20 }
local lowestRollingEntropy = 1
for _, windowSize in ipairs(rollingWindows) do
local entropy = calculateRollingEntropy(intervals, windowSize, intervalBin)
rolling["w" .. tostring(windowSize)] = entropy
if entropy < lowestRollingEntropy then
lowestRollingEntropy = entropy
end
end
local phases, cadenceSwitches, cadencePhaseDuration =
buildCadencePhases(intervals, timingSettings.minPhaseLength or 3)
local anchorTime = candidate.lastSeen or Util.GetNow()
local windowSummaries = {}
for _, seconds in ipairs(timingSettings.featureWindows or { 600, 1800, 7200 }) do
windowSummaries["w" .. tostring(seconds)] = buildWindowSummary(intervals, seconds, intervalBin, anchorTime)
end
local timing = candidate.timing
timing.averageInterval = average
timing.intervalVariance = variance
timing.coefficientVariation = coefficientVariation
timing.medianInterval = robust.median
timing.madInterval = robust.mad
timing.iqrInterval = robust.iqr
timing.averageMedianRatio = robust.median > 0 and (average / robust.median) or 0
timing.robustCoefficientVariation = robust.robustCoefficientVariation
timing.globalEntropy = globalEntropy
timing.rollingEntropy = rolling
timing.lowestRollingEntropy = lowestRollingEntropy
timing.dominantBuckets = buckets
timing.cadencePhases = phases
timing.cadenceSwitchCount = cadenceSwitches
timing.cadencePhaseDuration = cadencePhaseDuration
timing.windowSummaries = windowSummaries
timing.cadenceClass = classifyCadence(timing, candidate.behavior)
timing.intervalConsistency = timing.cadenceClass
local content = candidate.content
local templateTotal = content.templateTotal or candidate.totalMessages or 0
local topTemplateCount = 0
local uniqueTemplates = 0
for _, count in pairs(content.templateCounts or {}) do
uniqueTemplates = uniqueTemplates + 1
if count > topTemplateCount then
topTemplateCount = count
end
end
local topShingleCount = 0
local uniqueShingles = 0
for _, count in pairs(content.shingleCounts or {}) do
uniqueShingles = uniqueShingles + 1
if count > topShingleCount then
topShingleCount = count
end
end
local topIntentCount = 0
for _, count in pairs(content.adIntentCounts or {}) do
if count > topIntentCount then
topIntentCount = count
end
end
content.uniqueTemplateCount = uniqueTemplates
content.templateReusePercent = templateTotal > 0 and (topTemplateCount / templateTotal * 100) or 0
content.uniqueTemplateRatio = templateTotal > 0 and (uniqueTemplates / templateTotal) or 0
content.uniqueShingleCount = uniqueShingles
content.shingleReusePercent = (content.shingleTotal or 0) > 0 and (topShingleCount / content.shingleTotal * 100)
or 0
content.dominantIntentPercent = (content.adIntentTotal or 0) > 0 and (topIntentCount / content.adIntentTotal * 100)
or 0
local now = Util.GetNow()
local firstSeen = candidate.firstSeen or now
local lastSeen = candidate.lastSeen or now
local behavior = candidate.behavior
behavior.activeSpan = math.max(0, lastSeen - firstSeen)
behavior.postsPerHour = behavior.activeSpan > 0 and ((candidate.totalMessages or 0) / (behavior.activeSpan / 3600))
or (candidate.totalMessages or 0)
if BBT.Storage and BBT.Storage.GetBaselineComparison then
candidate.baseline = BBT.Storage.GetBaselineComparison(candidate)
end
end
function Scoring.HasMeaningfulLocalEvidence(candidate, settings)
settings = settings or {}
local promotion = settings.promotion or {}
local messageCount = candidate.totalMessages or 0
local content = candidate.content or {}
local timing = candidate.timing or {}
local behavior = candidate.behavior or {}
local baseline = candidate.baseline or {}
local intervalCount = timing.intervalCount or #(timing.intervals or {})
local topBucket = timing.dominantBuckets and timing.dominantBuckets[1]
local activeSpan = behavior.activeSpan or 0
if messageCount >= (promotion.messageCount or 3) then
if (content.templateReusePercent or 0) >= (promotion.templateReusePercent or 67) then
return true
end
if (content.nearDuplicateCount or 0) >= (promotion.nearDuplicateClusterCount or 3) then
return true
end
if (content.shingleReusePercent or 0) >= (promotion.shingleReusePercent or 67) then
return true
end
end
if
messageCount >= (promotion.timingSampleCount or 4) + 1
and intervalCount >= (promotion.timingSampleCount or 4)
and topBucket
and (topBucket.percent or 0) >= 75
and ((timing.lowestRollingEntropy or 1) <= 0.40 or (timing.robustCoefficientVariation or 1) <= 0.18)
then
return true
end
if
messageCount >= (promotion.highVolumeCount or 6)
and activeSpan <= (promotion.highVolumeWindowSeconds or 600)
then
if
(content.adIntentTotal or 0) >= 3
or (content.dominantIntentPercent or 0) >= 50
or (content.uniqueTemplateRatio or 1) <= 0.5
or ((baseline.sampleCount or 0) >= 50 and (baseline.postsPerHourPercentile or 0) >= 90)
then
return true
end
end
return false
end
function Scoring.Recalculate(candidate, settings)
if type(candidate) ~= "table" then
return nil
end
settings = settings or {}
Scoring.UpdateMetrics(candidate, settings)
local weightedReasons = {}
local familyScores = {
timing = scoreTiming(candidate, weightedReasons),
content = scoreContent(candidate, weightedReasons),
activity = scoreActivity(candidate, weightedReasons),
persistence = scorePersistence(candidate, weightedReasons),
baseline = scoreBaseline(candidate, weightedReasons),
}
local familyCount = countEvidenceFamilies(familyScores)
local rawLocalScore = familyScores.timing
+ familyScores.content
+ familyScores.activity
+ familyScores.persistence
+ familyScores.baseline
local recencyFactor = getRecencyFactor(candidate)
if recencyFactor < 1 then
rawLocalScore = rawLocalScore * recencyFactor
addWeightedReason(weightedReasons, 1, "Older evidence has been decayed in the score.")
end
local localScore = Util.Clamp(rawLocalScore, 0, 100)
local confidence = calculateConfidence(candidate, familyCount)
local network = candidate.network or {}
local networkContext = calculateNetworkContext(network)
local networkOnly = (candidate.totalMessages or 0) == 0 and (network.peerCount or 0) > 0
local displayScore = networkOnly and networkContext.score or localScore
local meaningful = Scoring.HasMeaningfulLocalEvidence(candidate, settings)
local status = networkOnly and "Peer Context Only"
or getStatus(localScore, confidence, familyCount, familyScores, candidate, meaningful)
local reasons = finalizeReasons(weightedReasons)
local statusCapReasons = networkOnly and { "Peer evidence only" } or getStatusCapReasons(candidate, familyCount)
candidate.features = {
version = FEATURE_VERSION,
updatedAt = Util.GetNow(),
familyScores = familyScores,
familyCount = familyCount,
rawLocalScore = rawLocalScore,
meaningfulLocalEvidence = meaningful,
statusCapReasons = statusCapReasons,
}
candidate.score = {
localScore = localScore,
displayScore = displayScore,
networkAdjustedScore = displayScore,
networkScore = networkContext.score,
networkConfidence = networkContext.confidence,
tier = status,
status = status,
statusCapReasons = statusCapReasons,
confidence = networkOnly and 0 or confidence,
evidenceFamilyCount = familyCount,
familyScores = familyScores,
reasons = reasons,
updatedAt = Util.GetNow(),
}
return candidate.score
end