File size: 48,501 Bytes
0bb52ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

hv-reader

=========



The reading experience, in one call.



Given a text, produce a ReadingProfile: pace, memory, passes, slip, and

wall — the five axes that describe what it is like to read a text.



This unifies seven component models:



    hv-tempo   → pace

    hv-forget  → memory

    hv-ttu     → ttu_s (total time)

    hv-fold    → passes

    hv-slip    → slip

    hv-hunger  → hunger (internal, feeds slip and wall)

    hv-wall    → wall



The profile is the artifact. The axes are the readings. The signature is

what predicts whether a text gets finished.



Pure stdlib. No dependencies.



Author: zeechimp

License: Apache-2.0

"""

from __future__ import annotations

import argparse
import json
import math
import os
import re
import sys
from collections import Counter, defaultdict
from dataclasses import dataclass, asdict, field
from typing import Any, Dict, List, Optional, Set, Tuple


# ============================================================================
# Lexicons
# ============================================================================
COMMON_WORDS = frozenset("""

the be to of and a in that have i it for not on with he as you do at this

but his by from they we say her she or an will my one all would there their

what so up out if about who get which go me when make can like time no just

him know take people into year your good some could them see other than then

now look only come its over think also back after use two how our work first

well way even new want because any these give day most us is are was were

been being has had having does did doing will would shall should can could

may might must man woman child water fire earth air sun moon star light dark

hand head eye ear mouth nose foot leg arm body face heart mind life death

food bread milk meat fish tree flower grass leaf root seed farm field hill

mountain river sea lake boat ship road street city town house room door

window wall floor roof bed chair table book page word line letter number

name place thing part side end start middle top bottom front back left

right high low long short big small old new young hot cold wet dry clean

dirty light heavy soft hard fast slow easy true false good bad happy sad

love hate fear hope help hurt win lose give take send bring buy sell pay

cost money price work play run walk jump sit stand sleep wake eat drink

cook wash read write speak hear see feel know think learn teach ask

answer tell show hide open close push pull carry hold drop throw catch

break fix build make do try use move turn stop start keep leave stay wait

meet join save spend show thank want wish walk stop continue morning

climb row push pull begin finish start end remain rest return arrive

depart leave enter exit follow lead sit stand lie rise fall drop

still quiet calm slow fast soft loud bright dark warm cool fresh clean

never always often sometimes rarely usually speak spoke spoken

take took taken give gave given see saw seen know knew known

think thought thought come came come go went gone say said said

tell told told find found found hold held held bring brought brought

buy bought bought teach taught taught catch caught caught build built built

send sent sent spend spent lose lost lost lead led led meet met met

read read read write wrote written run ran run swim swam swum

thing things word words time times year years day days

man men woman women child children person people

place places work works way ways life lives hand hands

eye eyes part parts end ends line lines side sides

name names head heads house houses friend friends

family families group groups country countries

world worlds city cities school schools

""".split())


ABSTRACT_SUFFIXES = (
    "tion", "sion", "ism", "ity", "ness", "ance", "ence",
    "ship", "hood", "ment", "ology", "itude", "acy",
)

SUBORDINATORS = frozenset("""

which that because although though while whereas since when if unless

provided assuming given whenever wherever whoever whichever

""".split())

HEDGES = frozenset("""

may might maybe perhaps possibly probably typically usually often

generally roughly approximately about somewhat rather

""".split())

CONDITIONALS = frozenset("""

if when unless provided assuming suppose supposing

""".split())

NEGATIONS = frozenset("""

not no never none without cannot can't don't doesn't won't

isn't aren't wasn't weren't nor neither

""".split())

BE_FORMS = frozenset("""

is are was were be been being am

""".split())

REENTRY_MARKERS = frozenset("""

above below previous preceding following

aforementioned noted mentioned discussed described

stated referred earlier later

""".split())

ARTICLES = frozenset("""

the this that these those such said aforementioned

""".split())

UNIQUE_REFERENTS = frozenset("""

sun moon earth world sky ground horizon

morning afternoon evening night noon midnight dawn dusk

""".split())

CERTAINTY_MARKERS = frozenset("""

definitely definitively certainly obviously clearly undoubtedly

unquestionably absolutely surely plainly evidently undeniably

unmistakably decidedly categorically conclusively decisively

resolutely proves proven prove proved proof impossible must always

never guaranteed

