-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_analyzer.py
More file actions
114 lines (92 loc) · 3.64 KB
/
Copy pathlog_analyzer.py
File metadata and controls
114 lines (92 loc) · 3.64 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
from collections import Counter
import string
LOG_FILE = "logs/keystrokes.log"
WORD_FILE = "logs/word_analysis.log"
TIMING_FILE = "logs/timing_analysis.log"
"""
Generator that yields (header,lines) for each session
in order do respect the DRY principle and ensure cleaner code
"""
def get_sessions():
current_header = None
current_lines= []
with open(LOG_FILE, "r", encoding="utf-8") as log_file:
for line in log_file:
if line.startswith("--- Session started"):
if current_header:
yield current_header, current_lines
current_header = line.strip()
current_lines = []
else:
if line.strip():
current_lines.append(line)
if current_header:
yield current_header, current_lines
def analyze_words():
with open(WORD_FILE, "w", encoding="utf-8") as w:
for header, lines in get_sessions():
counter = Counter()
current_word = ""
for line in lines:
parts = line.split(maxsplit=1)
if len(parts) < 2:
continue
key = parts[1].strip()
# we don't need [space] , [enter] etc. for the analysis
if key.startswith("["):
cleaned = " "
else:
cleaned = key
if cleaned == " ":
if current_word:
counter.update([current_word])
current_word = ""
else:
cleaned=cleaned.strip(string.punctuation) #don't want to count "word." separately from "word"
if cleaned:
current_word += cleaned
if current_word:
counter.update([current_word])
# write results for this session
w.write(header + "\n")
for word, count in counter.most_common():
w.write(f"{word},{count}\n")
w.write("\n")
def analyze_timing():
with open(TIMING_FILE, "w", encoding="utf-8") as ti:
for header, lines in get_sessions():
timestamps=[]
for line in lines:
parts=line.split(maxsplit=1)
if len(parts) < 2:
continue
t = float(parts[0])
timestamps.append(t)
total_keys = len(timestamps)
ti.write(header + "\n")
ti.write(f"Total keys: {total_keys}\n")
if total_keys == 0:
ti.write("No keystrokes recorded\n\n")
continue
session_duration = timestamps[-1] - timestamps[0]
ti.write(f"Session duration: {session_duration:.3f}s\n")
if total_keys == 1:
ti.write("Not enough data for timing statistics\n\n")
continue
intervals = [
timestamps[i] - timestamps[i - 1]
for i in range(1, total_keys)
]
avg_interval = sum(intervals) / len(intervals)
fastest = min(intervals)
slowest = max(intervals)
keys_per_second = total_keys / session_duration if session_duration > 0 else 0
pause_count = sum(1 for i in intervals if i > 1.0)
ti.write(f"Avg interval: {avg_interval:.3f}s\n")
ti.write(f"Fastest key: {fastest:.3f}s\n")
ti.write(f"Slowest key: {slowest:.3f}s\n")
ti.write(f"Typing speed: {keys_per_second:.2f} keys/s\n")
ti.write(f"Pause count (>1s): {pause_count}\n\n")
def analyze():
analyze_words()
analyze_timing()