-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbehavior_analyzer.py
More file actions
75 lines (60 loc) · 2.02 KB
/
Copy pathbehavior_analyzer.py
File metadata and controls
75 lines (60 loc) · 2.02 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
# behavior_analyzer.py
from dataclasses import dataclass
from typing import List, Dict
import numpy as np
@dataclass
class BehaviorFeatures:
num_clicks: int
avg_interval: float
std_interval: float
class BehaviorAnalyzer:
"""
Simple heuristic-based analyzer for mouse click behavior.
It uses:
- number of clicks
- average time between clicks
- variability (std) between clicks
"""
@staticmethod
def extract_features(click_timestamps: List[float]) -> BehaviorFeatures:
if len(click_timestamps) < 2:
return BehaviorFeatures(
num_clicks=len(click_timestamps),
avg_interval=0.0,
std_interval=0.0,
)
diffs = np.diff(sorted(click_timestamps))
avg_interval = float(np.mean(diffs))
std_interval = float(np.std(diffs))
return BehaviorFeatures(
num_clicks=len(click_timestamps),
avg_interval=avg_interval,
std_interval=std_interval,
)
@staticmethod
def compute_score(features: BehaviorFeatures) -> float:
"""
Returns a score between 0 and 1.
Higher => more likely to be human.
"""
# Not enough data
if features.num_clicks < 5:
return 0.2
score = 0.3 # base
# Reasonable average interval (not too perfect, not too slow)
if 0.15 <= features.avg_interval <= 1.5:
score += 0.4
# Some variability in timing (humans are not perfectly constant)
if 0.05 <= features.std_interval <= 0.6:
score += 0.3
# Clamp between 0 and 1
score = max(0.0, min(1.0, score))
return score
@staticmethod
def describe(features: BehaviorFeatures, score: float) -> Dict:
return {
"num_clicks": features.num_clicks,
"avg_interval_seconds": round(features.avg_interval, 3),
"std_interval_seconds": round(features.std_interval, 3),
"behavior_score": round(score, 3),
}