""".split())

CONCLUSION_MARKERS = (
    "therefore", "thus", "hence", "consequently", "accordingly",
    "it follows that", "we conclude", "we can conclude",
    "this shows", "this demonstrates", "this proves",
    "in conclusion", "as a result",
)

CONTRAST_MARKERS = (
    "however", "but", "yet", "although", "though", "nevertheless",
    "nonetheless", "conversely", "on the contrary", "in contrast",
    "on the other hand", "by contrast", "notwithstanding",
    "despite this", "even so",
)

EVIDENCE_MARKERS = (
    "according to", "studies show", "studies suggest",
    "research shows", "research suggests",
    "data show", "data suggest", "evidence indicates",
    "we measured", "we observed", "we found",
    "for example", "for instance", "specifically", "namely",
    "for one", "in fact", "as measured",
)

QUESTION_RAISERS = (
    "why", "how", "whether", "what caused", "the reason",
    "unclear", "unknown", "remains to be determined",
    "remains unclear", "puzzling", "mysterious",
    "unexplained", "open question", "puzzle",
)

ANSWER_MARKERS = (
    "because", "since", "as a result", "due to", "explained by",
    "the reason is", "this explains", "the cause", "attributable to",
    "results from", "arises from", "the mechanism is",
)

DIRECTION_GROUPS = {
    "up": frozenset("""

        increase increases increased increasing rise rises rose risen

        grow grows grew grown growth expand expands expanded expansion

        raise raises raised raising improve improves improved improving

        gain gains gained gaining positive higher highest more most

        upward up climb climbs climbed climbing

    """.split()),
    "down": frozenset("""

        decrease decreases decreased decreasing fall falls fell fallen

        shrink shrinks shrank shrunk contract contracts contracted

        lower lowers lowered lowering reduce reduces reduced reducing

        worsen worsens worsened worsening lose loses lost losing

        negative lower lowest less least downward down decline declines

        declined declining drop drops dropped dropping diminish

    """.split()),
    "cause": frozenset("""

        cause causes caused causing produce produces produced producing

        create creates created creating induce induces induced

        trigger triggers triggered triggering generate

    """.split()),
    "prevent": frozenset("""

        prevent prevents prevented preventing avoid avoids avoided

        block blocks blocked blocking inhibit inhibits inhibited

        prohibit prohibits prohibited prohibiting stop stops stopped

    """.split()),
    "support": frozenset("""

        support supports supported supporting confirm confirms confirmed

        agree agrees agreed approve approves approved accept accepts accepted

        affirm affirms affirmed

    """.split()),
    "oppose": frozenset("""

        oppose opposes opposed opposing deny denies denied contradict

        contradicts contradicted disagree disagrees disagreed

        reject rejects rejected refuse refuses refused

    """.split()),
}

GROUP_OPPOSITES = frozenset([
    ("up", "down"), ("down", "up"),
    ("cause", "prevent"), ("prevent", "cause"),
    ("support", "oppose"), ("oppose", "support"),
])

DISCOURSE_MARKERS = frozenset("""

therefore thus hence consequently accordingly however but yet although

though nevertheless nonetheless conversely meanwhile similarly

moreover furthermore additionally

