interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / aienux / crates / allen-reflex / reflex.om

4287 lines · 83003 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3note
4note aienux/crates/allen-reflex/reflex.om: ALLEN REFLEX v0 core in pure OM-2.
5note Library unit (no om2 header line, no fn main). Prefix: arx_.
6note Ports: action, dialog, text, lexicon, segment, policy, features, hv, span, model, lib.
7
8note =========================================================================
9note Section 1: Constants and Global State
10note =========================================================================
11
12const ARX_N_LABELS ulong 15
13const ARX_ACT_STATUS uchar 0
14const ARX_ACT_HELP uchar 1
15const ARX_ACT_EVIDENCE uchar 2
16const ARX_ACT_RECALL uchar 3
17const ARX_ACT_FACTS uchar 4
18const ARX_ACT_REMEMBER uchar 5
19const ARX_ACT_PREFER uchar 6
20const ARX_ACT_TASK uchar 7
21const ARX_ACT_PROMPT uchar 8
22const ARX_ACT_APPROVE uchar 9
23const ARX_ACT_DENY uchar 10
24const ARX_ACT_CANCEL uchar 11
25const ARX_ACT_NONE uchar 12
26const ARX_ACT_CLARIFY uchar 13
27const ARX_ACT_REFUSE uchar 14
28const ARX_ACT_INVALID uchar 255
29
30const ARX_MODE_REFLEX uchar 0
31const ARX_MODE_NEAREST uchar 1
32const ARX_MODE_PERCEPTRON uchar 2
33
34const ARX_ROLE_USER uchar 0
35const ARX_ROLE_ASST uchar 1
36
37const ARX_PEND_UNKNOWN uchar 0
38const ARX_PEND_YES uchar 1
39const ARX_PEND_NO uchar 2
40
41const ARX_GAP_STOP uchar 1
42const ARX_GAP_COMMA uchar 2
43const ARX_GAP_COLON uchar 4
44const ARX_GAP_SEMI uchar 8
45const ARX_GAP_DASH uchar 16
46const ARX_GAP_QUESTION uchar 32
47
48const ARX_REFUSE_NONE uchar 0
49const ARX_REFUSE_RULE_BREAK uchar 1
50const ARX_REFUSE_OVERRIDE uchar 2
51const ARX_REFUSE_FORBIDDEN uchar 3
52const ARX_REFUSE_BLANKET uchar 4
53const ARX_REFUSE_DATA_ORDER uchar 5
54
55const ARX_WHY_NO_USER_TURN uchar 0
56const ARX_WHY_INPUT_TOO_LONG uchar 1
57const ARX_WHY_NO_WORDS uchar 2
58const ARX_WHY_POLICY uchar 3
59const ARX_WHY_EXACT_RECALL uchar 4
60const ARX_WHY_SCORED uchar 5
61const ARX_WHY_LOW_SIMILARITY uchar 6
62const ARX_WHY_LOW_MARGIN uchar 7
63const ARX_WHY_LOW_AGREEMENT uchar 8
64const ARX_WHY_SPAN uchar 9
65
66const ARX_SPAN_OK uchar 0
67const ARX_SPAN_EMPTY uchar 1
68const ARX_SPAN_TOO_LONG uchar 2
69const ARX_SPAN_UNREP uchar 3
70
71const ARX_LEARN_OK uchar 0
72const ARX_LEARN_OFF uchar 1
73const ARX_LEARN_FULL uchar 2
74const ARX_LEARN_NO_USER uchar 3
75
76const ARX_SYM_WORD uchar 1
77const ARX_SYM_CHAR uchar 2
78const ARX_SYM_CONCEPT uchar 3
79const ARX_SYM_CTX uchar 4
80const ARX_SYM_SHAPE uchar 5
81const ARX_SYM_LEAD uchar 6
82
83const ARX_CH_WORD ulong 0
84const ARX_CH_BIGRAM ulong 1
85const ARX_CH_CHARS ulong 2
86const ARX_CH_CONCEPT ulong 3
87const ARX_CH_LEAD ulong 4
88const ARX_CH_CTX ulong 5
89const ARX_CH_CTXCONJ ulong 6
90const ARX_CH_SHAPE ulong 7
91const ARX_N_CHANNELS ulong 8
92const ARX_N_CONCEPTS ulong 18
93
94const ARX_SEED ulong 0x414c4c454e524546
95const ARX_HV_WORDS ulong 128
96const ARX_HV_BITS ulong 8192
97const ARX_MAX_EXEMPLARS ulong 256
98const ARX_MAX_CONFLICTS ulong 32
99const ARX_NOTE_BYTES ulong 96
100
101const ARX_MAX_TURNS_CAP ulong 32
102const ARX_TURN_STRIDE ulong 512
103const ARX_MAX_MSG_CAP ulong 4096
104const ARX_MAX_TOKENS ulong 256
105const ARX_TOK_STRIDE ulong 64
106const ARX_MAX_CLAUSES ulong 64
107const ARX_MAX_FEATS ulong 768
108
109const ARX_MAX_CUES ulong 256
110const ARX_MAX_CUE_WORDS ulong 8
111const ARX_MAX_STATS ulong 2048
112const ARX_MAX_TEXT ulong 48
113
114const ARX_SPARSE_POOL ulong 65536
115const ARX_PERC_CAP ulong 8192
116
117note --- Configuration globals ---
118global arx_cfg_mode uchar 1
119global arx_cfg_weights ushort 8
120global arx_cfg_hv_words ulong 1
121global arx_cfg_hv_k ulong 1
122global arx_cfg_hv_alpha f32 1
123global arx_cfg_hv_zband f32 1
124global arx_cfg_hv_epochs uint 1
125global arx_cfg_hv_eta f32 1
126global arx_cfg_max_ex ulong 1
127global arx_cfg_t_margin f32 1
128global arx_cfg_t_abs f32 1
129global arx_cfg_abs_label uchar 1
130global arx_cfg_t_agree f32 1
131global arx_cfg_exact f32 1
132global arx_cfg_perc_epochs uint 1
133global arx_cfg_sleep_dup f32 1
134global arx_cfg_sleep_con f32 1
135global arx_cfg_max_msg ulong 1
136global arx_cfg_max_toks ulong 1
137global arx_cfg_max_turns ulong 1
138global arx_learning uchar 1
139
140note --- Turn buffer globals ---
141global arx_turn_n ulong 1
142global arx_turn_role uchar 32
143global arx_turn_buf uchar 16384
144global arx_turn_len ulong 32
145global arx_turn_raw_len ulong 32
146
147note --- Dialog state globals ---
148global arx_dlg_pending uchar 1
149global arx_dlg_prev uchar 1
150global arx_dlg_has_user uchar 1
151global arx_dlg_last_user ulong 1
152global arx_dlg_msg uchar 4096
153global arx_dlg_msg_len ulong 1
154
155note --- Token globals ---
156global arx_tok_n ulong 1
157global arx_tok_start ulong 256
158global arx_tok_end ulong 256
159global arx_tok_gap uchar 256
160global arx_tok_alnum uchar 256
161global arx_tok_word uchar 16384
162global arx_tok_wlen ulong 256
163global arx_tok_stem uchar 16384
164global arx_tok_slen ulong 256
165global arx_tok_whash ulong 256
166global arx_tok_shash ulong 256
167
168note --- Clause globals ---
169global arx_cl_n ulong 1
170global arx_cl_start ulong 64
171global arx_cl_end ulong 64
172global arx_pri_start ulong 1
173global arx_pri_end ulong 1
174
175note --- Policy scratch and result ---
176global arx_pol_exempt uchar 256
177global arx_pol_rule uchar 1
178global arx_pol_tok ulong 1
179
180note --- Feature globals ---
181global arx_ft_n ulong 1
182global arx_ft_fam uchar 3072
183global arx_ft_val ulong 3072
184global arx_ft_len uchar 768
185global arx_ft_ch uchar 768
186global arx_ft_w ushort 768
187
188note --- HV scratch and model globals ---
189global arx_hv_cnt int 8192
190global arx_hv_acc ulong 128
191global arx_hv_tmp ulong 128
192global arx_hv_q ulong 128
193
194global arx_hv_n ulong 1
195global arx_hv_hvs ulong 32768
196global arx_hv_labels uchar 256
197global arx_hv_notes uchar 24576
198global arx_hv_nlen ulong 256
199global arx_hv_counts int 122880
200global arx_hv_sign ulong 1920
201global arx_hv_mask ulong 1920
202global arx_hv_nnz uint 15
203global arx_hv_nlabel uint 15
204global arx_hv_keep uchar 256
205
206note --- Sleep report globals ---
207global arx_slp_merged ulong 1
208global arx_slp_kept ulong 1
209global arx_slp_passes uint 1
210global arx_slp_con_n ulong 1
211global arx_slp_con_a ulong 32
212global arx_slp_con_b ulong 32
213global arx_slp_con_sim f32 32
214
215note --- Sparse & NearestModel globals ---
216global arx_sp_q_k ulong 768
217global arx_sp_q_w uint 768
218global arx_sp_q_n ulong 1
219
220global arx_nr_n ulong 1
221global arx_nr_off ulong 256
222global arx_nr_len ulong 256
223global arx_nr_labels uchar 256
224global arx_nr_notes uchar 24576
225global arx_nr_nlen ulong 256
226global arx_nr_nlabel uint 15
227global arx_nr_pool_n ulong 1
228global arx_nr_pool_k ulong 65536
229global arx_nr_pool_w uint 65536
230
231note --- PerceptronModel globals ---
232global arx_pc_w_used uchar 8192
233global arx_pc_w_key ulong 8192
234global arx_pc_w_val f32 122880
235global arx_pc_w_cnt ulong 1
236global arx_pc_u_used uchar 8192
237global arx_pc_u_key ulong 8192
238global arx_pc_u_val f32 122880
239global arx_pc_u_cnt ulong 1
240global arx_pc_tr_n ulong 1
241global arx_pc_tr_off ulong 256
242global arx_pc_tr_len ulong 256
243global arx_pc_tr_lbl uchar 256
244global arx_pc_nlabel uint 15
245global arx_pc_pool_n ulong 1
246global arx_pc_pool_k ulong 65536
247global arx_pc_pool_w uint 65536
248global arx_pc_order ulong 256
249
250note --- Scorer output globals ---
251global arx_sc_s f32 15
252global arx_sc_best_sim f32 1
253global arx_sc_has_near uchar 1
254global arx_sc_nearest ulong 1
255global arx_sc_agree f32 1
256
257note --- CueMemory globals ---
258global arx_cue_pre_n ulong 1
259global arx_cue_pre_act uchar 256
260global arx_cue_pre_len uchar 256
261global arx_cue_pre_h ulong 2048
262
263global arx_cue_suf_n ulong 1
264global arx_cue_suf_act uchar 256
265global arx_cue_suf_len uchar 256
266global arx_cue_suf_h ulong 2048
267
268global arx_cue_st_n ulong 1
269global arx_cue_st_act uchar 2048
270global arx_cue_st_h ulong 2048
271global arx_cue_st_c ushort 2048
272global arx_cue_st_p ushort 2048
273
274note --- Decision & Explanation output globals ---
275global arx_dec_act uchar 1
276global arx_dec_has_txt uchar 1
277global arx_dec_txt uchar 64
278global arx_dec_txt_len ulong 1
279
280global arx_exp_why uchar 1
281global arx_exp_refuse uchar 1
282global arx_exp_span_err uchar 1
283global arx_exp_top uchar 1
284global arx_exp_second uchar 1
285global arx_exp_margin f32 1
286global arx_exp_best_sim f32 1
287global arx_exp_agree f32 1
288global arx_exp_has_near uchar 1
289global arx_exp_near_act uchar 1
290global arx_exp_near_idx ulong 1
291global arx_exp_span_bs ulong 1
292global arx_exp_span_be ulong 1
293
294note =========================================================================
295note Section 2: Action Helpers
296note =========================================================================
297
298text arx_s_act_0 "status"
299text arx_s_act_1 "help"
300text arx_s_act_2 "evidence"
301text arx_s_act_3 "recall"
302text arx_s_act_4 "facts"
303text arx_s_act_5 "remember"
304text arx_s_act_6 "prefer"
305text arx_s_act_7 "task"
306text arx_s_act_8 "prompt"
307text arx_s_act_9 "approve"
308text arx_s_act_10 "deny"
309text arx_s_act_11 "cancel"
310text arx_s_act_12 "none"
311text arx_s_act_13 "clarify"
312text arx_s_act_14 "refuse"
313
314fn arx_copy_bytes dst edit uchar * pos ulong src view uchar * to ulong
315n = len src
316i = ulong 0
317while lt i n bound 4096
318put dst add pos i get src i
319i = add i 1
320end
321ret add pos n
322end
323
324fn arx_eq_bytes a view uchar * b view uchar * to bool
325na = len a
326if ne na len b
327ret false
328end
329i = ulong 0
330while lt i na bound 4096
331if ne get a i get b i
332ret false
333end
334i = add i 1
335end
336ret true
337end
338
339fn arx_act_write_name act uchar dst edit uchar * pos ulong to ulong
340if eq act ARX_ACT_STATUS
341ret arx_copy_bytes dst pos arx_s_act_0
342end
343if eq act ARX_ACT_HELP
344ret arx_copy_bytes dst pos arx_s_act_1
345end
346if eq act ARX_ACT_EVIDENCE
347ret arx_copy_bytes dst pos arx_s_act_2
348end
349if eq act ARX_ACT_RECALL
350ret arx_copy_bytes dst pos arx_s_act_3
351end
352if eq act ARX_ACT_FACTS
353ret arx_copy_bytes dst pos arx_s_act_4
354end
355if eq act ARX_ACT_REMEMBER
356ret arx_copy_bytes dst pos arx_s_act_5
357end
358if eq act ARX_ACT_PREFER
359ret arx_copy_bytes dst pos arx_s_act_6
360end
361if eq act ARX_ACT_TASK
362ret arx_copy_bytes dst pos arx_s_act_7
363end
364if eq act ARX_ACT_PROMPT
365ret arx_copy_bytes dst pos arx_s_act_8
366end
367if eq act ARX_ACT_APPROVE
368ret arx_copy_bytes dst pos arx_s_act_9
369end
370if eq act ARX_ACT_DENY
371ret arx_copy_bytes dst pos arx_s_act_10
372end
373if eq act ARX_ACT_CANCEL
374ret arx_copy_bytes dst pos arx_s_act_11
375end
376if eq act ARX_ACT_NONE
377ret arx_copy_bytes dst pos arx_s_act_12
378end
379if eq act ARX_ACT_CLARIFY
380ret arx_copy_bytes dst pos arx_s_act_13
381end
382if eq act ARX_ACT_REFUSE
383ret arx_copy_bytes dst pos arx_s_act_14
384end
385ret arx_copy_bytes dst pos arx_s_act_13
386end
387
388fn arx_act_from_name s view uchar * to uchar
389if arx_eq_bytes s arx_s_act_0
390ret ARX_ACT_STATUS
391end
392if arx_eq_bytes s arx_s_act_1
393ret ARX_ACT_HELP
394end
395if arx_eq_bytes s arx_s_act_2
396ret ARX_ACT_EVIDENCE
397end
398if arx_eq_bytes s arx_s_act_3
399ret ARX_ACT_RECALL
400end
401if arx_eq_bytes s arx_s_act_4
402ret ARX_ACT_FACTS
403end
404if arx_eq_bytes s arx_s_act_5
405ret ARX_ACT_REMEMBER
406end
407if arx_eq_bytes s arx_s_act_6
408ret ARX_ACT_PREFER
409end
410if arx_eq_bytes s arx_s_act_7
411ret ARX_ACT_TASK
412end
413if arx_eq_bytes s arx_s_act_8
414ret ARX_ACT_PROMPT
415end
416if arx_eq_bytes s arx_s_act_9
417ret ARX_ACT_APPROVE
418end
419if arx_eq_bytes s arx_s_act_10
420ret ARX_ACT_DENY
421end
422if arx_eq_bytes s arx_s_act_11
423ret ARX_ACT_CANCEL
424end
425if arx_eq_bytes s arx_s_act_12
426ret ARX_ACT_NONE
427end
428if arx_eq_bytes s arx_s_act_13
429ret ARX_ACT_CLARIFY
430end
431if arx_eq_bytes s arx_s_act_14
432ret ARX_ACT_REFUSE
433end
434ret ARX_ACT_INVALID
435end
436
437fn arx_act_has_text act uchar to bool
438if eq act ARX_ACT_REMEMBER
439ret true
440end
441if eq act ARX_ACT_PREFER
442ret true
443end
444if eq act ARX_ACT_TASK
445ret true
446end
447if eq act ARX_ACT_PROMPT
448ret true
449end
450ret false
451end
452
453fn arx_act_needs_pending act uchar to bool
454if eq act ARX_ACT_APPROVE
455ret true
456end
457if eq act ARX_ACT_DENY
458ret true
459end
460if eq act ARX_ACT_CANCEL
461ret true
462end
463ret false
464end
465
466fn arx_act_creates_pending act uchar to bool
467if eq act ARX_ACT_TASK
468ret true
469end
470if eq act ARX_ACT_PROMPT
471ret true
472end
473ret false
474end
475
476note =========================================================================
477note Section 3: Text Normalization, Tokenization, Stemming, Hashing
478note =========================================================================
479
480fn arx_fnv s view uchar * to ulong
481h = ulong 0xcbf29ce484222325
482n = len s
483i = ulong 0
484while lt i n bound 4096
485h = xor h ulong get s i
486h = mul h ulong 0x00000100000001b3
487i = add i 1
488end
489ret h
490end
491
492fn arx_is_space b uchar to bool
493if eq b 32
494ret true
495end
496if eq b 9
497ret true
498end
499if eq b 10
500ret true
501end
502if eq b 13
503ret true
504end
505if eq b 12
506ret true
507end
508ret false
509end
510
511fn arx_is_alpha b uchar to bool
512if ge b 65
513if le b 90
514ret true
515end
516end
517if ge b 97
518if le b 122
519ret true
520end
521end
522ret false
523end
524
525fn arx_is_digit b uchar to bool
526if ge b 48
527if le b 57
528ret true
529end
530end
531ret false
532end
533
534fn arx_is_alnum b uchar to bool
535if arx_is_alpha b
536ret true
537end
538ret arx_is_digit b
539end
540
541fn arx_to_lower b uchar to uchar
542if ge b 65
543if le b 90
544ret add b 32
545end
546end
547ret b
548end
549
550fn arx_edge_punct b uchar to bool
551if eq b 46
552ret true
553end
554if eq b 44
555ret true
556end
557if eq b 59
558ret true
559end
560if eq b 58
561ret true
562end
563if eq b 33
564ret true
565end
566if eq b 63
567ret true
568end
569if eq b 34
570ret true
571end
572if eq b 39
573ret true
574end
575if eq b 40
576ret true
577end
578if eq b 41
579ret true
580end
581if eq b 91
582ret true
583end
584if eq b 93
585ret true
586end
587if eq b 123
588ret true
589end
590if eq b 125
591ret true
592end
593if eq b 45
594ret true
595end
596if eq b 96
597ret true
598end
599if eq b 42
600ret true
601end
602ret false
603end
604
605fn arx_gap_flags s view uchar * to uchar
606f = uchar 0
607n = len s
608i = ulong 0
609while lt i n bound 4096
610b = get s i
611if eq b 46
612f = or f ARX_GAP_STOP
613else
614if eq b 33
615f = or f ARX_GAP_STOP
616else
617if eq b 63
618f = or f or ARX_GAP_STOP ARX_GAP_QUESTION
619else
620if eq b 44
621f = or f ARX_GAP_COMMA
622else
623if eq b 58
624f = or f ARX_GAP_COLON
625else
626if eq b 59
627f = or f ARX_GAP_SEMI
628else
629if eq b 45
630lok = false
631if eq i 0
632lok = true
633else
634pb = get s sub i 1
635if or eq pb 32 eq pb 45
636lok = true
637end
638end
639rok = false
640if eq add i 1 n
641rok = true
642else
643nb = get s add i 1
644if or eq nb 32 eq nb 45
645rok = true
646end
647end
648if and lok rok
649f = or f ARX_GAP_DASH
650end
651end
652end
653end
654end
655end
656end
657end
658i = add i 1
659end
660ret f
661end
662
663fn arx_lower_buf src view uchar * dst edit uchar * to ulong
664n = len src
665cap = len dst
666i = ulong 0
667j = ulong 0
668while lt i n bound 4096
669if ge j cap
670break
671end
672b = get src i
673curly = false
674if eq b 0xe2
675if lt add i 2 n
676if eq get src add i 1 0x80
677b2 = get src add i 2
678if or eq b2 0x99 eq b2 0x98
679curly = true
680end
681end
682end
683end
684if curly
685put dst j uchar 39
686j = add j 1
687i = add i 3
688else
689put dst j arx_to_lower b
690j = add j 1
691i = add i 1
692end
693end
694ret j
695end
696
697fn arx_ends_with buf view uchar * n ulong suf view uchar * to bool
698sn = len suf
699if lt n sn
700ret false
701end
702off = sub n sn
703i = ulong 0
704while lt i sn bound 64
705if ne get buf add off i get suf i
706ret false
707end
708i = add i 1
709end
710ret true
711end
712
713text arx_s_ing "ing"
714text arx_s_ies "ies"
715text arx_s_ed "ed"
716text arx_s_eed "eed"
717text arx_s_ss "ss"
718text arx_s_us "us"
719text arx_s_is "is"
720
721fn arx_stem_word src view uchar * n ulong dst edit uchar * to ulong
722all_a = true
723i = ulong 0
724while lt i n bound 64
725if not arx_is_alpha get src i
726all_a = false
727break
728end
729i = add i 1
730end
731out_n = n
732if all_a
733if gt n 3
734done = false
735if gt n 5
736if arx_ends_with src n arx_s_ing
737out_n = sub n 3
738done = true
739end
740end
741if not done
742if gt n 4
743if arx_ends_with src n arx_s_ies
744k = ulong 0
745lim = sub n 3
746while lt k lim bound 64
747put dst k get src k
748k = add k 1
749end
750put dst lim uchar 121
751ret add lim 1
752end
753end
754end
755if not done
756if gt n 4
757if arx_ends_with src n arx_s_ed
758if not arx_ends_with src n arx_s_eed
759out_n = sub n 2
760done = true
761end
762end
763end
764end
765if not done
766if eq get src sub n 1 115
767if not arx_ends_with src n arx_s_ss
768if not arx_ends_with src n arx_s_us
769if not arx_ends_with src n arx_s_is
770out_n = sub n 1
771end
772end
773end
774end
775end
776end
777end
778k = ulong 0
779while lt k out_n bound 64
780put dst k get src k
781k = add k 1
782end
783ret out_n
784end
785
786text arx_s_cant "can't"
787text arx_s_wont "won't"
788text arx_s_nt "n't"
789text arx_s_am_s "'m"
790text arx_s_re_s "'re"
791text arx_s_ll_s "'ll"
792text arx_s_ve_s "'ve"
793text arx_s_d_s "'d"
794text arx_s_s_s "'s"
795text arx_s_can "can"
796text arx_s_will "will"
797text arx_s_not "not"
798text arx_s_am "am"
799text arx_s_are "are"
800text arx_s_have "have"
801text arx_s_would "would"
802
803fn arx_store_tok_words idx ulong w view uchar * to void
804st = new uchar 64 0
805wn = len w
806if gt wn ARX_TOK_STRIDE
807wn = ARX_TOK_STRIDE
808end
809sn = arx_stem_word w wn st
810base = mul idx ARX_TOK_STRIDE
811k = ulong 0
812while lt k wn bound 64
813put arx_tok_word add base k get w k
814k = add k 1
815end
816k = ulong 0
817while lt k sn bound 64
818put arx_tok_stem add base k get st k
819k = add k 1
820end
821put arx_tok_wlen idx wn
822put arx_tok_slen idx sn
823put arx_tok_whash idx arx_fnv part w 0 wn
824put arx_tok_shash idx arx_fnv part st 0 sn
825end
826
827fn arx_store_tok s ulong e ulong gapv uchar alnumv uchar w view uchar * to void
828idx = get arx_tok_n 0
829if ge idx ARX_MAX_TOKENS
830ret
831end
832put arx_tok_start idx s
833put arx_tok_end idx e
834put arx_tok_gap idx gapv
835put arx_tok_alnum idx alnumv
836arx_store_tok_words idx w
837put arx_tok_n 0 add idx 1
838end
839
840fn arx_store_raw_tok s ulong e ulong w view uchar * to void
841idx = get arx_tok_n 0
842if ge idx ARX_MAX_TOKENS
843ret
844end
845wn = len w
846if gt wn ARX_TOK_STRIDE
847wn = ARX_TOK_STRIDE
848end
849base = mul idx ARX_TOK_STRIDE
850k = ulong 0
851while lt k wn bound 64
852put arx_tok_word add base k get w k
853put arx_tok_stem add base k get w k
854k = add k 1
855end
856h = arx_fnv part w 0 wn
857put arx_tok_start idx s
858put arx_tok_end idx e
859put arx_tok_gap idx 0
860put arx_tok_alnum idx 1
861put arx_tok_wlen idx wn
862put arx_tok_slen idx wn
863put arx_tok_whash idx h
864put arx_tok_shash idx h
865put arx_tok_n 0 add idx 1
866end
867
868fn arx_emit_expanded s ulong e ulong gapv uchar lw view uchar * to void
869n = len lw
870al = uchar 0
871k = ulong 0
872while lt k n bound 64
873if arx_is_alnum get lw k
874al = 1
875break
876end
877k = add k 1
878end
879if eq n 5
880if arx_eq_bytes lw arx_s_cant
881arx_store_tok s e gapv al arx_s_can
882arx_store_raw_tok s e arx_s_not
883ret
884end
885if arx_eq_bytes lw arx_s_wont
886arx_store_tok s e gapv al arx_s_will
887arx_store_raw_tok s e arx_s_not
888ret
889end
890end
891if gt n 3
892if arx_ends_with lw n arx_s_nt
893arx_store_tok s e gapv al part lw 0 sub n 3
894arx_store_raw_tok s e arx_s_not
895ret
896end
897if arx_ends_with lw n arx_s_re_s
898arx_store_tok s e gapv al part lw 0 sub n 3
899arx_store_raw_tok s e arx_s_are
900ret
901end
902if arx_ends_with lw n arx_s_ll_s
903arx_store_tok s e gapv al part lw 0 sub n 3
904arx_store_raw_tok s e arx_s_will
905ret
906end
907if arx_ends_with lw n arx_s_ve_s
908arx_store_tok s e gapv al part lw 0 sub n 3
909arx_store_raw_tok s e arx_s_have
910ret
911end
912end
913if gt n 2
914if arx_ends_with lw n arx_s_am_s
915arx_store_tok s e gapv al part lw 0 sub n 2
916arx_store_raw_tok s e arx_s_am
917ret
918end
919if arx_ends_with lw n arx_s_d_s
920arx_store_tok s e gapv al part lw 0 sub n 2
921arx_store_raw_tok s e arx_s_would
922ret
923end
924if arx_ends_with lw n arx_s_s_s
925arx_store_tok s e gapv al part lw 0 sub n 2
926ret
927end
928end
929arx_store_tok s e gapv al lw
930end
931
932fn arx_tok_emit_span msg view uchar * s ulong e ulong g uchar to void
933lw = new uchar 64 0
934lwn = arx_lower_buf part msg s sub e s lw
935arx_emit_expanded s e g part lw 0 lwn
936end
937
938fn arx_tokenize msg view uchar * max_toks ulong to ulong
939put arx_tok_n 0 0
940n = len msg
941cap = max_toks
942if gt cap ARX_MAX_TOKENS
943cap = ARX_MAX_TOKENS
944end
945i = ulong 0
946gstart = ulong 0
947while lt i n bound 4096
948if ge get arx_tok_n 0 cap
949break
950end
951if arx_is_space get msg i
952i = add i 1
953continue
954end
955s = i
956while lt i n bound 4096
957if arx_is_space get msg i
958break
959end
960i = add i 1
961end
962e = i
963while lt s e bound 4096
964if not arx_edge_punct get msg s
965break
966end
967s = add s 1
968end
969while gt e s bound 4096
970if not arx_edge_punct get msg sub e 1
971break
972end
973e = sub e 1
974end
975if eq s e
976continue
977end
978g = arx_gap_flags part msg gstart sub s gstart
979gstart = e
980arx_tok_emit_span msg s e g
981end
982if gt get arx_tok_n 0 cap
983put arx_tok_n 0 cap
984end
985ret get arx_tok_n 0
986end
987
988fn arx_find_ci hay view uchar * needle view uchar * to long
989hn = len hay
990nn = len needle
991if eq nn 0
992ret -1
993end
994if gt nn hn
995ret -1
996end
997lim = add sub hn nn 1
998i = ulong 0
999while lt i lim bound 4096
1000ok = true
1001j = ulong 0
1002while lt j nn bound 512
1003a = arx_to_lower get hay add i j
1004b = arx_to_lower get needle j
1005if ne a b
1006ok = false
1007break
1008end
1009j = add j 1
1010end
1011if ok
1012ret long i
1013end
1014i = add i 1
1015end
1016ret -1
1017end
1018
1019note =========================================================================
1020note Section 4: Dialog Roles, Turns, and Pending State
1021note =========================================================================
1022
1023text arx_s_act_key "\"action\""
1024
1025fn arx_parse_action s view uchar * to uchar
1026n = len s
1027kn = len arx_s_act_key
1028if lt n kn
1029ret ARX_ACT_INVALID
1030end
1031found = false
1032pos = ulong 0
1033lim = add sub n kn 1
1034i = ulong 0
1035while lt i lim bound 4096
1036ok = true
1037j = ulong 0
1038while lt j kn bound 16
1039if ne get s add i j get arx_s_act_key j
1040ok = false
1041break
1042end
1043j = add j 1
1044end
1045if ok
1046found = true
1047pos = add i kn
1048break
1049end
1050i = add i 1
1051end
1052if not found
1053ret ARX_ACT_INVALID
1054end
1055colon = false
1056while lt pos n bound 4096
1057if eq get s pos 58
1058colon = true
1059pos = add pos 1
1060break
1061end
1062pos = add pos 1
1063end
1064if not colon
1065ret ARX_ACT_INVALID
1066end
1067while lt pos n bound 4096
1068if not arx_is_space get s pos
1069break
1070end
1071pos = add pos 1
1072end
1073if ge pos n
1074ret ARX_ACT_INVALID
1075end
1076if ne get s pos 34
1077ret ARX_ACT_INVALID
1078end
1079pos = add pos 1
1080es = pos
1081while lt pos n bound 4096
1082if eq get s pos 34
1083ret arx_act_from_name part s es sub pos es
1084end
1085pos = add pos 1
1086end
1087ret ARX_ACT_INVALID
1088end
1089
1090fn arx_clear_turns to void
1091put arx_turn_n 0 0
1092end
1093
1094fn arx_push_turn role uchar txt view uchar * to void
1095idx = get arx_turn_n 0
1096if ge idx ARX_MAX_TURNS_CAP
1097ret
1098end
1099rn = len txt
1100n = rn
1101if gt n ARX_TURN_STRIDE
1102n = ARX_TURN_STRIDE
1103end
1104put arx_turn_role idx role
1105put arx_turn_len idx n
1106put arx_turn_raw_len idx rn
1107base = mul idx ARX_TURN_STRIDE
1108i = ulong 0
1109while lt i n bound 512
1110put arx_turn_buf add base i get txt i
1111i = add i 1
1112end
1113put arx_turn_n 0 add idx 1
1114end
1115
1116fn arx_exam_turns s view uchar * to void
1117arx_clear_turns
1118n = len s
1119role = ARX_ROLE_USER
1120seg = ulong 0
1121i = ulong 0
1122while lt i n bound 16384
1123is_sep = false
1124if lt add i 3 n
1125if eq get s i 32
1126if eq get s add i 1 126
1127if eq get s add i 2 126
1128if eq get s add i 3 32
1129is_sep = true
1130end
1131end
1132end
1133end
1134end
1135if is_sep
1136arx_push_turn role part s seg sub i seg
1137if eq role ARX_ROLE_USER
1138role = ARX_ROLE_ASST
1139else
1140role = ARX_ROLE_USER
1141end
1142i = add i 4
1143seg = i
1144else
1145i = add i 1
1146end
1147end
1148arx_push_turn role part s seg sub n seg
1149end
1150
1151fn arx_load_turn_buf idx ulong dst edit uchar * to ulong
1152n = get arx_turn_len idx
1153base = mul idx ARX_TURN_STRIDE
1154i = ulong 0
1155while lt i n bound 512
1156put dst i get arx_turn_buf add base i
1157i = add i 1
1158end
1159ret n
1160end
1161
1162fn arx_dialog_state max_turns ulong to void
1163tmp = new uchar 512 0
1164tn = get arx_turn_n 0
1165start = ulong 0
1166if gt tn max_turns
1167start = sub tn max_turns
1168end
1169has_u = uchar 0
1170last_u = ulong 0
1171i = start
1172while lt i tn bound 64
1173if eq get arx_turn_role i ARX_ROLE_USER
1174has_u = 1
1175last_u = i
1176end
1177i = add i 1
1178end
1179pend = ARX_PEND_UNKNOWN
1180prev = ARX_ACT_INVALID
1181if eq has_u 1
1182j = start
1183while lt j last_u bound 64
1184if eq get arx_turn_role j ARX_ROLE_ASST
1185ln = arx_load_turn_buf j tmp
1186a = arx_parse_action part tmp 0 ln
1187prev = a
1188if ne a ARX_ACT_INVALID
1189if arx_act_creates_pending a
1190pend = ARX_PEND_YES
1191else
1192if arx_act_needs_pending a
1193pend = ARX_PEND_NO
1194end
1195end
1196end
1197end
1198j = add j 1
1199end
1200end
1201put arx_dlg_pending 0 pend
1202put arx_dlg_prev 0 prev
1203put arx_dlg_has_user 0 has_u
1204put arx_dlg_last_user 0 last_u
1205end
1206
1207note =========================================================================
1208note Section 5: Lexicon Tables and Phrase Matching
1209note =========================================================================
1210
1211text arx_lex_c0 "remember\nmemorize\nmemorise\nstore\nsave\nkeep in mind\nnote for memory\ndo not forget\nfact\nmemory"
1212text arx_lex_c1 "prefer\npreference\nrather\nfrom now on\ni like\nalways\ntone\nstyle"
1213text arx_lex_c2 "task\ntodo\nto-do\nto do\nremind me\nmy list\nmake a note\nnote down"
1214text arx_lex_c3 "write\ndraft\ncompose\ngenerate\nsummarize\nsummarise\nexplain\ndescribe\nrewrite\ntranslate\npoem\nstory\nhaiku\nlimerick\nletter\nessay\ntoast\nreply\nmessage"
1215text arx_lex_c4 "approve\naccept\nyes\nyeah\nyep\nconfirm\ngo ahead\nsounds good\nlooks good\nagreed\nproceed"
1216text arx_lex_c5 "deny\nreject\nno\nnope\ndecline\nwrong\nnot good\nthrow it away\nthrow away\ndiscard\ndo not like\ndo not accept"
1217text arx_lex_c6 "cancel\nstop\nabort\nnever mind\nnevermind\nforget it\ncall it off\nhalt\ndrop\nchanged my mind\nhold on"
1218text arx_lex_c7 "status\nstate\nready\nlocked\npending\nbusy\nidle\ndoing\nrunning"
1219text arx_lex_c8 "help\noption\noptions\ncommand\ncommands\ncan you do\nmenu\ncapabilities\nactions"
1220text arx_lex_c9 "evidence\naudit\ncounter\ncounters\nhow many\nso far\nlog\nstatistics\nstats"
1221text arx_lex_c10 "recall\nread back\nlast\nlatest\nnewest\nmost recent\nprevious\nbring up\nrepeat"
1222text arx_lex_c11 "facts\nremembered\nknow about me\nstored\nyou remember\ntold you"
1223text arx_lex_c12 "weather\nnews\nprice\nstock\nbitcoin\nscore\nforecast\ntraffic\nrain\ntemperature\ntime is it\nwho won\nright now"
1224text arx_lex_c13 "light\nlights\nlamp\ntv\nmusic\nplay\nsong\nthermostat\nheater\nvolume\nspeaker\ndoor\ngarage\noven\nswitch on\nturn on\nturn off"
1225text arx_lex_c14 "book\norder\nflight\npizza\nticket\nhotel\ntaxi\nbuy me\npurchase\nshop"
1226text arx_lex_c15 "hi\nhello\nhey\nthanks\nthank you\nbye\ngoodbye\ngood morning\ngood night\nhave a nice"
1227text arx_lex_c16 "this\nthat\nit\nthing\none\nsomething\nstuff\nother\nbefore\nsame\nusual"
1228
1229text arx_lex_polite "please\nkindly\nok\nokay\nnow\njust\nso\nwell\nalso\nalright\nhey\nfirst\nthen\ncan you\ncould you\nwould you\nwill you\ni want you to\ni need you to\ni would like you to\ngo ahead and"
1230text arx_lex_filler "ok okay good great fine nice cool thanks thank you hmm um oh well so alright right perfect"
1231text arx_lex_filler_comma "ok okay well so alright hmm um oh"
1232text arx_lex_vacuous "this that it thing things one something anything stuff a an the my your some to for me task note fact preference new down please again too"
1233text arx_lex_write_verbs "write draft compose generate create make produce"
1234text arx_lex_forbidden "unlock rotate update upgrade shutdown reboot restart wipe erase delete deploy format uninstall rm destroy"
1235text arx_lex_device_obj "light lights lamp lamps tv television music radio heater fan oven stove alarm speaker kettle heating ac sprinkler sprinklers"
1236text arx_lex_determiners "a an the any this that your my some its every"
1237text arx_lex_negation "not never no"
1238text arx_lex_rb_verbs "ignore disregard forget disable bypass break override skip drop remove turn"
1239text arx_lex_rb_objs "rule rules instruction instructions policy policies safety restriction restrictions limit limits guardrail guardrails allowed approval filter filters prompt"
1240text arx_lex_override "developer mode\ndev mode\nadmin mode\ngod mode\ndebug mode\nsystem override\njailbreak\nno rules\nwithout rules\nunrestricted\npretend you\npretend to be\nact as if\nany command\nwithout asking\nsudo\nroot access\nyour instructions\nsystem prompt"
1241text arx_lex_blanket "automatically always every without"
1242text arx_lex_report "says said say tells told instructs wrote written asks"
1243text arx_lex_obey "do it\ndo that\ndo what\ndo as\nfollow\nobey\ncarry out\nexecute\nrun it\nact on\ngo on"
1244text arx_lex_text_frames "remember\nmemorize\nmemorise\nstore\nsave\nnote\nkeep in mind\nkeep this\ndo not forget\nmake a note\nremind me\ntask\nnew task\nadd\nput\nto do\ntodo\nprefer\ni prefer\ni like\ni would rather\nmy preference\nwrite\ndraft\ncompose\ngenerate\nsummarize\nexplain\ndescribe\nask the model"
1245text arx_lex_and_verbs "approve deny cancel recall show list check read tell give reject accept"
1246text arx_lex_trail_mark "afterwards afterward later next"
1247text arx_lex_trail_polite "please thanks"
1248text arx_lex_ambiguous "update format restart upgrade"
1249text arx_lex_verb_leads "to you and then please now just"
1250
1251fn arx_tok_eq_slice tidx ulong s view uchar * off ulong slen ulong to bool
1252wn = get arx_tok_wlen tidx
1253if ne wn slen
1254ret false
1255end
1256base = mul tidx ARX_TOK_STRIDE
1257i = ulong 0
1258while lt i wn bound 64
1259if ne get arx_tok_word add base i get s add off i
1260ret false
1261end
1262i = add i 1
1263end
1264ret true
1265end
1266
1267fn arx_tok_eq_text tidx ulong s view uchar * to bool
1268ret arx_tok_eq_slice tidx s 0 len s
1269end
1270
1271fn arx_word_in_list tidx ulong lst view uchar * to bool
1272n = len lst
1273i = ulong 0
1274while lt i n bound 2048
1275if eq get lst i 32
1276i = add i 1
1277continue
1278end
1279ws = i
1280while lt i n bound 2048
1281if eq get lst i 32
1282break
1283end
1284i = add i 1
1285end
1286if arx_tok_eq_slice tidx lst ws sub i ws
1287ret true
1288end
1289end
1290ret false
1291end
1292
1293fn arx_match_phrase_slice pos ulong tok_end ulong lst view uchar * ps ulong pe ulong to ulong
1294wc = ulong 0
1295p = ps
1296while lt p pe bound 256
1297if eq get lst p 32
1298p = add p 1
1299continue
1300end
1301ws = p
1302while lt p pe bound 256
1303if eq get lst p 32
1304break
1305end
1306p = add p 1
1307end
1308if ge add pos wc tok_end
1309ret 0
1310end
1311if not arx_tok_eq_slice add pos wc lst ws sub p ws
1312ret 0
1313end
1314wc = add wc 1
1315end
1316ret wc
1317end
1318
1319fn arx_longest_phrase pos ulong tok_end ulong lst view uchar * to ulong
1320n = len lst
1321best = ulong 0
1322i = ulong 0
1323while lt i n bound 2048
1324ps = i
1325while lt i n bound 2048
1326if eq get lst i 10
1327break
1328end
1329i = add i 1
1330end
1331pe = i
1332if lt i n
1333i = add i 1
1334end
1335wc = arx_match_phrase_slice pos tok_end lst ps pe
1336if gt wc best
1337best = wc
1338end
1339end
1340ret best
1341end
1342
1343fn arx_skip_polite pos ulong tok_end ulong to ulong
1344cur = pos
1345while lt cur tok_end bound 256
1346m = arx_longest_phrase cur tok_end arx_lex_polite
1347if eq m 0
1348ret cur
1349end
1350if gt add cur m tok_end
1351ret cur
1352end
1353cur = add cur m
1354end
1355ret cur
1356end
1357
1358note =========================================================================
1359note Section 6: Segmentation (Clauses and Primary Request)
1360note =========================================================================
1361
1362text arx_s_then "then"
1363text arx_s_and "and"
1364text arx_s_after "after"
1365text arx_s_that "that"
1366
1367fn arx_push_clause cs ulong ce ulong to void
1368cn = get arx_cl_n 0
1369if ge cn ARX_MAX_CLAUSES
1370ret
1371end
1372put arx_cl_start cn cs
1373put arx_cl_end cn ce
1374put arx_cl_n 0 add cn 1
1375end
1376
1377fn arx_clauses to ulong
1378put arx_cl_n 0 0
1379n = get arx_tok_n 0
1380s = ulong 0
1381i = ulong 0
1382while lt i n bound 256
1383g = get arx_tok_gap i
1384if gt i s
1385if ne and g ARX_GAP_SEMI 0
1386arx_push_clause s i
1387s = i
1388i = add i 1
1389continue
1390end
1391end
1392if arx_tok_eq_text i arx_s_then
1393if gt i s
1394prev_and = false
1395if gt sub i 1 s
1396if arx_tok_eq_text sub i 1 arx_s_and
1397prev_and = true
1398end
1399end
1400has_sep = ne and g or ARX_GAP_COMMA ARX_GAP_STOP 0
1401if or prev_and has_sep
1402ce = i
1403if prev_and
1404ce = sub i 1
1405end
1406arx_push_clause s ce
1407s = add i 1
1408i = add i 1
1409continue
1410end
1411end
1412end
1413if arx_tok_eq_text i arx_s_after
1414if lt add i 1 n
1415if arx_tok_eq_text add i 1 arx_s_that
1416if gt i s
1417prev_and = false
1418if gt sub i 1 s
1419if arx_tok_eq_text sub i 1 arx_s_and
1420prev_and = true
1421end
1422end
1423has_sep = ne and g or ARX_GAP_COMMA ARX_GAP_STOP 0
1424if or prev_and has_sep
1425ce = i
1426if prev_and
1427ce = sub i 1
1428end
1429arx_push_clause s ce
1430s = add i 2
1431i = add i 2
1432continue
1433end
1434end
1435end
1436end
1437end
1438if arx_tok_eq_text i arx_s_and
1439if gt i s
1440if lt add i 1 n
1441if arx_word_in_list add i 1 arx_lex_and_verbs
1442arx_push_clause s i
1443s = add i 1
1444i = add i 1
1445continue
1446end
1447end
1448end
1449end
1450i = add i 1
1451end
1452if lt s n
1453arx_push_clause s n
1454end
1455note Strip trailing markers and drop empty clauses
1456cn = get arx_cl_n 0
1457out_n = ulong 0
1458k = ulong 0
1459while lt k cn bound 64
1460cs = get arx_cl_start k
1461ce = get arx_cl_end k
1462while gt ce cs bound 256
1463if not arx_word_in_list sub ce 1 arx_lex_trail_mark
1464break
1465end
1466ce = sub ce 1
1467end
1468if gt ce cs
1469put arx_cl_start out_n cs
1470put arx_cl_end out_n ce
1471out_n = add out_n 1
1472end
1473k = add k 1
1474end
1475put arx_cl_n 0 out_n
1476ret out_n
1477end
1478
1479fn arx_all_filler cs ulong ce ulong to bool
1480if le ce cs
1481ret false
1482end
1483i = cs
1484while lt i ce bound 256
1485if ne get arx_tok_alnum i 0
1486if not arx_word_in_list i arx_lex_filler
1487ret false
1488end
1489end
1490i = add i 1
1491end
1492ret true
1493end
1494
1495fn arx_primary to bool
1496cn = get arx_cl_n 0
1497if eq cn 0
1498ret false
1499end
1500cs = get arx_cl_start 0
1501ce = get arx_cl_end 0
1502k = ulong 0
1503while lt k cn bound 64
1504ks = get arx_cl_start k
1505ke = get arx_cl_end k
1506if not arx_all_filler ks ke
1507cs = ks
1508ce = ke
1509break
1510end
1511k = add k 1
1512end
1513note Drop leading filler sentences
1514while lt cs ce bound 64
1515j = add cs 1
1516while lt j ce bound 256
1517if ne and get arx_tok_gap j ARX_GAP_STOP 0
1518break
1519end
1520j = add j 1
1521end
1522if lt j ce
1523if arx_all_filler cs j
1524cs = j
1525continue
1526end
1527end
1528break
1529end
1530note Drop leading FILLER_COMMA before comma
1531while gt sub ce cs 1 bound 64
1532if arx_word_in_list cs arx_lex_filler_comma
1533if ne and get arx_tok_gap add cs 1 ARX_GAP_COMMA 0
1534cs = add cs 1
1535continue
1536end
1537end
1538break
1539end
1540put arx_pri_start 0 cs
1541put arx_pri_end 0 ce
1542ret true
1543end
1544
1545note =========================================================================
1546note Section 7: Policy Layer
1547note =========================================================================
1548
1549text arx_s_shut "shut"
1550text arx_s_down "down"
1551text arx_s_power "power"
1552text arx_s_off "off"
1553text arx_s_turn "turn"
1554text arx_s_switch "switch"
1555text arx_s_approve "approve"
1556text arx_s_accept "accept"
1557
1558fn arx_negated i ulong lo ulong to bool
1559from = lo
1560if ge i add lo 2
1561from = sub i 2
1562end
1563k = from
1564while lt k i bound 4
1565if arx_word_in_list k arx_lex_negation
1566ret true
1567end
1568k = add k 1
1569end
1570ret false
1571end
1572
1573fn arx_check_split_pair i ulong cl_k ulong a view uchar * b view uchar * to bool
1574cs = get arx_cl_start cl_k
1575ce = get arx_cl_end cl_k
1576if not arx_tok_eq_text i a
1577ret false
1578end
1579if arx_negated i cs
1580ret false
1581end
1582hi = add i 5
1583if gt hi ce
1584hi = ce
1585end
1586found = false
1587k = add i 1
1588while lt k hi bound 8
1589if arx_tok_eq_text k b
1590found = true
1591break
1592end
1593k = add k 1
1594end
1595if not found
1596ret false
1597end
1598j = cs
1599while lt j ce bound 256
1600if arx_word_in_list j arx_lex_device_obj
1601ret false
1602end
1603j = add j 1
1604end
1605ret true
1606end
1607
1608fn arx_policy_check_tok i ulong k ulong lead ulong to uchar
1609cs = get arx_cl_start k
1610ce = get arx_cl_end k
1611n = get arx_tok_n 0
1612if arx_word_in_list i arx_lex_rb_verbs
1613if not arx_negated i cs
1614hi = add i 4
1615if gt hi ce
1616hi = ce
1617end
1618j = add i 1
1619while lt j hi bound 8
1620if arx_word_in_list j arx_lex_rb_objs
1621ret ARX_REFUSE_RULE_BREAK
1622end
1623j = add j 1
1624end
1625end
1626end
1627if gt arx_longest_phrase i n arx_lex_override 0
1628ret ARX_REFUSE_OVERRIDE
1629end
1630if arx_word_in_list i arx_lex_forbidden
1631if not arx_negated i cs
1632noun = false
1633if gt i cs
1634if arx_word_in_list sub i 1 arx_lex_determiners
1635noun = true
1636end
1637end
1638amb = arx_word_in_list i arx_lex_ambiguous
1639vpos = eq i lead
1640if gt i cs
1641if arx_word_in_list sub i 1 arx_lex_verb_leads
1642vpos = true
1643end
1644end
1645if not noun
1646if or not amb vpos
1647ret ARX_REFUSE_FORBIDDEN
1648end
1649end
1650end
1651end
1652if arx_check_split_pair i k arx_s_shut arx_s_down
1653ret ARX_REFUSE_FORBIDDEN
1654end
1655if arx_check_split_pair i k arx_s_power arx_s_off
1656ret ARX_REFUSE_FORBIDDEN
1657end
1658if arx_check_split_pair i k arx_s_power arx_s_down
1659ret ARX_REFUSE_FORBIDDEN
1660end
1661if arx_check_split_pair i k arx_s_turn arx_s_off
1662ret ARX_REFUSE_FORBIDDEN
1663end
1664if arx_check_split_pair i k arx_s_switch arx_s_off
1665ret ARX_REFUSE_FORBIDDEN
1666end
1667if or arx_tok_eq_text i arx_s_approve arx_tok_eq_text i arx_s_accept
1668j = cs
1669while lt j ce bound 256
1670if arx_word_in_list j arx_lex_blanket
1671ret ARX_REFUSE_BLANKET
1672end
1673j = add j 1
1674end
1675end
1676ret ARX_REFUSE_NONE
1677end
1678
1679fn arx_policy_check to uchar
1680put arx_pol_rule 0 ARX_REFUSE_NONE
1681put arx_pol_tok 0 0
1682n = get arx_tok_n 0
1683i = ulong 0
1684while lt i n bound 256
1685put arx_pol_exempt i 0
1686i = add i 1
1687end
1688cn = get arx_cl_n 0
1689k = ulong 0
1690while lt k cn bound 64
1691cs = get arx_cl_start k
1692ce = get arx_cl_end k
1693lead = arx_skip_polite cs ce
1694f = arx_longest_phrase lead ce arx_lex_text_frames
1695if gt f 0
1696j = add lead f
1697while lt j ce bound 256
1698put arx_pol_exempt j 1
1699j = add j 1
1700end
1701end
1702k = add k 1
1703end
1704has_rep = false
1705rep_at = ulong 0
1706k = ulong 0
1707while lt k cn bound 64
1708cs = get arx_cl_start k
1709ce = get arx_cl_end k
1710lead = arx_skip_polite cs ce
1711i = cs
1712while lt i ce bound 256
1713if eq get arx_pol_exempt i 0
1714r = arx_policy_check_tok i k lead
1715if ne r ARX_REFUSE_NONE
1716put arx_pol_rule 0 r
1717put arx_pol_tok 0 i
1718ret r
1719end
1720if not has_rep
1721if arx_word_in_list i arx_lex_report
1722has_rep = true
1723rep_at = i
1724end
1725end
1726end
1727i = add i 1
1728end
1729k = add k 1
1730end
1731if has_rep
1732i = add rep_at 1
1733while lt i n bound 256
1734if eq get arx_pol_exempt i 0
1735if gt arx_longest_phrase i n arx_lex_obey 0
1736put arx_pol_rule 0 ARX_REFUSE_DATA_ORDER
1737put arx_pol_tok 0 i
1738ret ARX_REFUSE_DATA_ORDER
1739end
1740end
1741i = add i 1
1742end
1743end
1744ret ARX_REFUSE_NONE
1745end
1746
1747note =========================================================================
1748note Section 8: Feature Extraction
1749note =========================================================================
1750
1751fn arx_feat_key idx ulong to ulong
1752b = new uchar 40 0
1753put b 0 get arx_ft_ch idx
1754fl = ulong get arx_ft_len idx
1755base = mul idx 4
1756k = ulong 0
1757while lt k fl bound 4
1758off = add 1 mul k 9
1759put b off get arx_ft_fam add base k
1760v = get arx_ft_val add base k
1761put b add off 1 uchar and v 255
1762put b add off 2 uchar and shr v 8 255
1763put b add off 3 uchar and shr v 16 255
1764put b add off 4 uchar and shr v 24 255
1765put b add off 5 uchar and shr v 32 255
1766put b add off 6 uchar and shr v 40 255
1767put b add off 7 uchar and shr v 48 255
1768put b add off 8 uchar and shr v 56 255
1769k = add k 1
1770end
1771ret arx_fnv part b 0 add 1 mul fl 9
1772end
1773
1774fn arx_rest_content cs ulong ce ulong to ulong
1775lead = arx_skip_polite cs ce
1776f = arx_longest_phrase lead ce arx_lex_text_frames
1777st = add lead f
1778if gt st ce
1779st = ce
1780end
1781cnt = ulong 0
1782i = st
1783while lt i ce bound 256
1784if ne get arx_tok_alnum i 0
1785if not arx_word_in_list i arx_lex_vacuous
1786if not arx_word_in_list i arx_lex_filler
1787cnt = add cnt 1
1788end
1789end
1790end
1791i = add i 1
1792end
1793ret cnt
1794end
1795
1796fn arx_concepts_at i ulong ce ulong to uint
1797m = uint 0
1798if gt arx_longest_phrase i ce arx_lex_c0 0
1799m = or m shl uint 1 0
1800end
1801if gt arx_longest_phrase i ce arx_lex_c1 0
1802m = or m shl uint 1 1
1803end
1804if gt arx_longest_phrase i ce arx_lex_c2 0
1805m = or m shl uint 1 2
1806end
1807if gt arx_longest_phrase i ce arx_lex_c3 0
1808m = or m shl uint 1 3
1809end
1810if gt arx_longest_phrase i ce arx_lex_c4 0
1811m = or m shl uint 1 4
1812end
1813if gt arx_longest_phrase i ce arx_lex_c5 0
1814m = or m shl uint 1 5
1815end
1816if gt arx_longest_phrase i ce arx_lex_c6 0
1817m = or m shl uint 1 6
1818end
1819if gt arx_longest_phrase i ce arx_lex_c7 0
1820m = or m shl uint 1 7
1821end
1822if gt arx_longest_phrase i ce arx_lex_c8 0
1823m = or m shl uint 1 8
1824end
1825if gt arx_longest_phrase i ce arx_lex_c9 0
1826m = or m shl uint 1 9
1827end
1828if gt arx_longest_phrase i ce arx_lex_c10 0
1829m = or m shl uint 1 10
1830end
1831if gt arx_longest_phrase i ce arx_lex_c11 0
1832m = or m shl uint 1 11
1833end
1834if gt arx_longest_phrase i ce arx_lex_c12 0
1835m = or m shl uint 1 12
1836end
1837if gt arx_longest_phrase i ce arx_lex_c13 0
1838m = or m shl uint 1 13
1839end
1840if gt arx_longest_phrase i ce arx_lex_c14 0
1841m = or m shl uint 1 14
1842end
1843if gt arx_longest_phrase i ce arx_lex_c15 0
1844m = or m shl uint 1 15
1845end
1846if gt arx_longest_phrase i ce arx_lex_c16 0
1847m = or m shl uint 1 16
1848end
1849ret m
1850end
1851
1852fn arx_concepts cs ulong ce ulong to uint
1853m = uint 0
1854i = cs
1855while lt i ce bound 256
1856m = or m arx_concepts_at i ce
1857i = add i 1
1858end
1859ret m
1860end
1861
1862fn arx_ft_push1 ch ulong f0 uchar v0 ulong to void
1863wt = get arx_cfg_weights ch
1864if eq wt 0
1865ret
1866end
1867idx = get arx_ft_n 0
1868if ge idx ARX_MAX_FEATS
1869ret
1870end
1871b = mul idx 4
1872put arx_ft_fam b f0
1873put arx_ft_val b v0
1874put arx_ft_len idx 1
1875put arx_ft_ch idx uchar ch
1876put arx_ft_w idx wt
1877put arx_ft_n 0 add idx 1
1878end
1879
1880fn arx_ft_push2 ch ulong f0 uchar v0 ulong f1 uchar v1 ulong to void
1881wt = get arx_cfg_weights ch
1882if eq wt 0
1883ret
1884end
1885idx = get arx_ft_n 0
1886if ge idx ARX_MAX_FEATS
1887ret
1888end
1889b = mul idx 4
1890put arx_ft_fam b f0
1891put arx_ft_val b v0
1892put arx_ft_fam add b 1 f1
1893put arx_ft_val add b 1 v1
1894put arx_ft_len idx 2
1895put arx_ft_ch idx uchar ch
1896put arx_ft_w idx wt
1897put arx_ft_n 0 add idx 1
1898end
1899
1900fn arx_ft_push_chars buf view uchar * pos ulong g ulong to void
1901wt = get arx_cfg_weights ARX_CH_CHARS
1902if eq wt 0
1903ret
1904end
1905idx = get arx_ft_n 0
1906if ge idx ARX_MAX_FEATS
1907ret
1908end
1909b = mul idx 4
1910k = ulong 0
1911while lt k g bound 4
1912put arx_ft_fam add b k ARX_SYM_CHAR
1913put arx_ft_val add b k ulong get buf add pos k
1914k = add k 1
1915end
1916put arx_ft_len idx uchar g
1917put arx_ft_ch idx uchar ARX_CH_CHARS
1918put arx_ft_w idx wt
1919put arx_ft_n 0 add idx 1
1920end
1921
1922fn arx_extract_char_ngrams tidx ulong to void
1923buf = new uchar 34 0
1924wn = get arx_tok_wlen tidx
1925if gt wn 32
1926wn = 32
1927end
1928put buf 0 uchar 94
1929base = mul tidx ARX_TOK_STRIDE
1930k = ulong 0
1931while lt k wn bound 32
1932put buf add k 1 get arx_tok_word add base k
1933k = add k 1
1934end
1935put buf add wn 1 uchar 36
1936sn = add wn 2
1937if ge sn 3
1938lim3 = sub sn 2
1939i = ulong 0
1940while lt i lim3 bound 34
1941arx_ft_push_chars buf i 3
1942i = add i 1
1943end
1944end
1945if ge sn 4
1946lim4 = sub sn 3
1947i = ulong 0
1948while lt i lim4 bound 34
1949arx_ft_push_chars buf i 4
1950i = add i 1
1951end
1952end
1953end
1954
1955fn arx_ef_concepts_lead cs ulong ce ulong to void
1956pend = ulong get arx_dlg_pending 0
1957cm = arx_concepts cs ce
1958k = ulong 0
1959while lt k ARX_N_CONCEPTS bound 18
1960if ne and cm shl uint 1 uint k 0
1961arx_ft_push1 ARX_CH_CONCEPT ARX_SYM_CONCEPT k
1962arx_ft_push2 ARX_CH_CTXCONJ ARX_SYM_CTX pend ARX_SYM_CONCEPT k
1963end
1964k = add k 1
1965end
1966lead = arx_skip_polite cs ce
1967if lt lead ce
1968hl = get arx_tok_shash lead
1969arx_ft_push1 ARX_CH_LEAD ARX_SYM_LEAD hl
1970arx_ft_push2 ARX_CH_CTXCONJ ARX_SYM_CTX pend ARX_SYM_LEAD hl
1971if lt add lead 1 ce
1972h2 = get arx_tok_shash add lead 1
1973arx_ft_push2 ARX_CH_LEAD ARX_SYM_LEAD hl ARX_SYM_WORD h2
1974end
1975end
1976arx_ft_push1 ARX_CH_CTX ARX_SYM_CTX pend
1977prev_a = get arx_dlg_prev 0
1978prev_v = ulong 0
1979if ne prev_a ARX_ACT_INVALID
1980prev_v = add ulong prev_a 1
1981end
1982arx_ft_push1 ARX_CH_CTX ARX_SYM_CTX add 16 prev_v
1983end
1984
1985fn arx_ef_ctx_shape cs ulong ce ulong wn ulong ends_q bool to void
1986arx_ef_concepts_lead cs ce
1987lb = ulong 4
1988if le wn 1
1989lb = 0
1990else
1991if eq wn 2
1992lb = 1
1993else
1994if le wn 4
1995lb = 2
1996else
1997if le wn 8
1998lb = 3
1999end
2000end
2001end
2002end
2003arx_ft_push1 ARX_CH_SHAPE ARX_SYM_SHAPE lb
2004has_q = ends_q
2005if not has_q
2006j = add cs 1
2007while lt j ce bound 256
2008if ne and get arx_tok_gap j ARX_GAP_QUESTION 0
2009has_q = true
2010break
2011end
2012j = add j 1
2013end
2014end
2015if has_q
2016arx_ft_push1 ARX_CH_SHAPE ARX_SYM_SHAPE 10
2017end
2018rc = arx_rest_content cs ce
2019rv = ulong 22
2020if eq rc 0
2021rv = 20
2022else
2023if le rc 2
2024rv = 21
2025end
2026end
2027arx_ft_push1 ARX_CH_SHAPE ARX_SYM_SHAPE rv
2028end
2029
2030fn arx_extract_features cs ulong ce ulong ends_q bool to ulong
2031words = new ulong 256 0
2032seen = new ulong 256 0
2033put arx_ft_n 0 0
2034wn = ulong 0
2035i = cs
2036while lt i ce bound 256
2037if ne get arx_tok_alnum i 0
2038put words wn i
2039wn = add wn 1
2040end
2041i = add i 1
2042end
2043sn = ulong 0
2044i = ulong 0
2045while lt i wn bound 256
2046h = get arx_tok_shash get words i
2047dup = false
2048j = ulong 0
2049while lt j sn bound 256
2050if eq get seen j h
2051dup = true
2052break
2053end
2054j = add j 1
2055end
2056if not dup
2057put seen sn h
2058sn = add sn 1
2059arx_ft_push1 ARX_CH_WORD ARX_SYM_WORD h
2060end
2061i = add i 1
2062end
2063if ge wn 2
2064lim = sub wn 1
2065i = ulong 0
2066while lt i lim bound 256
2067ha = get arx_tok_shash get words i
2068hb = get arx_tok_shash get words add i 1
2069arx_ft_push2 ARX_CH_BIGRAM ARX_SYM_WORD ha ARX_SYM_WORD hb
2070i = add i 1
2071end
2072end
2073if ne get arx_cfg_weights ARX_CH_CHARS 0
2074i = ulong 0
2075while lt i wn bound 256
2076arx_extract_char_ngrams get words i
2077i = add i 1
2078end
2079end
2080arx_ef_ctx_shape cs ce wn ends_q
2081ret get arx_ft_n 0
2082end
2083
2084note =========================================================================
2085note Section 9: Binary Hypervectors
2086note =========================================================================
2087
2088fn arx_splitmix_next s ulong to ulong
2089z = s
2090z = mul xor z shr z 30 ulong 0xbf58476d1ce4e5b9
2091z = mul xor z shr z 27 ulong 0x94d049bb133111eb
2092ret xor z shr z 31
2093end
2094
2095fn arx_popcnt64 x ulong to uint
2096v = sub x and shr x 1 ulong 0x5555555555555555
2097v = add and v ulong 0x3333333333333333 and shr v 2 ulong 0x3333333333333333
2098v = and add v shr v 4 ulong 0x0f0f0f0f0f0f0f0f
2099ret uint shr mul v ulong 0x0101010101010101 56
2100end
2101
2102fn arx_hv_item fam ulong val ulong dst edit ulong * to void
2103s = xor xor ARX_SEED mul fam ulong 0xd6e8feb86659fd93 val
2104s = add s ulong 0x9e3779b97f4a7c15
2105w = len dst
2106i = ulong 0
2107while lt i w bound 128
2108s = add s ulong 0x9e3779b97f4a7c15
2109put dst i arx_splitmix_next s
2110i = add i 1
2111end
2112end
2113
2114fn arx_hv_bind_sym fam ulong val ulong rot ulong w ulong to void
2115tmp = new ulong 128 0
2116arx_hv_item fam val part tmp 0 w
2117x = ulong 0
2118while lt x w bound 128
2119dst_x = add x rot
2120if ge dst_x w
2121dst_x = sub dst_x w
2122end
2123xv = xor get arx_hv_acc dst_x get tmp x
2124put arx_hv_acc dst_x xv
2125x = add x 1
2126end
2127end
2128
2129fn arx_hv_bind_feat fidx ulong w ulong to void
2130x = ulong 0
2131while lt x w bound 128
2132put arx_hv_acc x 0
2133x = add x 1
2134end
2135fl = ulong get arx_ft_len fidx
2136base = mul fidx 4
2137j = ulong 0
2138while lt j fl bound 4
2139idx = add base j
2140fam = ulong get arx_ft_fam idx
2141val = get arx_ft_val idx
2142rot = rem sub sub fl 1 j w
2143arx_hv_bind_sym fam val rot w
2144j = add j 1
2145end
2146end
2147
2148fn arx_hv_accum_feat f ulong w ulong to void
2149arx_hv_bind_feat f w
2150wt = int get arx_ft_w f
2151x = ulong 0
2152while lt x w bound 128
2153v = get arx_hv_acc x
2154base = mul x 64
2155b = ulong 0
2156while lt b 64 bound 64
2157bit = int and shr v b 1
2158delta = mul sub mul 2 bit 1 wt
2159ci = add base b
2160nv = add get arx_hv_cnt ci delta
2161put arx_hv_cnt ci nv
2162b = add b 1
2163end
2164x = add x 1
2165end
2166end
2167
2168fn arx_hv_finalize_q w ulong to void
2169tmp = new ulong 128 0
2170arx_hv_item 7 0 part tmp 0 w
2171x = ulong 0
2172while lt x w bound 128
2173o = ulong 0
2174tv = get tmp x
2175base = mul x 64
2176b = ulong 0
2177while lt b 64 bound 64
2178c = get arx_hv_cnt add base b
2179bit = ulong 0
2180if gt c 0
2181bit = 1
2182else
2183if eq c 0
2184bit = and shr tv b 1
2185end
2186end
2187o = or o shl bit b
2188b = add b 1
2189end
2190put arx_hv_q x o
2191x = add x 1
2192end
2193end
2194
2195fn arx_hv_encode_to_q w ulong to void
2196d = mul w 64
2197i = ulong 0
2198while lt i d bound 8192
2199put arx_hv_cnt i 0
2200i = add i 1
2201end
2202fn_cnt = get arx_ft_n 0
2203f = ulong 0
2204while lt f fn_cnt bound 768
2205arx_hv_accum_feat f w
2206f = add f 1
2207end
2208arx_hv_finalize_q w
2209end
2210
2211fn arx_hv_sim_q_ex ex_idx ulong w ulong to f32
2212ham = uint 0
2213base = mul ex_idx w
2214x = ulong 0
2215while lt x w bound 128
2216va = get arx_hv_q x
2217vb = get arx_hv_hvs add base x
2218pc = arx_popcnt64 xor va vb
2219ham = add ham pc
2220x = add x 1
2221end
2222d = f32 mul w 64
2223ret sub 1.0f32 div mul 2.0f32 f32 ham d
2224end
2225
2226fn arx_hv_sim_ex_ex a ulong b ulong w ulong to f32
2227ham = uint 0
2228ba = mul a w
2229bb = mul b w
2230x = ulong 0
2231while lt x w bound 128
2232va = get arx_hv_hvs add ba x
2233vb = get arx_hv_hvs add bb x
2234pc = arx_popcnt64 xor va vb
2235ham = add ham pc
2236x = add x 1
2237end
2238d = f32 mul w 64
2239ret sub 1.0f32 div mul 2.0f32 f32 ham d
2240end
2241
2242note =========================================================================
2243note Section 10: Span Extraction and CueMemory
2244note =========================================================================
2245
2246text arx_s_thank "thank"
2247text arx_s_you "you"
2248
2249fn arx_cue_reset to void
2250put arx_cue_pre_n 0 0
2251put arx_cue_suf_n 0 0
2252put arx_cue_st_n 0 0
2253end
2254
2255fn arx_cue_pre_same idx ulong s ulong clen ulong to bool
2256base = mul idx ARX_MAX_CUE_WORDS
2257k = ulong 0
2258while lt k clen bound 8
2259ha = get arx_cue_pre_h add base k
2260hb = get arx_tok_whash add s k
2261if ne ha hb
2262ret false
2263end
2264k = add k 1
2265end
2266ret true
2267end
2268
2269fn arx_cue_add_pre a uchar s ulong e ulong to void
2270clen = sub e s
2271if gt clen ARX_MAX_CUE_WORDS
2272ret
2273end
2274if ge add get arx_cue_pre_n 0 get arx_cue_suf_n 0 ARX_MAX_CUES
2275ret
2276end
2277pn = get arx_cue_pre_n 0
2278i = ulong 0
2279while lt i pn bound 256
2280if eq get arx_cue_pre_act i a
2281if eq ulong get arx_cue_pre_len i clen
2282if arx_cue_pre_same i s clen
2283ret
2284end
2285end
2286end
2287i = add i 1
2288end
2289put arx_cue_pre_act pn a
2290put arx_cue_pre_len pn uchar clen
2291base = mul pn ARX_MAX_CUE_WORDS
2292k = ulong 0
2293while lt k clen bound 8
2294hv = get arx_tok_whash add s k
2295put arx_cue_pre_h add base k hv
2296k = add k 1
2297end
2298put arx_cue_pre_n 0 add pn 1
2299end
2300
2301fn arx_cue_suf_same idx ulong s ulong clen ulong to bool
2302base = mul idx ARX_MAX_CUE_WORDS
2303k = ulong 0
2304while lt k clen bound 8
2305ha = get arx_cue_suf_h add base k
2306hb = get arx_tok_whash add s k
2307if ne ha hb
2308ret false
2309end
2310k = add k 1
2311end
2312ret true
2313end
2314
2315fn arx_cue_add_suf a uchar s ulong e ulong to void
2316clen = sub e s
2317if gt clen ARX_MAX_CUE_WORDS
2318ret
2319end
2320if ge add get arx_cue_pre_n 0 get arx_cue_suf_n 0 ARX_MAX_CUES
2321ret
2322end
2323sn = get arx_cue_suf_n 0
2324i = ulong 0
2325while lt i sn bound 256
2326if eq get arx_cue_suf_act i a
2327if eq ulong get arx_cue_suf_len i clen
2328if arx_cue_suf_same i s clen
2329ret
2330end
2331end
2332end
2333i = add i 1
2334end
2335put arx_cue_suf_act sn a
2336put arx_cue_suf_len sn uchar clen
2337base = mul sn ARX_MAX_CUE_WORDS
2338k = ulong 0
2339while lt k clen bound 8
2340hv = get arx_tok_whash add s k
2341put arx_cue_suf_h add base k hv
2342k = add k 1
2343end
2344put arx_cue_suf_n 0 add sn 1
2345end
2346
2347fn arx_cue_bump a uchar h ulong is_cue bool to void
2348sn = get arx_cue_st_n 0
2349i = ulong 0
2350while lt i sn bound 2048
2351if eq get arx_cue_st_act i a
2352if eq get arx_cue_st_h i h
2353if is_cue
2354v = get arx_cue_st_c i
2355if lt v 65535
2356put arx_cue_st_c i add v 1
2357end
2358else
2359v = get arx_cue_st_p i
2360if lt v 65535
2361put arx_cue_st_p i add v 1
2362end
2363end
2364ret
2365end
2366end
2367i = add i 1
2368end
2369if ge sn ARX_MAX_STATS
2370ret
2371end
2372put arx_cue_st_act sn a
2373put arx_cue_st_h sn h
2374if is_cue
2375put arx_cue_st_c sn 1
2376put arx_cue_st_p sn 0
2377else
2378put arx_cue_st_c sn 0
2379put arx_cue_st_p sn 1
2380end
2381put arx_cue_st_n 0 add sn 1
2382end
2383
2384note Returns 0=unknown, 1=false (content), 2=true (cue)
2385fn arx_cue_like a uchar h ulong to uchar
2386sn = get arx_cue_st_n 0
2387i = ulong 0
2388while lt i sn bound 2048
2389if eq get arx_cue_st_act i a
2390if eq get arx_cue_st_h i h
2391if gt get arx_cue_st_c i get arx_cue_st_p i
2392ret 2
2393end
2394ret 1
2395end
2396end
2397i = add i 1
2398end
2399ret 0
2400end
2401
2402fn arx_cue_learn cs ulong ce ulong act uchar bs ulong be ulong to bool
2403lead = arx_skip_polite cs ce
2404has_s = false
2405s0 = ulong 0
2406i = cs
2407while lt i ce bound 256
2408if ge get arx_tok_start i bs
2409has_s = true
2410s0 = i
2411break
2412end
2413i = add i 1
2414end
2415has_e = false
2416e0 = ulong 0
2417if gt ce cs
2418i = sub ce 1
2419while ge i cs bound 256
2420if le get arx_tok_end i be
2421has_e = true
2422e0 = i
2423break
2424end
2425if eq i cs
2426break
2427end
2428i = sub i 1
2429end
2430end
2431if not and has_s has_e
2432ret false
2433end
2434if gt s0 e0
2435ret false
2436end
2437if lt s0 lead
2438ret false
2439end
2440e1 = add e0 1
2441arx_cue_add_pre act lead s0
2442if lt e1 ce
2443arx_cue_add_suf act e1 ce
2444end
2445i = lead
2446while lt i s0 bound 256
2447arx_cue_bump act get arx_tok_whash i true
2448i = add i 1
2449end
2450i = s0
2451while lt i e1 bound 256
2452arx_cue_bump act get arx_tok_whash i false
2453i = add i 1
2454end
2455ret true
2456end
2457
2458fn arx_cue_match_pre_at ci ulong p ulong j ulong m ulong to bool
2459base = mul ci ARX_MAX_CUE_WORDS
2460k = ulong 0
2461while lt k m bound 8
2462if ne get arx_tok_whash add p k get arx_cue_pre_h add base add j k
2463ret false
2464end
2465k = add k 1
2466end
2467ret true
2468end
2469
2470fn arx_cue_best_pre_for ci ulong p ulong lead ulong ce ulong best ulong to ulong
2471clen = ulong get arx_cue_pre_len ci
2472bm = best
2473j = ulong 0
2474while lt j clen bound 8
2475m = sub clen j
2476skip = or le m bm ge add p m ce
2477if not and eq p lead eq j 0
2478if or lt m 2 eq p lead
2479skip = true
2480end
2481end
2482if not skip
2483if arx_cue_match_pre_at ci p j m
2484bm = m
2485end
2486end
2487j = add j 1
2488end
2489ret bm
2490end
2491
2492note Returns 0 if no prefix cue matched, or (sv + 1) if matched.
2493fn arx_cue_best_pre lead ulong ce ulong act uchar to ulong
2494res = ulong 0
2495best = ulong 0
2496p_hi = add lead 3
2497if gt p_hi ce
2498p_hi = ce
2499end
2500pn = get arx_cue_pre_n 0
2501p = lead
2502while lt p p_hi bound 4
2503ci = ulong 0
2504while lt ci pn bound 256
2505if eq get arx_cue_pre_act ci act
2506if gt get arx_cue_pre_len ci 0
2507nm = arx_cue_best_pre_for ci p lead ce best
2508if gt nm best
2509best = nm
2510res = add add p nm 1
2511end
2512end
2513end
2514ci = add ci 1
2515end
2516p = add p 1
2517end
2518ret res
2519end
2520
2521fn arx_cue_colon_dash lead ulong ce ulong act uchar pr ulong to ulong
2522from = add lead 1
2523chk_s = lead
2524if gt pr 0
2525from = sub pr 1
2526chk_s = from
2527end
2528hi = add lead 8
2529if gt hi ce
2530hi = ce
2531end
2532j = from
2533while lt j hi bound 16
2534if ne and get arx_tok_gap j or ARX_GAP_COLON ARX_GAP_DASH 0
2535ok = true
2536t = chk_s
2537while lt t j bound 16
2538if eq arx_cue_like act get arx_tok_whash t 1
2539ok = false
2540break
2541end
2542t = add t 1
2543end
2544if ok
2545ret add j 1
2546end
2547ret pr
2548end
2549j = add j 1
2550end
2551ret pr
2552end
2553
2554fn arx_cue_bounds_start cs ulong ce ulong act uchar to ulong
2555lead = arx_skip_polite cs ce
2556pr = arx_cue_best_pre lead ce act
2557pr = arx_cue_colon_dash lead ce act pr
2558v = lead
2559if gt pr 0
2560v = sub pr 1
2561else
2562if ne act ARX_ACT_PROMPT
2563f = arx_longest_phrase v ce arx_lex_text_frames
2564if lt add v f ce
2565v = add v f
2566end
2567end
2568end
2569while lt add v 1 ce bound 16
2570if ge v add lead 6
2571break
2572end
2573if ne arx_cue_like act get arx_tok_whash v 2
2574break
2575end
2576v = add v 1
2577end
2578if gt v ce
2579v = ce
2580end
2581ret v
2582end
2583
2584fn arx_cue_bounds_end s ulong ce ulong act uchar to ulong
2585e = ce
2586bests = ulong 0
2587sn = get arx_cue_suf_n 0
2588ci = ulong 0
2589while lt ci sn bound 256
2590clen = ulong get arx_cue_suf_len ci
2591if eq get arx_cue_suf_act ci act
2592if and gt clen bests lt add s clen e
2593if arx_cue_suf_same ci sub e clen clen
2594bests = clen
2595end
2596end
2597end
2598ci = add ci 1
2599end
2600e = sub e bests
2601while gt e add s 1 bound 32
2602if not arx_word_in_list sub e 1 arx_lex_trail_polite
2603break
2604end
2605e = sub e 1
2606end
2607if gt e add s 2
2608if arx_tok_eq_text sub e 2 arx_s_thank
2609if arx_tok_eq_text sub e 1 arx_s_you
2610e = sub e 2
2611end
2612end
2613end
2614ret e
2615end
2616
2617fn arx_cue_copy_span msg view uchar * bs ulong be ulong to uchar
2618j = ulong 0
2619k = bs
2620while lt k be bound 4096
2621b = get msg k
2622curly = false
2623if eq b 0xe2
2624if lt add k 2 be
2625if eq get msg add k 1 0x80
2626b2 = get msg add k 2
2627if or eq b2 0x99 eq b2 0x98
2628curly = true
2629end
2630end
2631end
2632end
2633ch = b
2634if curly
2635ch = 39
2636k = add k 3
2637else
2638k = add k 1
2639end
2640if ge j ARX_MAX_TEXT
2641ret ARX_SPAN_TOO_LONG
2642end
2643if or lt ch 0x20 gt ch 0x7e
2644ret ARX_SPAN_UNREP
2645end
2646if or eq ch 34 eq ch 92
2647ret ARX_SPAN_UNREP
2648end
2649put arx_dec_txt j ch
2650j = add j 1
2651end
2652put arx_dec_txt_len 0 j
2653put arx_exp_span_bs 0 bs
2654put arx_exp_span_be 0 be
2655ret ARX_SPAN_OK
2656end
2657
2658fn arx_cue_balance_be msg view uchar * bs ulong be ulong to ulong
2659opens = ulong 0
2660closes = ulong 0
2661k = bs
2662while lt k be bound 4096
2663b = get msg k
2664if eq b 40
2665opens = add opens 1
2666end
2667if eq b 41
2668closes = add closes 1
2669end
2670k = add k 1
2671end
2672if gt opens closes
2673if lt be len msg
2674if eq get msg be 41
2675ret add be 1
2676end
2677end
2678end
2679ret be
2680end
2681
2682fn arx_cue_extract msg view uchar * cs ulong ce ulong act uchar to uchar
2683s = arx_cue_bounds_start cs ce act
2684e = arx_cue_bounds_end s ce act
2685if ge s e
2686ret ARX_SPAN_EMPTY
2687end
2688has_c = false
2689i = s
2690while lt i e bound 256
2691if ne get arx_tok_alnum i 0
2692if not arx_word_in_list i arx_lex_vacuous
2693skip_w = false
2694if eq act ARX_ACT_PROMPT
2695if arx_word_in_list i arx_lex_write_verbs
2696skip_w = true
2697end
2698end
2699if not skip_w
2700has_c = true
2701break
2702end
2703end
2704end
2705i = add i 1
2706end
2707if not has_c
2708ret ARX_SPAN_EMPTY
2709end
2710bs = get arx_tok_start s
2711be = arx_cue_balance_be msg bs get arx_tok_end sub e 1
2712ret arx_cue_copy_span msg bs be
2713end
2714
2715note =========================================================================
2716note Section 11: Scorer Models (HvModel, NearestModel, PerceptronModel)
2717note =========================================================================
2718
2719fn arx_argmax s view f32 * to ulong
2720best = ulong 0
2721bv = get s 0
2722l = ulong 1
2723while lt l ARX_N_LABELS bound 15
2724v = get s l
2725if gt v bv
2726bv = v
2727best = l
2728end
2729l = add l 1
2730end
2731ret best
2732end
2733
2734fn arx_ins_top top edit f32 * off ulong kk ulong s f32 to void
2735if eq kk 0
2736ret
2737end
2738last = sub kk 1
2739if le s get top add off last
2740ret
2741end
2742p = last
2743put top add off p s
2744while gt p 0 bound 8
2745cur = get top add off p
2746prv = get top add off sub p 1
2747if le cur prv
2748break
2749end
2750put top add off p prv
2751put top add off sub p 1 cur
2752p = sub p 1
2753end
2754end
2755
2756fn arx_ins_glob gs edit f32 8 gi edit ulong 8 ka ulong s f32 idx ulong to void
2757if eq ka 0
2758ret
2759end
2760last = sub ka 1
2761if le s get gs last
2762ret
2763end
2764p = last
2765put gs p s
2766put gi p idx
2767while gt p 0 bound 8
2768cur = get gs p
2769prv = get gs sub p 1
2770if le cur prv
2771break
2772end
2773ci = get gi p
2774pi = get gi sub p 1
2775put gs p prv
2776put gi p pi
2777put gs sub p 1 cur
2778put gi sub p 1 ci
2779p = sub p 1
2780end
2781end
2782
2783note --- HvModel ---
2784
2785fn arx_hv_reset to void
2786put arx_hv_n 0 0
2787i = ulong 0
2788while lt i ARX_N_LABELS bound 15
2789put arx_hv_nnz i 0
2790put arx_hv_nlabel i 0
2791i = add i 1
2792end
2793end
2794
2795fn arx_hv_push_q lbl uchar note_s view uchar * to long
2796n = get arx_hv_n 0
2797if ge n get arx_cfg_max_ex 0
2798ret -1
2799end
2800if ge n ARX_MAX_EXEMPLARS
2801ret -1
2802end
2803w = get arx_cfg_hv_words 0
2804base = mul n w
2805x = ulong 0
2806while lt x w bound 128
2807put arx_hv_hvs add base x get arx_hv_q x
2808x = add x 1
2809end
2810put arx_hv_labels n lbl
2811nlen = len note_s
2812if gt nlen ARX_NOTE_BYTES
2813nlen = ARX_NOTE_BYTES
2814end
2815nbase = mul n ARX_NOTE_BYTES
2816k = ulong 0
2817while lt k nlen bound 96
2818put arx_hv_notes add nbase k get note_s k
2819k = add k 1
2820end
2821put arx_hv_nlen n nlen
2822li = ulong lbl
2823put arx_hv_nlabel li add get arx_hv_nlabel li 1
2824put arx_hv_n 0 add n 1
2825ret long n
2826end
2827
2828fn arx_hv_add_word bbase ulong v ulong wt int to void
2829b = ulong 0
2830while lt b 64 bound 64
2831bit = int and shr v b 1
2832delta = mul sub mul 2 bit 1 wt
2833ci = add bbase b
2834nv = add get arx_hv_counts ci delta
2835put arx_hv_counts ci nv
2836b = add b 1
2837end
2838end
2839
2840fn arx_hv_add_counts l ulong ex_idx ulong wt int to void
2841w = get arx_cfg_hv_words 0
2842cbase = mul l mul w 64
2843hbase = mul ex_idx w
2844x = ulong 0
2845while lt x w bound 128
2846arx_hv_add_word add cbase mul x 64 get arx_hv_hvs add hbase x wt
2847x = add x 1
2848end
2849end
2850
2851fn arx_hv_calc_band l ulong to f32
2852d = mul get arx_cfg_hv_words 0 64
2853cbase = mul l d
2854sum = 0.0f32
2855i = ulong 0
2856while lt i d bound 8192
2857sum = add sum f32 get arx_hv_counts add cbase i
2858i = add i 1
2859end
2860mean = div sum f32 d
2861vsum = 0.0f32
2862i = ulong 0
2863while lt i d bound 8192
2864t = sub f32 get arx_hv_counts add cbase i mean
2865vsum = add vsum mul t t
2866i = add i 1
2867end
2868ret mul get arx_cfg_hv_zband 0 fsqrt div vsum f32 d
2869end
2870
2871fn arx_hv_quantize_word bbase ulong sidx ulong band f32 to uint
2872sv = ulong 0
2873mv = ulong 0
2874b = ulong 0
2875while lt b 64 bound 64
2876v = f32 get arx_hv_counts add bbase b
2877if gt v 0.0f32
2878sv = or sv shl ulong 1 b
2879end
2880if or gt v band gt neg v band
2881mv = or mv shl ulong 1 b
2882end
2883b = add b 1
2884end
2885put arx_hv_sign sidx sv
2886put arx_hv_mask sidx mv
2887ret arx_popcnt64 mv
2888end
2889
2890fn arx_hv_quantize l ulong to void
2891band = arx_hv_calc_band l
2892w = get arx_cfg_hv_words 0
2893cbase = mul l mul w 64
2894sbase = mul l w
2895nnz = uint 0
2896x = ulong 0
2897while lt x w bound 128
2898nnz = add nnz arx_hv_quantize_word add cbase mul x 64 add sbase x band
2899x = add x 1
2900end
2901put arx_hv_nnz l nnz
2902end
2903
2904fn arx_hv_proto_sim_q l ulong to f32
2905if eq get arx_hv_nlabel l 0
2906ret -1.0f32
2907end
2908nnz = get arx_hv_nnz l
2909if eq nnz 0
2910ret -1.0f32
2911end
2912w = get arx_cfg_hv_words 0
2913sbase = mul l w
2914agree = long 0
2915dis = long 0
2916x = ulong 0
2917while lt x w bound 128
2918m = get arx_hv_mask add sbase x
2919diff = xor get arx_hv_q x get arx_hv_sign add sbase x
2920dis = add dis long arx_popcnt64 and diff m
2921agree = add agree long arx_popcnt64 and not diff m
2922x = add x 1
2923end
2924d = f32 mul w 64
2925ret div f32 sub agree dis fsqrt mul f32 nnz d
2926end
2927
2928fn arx_hv_proto_sim_ex ex_idx ulong l ulong to f32
2929if eq get arx_hv_nlabel l 0
2930ret -1.0f32
2931end
2932nnz = get arx_hv_nnz l
2933if eq nnz 0
2934ret -1.0f32
2935end
2936w = get arx_cfg_hv_words 0
2937sbase = mul l w
2938hbase = mul ex_idx w
2939net = long 0
2940x = ulong 0
2941while lt x w bound 128
2942m = get arx_hv_mask add sbase x
2943va = get arx_hv_hvs add hbase x
2944vb = get arx_hv_sign add sbase x
2945diff = xor va vb
2946p_a = long arx_popcnt64 and not diff m
2947p_d = long arx_popcnt64 and diff m
2948net = add net sub p_a p_d
2949x = add x 1
2950end
2951ret div f32 net fsqrt mul f32 nnz f32 mul w 64
2952end
2953
2954fn arx_hv_rebuild_pass to ulong
2955sc = new f32 15 -9.0f32
2956n = get arx_hv_n 0
2957eta = get arx_cfg_hv_eta 0
2958errs = ulong 0
2959i = ulong 0
2960while lt i n bound 256
2961y = ulong get arx_hv_labels i
2962l = ulong 0
2963while lt l ARX_N_LABELS bound 15
2964if gt get arx_hv_nlabel l 0
2965put sc l arx_hv_proto_sim_ex i l
2966else
2967put sc l -9.0f32
2968end
2969l = add l 1
2970end
2971p = arx_argmax sc
2972if ne p y
2973errs = add errs 1
2974wy = int mul mul eta sub 1.0f32 get sc y 64.0f32
2975wp = int mul mul eta sub 1.0f32 get sc p 64.0f32
2976if lt wy 1
2977wy = 1
2978end
2979if lt wp 1
2980wp = 1
2981end
2982arx_hv_add_counts y i wy
2983arx_hv_add_counts p i neg wp
2984arx_hv_quantize y
2985arx_hv_quantize p
2986end
2987i = add i 1
2988end
2989ret errs
2990end
2991
2992fn arx_hv_rebuild to uint
2993w = get arx_cfg_hv_words 0
2994tot_c = mul ARX_N_LABELS mul w 64
2995i = ulong 0
2996while lt i tot_c bound 131072
2997put arx_hv_counts i 0
2998i = add i 1
2999end
3000n = get arx_hv_n 0
3001i = ulong 0
3002while lt i n bound 256
3003arx_hv_add_counts ulong get arx_hv_labels i i 64
3004i = add i 1
3005end
3006l = ulong 0
3007while lt l ARX_N_LABELS bound 15
3008arx_hv_quantize l
3009l = add l 1
3010end
3011passes = uint 0
3012epochs = get arx_cfg_hv_epochs 0
3013ep = uint 0
3014while lt ep epochs bound 100
3015passes = add passes 1
3016if eq arx_hv_rebuild_pass 0
3017break
3018end
3019ep = add ep 1
3020end
3021ret passes
3022end
3023
3024fn arx_hv_online idx ulong wrong uchar to void
3025y = ulong get arx_hv_labels idx
3026sy = arx_hv_proto_sim_ex idx y
3027if lt sy -1.0f32
3028sy = -1.0f32
3029end
3030wy = int mul sub 1.0f32 sy 64.0f32
3031if lt wy 1
3032wy = 1
3033end
3034arx_hv_add_counts y idx wy
3035arx_hv_quantize y
3036if ne wrong ARX_ACT_INVALID
3037p = ulong wrong
3038if ne p y
3039if gt get arx_hv_nlabel p 0
3040sp = arx_hv_proto_sim_ex idx p
3041wp = int mul mul add 1.0f32 sp 0.5f32 64.0f32
3042if lt wp 1
3043wp = 1
3044end
3045arx_hv_add_counts p idx neg wp
3046arx_hv_quantize p
3047end
3048end
3049end
3050end
3051
3052fn arx_scores_empty to void
3053l = ulong 0
3054while lt l ARX_N_LABELS bound 15
3055put arx_sc_s l -9.0f32
3056l = add l 1
3057end
3058put arx_sc_best_sim 0 0.0f32
3059put arx_sc_has_near 0 0
3060put arx_sc_nearest 0 0
3061put arx_sc_agree 0 1.0f32
3062end
3063
3064fn arx_hv_score_agree gs view f32 8 gi view ulong 8 ka ulong to f32
3065sc_tmp = new f32 15 0.0f32
3066l = ulong 0
3067while lt l ARX_N_LABELS bound 15
3068put sc_tmp l get arx_sc_s l
3069l = add l 1
3070end
3071tl = arx_argmax sc_tmp
3072cnt_v = ulong 0
3073cnt_m = ulong 0
3074j = ulong 0
3075while lt j ka bound 8
3076if gt get gs j -2.0f32
3077cnt_v = add cnt_v 1
3078if eq ulong get arx_hv_labels get gi j tl
3079cnt_m = add cnt_m 1
3080end
3081end
3082j = add j 1
3083end
3084if eq cnt_v 0
3085cnt_v = 1
3086end
3087ret div f32 cnt_m f32 cnt_v
3088end
3089
3090fn arx_hv_score_labels top view f32 120 kk ulong to void
3091l = ulong 0
3092while lt l ARX_N_LABELS bound 15
3093nl = ulong get arx_hv_nlabel l
3094if gt nl 0
3095m = nl
3096if gt m kk
3097m = kk
3098end
3099sum = 0.0f32
3100j = ulong 0
3101while lt j m bound 8
3102sum = add sum get top add mul l 8 j
3103j = add j 1
3104end
3105al = get arx_cfg_hv_alpha 0
3106pr = 0.0f32
3107if lt al 1.0f32
3108pr = arx_hv_proto_sim_q l
3109end
3110knn = div sum f32 m
3111p1 = mul al knn
3112p2 = mul sub 1.0f32 al pr
3113put arx_sc_s l add p1 p2
3114end
3115l = add l 1
3116end
3117end
3118
3119fn arx_hv_score_q to void
3120top = new f32 120 -2.0f32
3121gs = new f32 8 -2.0f32
3122gi = new ulong 8 0
3123arx_scores_empty
3124n = get arx_hv_n 0
3125if eq n 0
3126ret
3127end
3128kk = get arx_cfg_hv_k 0
3129if lt kk 1
3130kk = 1
3131end
3132if gt kk 8
3133kk = 8
3134end
3135ka = kk
3136if lt ka 3
3137ka = 3
3138end
3139i = ulong 0
3140while lt i n bound 256
3141s = arx_hv_sim_q_ex i get arx_cfg_hv_words 0
3142l = ulong get arx_hv_labels i
3143arx_ins_top top mul l 8 kk s
3144arx_ins_glob gs gi ka s i
3145i = add i 1
3146end
3147put arx_sc_best_sim 0 get gs 0
3148put arx_sc_has_near 0 1
3149put arx_sc_nearest 0 get gi 0
3150arx_hv_score_labels top kk
3151put arx_sc_agree 0 arx_hv_score_agree gs gi ka
3152end
3153
3154fn arx_hv_copy_ex dst ulong src ulong w ulong to void
3155sb = mul src w
3156db = mul dst w
3157x = ulong 0
3158while lt x w bound 128
3159v = get arx_hv_hvs add sb x
3160put arx_hv_hvs add db x v
3161x = add x 1
3162end
3163put arx_hv_labels dst get arx_hv_labels src
3164nlen = get arx_hv_nlen src
3165put arx_hv_nlen dst nlen
3166sb = mul src ARX_NOTE_BYTES
3167db = mul dst ARX_NOTE_BYTES
3168x = ulong 0
3169while lt x nlen bound 96
3170b = get arx_hv_notes add sb x
3171put arx_hv_notes add db x b
3172x = add x 1
3173end
3174end
3175
3176fn arx_hv_sleep_compact n ulong w ulong to void
3177dst = ulong 0
3178l = ulong 0
3179while lt l ARX_N_LABELS bound 15
3180put arx_hv_nlabel l 0
3181l = add l 1
3182end
3183i = ulong 0
3184while lt i n bound 256
3185if ne get arx_hv_keep i 0
3186if ne dst i
3187arx_hv_copy_ex dst i w
3188end
3189li = ulong get arx_hv_labels dst
3190put arx_hv_nlabel li add get arx_hv_nlabel li 1
3191dst = add dst 1
3192end
3193i = add i 1
3194end
3195put arx_hv_n 0 dst
3196end
3197
3198fn arx_hv_sleep_add_conflict a ulong b ulong simv f32 to void
3199cn = get arx_slp_con_n 0
3200p = ulong 0
3201if lt cn ARX_MAX_CONFLICTS
3202put arx_slp_con_a cn a
3203put arx_slp_con_b cn b
3204put arx_slp_con_sim cn simv
3205p = cn
3206put arx_slp_con_n 0 add cn 1
3207else
3208if le simv get arx_slp_con_sim sub ARX_MAX_CONFLICTS 1
3209ret
3210end
3211p = sub ARX_MAX_CONFLICTS 1
3212put arx_slp_con_a p a
3213put arx_slp_con_b p b
3214put arx_slp_con_sim p simv
3215end
3216while gt p 0 bound 32
3217cs = get arx_slp_con_sim p
3218ps = get arx_slp_con_sim sub p 1
3219if le cs ps
3220break
3221end
3222ca = get arx_slp_con_a p
3223cb = get arx_slp_con_b p
3224pa = get arx_slp_con_a sub p 1
3225pb = get arx_slp_con_b sub p 1
3226put arx_slp_con_a p pa
3227put arx_slp_con_b p pb
3228put arx_slp_con_sim p ps
3229put arx_slp_con_a sub p 1 ca
3230put arx_slp_con_b sub p 1 cb
3231put arx_slp_con_sim sub p 1 cs
3232p = sub p 1
3233end
3234end
3235
3236fn arx_hv_sleep dup f32 conflict f32 to void
3237w = get arx_cfg_hv_words 0
3238n = get arx_hv_n 0
3239i = ulong 0
3240while lt i n bound 256
3241put arx_hv_keep i 1
3242i = add i 1
3243end
3244merged = ulong 0
3245i = ulong 0
3246while lt i n bound 256
3247if ne get arx_hv_keep i 0
3248j = add i 1
3249while lt j n bound 256
3250if ne get arx_hv_keep j 0
3251if eq get arx_hv_labels i get arx_hv_labels j
3252if ge arx_hv_sim_ex_ex i j w dup
3253put arx_hv_keep j 0
3254merged = add merged 1
3255end
3256end
3257end
3258j = add j 1
3259end
3260end
3261i = add i 1
3262end
3263if gt merged 0
3264arx_hv_sleep_compact n w
3265end
3266n2 = get arx_hv_n 0
3267put arx_slp_con_n 0 0
3268i = ulong 0
3269while lt i n2 bound 256
3270j = add i 1
3271while lt j n2 bound 256
3272if ne get arx_hv_labels i get arx_hv_labels j
3273s = arx_hv_sim_ex_ex i j w
3274if ge s conflict
3275arx_hv_sleep_add_conflict i j s
3276end
3277end
3278j = add j 1
3279end
3280i = add i 1
3281end
3282passes = arx_hv_rebuild
3283put arx_slp_merged 0 merged
3284put arx_slp_kept 0 n2
3285put arx_slp_passes 0 passes
3286end
3287
3288note --- Sparse Feature Helpers & NearestModel ---
3289
3290fn arx_sparse_build_q to ulong
3291n = get arx_ft_n 0
3292i = ulong 0
3293while lt i n bound 768
3294put arx_sp_q_k i arx_feat_key i
3295put arx_sp_q_w i uint get arx_ft_w i
3296i = add i 1
3297end
3298note Insertion sort by key (n <= 768)
3299i = ulong 1
3300while lt i n bound 768
3301kk = get arx_sp_q_k i
3302kw = get arx_sp_q_w i
3303j = i
3304while gt j 0 bound 768
3305pk = get arx_sp_q_k sub j 1
3306if le pk kk
3307break
3308end
3309put arx_sp_q_k j pk
3310put arx_sp_q_w j get arx_sp_q_w sub j 1
3311j = sub j 1
3312end
3313put arx_sp_q_k j kk
3314put arx_sp_q_w j kw
3315i = add i 1
3316end
3317note Merge duplicates
3318if eq n 0
3319put arx_sp_q_n 0 0
3320ret 0
3321end
3322m = ulong 1
3323i = ulong 1
3324while lt i n bound 768
3325kk = get arx_sp_q_k i
3326kw = get arx_sp_q_w i
3327if eq kk get arx_sp_q_k sub m 1
3328put arx_sp_q_w sub m 1 add get arx_sp_q_w sub m 1 kw
3329else
3330put arx_sp_q_k m kk
3331put arx_sp_q_w m kw
3332m = add m 1
3333end
3334i = add i 1
3335end
3336put arx_sp_q_n 0 m
3337ret m
3338end
3339
3340fn arx_dice_tot bo ulong nb ulong to ulong
3341na = get arx_sp_q_n 0
3342tot = ulong 0
3343i = ulong 0
3344while lt i na bound 768
3345tot = add tot ulong get arx_sp_q_w i
3346i = add i 1
3347end
3348j = ulong 0
3349while lt j nb bound 768
3350tot = add tot ulong get arx_nr_pool_w add bo j
3351j = add j 1
3352end
3353ret tot
3354end
3355
3356fn arx_dice_q_nr ex_idx ulong to f32
3357na = get arx_sp_q_n 0
3358bo = get arx_nr_off ex_idx
3359nb = get arx_nr_len ex_idx
3360i = ulong 0
3361j = ulong 0
3362inter = ulong 0
3363while lt i na bound 1600
3364if ge j nb
3365break
3366end
3367ka = get arx_sp_q_k i
3368kb = get arx_nr_pool_k add bo j
3369if eq ka kb
3370mn = get arx_sp_q_w i
3371if lt get arx_nr_pool_w add bo j mn
3372mn = get arx_nr_pool_w add bo j
3373end
3374inter = add inter ulong mn
3375i = add i 1
3376j = add j 1
3377else
3378if lt ka kb
3379i = add i 1
3380else
3381j = add j 1
3382end
3383end
3384end
3385tot = arx_dice_tot bo nb
3386if eq tot 0
3387ret 0.0f32
3388end
3389ret div mul 2.0f32 f32 inter f32 tot
3390end
3391
3392fn arx_nr_reset to void
3393put arx_nr_n 0 0
3394put arx_nr_pool_n 0 0
3395l = ulong 0
3396while lt l ARX_N_LABELS bound 15
3397put arx_nr_nlabel l 0
3398l = add l 1
3399end
3400end
3401
3402fn arx_nr_push_q lbl uchar note_s view uchar * to long
3403n = get arx_nr_n 0
3404if ge n get arx_cfg_max_ex 0
3405ret -1
3406end
3407if ge n ARX_MAX_EXEMPLARS
3408ret -1
3409end
3410qn = get arx_sp_q_n 0
3411po = get arx_nr_pool_n 0
3412if gt add po qn ARX_SPARSE_POOL
3413ret -1
3414end
3415i = ulong 0
3416while lt i qn bound 768
3417put arx_nr_pool_k add po i get arx_sp_q_k i
3418put arx_nr_pool_w add po i get arx_sp_q_w i
3419i = add i 1
3420end
3421put arx_nr_off n po
3422put arx_nr_len n qn
3423put arx_nr_pool_n 0 add po qn
3424put arx_nr_labels n lbl
3425nlen = len note_s
3426if gt nlen ARX_NOTE_BYTES
3427nlen = ARX_NOTE_BYTES
3428end
3429nbase = mul n ARX_NOTE_BYTES
3430k = ulong 0
3431while lt k nlen bound 96
3432put arx_nr_notes add nbase k get note_s k
3433k = add k 1
3434end
3435put arx_nr_nlen n nlen
3436li = ulong lbl
3437put arx_nr_nlabel li add get arx_nr_nlabel li 1
3438put arx_nr_n 0 add n 1
3439ret long n
3440end
3441
3442fn arx_nr_score_agree gs view f32 8 gi view ulong 8 ka ulong to f32
3443sc_tmp = new f32 15 0.0f32
3444l = ulong 0
3445while lt l ARX_N_LABELS bound 15
3446put sc_tmp l get arx_sc_s l
3447l = add l 1
3448end
3449tl = arx_argmax sc_tmp
3450cnt_v = ulong 0
3451cnt_m = ulong 0
3452j = ulong 0
3453while lt j ka bound 8
3454if gt get gs j -2.0f32
3455cnt_v = add cnt_v 1
3456if eq ulong get arx_nr_labels get gi j tl
3457cnt_m = add cnt_m 1
3458end
3459end
3460j = add j 1
3461end
3462if eq cnt_v 0
3463cnt_v = 1
3464end
3465ret div f32 cnt_m f32 cnt_v
3466end
3467
3468fn arx_nr_score_labels top view f32 120 kk ulong to void
3469l = ulong 0
3470while lt l ARX_N_LABELS bound 15
3471nl = ulong get arx_nr_nlabel l
3472if gt nl 0
3473m = nl
3474if gt m kk
3475m = kk
3476end
3477sum = 0.0f32
3478j = ulong 0
3479while lt j m bound 8
3480sum = add sum get top add mul l 8 j
3481j = add j 1
3482end
3483put arx_sc_s l div sum f32 m
3484end
3485l = add l 1
3486end
3487end
3488
3489fn arx_nr_score_q to void
3490top = new f32 120 -2.0f32
3491gs = new f32 8 -2.0f32
3492gi = new ulong 8 0
3493arx_scores_empty
3494n = get arx_nr_n 0
3495if eq n 0
3496ret
3497end
3498kk = get arx_cfg_hv_k 0
3499if lt kk 1
3500kk = 1
3501end
3502if gt kk 8
3503kk = 8
3504end
3505ka = kk
3506if lt ka 3
3507ka = 3
3508end
3509i = ulong 0
3510while lt i n bound 256
3511d = arx_dice_q_nr i
3512l = ulong get arx_nr_labels i
3513arx_ins_top top mul l 8 kk d
3514arx_ins_glob gs gi ka d i
3515i = add i 1
3516end
3517put arx_sc_best_sim 0 get gs 0
3518put arx_sc_has_near 0 1
3519put arx_sc_nearest 0 get gi 0
3520arx_nr_score_labels top kk
3521put arx_sc_agree 0 arx_nr_score_agree gs gi ka
3522end
3523
3524note --- PerceptronModel ---
3525
3526fn arx_pc_clear_weights to void
3527i = ulong 0
3528while lt i ARX_PERC_CAP bound 8192
3529put arx_pc_w_used i 0
3530put arx_pc_u_used i 0
3531i = add i 1
3532end
3533put arx_pc_w_cnt 0 0
3534put arx_pc_u_cnt 0 0
3535end
3536
3537fn arx_pc_reset to void
3538arx_pc_clear_weights
3539put arx_pc_tr_n 0 0
3540put arx_pc_pool_n 0 0
3541l = ulong 0
3542while lt l ARX_N_LABELS bound 15
3543put arx_pc_nlabel l 0
3544l = add l 1
3545end
3546end
3547
3548fn arx_pc_w_find key ulong create bool to long
3549slot = and key sub ARX_PERC_CAP 1
3550step = ulong 0
3551while lt step ARX_PERC_CAP bound 8192
3552if eq get arx_pc_w_used slot 0
3553if not create
3554ret -1
3555end
3556if ge get arx_pc_w_cnt 0 sub ARX_PERC_CAP 64
3557ret -1
3558end
3559put arx_pc_w_used slot 1
3560put arx_pc_w_key slot key
3561base = mul slot ARX_N_LABELS
3562l = ulong 0
3563while lt l ARX_N_LABELS bound 15
3564put arx_pc_w_val add base l 0.0f32
3565l = add l 1
3566end
3567put arx_pc_w_cnt 0 add get arx_pc_w_cnt 0 1
3568ret long slot
3569end
3570if eq get arx_pc_w_key slot key
3571ret long slot
3572end
3573slot = and add slot 1 sub ARX_PERC_CAP 1
3574step = add step 1
3575end
3576ret -1
3577end
3578
3579fn arx_pc_u_find key ulong create bool to long
3580slot = and key sub ARX_PERC_CAP 1
3581step = ulong 0
3582while lt step ARX_PERC_CAP bound 8192
3583if eq get arx_pc_u_used slot 0
3584if not create
3585ret -1
3586end
3587if ge get arx_pc_u_cnt 0 sub ARX_PERC_CAP 64
3588ret -1
3589end
3590put arx_pc_u_used slot 1
3591put arx_pc_u_key slot key
3592base = mul slot ARX_N_LABELS
3593l = ulong 0
3594while lt l ARX_N_LABELS bound 15
3595put arx_pc_u_val add base l 0.0f32
3596l = add l 1
3597end
3598put arx_pc_u_cnt 0 add get arx_pc_u_cnt 0 1
3599ret long slot
3600end
3601if eq get arx_pc_u_key slot key
3602ret long slot
3603end
3604slot = and add slot 1 sub ARX_PERC_CAP 1
3605step = add step 1
3606end
3607ret -1
3608end
3609
3610fn arx_pc_push_q lbl uchar to void
3611n = get arx_pc_tr_n 0
3612if ge n ARX_MAX_EXEMPLARS
3613ret
3614end
3615qn = get arx_sp_q_n 0
3616po = get arx_pc_pool_n 0
3617if gt add po qn ARX_SPARSE_POOL
3618ret
3619end
3620i = ulong 0
3621while lt i qn bound 768
3622put arx_pc_pool_k add po i get arx_sp_q_k i
3623put arx_pc_pool_w add po i get arx_sp_q_w i
3624i = add i 1
3625end
3626put arx_pc_tr_off n po
3627put arx_pc_tr_len n qn
3628put arx_pc_pool_n 0 add po qn
3629put arx_pc_tr_lbl n lbl
3630li = ulong lbl
3631put arx_pc_nlabel li add get arx_pc_nlabel li 1
3632put arx_pc_tr_n 0 add n 1
3633end
3634
3635fn arx_pc_raw_tr tr_idx ulong sc edit f32 * to void
3636l = ulong 0
3637while lt l ARX_N_LABELS bound 15
3638put sc l 0.0f32
3639l = add l 1
3640end
3641off = get arx_pc_tr_off tr_idx
3642xn = get arx_pc_tr_len tr_idx
3643i = ulong 0
3644while lt i xn bound 768
3645sl = arx_pc_w_find get arx_pc_pool_k add off i false
3646if ge sl 0
3647vf = f32 get arx_pc_pool_w add off i
3648base = mul ulong sl ARX_N_LABELS
3649l = ulong 0
3650while lt l ARX_N_LABELS bound 15
3651wv = get arx_pc_w_val add base l
3652nv = add get sc l mul wv vf
3653put sc l nv
3654l = add l 1
3655end
3656end
3657i = add i 1
3658end
3659end
3660
3661fn arx_pc_add_u fk ulong cv f32 y ulong p ulong to void
3662us = arx_pc_u_find fk true
3663if ge us 0
3664ub = mul ulong us ARX_N_LABELS
3665uy = add ub y
3666up = add ub p
3667put arx_pc_u_val uy add get arx_pc_u_val uy cv
3668put arx_pc_u_val up sub get arx_pc_u_val up cv
3669end
3670end
3671
3672fn arx_pc_fit_update_one fk ulong vf f32 y ulong p ulong c f32 to void
3673ws = arx_pc_w_find fk true
3674if ge ws 0
3675wb = mul ulong ws ARX_N_LABELS
3676iy = add wb y
3677ip = add wb p
3678put arx_pc_w_val iy add get arx_pc_w_val iy vf
3679put arx_pc_w_val ip sub get arx_pc_w_val ip vf
3680arx_pc_add_u fk mul c vf y p
3681end
3682end
3683
3684fn arx_pc_fit_update idx ulong y ulong p ulong c f32 to void
3685off = get arx_pc_tr_off idx
3686xn = get arx_pc_tr_len idx
3687k = ulong 0
3688while lt k xn bound 768
3689pi = add off k
3690fk = get arx_pc_pool_k pi
3691vf = f32 get arx_pc_pool_w pi
3692arx_pc_fit_update_one fk vf y p c
3693k = add k 1
3694end
3695end
3696
3697fn arx_pc_fit_avg c f32 to void
3698s = ulong 0
3699while lt s ARX_PERC_CAP bound 8192
3700if ne get arx_pc_w_used s 0
3701us = arx_pc_u_find get arx_pc_w_key s false
3702if ge us 0
3703wb = mul s ARX_N_LABELS
3704ub = mul ulong us ARX_N_LABELS
3705l = ulong 0
3706while lt l ARX_N_LABELS bound 15
3707wi = add wb l
3708ui = add ub l
3709uv = div get arx_pc_u_val ui c
3710wv = get arx_pc_w_val wi
3711put arx_pc_w_val wi sub wv uv
3712l = add l 1
3713end
3714end
3715end
3716s = add s 1
3717end
3718end
3719
3720fn arx_pc_fit to void
3721sc = new f32 15 0.0f32
3722arx_pc_clear_weights
3723n = get arx_pc_tr_n 0
3724if eq n 0
3725ret
3726end
3727i = ulong 0
3728while lt i n bound 256
3729put arx_pc_order i i
3730i = add i 1
3731end
3732c = 1.0f32
3733rng = ARX_SEED
3734ep = uint 0
3735while lt ep get arx_cfg_perc_epochs 0 bound 100
3736if gt n 1
3737i = sub n 1
3738while gt i 0 bound 256
3739rng = add rng ulong 0x9e3779b97f4a7c15
3740j = rem arx_splitmix_next rng add i 1
3741ti = get arx_pc_order i
3742put arx_pc_order i get arx_pc_order j
3743put arx_pc_order j ti
3744i = sub i 1
3745end
3746end
3747oi = ulong 0
3748while lt oi n bound 256
3749idx = get arx_pc_order oi
3750y = ulong get arx_pc_tr_lbl idx
3751arx_pc_raw_tr idx sc
3752l = ulong 0
3753while lt l ARX_N_LABELS bound 15
3754if eq get arx_pc_nlabel l 0
3755put sc l -1.0e38f32
3756end
3757l = add l 1
3758end
3759p = arx_argmax sc
3760if ne p y
3761arx_pc_fit_update idx y p c
3762end
3763c = add c 1.0f32
3764oi = add oi 1
3765end
3766ep = add ep 1
3767end
3768arx_pc_fit_avg c
3769end
3770
3771fn arx_pc_score_q to void
3772raw = new f32 15 0.0f32
3773arx_scores_empty
3774qn = get arx_sp_q_n 0
3775tot = 0.0f32
3776i = ulong 0
3777while lt i qn bound 768
3778vf = f32 get arx_sp_q_w i
3779tot = add tot vf
3780sl = arx_pc_w_find get arx_sp_q_k i false
3781if ge sl 0
3782base = mul ulong sl ARX_N_LABELS
3783l = ulong 0
3784while lt l ARX_N_LABELS bound 15
3785wv = get arx_pc_w_val add base l
3786nv = add get raw l mul wv vf
3787put raw l nv
3788l = add l 1
3789end
3790end
3791i = add i 1
3792end
3793if lt tot 1.0f32
3794tot = 1.0f32
3795end
3796l = ulong 0
3797while lt l ARX_N_LABELS bound 15
3798if gt get arx_pc_nlabel l 0
3799put arx_sc_s l div get raw l tot
3800end
3801l = add l 1
3802end
3803put arx_sc_best_sim 0 1.0f32
3804put arx_sc_agree 0 1.0f32
3805end
3806
3807fn arx_pc_online_step y ulong p ulong to void
3808qn = get arx_sp_q_n 0
3809i = ulong 0
3810while lt i qn bound 768
3811ws = arx_pc_w_find get arx_sp_q_k i true
3812if ge ws 0
3813vf = f32 get arx_sp_q_w i
3814wb = mul ulong ws ARX_N_LABELS
3815iy = add wb y
3816ny = add get arx_pc_w_val iy vf
3817put arx_pc_w_val iy ny
3818ip = add wb p
3819np = sub get arx_pc_w_val ip vf
3820put arx_pc_w_val ip np
3821end
3822i = add i 1
3823end
3824end
3825
3826fn arx_pc_online_q lbl uchar to void
3827sc_tmp = new f32 15 0.0f32
3828y = ulong lbl
3829arx_pc_push_q lbl
3830step = ulong 0
3831while lt step 8 bound 8
3832arx_pc_score_q
3833l = ulong 0
3834while lt l ARX_N_LABELS bound 15
3835put sc_tmp l get arx_sc_s l
3836l = add l 1
3837end
3838p = arx_argmax sc_tmp
3839if eq p y
3840break
3841end
3842arx_pc_online_step y p
3843step = add step 1
3844end
3845end
3846
3847note =========================================================================
3848note Section 12: Reflex Core API
3849note =========================================================================
3850
3851fn arx_cfg_frozen mode uchar to void
3852put arx_cfg_mode 0 mode
3853put arx_cfg_hv_words 0 ARX_HV_WORDS
3854put arx_cfg_hv_k 0 3
3855put arx_cfg_hv_alpha 0 0.5f32
3856put arx_cfg_hv_zband 0 0.42f32
3857put arx_cfg_hv_epochs 0 40
3858put arx_cfg_hv_eta 0 0.5f32
3859put arx_cfg_max_ex 0 ARX_MAX_EXEMPLARS
3860put arx_cfg_t_margin 0 0.0f32
3861put arx_cfg_t_abs 0 -1.0f32
3862put arx_cfg_abs_label 0 ARX_ACT_NONE
3863put arx_cfg_t_agree 0 0.0f32
3864put arx_cfg_exact 0 0.97f32
3865put arx_cfg_perc_epochs 0 20
3866put arx_cfg_sleep_dup 0 0.95f32
3867put arx_cfg_sleep_con 0 0.80f32
3868put arx_cfg_max_msg 0 2048
3869put arx_cfg_max_toks 0 256
3870put arx_cfg_max_turns 0 16
3871if eq mode ARX_MODE_REFLEX
3872put arx_cfg_weights 0 3
3873put arx_cfg_weights 1 0
3874put arx_cfg_weights 2 1
3875put arx_cfg_weights 3 6
3876put arx_cfg_weights 4 4
3877put arx_cfg_weights 5 3
3878put arx_cfg_weights 6 3
3879put arx_cfg_weights 7 2
3880else
3881if eq mode ARX_MODE_NEAREST
3882put arx_cfg_weights 0 0
3883put arx_cfg_weights 1 2
3884put arx_cfg_weights 2 0
3885put arx_cfg_weights 3 6
3886put arx_cfg_weights 4 4
3887put arx_cfg_weights 5 0
3888put arx_cfg_weights 6 3
3889put arx_cfg_weights 7 2
3890put arx_cfg_hv_k 0 5
3891else
3892put arx_cfg_weights 0 0
3893put arx_cfg_weights 1 2
3894put arx_cfg_weights 2 1
3895put arx_cfg_weights 3 4
3896put arx_cfg_weights 4 4
3897put arx_cfg_weights 5 3
3898put arx_cfg_weights 6 3
3899put arx_cfg_weights 7 2
3900put arx_cfg_perc_epochs 0 10
3901end
3902end
3903end
3904
3905fn arx_init mode uchar to void
3906arx_cfg_frozen mode
3907arx_hv_reset
3908arx_nr_reset
3909arx_pc_reset
3910arx_cue_reset
3911put arx_learning 0 1
3912end
3913
3914fn arx_set_learning on bool to void
3915if on
3916put arx_learning 0 1
3917else
3918put arx_learning 0 0
3919end
3920end
3921
3922fn arx_exemplars to ulong
3923m = get arx_cfg_mode 0
3924if eq m ARX_MODE_REFLEX
3925ret get arx_hv_n 0
3926end
3927if eq m ARX_MODE_NEAREST
3928ret get arx_nr_n 0
3929end
3930ret 0
3931end
3932
3933fn arx_exemplar_label idx ulong to uchar
3934m = get arx_cfg_mode 0
3935if eq m ARX_MODE_REFLEX
3936if lt idx get arx_hv_n 0
3937ret get arx_hv_labels idx
3938end
3939end
3940if eq m ARX_MODE_NEAREST
3941if lt idx get arx_nr_n 0
3942ret get arx_nr_labels idx
3943end
3944end
3945ret ARX_ACT_INVALID
3946end
3947
3948fn arx_analyse to uchar
3949msg = new uchar 4096 0
3950arx_dialog_state get arx_cfg_max_turns 0
3951if eq get arx_dlg_has_user 0 0
3952ret ARX_WHY_NO_USER_TURN
3953end
3954lu = get arx_dlg_last_user 0
3955if gt get arx_turn_raw_len lu get arx_cfg_max_msg 0
3956ret ARX_WHY_INPUT_TOO_LONG
3957end
3958mlen = arx_load_turn_buf lu msg
3959i = ulong 0
3960while lt i mlen bound 4096
3961put arx_dlg_msg i get msg i
3962i = add i 1
3963end
3964put arx_dlg_msg_len 0 mlen
3965tn = arx_tokenize part msg 0 mlen get arx_cfg_max_toks 0
3966has_al = false
3967i = ulong 0
3968while lt i tn bound 256
3969if ne get arx_tok_alnum i 0
3970has_al = true
3971break
3972end
3973i = add i 1
3974end
3975if not has_al
3976ret ARX_WHY_NO_WORDS
3977end
3978cn_ign = arx_clauses
3979if not arx_primary
3980ret ARX_WHY_NO_WORDS
3981end
3982ret ARX_WHY_SCORED
3983end
3984
3985fn arx_build_feats to ulong
3986cs = get arx_pri_start 0
3987ce = get arx_pri_end 0
3988from = get arx_tok_end sub ce 1
3989to_b = get arx_dlg_msg_len 0
3990if lt ce get arx_tok_n 0
3991to_b = get arx_tok_start ce
3992end
3993ends_q = false
3994i = from
3995while lt i to_b bound 4096
3996if eq get arx_dlg_msg i 63
3997ends_q = true
3998break
3999end
4000i = add i 1
4001end
4002ret arx_extract_features cs ce ends_q
4003end
4004
4005fn arx_score_current to void
4006m = get arx_cfg_mode 0
4007if eq m ARX_MODE_REFLEX
4008arx_hv_encode_to_q get arx_cfg_hv_words 0
4009arx_hv_score_q
4010else
4011sp_ign = arx_sparse_build_q
4012if eq m ARX_MODE_NEAREST
4013arx_nr_score_q
4014else
4015arx_pc_score_q
4016end
4017end
4018end
4019
4020fn arx_exp_reset why uchar to void
4021put arx_exp_why 0 why
4022put arx_exp_refuse 0 ARX_REFUSE_NONE
4023put arx_exp_span_err 0 ARX_SPAN_OK
4024put arx_exp_top 0 ARX_ACT_CLARIFY
4025put arx_exp_second 0 ARX_ACT_CLARIFY
4026put arx_exp_margin 0 0.0f32
4027put arx_exp_best_sim 0 0.0f32
4028put arx_exp_agree 0 0.0f32
4029put arx_exp_has_near 0 0
4030put arx_exp_near_act 0 ARX_ACT_INVALID
4031put arx_exp_near_idx 0 0
4032put arx_exp_span_bs 0 0
4033put arx_exp_span_be 0 0
4034put arx_dec_has_txt 0 0
4035put arx_dec_txt_len 0 0
4036end
4037
4038fn arx_decide_select top ulong sec ulong margin f32 to uchar
4039arx_exp_reset ARX_WHY_SCORED
4040put arx_exp_top 0 uchar top
4041put arx_exp_second 0 uchar sec
4042put arx_exp_margin 0 margin
4043put arx_exp_best_sim 0 get arx_sc_best_sim 0
4044put arx_exp_agree 0 get arx_sc_agree 0
4045nr_lbl = ARX_ACT_INVALID
4046if ne get arx_sc_has_near 0 0
4047ni = get arx_sc_nearest 0
4048nr_lbl = arx_exemplar_label ni
4049put arx_exp_has_near 0 1
4050put arx_exp_near_idx 0 ni
4051put arx_exp_near_act 0 nr_lbl
4052end
4053act = uchar top
4054exact_ok = false
4055if ge get arx_sc_best_sim 0 get arx_cfg_exact 0
4056if ne nr_lbl ARX_ACT_INVALID
4057blocked = false
4058if eq get arx_dlg_pending 0 ARX_PEND_NO
4059if arx_act_needs_pending nr_lbl
4060blocked = true
4061end
4062end
4063if not blocked
4064exact_ok = true
4065end
4066end
4067end
4068if exact_ok
4069act = nr_lbl
4070put arx_exp_why 0 ARX_WHY_EXACT_RECALL
4071else
4072if lt get arx_sc_best_sim 0 get arx_cfg_t_abs 0
4073act = get arx_cfg_abs_label 0
4074put arx_exp_why 0 ARX_WHY_LOW_SIMILARITY
4075else
4076if and lt margin get arx_cfg_t_margin 0 ne act ARX_ACT_CLARIFY
4077act = ARX_ACT_CLARIFY
4078put arx_exp_why 0 ARX_WHY_LOW_MARGIN
4079else
4080if and lt get arx_sc_agree 0 get arx_cfg_t_agree 0 ne act ARX_ACT_CLARIFY
4081act = ARX_ACT_CLARIFY
4082put arx_exp_why 0 ARX_WHY_LOW_AGREEMENT
4083end
4084end
4085end
4086end
4087ret act
4088end
4089
4090fn arx_decide_extract_span act uchar to uchar
4091msg = new uchar 4096 0
4092mlen = get arx_dlg_msg_len 0
4093i = ulong 0
4094while lt i mlen bound 4096
4095put msg i get arx_dlg_msg i
4096i = add i 1
4097end
4098se = arx_cue_extract part msg 0 mlen get arx_pri_start 0 get arx_pri_end 0 act
4099if eq se ARX_SPAN_OK
4100put arx_dec_act 0 act
4101put arx_dec_has_txt 0 1
4102ret act
4103end
4104put arx_exp_why 0 ARX_WHY_SPAN
4105put arx_exp_span_err 0 se
4106put arx_dec_act 0 ARX_ACT_CLARIFY
4107ret ARX_ACT_CLARIFY
4108end
4109
4110fn arx_decide to uchar
4111sc_arr = new f32 15 0.0f32
4112st = arx_analyse
4113if ne st ARX_WHY_SCORED
4114arx_exp_reset st
4115put arx_dec_act 0 ARX_ACT_CLARIFY
4116ret ARX_ACT_CLARIFY
4117end
4118rr = arx_policy_check
4119if ne rr ARX_REFUSE_NONE
4120arx_exp_reset ARX_WHY_POLICY
4121put arx_exp_refuse 0 rr
4122put arx_dec_act 0 ARX_ACT_REFUSE
4123ret ARX_ACT_REFUSE
4124end
4125fn_ign = arx_build_feats
4126arx_score_current
4127if eq get arx_dlg_pending 0 ARX_PEND_NO
4128put arx_sc_s ulong ARX_ACT_APPROVE -9.0f32
4129put arx_sc_s ulong ARX_ACT_DENY -9.0f32
4130put arx_sc_s ulong ARX_ACT_CANCEL -9.0f32
4131end
4132l = ulong 0
4133while lt l ARX_N_LABELS bound 15
4134put sc_arr l get arx_sc_s l
4135l = add l 1
4136end
4137top = arx_argmax sc_arr
4138top_s = get sc_arr top
4139put sc_arr top -1.0e38f32
4140sec = arx_argmax sc_arr
4141act = arx_decide_select top sec sub top_s get sc_arr sec
4142if arx_act_has_text act
4143ret arx_decide_extract_span act
4144end
4145put arx_dec_act 0 act
4146ret act
4147end
4148
4149text arx_s_json_p1 "{\"action\":\""
4150text arx_s_json_p2 "\",\"args\":{"
4151text arx_s_json_p3 "\"text\":\""
4152text arx_s_json_p4 "\"}}"
4153text arx_s_json_p5 "}}"
4154
4155fn arx_decision_json dst edit uchar * to ulong
4156p = ulong 0
4157p = arx_copy_bytes dst p arx_s_json_p1
4158p = arx_act_write_name get arx_dec_act 0 dst p
4159p = arx_copy_bytes dst p arx_s_json_p2
4160if ne get arx_dec_has_txt 0 0
4161p = arx_copy_bytes dst p arx_s_json_p3
4162tn = get arx_dec_txt_len 0
4163i = ulong 0
4164while lt i tn bound 64
4165put dst add p i get arx_dec_txt i
4166i = add i 1
4167end
4168p = add p tn
4169p = arx_copy_bytes dst p arx_s_json_p4
4170else
4171p = arx_copy_bytes dst p arx_s_json_p5
4172end
4173ret p
4174end
4175
4176fn arx_store_model lbl uchar wrong uchar online bool msg view uchar * to uchar
4177m = get arx_cfg_mode 0
4178if eq m ARX_MODE_REFLEX
4179arx_hv_encode_to_q get arx_cfg_hv_words 0
4180idx = arx_hv_push_q lbl msg
4181if lt idx 0
4182ret ARX_LEARN_FULL
4183end
4184if online
4185arx_hv_online ulong idx wrong
4186end
4187ret ARX_LEARN_OK
4188end
4189sp_ign = arx_sparse_build_q
4190if eq m ARX_MODE_NEAREST
4191idx = arx_nr_push_q lbl msg
4192if lt idx 0
4193ret ARX_LEARN_FULL
4194end
4195ret ARX_LEARN_OK
4196end
4197if online
4198arx_pc_online_q lbl
4199else
4200arx_pc_push_q lbl
4201end
4202ret ARX_LEARN_OK
4203end
4204
4205fn arx_store_cue lbl uchar msg view uchar * txt view uchar * to void
4206bs = arx_find_ci msg txt
4207if ge bs 0
4208cs = get arx_pri_start 0
4209ce = get arx_pri_end 0
4210ub = ulong bs
4211ue = add ub len txt
4212cl_ign = arx_cue_learn cs ce lbl ub ue
4213end
4214end
4215
4216fn arx_store lbl uchar has_txt bool txt view uchar * wrong uchar online bool to uchar
4217msg = new uchar 4096 0
4218if ne arx_analyse ARX_WHY_SCORED
4219ret ARX_LEARN_NO_USER
4220end
4221fn_ign = arx_build_feats
4222mlen = get arx_dlg_msg_len 0
4223i = ulong 0
4224while lt i mlen bound 4096
4225put msg i get arx_dlg_msg i
4226i = add i 1
4227end
4228res = arx_store_model lbl wrong online part msg 0 mlen
4229if ne res ARX_LEARN_OK
4230ret res
4231end
4232if has_txt
4233if arx_act_has_text lbl
4234arx_store_cue lbl part msg 0 mlen txt
4235end
4236end
4237ret ARX_LEARN_OK
4238end
4239
4240text arx_s_dummy "x"
4241
4242fn arx_train lbl uchar has_txt bool txt view uchar * to uchar
4243ret arx_store lbl has_txt txt ARX_ACT_INVALID false
4244end
4245
4246fn arx_finish_training to void
4247m = get arx_cfg_mode 0
4248if eq m ARX_MODE_REFLEX
4249rb_ign = arx_hv_rebuild
4250else
4251if eq m ARX_MODE_PERCEPTRON
4252arx_pc_fit
4253end
4254end
4255end
4256
4257fn arx_learn_correct lbl uchar has_txt bool txt view uchar * to uchar
4258if eq get arx_learning 0 0
4259ret ARX_LEARN_OFF
4260end
4261pred = arx_decide
4262wrong = ARX_ACT_INVALID
4263if ne pred lbl
4264wrong = pred
4265end
4266ret arx_store lbl has_txt txt wrong true
4267end
4268
4269fn arx_sleep to void
4270m = get arx_cfg_mode 0
4271if eq m ARX_MODE_REFLEX
4272arx_hv_sleep get arx_cfg_sleep_dup 0 get arx_cfg_sleep_con 0
4273else
4274if eq m ARX_MODE_NEAREST
4275put arx_slp_merged 0 0
4276put arx_slp_kept 0 get arx_nr_n 0
4277put arx_slp_passes 0 0
4278put arx_slp_con_n 0 0
4279else
4280arx_pc_fit
4281put arx_slp_merged 0 0
4282put arx_slp_kept 0 0
4283put arx_slp_passes 0 0
4284put arx_slp_con_n 0 0
4285end
4286end
4287end