""".split())


# ============================================================================
# Regexes
# ============================================================================
_WORD_RE = re.compile(r"[A-Za-z][A-Za-z'\-]*")
_SENT_SPLIT_RE = re.compile(r"(?<=[.!?])\s+(?=[A-Z\"'(])")
_NUMBER_RE = re.compile(r"\b\d+(?:[.,]\d+)*\b")
_STANDALONE_DEMON_RE = re.compile(
    r'^\s*(this|that|these|those)\s*[.,!?;:]*\s*$', re.IGNORECASE
)


# ============================================================================
# Tokenization helpers
# ============================================================================
def _stem(w: str) -> str:
    w = w.lower()
    if len(w) <= 4:
        return w
    for suffix in ("ingly", "edly", "ing", "ed", "ly", "es", "s"):
        if w.endswith(suffix) and len(w) - len(suffix) >= 3:
            base = w[: -len(suffix)]
            if len(base) >= 2 and base[-1] == base[-2] and base[-1] not in "aeiou":
                base = base[:-1]
            return base
    return w


def _words(text: str) -> List[str]:
    return _WORD_RE.findall(text)


def _content_words(text: str) -> List[str]:
    return [
        w.lower() for w in _words(text)
        if w.lower() not in COMMON_WORDS
        and w.lower() not in DISCOURSE_MARKERS
        and len(w) >= 3
    ]


def _sentences(text: str) -> List[str]:
    return [s.strip() for s in _SENT_SPLIT_RE.split(text) if s.strip()]


def _is_common(w: str) -> bool:
    lw = w.lower()
    if lw in COMMON_WORDS:
        return True
    return _stem(lw) in COMMON_WORDS


def _is_rare(w: str) -> bool:
    return len(w) >= 7 and not _is_common(w)


def _has_conclusion_marker(text: str) -> bool:
    low = text.lower().strip()
    for m in CONCLUSION_MARKERS:
        if low.startswith(m):
            return True
        if f" {m} " in f" {low} ":
            return True
    return False


def _has_contrast_marker(text: str) -> Tuple[bool, str]:
    low = text.lower().strip()
    for m in CONTRAST_MARKERS:
        if low.startswith(m):
            return True, m
        if f" {m} " in f" {low} ":
            return True, m
    return False, ""


def _has_evidence_marker(text: str) -> bool:
    low = text.lower()
    return any(m in low for m in EVIDENCE_MARKERS)


# ============================================================================
# Config
# ============================================================================
@dataclass
class HVReaderConfig:
    # Pace (hv-tempo weights)
    baseline_wpm: float = 220.0
    w_sentence_len_excess: float = 0.40
    w_clause_rate: float = 0.08
    w_rare_rate: float = 0.70
    w_abstract_rate: float = 0.40
    w_digit_rate: float = 0.30
    w_negation_rate: float = 0.40
    w_hedge_rate: float = 0.40
    w_conditional_rate: float = 0.60
    w_passive_rate: float = 0.30
    w_list_bonus: float = -0.50
    max_log_slowdown: float = 1.5

    # Memory (hv-forget)
    memory_target_days: float = 7.0
    memory_stability_base: float = 1.0
    memory_stability_density: float = 5.0
    memory_stability_rare: float = 3.0
    memory_stability_salience: float = 2.0

    # Fold (hv-fold)
    fold_demon_weight: float = 2.0
    fold_definite_only: float = 0.5
    fold_reentry: float = 1.0
    fold_forward: float = 0.5
    fold_resolved: float = 0.1
    exempt_first_sentence: bool = False

    # Slip (hv-slip)
    monotone_window: int = 3
    slip_monotone_weight: float = 0.4
    slip_repetition_weight: float = 0.3
    slip_absence_weight: float = 0.3

    # Hunger (hv-hunger)
    hunger_question_weight: float = 1.0
    hunger_answer_weight: float = 1.0

    # Wall (hv-wall)
    wall_threshold: float = 0.35
    certainty_scale: float = 6.0
    hedge_scale: float = 5.0
    contradiction_scale: float = 0.4
    specificity_scale: float = 5.0
    gap_confidence_jump_weight: float = 0.5
    contrast_bonus: float = 0.15
    contradiction_min: float = 0.30
    overclaim_min: float = 0.30
    gap_min: float = 0.25

    # Reporting
    min_sentence_words: int = 3
    top_reasons: int = 3

    version: str = "0.1.1"


# ============================================================================
# Report dataclasses
# ============================================================================
@dataclass
class SpanProfile:
    id: int
    text: str
    span: Tuple[int, int]
    n_words: int
    wpm: float
    slowdown: float
    density: float
    stability_days: float
    retention_week: float
    fold_load: float
    slip: float
    hunger_delta: float
    hunger: float
    wall: float
    wall_type: str
    reasons: List[str] = field(default_factory=list)

    def to_dict(self) -> dict:
        return {
            "id": self.id,
            "text": self.text,
            "span": list(self.span),
            "n_words": self.n_words,
            "wpm": round(self.wpm, 2),
            "slowdown": round(self.slowdown, 3),
            "density": round(self.density, 4),
            "stability_days": round(self.stability_days, 3),
            "retention_week": round(self.retention_week, 4),
            "fold_load": round(self.fold_load, 3),
            "slip": round(self.slip, 3),
            "hunger_delta": round(self.hunger_delta, 3),
            "hunger": round(self.hunger, 3),
            "wall": round(self.wall, 4),
            "wall_type": self.wall_type,
            "reasons": list(self.reasons),
        }


@dataclass
class ReadingProfile:
    text: str
    n_sentences: int
    n_words: int

    pace: float
    memory: float
    passes: float
    slip: float
    wall: float

    ttu_s: float
    mean_wpm: float
    hunger_final: float

    spans: List[SpanProfile]
    slowest_span: Optional[SpanProfile]
    wall_span: Optional[SpanProfile]
    summary: str

    def to_dict(self) -> dict:
        return {
            "text": self.text,
            "n_sentences": self.n_sentences,
            "n_words": self.n_words,
            "profile": {
                "pace": round(self.pace, 4),
                "memory": round(self.memory, 4),
                "passes": round(self.passes, 4),
                "slip": round(self.slip, 4),
                "wall": round(self.wall, 4),
            },
            "ttu_s": round(self.ttu_s, 2),
            "mean_wpm": round(self.mean_wpm, 2),
            "hunger_final": round(self.hunger_final, 3),
            "slowest_span_id": self.slowest_span.id if self.slowest_span else None,
            "wall_span_id": self.wall_span.id if self.wall_span else None,
            "summary": self.summary,
            "spans": [s.to_dict() for s in self.spans],
        }


# ============================================================================
# Pace (hv-tempo)
# ============================================================================
def _count_list_markers(text: str) -> int:
    bullets = len(re.findall(r"(?:^|\n)\s*[-*•]\s+\S", text))
    numbered = re.findall(r"(?:^|[\s;:.])\d+[.)]\s+\S", text)
    n_numbered = len(numbered) if len(numbered) >= 2 else 0
    return max(bullets, n_numbered)


def _pace_features(sentence: str) -> Dict[str, float]:
    words = _words(sentence)
    n_words = len(words)
    if n_words == 0:
        return {k: 0.0 for k in [
            "n_words", "mean_sentence_len", "sentence_len_signed",
            "clauses_per_sentence", "rare_rate", "abstract_rate",
            "digit_rate", "negation_rate", "hedge_rate",
            "conditional_rate", "passive_rate", "list_rate",
        ]}

    n_sentences = max(1, len([s for s in _SENT_SPLIT_RE.split(sentence) if s.strip()]))
    mean_len = n_words / n_sentences
    signed_len = (mean_len - 15.0) / 10.0

    n_clauses = (
        sentence.count(",") + sentence.count(";") + sentence.count(":")
        + sum(1 for w in words if w.lower() in SUBORDINATORS)
    )
    clauses_per_sentence = n_clauses / n_sentences

    n_rare = sum(1 for w in words if len(w) >= 7 and not _is_common(w))
    rare_rate = n_rare / n_words

    n_abstract = sum(
        1 for w in words
        if len(w) > 5 and w.lower().endswith(ABSTRACT_SUFFIXES)
    )
    abstract_rate = n_abstract / n_words

    digit_rate = len(_NUMBER_RE.findall(sentence)) / n_words
    negation_rate = sum(1 for w in words if w.lower() in NEGATIONS) / n_words
    hedge_rate = sum(1 for w in words if w.lower() in HEDGES) / n_words
    conditional_rate = sum(1 for w in words if w.lower() in CONDITIONALS) / n_words

    n_passive = 0
    for i, w in enumerate(words):
        if w.lower() in BE_FORMS and i + 1 < len(words):
            nxt = words[i + 1].lower()
            if (nxt.endswith("ed") and len(nxt) > 3) or nxt in (
                "gone", "seen", "written", "taken", "made", "known",
                "found", "given", "held", "sent", "left", "kept",
            ):
                n_passive += 1
    passive_rate = n_passive / n_sentences

    n_list = _count_list_markers(sentence)
    list_rate = n_list / n_sentences

    return {
        "n_words": float(n_words),
        "mean_sentence_len": mean_len,
        "sentence_len_signed": signed_len,
        "clauses_per_sentence": clauses_per_sentence,
        "rare_rate": rare_rate,
        "abstract_rate": abstract_rate,
        "digit_rate": digit_rate,
        "negation_rate": negation_rate,
        "hedge_rate": hedge_rate,
        "conditional_rate": conditional_rate,
        "passive_rate": passive_rate,
        "list_rate": list_rate,
    }


def _pace_slowdown(

    f: Dict[str, float], cfg: HVReaderConfig

) -> Tuple[float, Dict[str, float]]:
    contrib = {
        "sentence_length": cfg.w_sentence_len_excess * f["sentence_len_signed"],
        "clause_density": cfg.w_clause_rate * f["clauses_per_sentence"],
        "rare_words": cfg.w_rare_rate * f["rare_rate"],
        "abstract_terms": cfg.w_abstract_rate * f["abstract_rate"],
        "numerals": cfg.w_digit_rate * f["digit_rate"],
        "negation": cfg.w_negation_rate * f["negation_rate"],
        "hedging": cfg.w_hedge_rate * f["hedge_rate"],
        "conditionals": cfg.w_conditional_rate * f["conditional_rate"],
        "passive_voice": cfg.w_passive_rate * f["passive_rate"],
        "list_structure": cfg.w_list_bonus * f["list_rate"],
    }
    log_slowdown = sum(contrib.values())
    log_slowdown = max(
        -cfg.max_log_slowdown, min(cfg.max_log_slowdown, log_slowdown)
    )
    return math.exp(log_slowdown), contrib


# ============================================================================
# Memory (hv-forget)
# ============================================================================
def _memory_stability(

    sentence: str, density: float, cfg: HVReaderConfig

) -> Tuple[float, float]:
    words = _words(sentence)
    n_content = max(1, len(_content_words(sentence)))

    n_rare = sum(1 for w in words if _is_rare(w))
    rare_ratio = n_rare / n_content

    n_digits = len(_NUMBER_RE.findall(sentence))
    n_proper = sum(
        1 for i, w in enumerate(words)
        if i > 0 and w[0].isupper()
        and w.lower() not in COMMON_WORDS
        and w.lower() not in DISCOURSE_MARKERS
        and len(w) >= 3
    )
    salience = min(1.0, (n_digits + n_proper) / n_content)

    stability = (
        cfg.memory_stability_base
        + cfg.memory_stability_density * density
        + cfg.memory_stability_rare * rare_ratio
        + cfg.memory_stability_salience * salience
    )
    return stability, salience


def _memory_retention(stability: float, days: float) -> float:
    if stability <= 0:
        return 0.0
    return math.exp(-days / stability)


# ============================================================================
# Density
# ============================================================================
def _density_per_sentence(sentences: List[str]) -> List[float]:
    seen: Set[str] = set()
    out: List[float] = []
    for i, s in enumerate(sentences):
        content = [_stem(w) for w in _content_words(s)]
        if not content:
            out.append(0.0)
            continue
        if i == 0:
            out.append(1.0)
        else:
            novel = [w for w in content if w not in seen]
            out.append(len(novel) / len(content))
        seen.update(content)
    return out


# ============================================================================
# Fold (hv-fold)
# ============================================================================
def _definite_nps(sentence: str) -> List[Tuple[str, str]]:
    out: List[Tuple[str, str]] = []
    words = list(_WORD_RE.finditer(sentence))
    for idx, w in enumerate(words):
        if w.group(0).lower() not in ARTICLES:
            continue
        tail = words[idx + 1: idx + 3]
        content = [
            tw.group(0).lower() for tw in tail
            if tw.group(0).lower() not in COMMON_WORDS
            and len(tw.group(0)) >= 3
        ]
        if not content:
            continue
        if any(c in UNIQUE_REFERENTS for c in content):
            continue
        head = content[-1]
        end = tail[-1].end() if tail else w.end()
        out.append((sentence[w.start():end], head))
    return out


def _fold_loads(sentences: List[str], cfg: HVReaderConfig) -> List[float]:
    n = len(sentences)
    word_sentences: Dict[str, Set[int]] = defaultdict(set)
    for i, s in enumerate(sentences):
        for w in _words(s):
            lw = w.lower()
            if lw not in COMMON_WORDS and len(lw) >= 3:
                word_sentences[_stem(lw)].add(i)

    loads: List[float] = []
    for i, s in enumerate(sentences):
        load = 0.0
        if _STANDALONE_DEMON_RE.match(s.strip()):
            load += cfg.fold_demon_weight
        for _, head in _definite_nps(s):
            stem = _stem(head)
            occ = word_sentences.get(stem, set())
            prior = [j for j in occ if j < i]
            later = [j for j in occ if j > i]
            if prior:
                load += cfg.fold_resolved
            elif later:
                delay = min(later) - i
                load += cfg.fold_forward * delay
            else:
                if i == 0 and cfg.exempt_first_sentence:
                    pass
                else:
                    load += cfg.fold_definite_only
        if any(w.lower() in REENTRY_MARKERS for w in _words(s)):
            load += cfg.fold_reentry
        loads.append(load)
    return loads


# ============================================================================
# Slip (hv-slip)
# ============================================================================
def _slip_per_sentence(

    sentences: List[str], cfg: HVReaderConfig

) -> List[float]:
    n = len(sentences)
    if n == 0:
        return []

    lengths = [len(_words(s)) for s in sentences]

    slips: List[float] = []
    prev_content: Set[str] = set()

    for i, s in enumerate(sentences):
        lo = max(0, i - cfg.monotone_window)
        hi = min(n, i + cfg.monotone_window + 1)
        window = lengths[lo:hi]
        if len(window) > 1:
            mean = sum(window) / len(window)
            var = sum((x - mean) ** 2 for x in window) / len(window)
            std = math.sqrt(var)
            monotone = max(0.0, 1.0 - std / 8.0)
        else:
            monotone = 0.0

        content = {_stem(w) for w in _content_words(s)}
        if prev_content and content:
            overlap = len(content & prev_content) / max(1, len(content))
        else:
            overlap = 0.0
        prev_content = content

        n_digits = len(_NUMBER_RE.findall(s))
        words_s = _words(s)
        n_proper = sum(
            1 for j, w in enumerate(words_s)
            if j > 0 and w[0].isupper()
            and w.lower() not in COMMON_WORDS
            and len(w) >= 3
        )
        absence = 1.0 if (n_digits + n_proper) == 0 else 0.0

        slip = (
            cfg.slip_monotone_weight * monotone
            + cfg.slip_repetition_weight * overlap
            + cfg.slip_absence_weight * absence
        )
        slips.append(max(0.0, min(1.0, slip)))

    return slips


# ============================================================================
# Hunger (hv-hunger)
# ============================================================================
def _hunger_deltas(sentences: List[str], cfg: HVReaderConfig) -> List[float]:
    deltas: List[float] = []
    for s in sentences:
        low = s.lower()
        raised = sum(1 for m in QUESTION_RAISERS if m in low)
        answered = sum(1 for m in ANSWER_MARKERS if m in low)
        delta = (
            cfg.hunger_question_weight * raised
            - cfg.hunger_answer_weight * answered
        )
        deltas.append(delta)
    return deltas


# ============================================================================
# Wall (hv-wall)
# ============================================================================
def _direction_of(word: str) -> List[str]:
    w = word.lower()
    sw = _stem(w)
    out = []
    for g, words in DIRECTION_GROUPS.items():
        if w in words or sw in words:
            out.append(g)
    return out


def _closest_topic(words: List[str], idx: int) -> Optional[int]:
    best = None
    best_key: Tuple[int, int] = (10, 1)
    for j in range(max(0, idx - 3), min(len(words), idx + 4)):
        if j == idx:
            continue
        cand = words[j]
        if cand in COMMON_WORDS or len(cand) < 4:
            continue
        if _direction_of(cand):
            continue
        if cand in DISCOURSE_MARKERS:
            continue
        after = 0 if j > idx else 1
        key = (abs(j - idx), after)
        if key < best_key:
            best_key = key
            best = j
    return best


def _direction_pairs(sentence: str) -> List[Tuple[str, str, str]]:
    words = [w.lower() for w in _words(sentence)]
    out: List[Tuple[str, str, str]] = []
    for i, w in enumerate(words):
        groups = _direction_of(w)
        if not groups:
            continue
        j = _closest_topic(words, i)
        if j is None:
            continue
        topic = _stem(words[j])
        for g in groups:
            out.append((topic, g, w))
    return out


def _wall_confidence(

    sentence: str, cfg: HVReaderConfig

) -> Tuple[float, int, int]:
    words = [w.lower() for w in _words(sentence)]
    n = max(1, len(words))
    cert = sum(1 for w in words if w in CERTAINTY_MARKERS)
    hedg = sum(1 for w in words if w in HEDGES)
    conf = min(1.0, cfg.certainty_scale * cert / n)
    hedge = min(1.0, cfg.hedge_scale * hedg / n)
    return max(0.0, conf - 0.5 * hedge), cert, hedg


def _wall_evidence(

    sentence: str, cfg: HVReaderConfig

) -> Tuple[float, float, bool]:
    words = _words(sentence)
    content = _content_words(sentence)
    n_content = max(1, len(content))
    n_digits = len(_NUMBER_RE.findall(sentence))
    n_proper = sum(
        1 for i, w in enumerate(words)
        if i > 0 and w[0].isupper()
        and w.lower() not in COMMON_WORDS
        and w.lower() not in DISCOURSE_MARKERS
        and len(w) >= 3
    )
    n_rare = sum(1 for w in content if len(w) >= 8)
    spec = (
        0.5 * (n_digits / n_content)
        + 0.3 * (n_proper / n_content)
        + 0.2 * (n_rare / n_content)
    )
    spec = min(1.0, cfg.specificity_scale * spec)
    attribution = _has_evidence_marker(sentence)
    evidence = 0.6 * spec + 0.4 * (1.0 if attribution else 0.0)
    return evidence, spec, attribution


def _wall_per_sentence(

    sentences: List[str], cfg: HVReaderConfig

) -> List[Tuple[float, str, List[str]]]:
    n = len(sentences)
    out: List[Tuple[float, str, List[str]]] = []

    prior_sentences: List[str] = []
    prior_conf: List[float] = []
    prior_evid: List[float] = []

    for i, s in enumerate(sentences):
        confidence, cert_count, _ = _wall_confidence(s, cfg)
        evidence, _spec, attribution = _wall_evidence(s, cfg)
        n_words = len(_words(s))

        overclaim = (
            max(0.0, confidence - evidence)
            if n_words >= cfg.min_sentence_words
            else 0.0
        )

        # Contradiction via direction-group conflicts.
        contradiction = 0.0
        contra_idx: Optional[int] = None
        pairs: List[Tuple[str, str]] = []
        if prior_sentences:
            curr_pairs = _direction_pairs(s)
            prior_dirs: Dict[str, List[Tuple[int, str, str]]] = {}
            for j, p in enumerate(prior_sentences):
                for topic, g, src in _direction_pairs(p):
                    prior_dirs.setdefault(topic, []).append((j, g, src))
            for topic, curr_g, curr_src in curr_pairs:
                for prior_idx, prior_g, prior_src in prior_dirs.get(topic, []):
                    if (prior_g, curr_g) in GROUP_OPPOSITES:
                        pairs.append((
                            f"{topic}:{prior_src}↔{curr_src}",
                            f"({prior_g} vs {curr_g})",
                        ))
                        contra_idx = prior_idx
            if pairs:
                contradiction = min(
                    1.0, cfg.contradiction_scale * math.sqrt(len(pairs))
                )

        # Gap.
        gap = 0.0
        has_conclusion = _has_conclusion_marker(s)
        if has_conclusion and prior_evid:
            prior_ev_mean = sum(prior_evid) / len(prior_evid)
            ev_deficit = max(0.0, confidence - prior_ev_mean)
            prior_conf_mean = (
                sum(prior_conf) / len(prior_conf) if prior_conf else 0.0
            )
            conf_jump = max(0.0, confidence - prior_conf_mean)
            gap = ev_deficit + cfg.gap_confidence_jump_weight * conf_jump

        # Priority ordering.
        if contradiction >= cfg.contradiction_min:
            wall, wtype = contradiction, "contradiction"
        elif overclaim >= cfg.overclaim_min:
            wall, wtype = overclaim, "overclaim"
        elif gap >= cfg.gap_min:
            wall, wtype = gap, "gap"
        else:
            best = max(overclaim, contradiction, gap)
            if best <= 0.0:
                wall, wtype = 0.0, "neutral"
            else:
                wall = best
                if best == contradiction:
                    wtype = "contradiction"
                elif best == overclaim:
                    wtype = "overclaim"
                else:
                    wtype = "gap"

        wall = min(1.0, wall)

        has_contrast, _contrast_word = _has_contrast_marker(s)
        if has_contrast and wall > 0.05:
            wall = min(1.0, wall + cfg.contrast_bonus)

        reasons: List[str] = []
        if wtype == "contradiction" and pairs:
            reasons.append(f"conflict with sentence {contra_idx}: {pairs[0][0]}")
        elif wtype == "overclaim":
            if cert_count:
                matched = [
                    w.lower() for w in _words(s)
                    if w.lower() in CERTAINTY_MARKERS
                ]
                reasons.append(
                    f"certainty markers: "
                    f"{', '.join(repr(m) for m in matched[:3])}"
                )
            if not attribution:
                reasons.append("no attribution marker")
        elif wtype == "gap":
            if has_conclusion:
                reasons.append("conclusion marker with weak prior evidence")

        out.append((wall, wtype if wall > 0.0 else "neutral", reasons))

        prior_sentences.append(s)
        prior_conf.append(confidence)
        prior_evid.append(evidence)

    return out


# ============================================================================
# The model
# ============================================================================
class HVReader:
    """Unified reading-experience model."""

    def __init__(self, config: Optional[HVReaderConfig] = None):
        self.config = config or HVReaderConfig()
        self._obs = 0

    def __repr__(self) -> str:
        return (
            f"HVReader(baseline_wpm={self.config.baseline_wpm}, "
            f"wall_threshold={self.config.wall_threshold}, "
            f"version={self.config.version})"
        )

    def analyze(self, text: str) -> ReadingProfile:
        if not text or not text.strip():
            return self._empty(text)

        sentences = _sentences(text)
        n = len(sentences)
        if n == 0:
            return self._empty(text)

        spans: List[Tuple[int, int]] = []
        cursor = 0
        for s in sentences:
            i = text.find(s, cursor)
            if i < 0:
                i = cursor
            spans.append((i, i + len(s)))
            cursor = i + len(s)

        pace_feats = [_pace_features(s) for s in sentences]
        slowdowns: List[float] = []
        wpms: List[float] = []
        for f in pace_feats:
            sd, _ = _pace_slowdown(f, self.config)
            slowdowns.append(sd)
            wpms.append(
                self.config.baseline_wpm / sd
                if sd > 0 else self.config.baseline_wpm
            )

        densities = _density_per_sentence(sentences)
        fold_loads = _fold_loads(sentences, self.config)
        slips = _slip_per_sentence(sentences, self.config)
        hunger_deltas = _hunger_deltas(sentences, self.config)

        hunger_cumulative: List[float] = []
        h = 0.0
        for d in hunger_deltas:
            h += d
            hunger_cumulative.append(h)

        stabilities: List[float] = []
        retentions: List[float] = []
        for s, dens in zip(sentences, densities):
            stab, _ = _memory_stability(s, dens, self.config)
            stabilities.append(stab)
            retentions.append(
                _memory_retention(stab, self.config.memory_target_days)
            )

        wall_per = _wall_per_sentence(sentences, self.config)

        span_profiles: List[SpanProfile] = []
        for i in range(n):
            wall_score, wall_type, reasons = wall_per[i]
            n_words_span = int(pace_feats[i]["n_words"])
            span_profiles.append(SpanProfile(
                id=i,
                text=sentences[i],
                span=spans[i],
                n_words=n_words_span,
                wpm=wpms[i],
                slowdown=slowdowns[i],
                density=densities[i],
                stability_days=stabilities[i],
                retention_week=retentions[i],
                fold_load=fold_loads[i],
                slip=slips[i],
                hunger_delta=hunger_deltas[i],
                hunger=hunger_cumulative[i],
                wall=wall_score,
                wall_type=wall_type,
                reasons=reasons[: self.config.top_reasons],
            ))

        n_words_total = sum(s.n_words for s in span_profiles)

        mean_wpm = (
            sum(s.wpm * s.n_words for s in span_profiles)
            / max(1, n_words_total)
        )
        pace = max(0.0, min(1.0, mean_wpm / 400.0))

        memory = sum(retentions) / n if n else 0.0

        total_load = sum(fold_loads)
        passes = 1.0 + total_load / max(1, n)

        slip = sum(slips) / n if n else 0.0

        wall_scores = [s.wall for s in span_profiles]
        wall_max = max(wall_scores) if wall_scores else 0.0
        wall = wall_max

        wps = mean_wpm / 60.0
        ttu_s = n_words_total / wps if wps > 0 else 0.0

        hunger_final = hunger_cumulative[-1] if hunger_cumulative else 0.0

        slowest = (
            min(span_profiles, key=lambda s: s.wpm)
            if span_profiles else None
        )

        wall_span: Optional[SpanProfile] = None
        if wall > 0.15:
            wall_span = max(span_profiles, key=lambda s: s.wall)

        summary = self._summary(
            span_profiles, pace, memory, passes, slip, wall
        )

        self._obs += 1
        return ReadingProfile(
            text=text,
            n_sentences=n,
            n_words=n_words_total,
            pace=pace,
            memory=memory,
            passes=passes,
            slip=slip,
            wall=wall,
            ttu_s=ttu_s,
            mean_wpm=mean_wpm,
            hunger_final=hunger_final,
            spans=span_profiles,
            slowest_span=slowest,
            wall_span=wall_span,
            summary=summary,
        )

    def _empty(self, text: str) -> ReadingProfile:
        return ReadingProfile(
            text=text,
            n_sentences=0,
            n_words=0,
            pace=0.0,
            memory=0.0,
            passes=1.0,
            slip=0.0,
            wall=0.0,
            ttu_s=0.0,
            mean_wpm=0.0,
            hunger_final=0.0,
            spans=[],
            slowest_span=None,
            wall_span=None,
            summary="Empty text.",
        )

    @staticmethod
    def _summary(

        spans: List[SpanProfile],

        pace: float,

        memory: float,

        passes: float,

        slip: float,

        wall: float,

    ) -> str:
        if not spans:
            return "Empty text."
        parts = []
        parts.append(f"reads at {pace * 400:.0f} WPM (pace {pace:.2f})")
        parts.append(f"memory after a week: {memory * 100:.0f}%")
        parts.append(f"requires {passes:.2f} passes")
        parts.append(f"slip probability: {slip:.2f}")
        if wall > 0.15:
            wall_span = max(spans, key=lambda s: s.wall)
            parts.append(
                f"wall at sentence {wall_span.id} "
                f"({wall_span.wall_type}, {wall:.2f})"
            )
        else:
            parts.append("no wall")
        return ". ".join(parts).capitalize() + "."

    def render(self, profile: ReadingProfile) -> str:
        lines: List[str] = []
        bar = "=" * 72
        lines.append(bar)
        lines.append("hv-reader — the reading experience")
        lines.append(bar)
        lines.append("")
        lines.append(f"  text      : {profile.n_sentences} sentences, "
                     f"{profile.n_words} words")
        lines.append("")
        lines.append("  READING PROFILE")
        lines.append("  " + "-" * 68)
        lines.append(f"   pace     {profile.pace:>6.3f}   "
                     f"({profile.mean_wpm:.0f} WPM)")
        lines.append(f"   memory   {profile.memory:>6.3f}   "
                     f"(fraction surviving 1 week)")
        lines.append(f"   passes   {profile.passes:>6.3f}   "
                     f"(reads needed)")
        lines.append(f"   slip     {profile.slip:>6.3f}   "
                     f"(attention-lapse probability)")
        lines.append(f"   wall     {profile.wall:>6.3f}   "
                     f"(reader-refusal probability)")
        lines.append("")
        lines.append(f"  ttu      : {profile.ttu_s:.1f} s "
                     f"(total reading time)")
        lines.append(f"  hunger   : {profile.hunger_final:+.2f} "
                     f"(unresolved questions)")
        lines.append("")

        if not profile.spans:
            lines.append("  (no content)")
            return "\n".join(lines)

        lines.append("  PER-SENTENCE")
        lines.append("  " + "-" * 68)
        lines.append(
            f"  {'id':>3}  {'wpm':>5}  {'dens':>5}  {'ret':>5}  "
            f"{'fold':>5}  {'slip':>5}  {'wall':>5}  type"
        )
        for s in profile.spans:
            marker = (
                "*" if (profile.wall_span and s.id == profile.wall_span.id)
                else " "
            )
            lines.append(
                f"  {marker}{s.id:>2}  {s.wpm:>5.0f}  {s.density:>5.2f}  "
                f"{s.retention_week:>5.2f}  {s.fold_load:>5.2f}  "
                f"{s.slip:>5.2f}  {s.wall:>5.2f}  {s.wall_type}"
            )
        lines.append("")

        if profile.slowest_span:
            s = profile.slowest_span
            lines.append("  SLOWEST SPAN")
            lines.append("  " + "-" * 68)
            lines.append(f"   [{s.id}] {s.wpm:.0f} WPM "
                         f"(slowdown {s.slowdown:.2f}x)")
            lines.append(f"   \"{self._shorten(s.text, 60)}\"")
            lines.append("")

        if profile.wall_span:
            s = profile.wall_span
            lines.append("  WALL SPAN")
            lines.append("  " + "-" * 68)
            lines.append(f"   [{s.id}] {s.wall_type} "
                         f"(score {s.wall:.3f})")
            lines.append(f"   \"{self._shorten(s.text, 60)}\"")
            for r in s.reasons:
                lines.append(f"     - {r}")
            lines.append("")

        lines.append("  SUMMARY")
        lines.append("  " + "-" * 68)
        lines.append(f"   {profile.summary}")
        lines.append("")

        return "\n".join(lines)

    @staticmethod
    def _shorten(s: str, n: int) -> str:
        s = s.strip().replace("\n", " ")
        if len(s) <= n:
            return s
        return s[: n - 1].rsplit(" ", 1)[0] + "…"

    def save_pretrained(self, save_dir: str) -> None:
        os.makedirs(save_dir, exist_ok=True)
        payload = {
            "config": asdict(self.config),
            "observations": self._obs,
        }
        with open(os.path.join(save_dir, "config.json"), "w") as f:
            json.dump(payload, f, indent=2)

    @classmethod
    def from_pretrained(cls, save_dir: str) -> "HVReader":
        with open(os.path.join(save_dir, "config.json"), "r") as f:
            payload = json.load(f)
        cfg_dict = payload.get("config", {})
        known = {f.name for f in HVReaderConfig.__dataclass_fields__.values()}
        cfg_dict = {k: v for k, v in cfg_dict.items() if k in known}
        cfg = HVReaderConfig(**cfg_dict)
        obj = cls(config=cfg)
        obj._obs = int(payload.get("observations", 0))
        return obj


# ============================================================================
# Demo
# ============================================================================
SAMPLE_FICTION = (
    "The old man walked slowly to the boat. He stopped, looked at the "
    "water, and then continued. The sea was quiet that morning. He "
    "pushed the boat into the water and climbed in. The oars were cold "
    "in his hands. He rowed out past the harbor and into the open sea."
)

SAMPLE_ACADEMIC = (
    "A black hole is a region of spacetime where gravity is so strong "
    "that nothing — no particles or even electromagnetic radiation such "
    "as light — can escape from it. The theory of general relativity "
    "predicts that a sufficiently compact mass can deform spacetime to "
    "form a black hole. The boundary of the region from which no escape "
    "is possible is called the event horizon. Although the event horizon "
    "has profound effects on the fate of an object that crosses it, it "
    "has no locally detectable features. A black hole acts as a perfect "
    "black body, and moreover, it emits Hawking radiation."
)

SAMPLE_OVERCLAIM = (
    "The data suggests some correlation between the policy and the outcome. "
    "Results appear to indicate a modest effect in some subpopulations. "
    "The mechanism remains unclear, and further work is needed to establish "
    "causality. "
    "Therefore, the policy definitively causes the outcome in all cases, "
    "and this is unquestionably proven by the evidence."
)

SAMPLE_MONOTONE = (
    "The system processes the input. The system processes the data. "
    "The system processes the output. The system processes the result. "
    "The system processes the value. The system processes the record."
)


def _demo(output_dir: str = "./hv_reader_output") -> None:
    os.makedirs(output_dir, exist_ok=True)
    m = HVReader()

    samples = [
        ("Fiction", SAMPLE_FICTION),
        ("Academic", SAMPLE_ACADEMIC),
        ("Overclaim", SAMPLE_OVERCLAIM),
        ("Monotone", SAMPLE_MONOTONE),
    ]

    for name, text in samples:
        print()
        print("#" * 72)
        print(f"#  {name}")
        print("#" * 72)
        print(m.render(m.analyze(text)))

    print()
    print("=" * 72)
    print("Summary across samples")
    print("=" * 72)
    print(
        f"  {'sample':<12}  {'words':>6}  {'pace':>6}  {'mem':>6}  "
        f"{'pass':>5}  {'slip':>6}  {'wall':>5}  {'ttu':>6}"
    )
    print("  " + "-" * 68)
    for name, text in samples:
        r = m.analyze(text)
        print(
            f"  {name:<12}  {r.n_words:>6}  {r.pace:>6.3f}  "
            f"{r.memory:>6.3f}  {r.passes:>5.2f}  "
            f"{r.slip:>6.3f}  {r.wall:>5.2f}  {r.ttu_s:>5.1f}s"
        )

    print()
    print("=" * 72)
    print("Save / load round trip")
    print("=" * 72)
    save_path = os.path.join(output_dir, "hv_reader_model")
    m.save_pretrained(save_path)
    m2 = HVReader.from_pretrained(save_path)
    print(f"  saved to  : {save_path}")
    print(f"  reloaded  : {m2!r}")
    a = m.analyze(SAMPLE_OVERCLAIM)
    b = m2.analyze(SAMPLE_OVERCLAIM)
    print(f"  pace      : {a.pace:.4f}")
    print(f"  wall      : {a.wall:.4f}")
    print(f"  identical : "
          f"{abs(a.pace - b.pace) < 1e-9 and abs(a.wall - b.wall) < 1e-9}")
    print()


# ============================================================================
# CLI
# ============================================================================
def _cli() -> None:
    p = argparse.ArgumentParser(
        description="hv-reader: the reading experience in one call."
    )
    p.add_argument("--text", type=str, default="",
                   help="text to analyze (or '-' to read stdin)")
    p.add_argument("--json", action="store_true",
                   help="output JSON instead of a rendered report")
    p.add_argument("--profile", action="store_true",
                   help="print only the five-axis profile")
    p.add_argument("--save-to", type=str, default="",
                   help="save the model to this directory")
    p.add_argument("--outdir", type=str, default="./hv_reader_output")
    args = p.parse_args()

    text = sys.stdin.read() if args.text == "-" else args.text

    m = HVReader()
    if args.save_to:
        m.save_pretrained(args.save_to)
        print(f"saved to {args.save_to}", file=sys.stderr)

    if not text:
        _demo(args.outdir)
        return

    profile = m.analyze(text)

    if args.profile:
        print(f"pace    {profile.pace:.3f}")
        print(f"memory  {profile.memory:.3f}")
        print(f"passes  {profile.passes:.3f}")
        print(f"slip    {profile.slip:.3f}")
        print(f"wall    {profile.wall:.3f}")
        return

    if args.json:
        print(json.dumps(profile.to_dict(), indent=2, ensure_ascii=False))
    else:
        print(m.render(profile))


if __name__ == "__main__":
    _cli()