runmat-runtime 0.6.0

Core runtime for RunMat with builtins, BLAS/LAPACK integration, and execution APIs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
//! Word encoding compatibility objects and word/index lookup helpers.

use std::cell::Cell;
use std::collections::HashMap;

use runmat_builtins::{
    Access, BuiltinCompletionPolicy, BuiltinDescriptor, BuiltinErrorDescriptor, BuiltinOutputMode,
    BuiltinParamArity, BuiltinParamDescriptor, BuiltinParamType, BuiltinSignatureDescriptor,
    CharArray, ClassDef, LogicalArray, ObjectInstance, PropertyDef, ResolveContext, StringArray,
    Tensor, Type, Value,
};
use runmat_macros::runtime_builtin;

use crate::builtins::strings::core::compat::scalar_text;
use crate::builtins::strings::text_analytics::documents::{
    documents_from_object, TOKENIZED_DOCUMENT_CLASS,
};
use crate::builtins::strings::text_analytics::embeddings::{
    build_word_lookup, word_embedding_vocabulary_from_object, WORD_EMBEDDING_CLASS,
};
use crate::{build_runtime_error, gather_if_needed_async, BuiltinResult};

pub const WORD_ENCODING_CLASS: &str = "wordEncoding";

thread_local! {
    static WORD_ENCODING_CLASS_REGISTERED: Cell<bool> = const { Cell::new(false) };
}

const OUT_ENCODING: [BuiltinParamDescriptor; 1] = [BuiltinParamDescriptor {
    name: "enc",
    ty: BuiltinParamType::Any,
    arity: BuiltinParamArity::Required,
    default: None,
    description: "Word encoding compatibility object.",
}];

const OUT_INDICES: [BuiltinParamDescriptor; 1] = [BuiltinParamDescriptor {
    name: "M",
    ty: BuiltinParamType::NumericArray,
    arity: BuiltinParamArity::Required,
    default: None,
    description: "Word encoding indices, with NaN for words outside the vocabulary.",
}];

const OUT_WORDS: [BuiltinParamDescriptor; 1] = [BuiltinParamDescriptor {
    name: "words",
    ty: BuiltinParamType::Any,
    arity: BuiltinParamArity::Required,
    default: None,
    description: "Words mapped from encoding indices.",
}];

const OUT_LOGICAL: [BuiltinParamDescriptor; 1] = [BuiltinParamDescriptor {
    name: "tf",
    ty: BuiltinParamType::LogicalArray,
    arity: BuiltinParamArity::Required,
    default: None,
    description: "Logical membership mask.",
}];

const IN_DOCUMENTS_OR_WORDS: [BuiltinParamDescriptor; 1] = [BuiltinParamDescriptor {
    name: "documentsOrWords",
    ty: BuiltinParamType::Any,
    arity: BuiltinParamArity::Required,
    default: None,
    description: "tokenizedDocument object or word vector.",
}];

const IN_DOCUMENTS_OR_WORDS_REST: [BuiltinParamDescriptor; 2] = [
    BuiltinParamDescriptor {
        name: "documentsOrWords",
        ty: BuiltinParamType::Any,
        arity: BuiltinParamArity::Required,
        default: None,
        description: "tokenizedDocument object or word vector.",
    },
    BuiltinParamDescriptor {
        name: "NameValue",
        ty: BuiltinParamType::Any,
        arity: BuiltinParamArity::Variadic,
        default: None,
        description: "Name-value options: Order, MaxNumWords.",
    },
];

const IN_WORDS: [BuiltinParamDescriptor; 2] = [
    BuiltinParamDescriptor {
        name: "enc",
        ty: BuiltinParamType::Any,
        arity: BuiltinParamArity::Required,
        default: None,
        description: "wordEncoding object.",
    },
    BuiltinParamDescriptor {
        name: "words",
        ty: BuiltinParamType::Any,
        arity: BuiltinParamArity::Required,
        default: None,
        description: "Words to map to indices.",
    },
];

const IN_WORDS_REST: [BuiltinParamDescriptor; 3] = [
    BuiltinParamDescriptor {
        name: "enc",
        ty: BuiltinParamType::Any,
        arity: BuiltinParamArity::Required,
        default: None,
        description: "wordEncoding object.",
    },
    BuiltinParamDescriptor {
        name: "words",
        ty: BuiltinParamType::Any,
        arity: BuiltinParamArity::Required,
        default: None,
        description: "Words to map to indices.",
    },
    BuiltinParamDescriptor {
        name: "NameValue",
        ty: BuiltinParamType::Any,
        arity: BuiltinParamArity::Variadic,
        default: None,
        description: "Name-value options: IgnoreCase.",
    },
];

const IN_INDICES: [BuiltinParamDescriptor; 2] = [
    BuiltinParamDescriptor {
        name: "enc",
        ty: BuiltinParamType::Any,
        arity: BuiltinParamArity::Required,
        default: None,
        description: "wordEncoding object.",
    },
    BuiltinParamDescriptor {
        name: "M",
        ty: BuiltinParamType::NumericArray,
        arity: BuiltinParamArity::Required,
        default: None,
        description: "Positive integer word encoding indices.",
    },
];

const IN_VOCABULARY_WORDS: [BuiltinParamDescriptor; 2] = [
    BuiltinParamDescriptor {
        name: "embOrEnc",
        ty: BuiltinParamType::Any,
        arity: BuiltinParamArity::Required,
        default: None,
        description: "wordEmbedding or wordEncoding object.",
    },
    BuiltinParamDescriptor {
        name: "words",
        ty: BuiltinParamType::Any,
        arity: BuiltinParamArity::Required,
        default: None,
        description: "Words to test.",
    },
];

const IN_VOCABULARY_WORDS_REST: [BuiltinParamDescriptor; 3] = [
    BuiltinParamDescriptor {
        name: "embOrEnc",
        ty: BuiltinParamType::Any,
        arity: BuiltinParamArity::Required,
        default: None,
        description: "wordEmbedding or wordEncoding object.",
    },
    BuiltinParamDescriptor {
        name: "words",
        ty: BuiltinParamType::Any,
        arity: BuiltinParamArity::Required,
        default: None,
        description: "Words to test.",
    },
    BuiltinParamDescriptor {
        name: "NameValue",
        ty: BuiltinParamType::Any,
        arity: BuiltinParamArity::Variadic,
        default: None,
        description: "Name-value options: IgnoreCase.",
    },
];

const ERROR_ENCODING_INVALID_INPUT: BuiltinErrorDescriptor = BuiltinErrorDescriptor {
    code: "RM.WORDENCODING.INVALID_INPUT",
    identifier: Some("RunMat:wordEncoding:InvalidInput"),
    when: "Inputs do not match a supported wordEncoding form.",
    message: "wordEncoding received invalid input",
};

const ERROR_WORD2IND_INVALID_INPUT: BuiltinErrorDescriptor = BuiltinErrorDescriptor {
    code: "RM.WORD2IND.INVALID_INPUT",
    identifier: Some("RunMat:word2ind:InvalidInput"),
    when: "Inputs do not match a supported word2ind form.",
    message: "word2ind received invalid input",
};

const ERROR_IND2WORD_INVALID_INPUT: BuiltinErrorDescriptor = BuiltinErrorDescriptor {
    code: "RM.IND2WORD.INVALID_INPUT",
    identifier: Some("RunMat:ind2word:InvalidInput"),
    when: "Inputs do not match a supported ind2word form.",
    message: "ind2word received invalid input",
};

const ERROR_IS_VOCABULARY_WORD_INVALID_INPUT: BuiltinErrorDescriptor = BuiltinErrorDescriptor {
    code: "RM.ISVOCABULARYWORD.INVALID_INPUT",
    identifier: Some("RunMat:isVocabularyWord:InvalidInput"),
    when: "Inputs do not match a supported isVocabularyWord form.",
    message: "isVocabularyWord received invalid input",
};

const WORD_ENCODING_ERRORS: [BuiltinErrorDescriptor; 1] = [ERROR_ENCODING_INVALID_INPUT];
const WORD2IND_ERRORS: [BuiltinErrorDescriptor; 1] = [ERROR_WORD2IND_INVALID_INPUT];
const IND2WORD_ERRORS: [BuiltinErrorDescriptor; 1] = [ERROR_IND2WORD_INVALID_INPUT];
const IS_VOCABULARY_WORD_ERRORS: [BuiltinErrorDescriptor; 1] =
    [ERROR_IS_VOCABULARY_WORD_INVALID_INPUT];

fn any_type(_args: &[Type], _ctx: &ResolveContext) -> Type {
    Type::Unknown
}

pub const WORD_ENCODING_DESCRIPTOR: BuiltinDescriptor = BuiltinDescriptor {
    signatures: &[
        BuiltinSignatureDescriptor {
            label: "enc = wordEncoding(documents)",
            inputs: &IN_DOCUMENTS_OR_WORDS,
            outputs: &OUT_ENCODING,
        },
        BuiltinSignatureDescriptor {
            label: "enc = wordEncoding(words)",
            inputs: &IN_DOCUMENTS_OR_WORDS,
            outputs: &OUT_ENCODING,
        },
        BuiltinSignatureDescriptor {
            label: "enc = wordEncoding(documents, Name, Value)",
            inputs: &IN_DOCUMENTS_OR_WORDS_REST,
            outputs: &OUT_ENCODING,
        },
    ],
    output_mode: BuiltinOutputMode::Fixed,
    completion_policy: BuiltinCompletionPolicy::Public,
    errors: &WORD_ENCODING_ERRORS,
};

pub const WORD2IND_DESCRIPTOR: BuiltinDescriptor = BuiltinDescriptor {
    signatures: &[
        BuiltinSignatureDescriptor {
            label: "M = word2ind(enc, words)",
            inputs: &IN_WORDS,
            outputs: &OUT_INDICES,
        },
        BuiltinSignatureDescriptor {
            label: "M = word2ind(enc, words, 'IgnoreCase', true)",
            inputs: &IN_WORDS_REST,
            outputs: &OUT_INDICES,
        },
    ],
    output_mode: BuiltinOutputMode::Fixed,
    completion_policy: BuiltinCompletionPolicy::Public,
    errors: &WORD2IND_ERRORS,
};

pub const IND2WORD_DESCRIPTOR: BuiltinDescriptor = BuiltinDescriptor {
    signatures: &[BuiltinSignatureDescriptor {
        label: "words = ind2word(enc, M)",
        inputs: &IN_INDICES,
        outputs: &OUT_WORDS,
    }],
    output_mode: BuiltinOutputMode::Fixed,
    completion_policy: BuiltinCompletionPolicy::Public,
    errors: &IND2WORD_ERRORS,
};

pub const IS_VOCABULARY_WORD_DESCRIPTOR: BuiltinDescriptor = BuiltinDescriptor {
    signatures: &[
        BuiltinSignatureDescriptor {
            label: "tf = isVocabularyWord(emb, words)",
            inputs: &IN_VOCABULARY_WORDS,
            outputs: &OUT_LOGICAL,
        },
        BuiltinSignatureDescriptor {
            label: "tf = isVocabularyWord(enc, words)",
            inputs: &IN_VOCABULARY_WORDS,
            outputs: &OUT_LOGICAL,
        },
        BuiltinSignatureDescriptor {
            label: "tf = isVocabularyWord(___, 'IgnoreCase', true)",
            inputs: &IN_VOCABULARY_WORDS_REST,
            outputs: &OUT_LOGICAL,
        },
    ],
    output_mode: BuiltinOutputMode::Fixed,
    completion_policy: BuiltinCompletionPolicy::Public,
    errors: &IS_VOCABULARY_WORD_ERRORS,
};

#[runtime_builtin(
    name = "wordEncoding",
    category = "strings/text_analytics",
    summary = "Create a word encoding object that maps words to indices and back.",
    keywords = "wordEncoding,text analytics,words,indices,vocabulary",
    accel = "sink",
    type_resolver(any_type),
    descriptor(crate::builtins::strings::text_analytics::encoding::WORD_ENCODING_DESCRIPTOR),
    builtin_path = "crate::builtins::strings::text_analytics::encoding"
)]
async fn word_encoding_builtin(args: Vec<Value>) -> BuiltinResult<Value> {
    let gathered = gather_args(args, "wordEncoding").await?;
    let (source, options) = parse_word_encoding_args(gathered)?;
    word_encoding_object(build_word_encoding(source, options)?)
}

#[runtime_builtin(
    name = "word2ind",
    category = "strings/text_analytics",
    summary = "Map words to indices in a wordEncoding object.",
    keywords = "word2ind,wordEncoding,text analytics,indices,vocabulary",
    accel = "sink",
    type_resolver(any_type),
    descriptor(crate::builtins::strings::text_analytics::encoding::WORD2IND_DESCRIPTOR),
    builtin_path = "crate::builtins::strings::text_analytics::encoding"
)]
async fn word2ind_builtin(args: Vec<Value>) -> BuiltinResult<Value> {
    let gathered = gather_args(args, "word2ind").await?;
    let (object, words, options) = parse_word2ind_args(gathered)?;
    let encoding = word_encoding_from_object(&object, "word2ind")?;
    let lookup = build_word_lookup(&encoding.vocabulary, options.ignore_case);
    let indices = words
        .words
        .into_iter()
        .map(|word| {
            let key = if options.ignore_case {
                word.to_lowercase()
            } else {
                word
            };
            lookup
                .get(&key)
                .map(|idx| (*idx + 1) as f64)
                .unwrap_or(f64::NAN)
        })
        .collect::<Vec<_>>();
    Tensor::new(indices, words.shape)
        .map(Value::Tensor)
        .map_err(|err| encoding_error("word2ind", err))
}

#[runtime_builtin(
    name = "ind2word",
    category = "strings/text_analytics",
    summary = "Map wordEncoding indices back to words.",
    keywords = "ind2word,wordEncoding,text analytics,indices,vocabulary",
    accel = "sink",
    type_resolver(any_type),
    descriptor(crate::builtins::strings::text_analytics::encoding::IND2WORD_DESCRIPTOR),
    builtin_path = "crate::builtins::strings::text_analytics::encoding"
)]
async fn ind2word_builtin(args: Vec<Value>) -> BuiltinResult<Value> {
    let gathered = gather_args(args, "ind2word").await?;
    let (object, indices) = parse_ind2word_args(gathered)?;
    let encoding = word_encoding_from_object(&object, "ind2word")?;
    let words = indices
        .values
        .into_iter()
        .map(|idx| {
            let word_idx = positive_index(idx, encoding.vocabulary.len(), "ind2word")?;
            Ok(encoding.vocabulary[word_idx].clone())
        })
        .collect::<BuiltinResult<Vec<_>>>()?;
    StringArray::new(words, indices.shape)
        .map(Value::StringArray)
        .map_err(|err| encoding_error("ind2word", err))
}

#[runtime_builtin(
    name = "isVocabularyWord",
    category = "strings/text_analytics",
    summary = "Test whether words are in a wordEmbedding or wordEncoding vocabulary.",
    keywords = "isVocabularyWord,wordEmbedding,wordEncoding,text analytics,vocabulary",
    accel = "sink",
    type_resolver(any_type),
    descriptor(crate::builtins::strings::text_analytics::encoding::IS_VOCABULARY_WORD_DESCRIPTOR),
    builtin_path = "crate::builtins::strings::text_analytics::encoding"
)]
async fn is_vocabulary_word_builtin(args: Vec<Value>) -> BuiltinResult<Value> {
    let gathered = gather_args(args, "isVocabularyWord").await?;
    let (object, words, options) = parse_is_vocabulary_word_args(gathered)?;
    let vocabulary = if object.is_class(WORD_ENCODING_CLASS) {
        word_encoding_from_object(&object, "isVocabularyWord")?.vocabulary
    } else if object.is_class(WORD_EMBEDDING_CLASS) {
        word_embedding_vocabulary_from_object(&object, "isVocabularyWord")?
    } else {
        return Err(encoding_error(
            "isVocabularyWord",
            format!(
                "isVocabularyWord: expected wordEmbedding or wordEncoding object, got {}",
                object.class_name
            ),
        ));
    };
    let lookup = build_word_lookup(&vocabulary, options.ignore_case);
    let flags = words
        .words
        .into_iter()
        .map(|word| {
            let key = if options.ignore_case {
                word.to_lowercase()
            } else {
                word
            };
            u8::from(lookup.contains_key(&key))
        })
        .collect::<Vec<_>>();
    LogicalArray::new(flags, words.shape)
        .map(Value::LogicalArray)
        .map_err(|err| encoding_error("isVocabularyWord", err))
}

async fn gather_args(args: Vec<Value>, fn_name: &str) -> BuiltinResult<Vec<Value>> {
    let mut out = Vec::with_capacity(args.len());
    for arg in args {
        out.push(gather_if_needed_async(&arg).await.map_err(|err| {
            encoding_error(fn_name, format!("{fn_name}: failed to gather input: {err}"))
        })?);
    }
    Ok(out)
}

#[derive(Clone, Debug)]
pub(in crate::builtins::strings::text_analytics) struct WordEncodingModel {
    pub vocabulary: Vec<String>,
}

pub(in crate::builtins::strings::text_analytics) fn word_encoding_from_object(
    object: &ObjectInstance,
    fn_name: &str,
) -> BuiltinResult<WordEncodingModel> {
    if !object.is_class(WORD_ENCODING_CLASS) {
        return Err(encoding_error(
            fn_name,
            format!(
                "{fn_name}: expected wordEncoding object, got {}",
                object.class_name
            ),
        ));
    }
    let vocabulary = match object.properties.get("Vocabulary") {
        Some(Value::StringArray(array)) => array.data.clone(),
        other => {
            return Err(encoding_error(
                fn_name,
                format!(
                    "{fn_name}: wordEncoding object has invalid Vocabulary property: {other:?}"
                ),
            ));
        }
    };
    match object.properties.get("NumWords") {
        Some(Value::Num(value)) if *value == vocabulary.len() as f64 => {}
        other => {
            return Err(encoding_error(
                fn_name,
                format!("{fn_name}: wordEncoding object has invalid NumWords property: {other:?}"),
            ));
        }
    }
    Ok(WordEncodingModel { vocabulary })
}

fn word_encoding_object(model: WordEncodingModel) -> BuiltinResult<Value> {
    ensure_word_encoding_class_registered();
    let mut object = ObjectInstance::new(WORD_ENCODING_CLASS.to_string());
    object.properties.insert(
        "NumWords".to_string(),
        Value::Num(model.vocabulary.len() as f64),
    );
    object.properties.insert(
        "Vocabulary".to_string(),
        Value::StringArray(
            StringArray::new(model.vocabulary.clone(), vec![1, model.vocabulary.len()])
                .map_err(|err| encoding_error("wordEncoding", err))?,
        ),
    );
    Ok(Value::Object(object))
}

fn ensure_word_encoding_class_registered() {
    WORD_ENCODING_CLASS_REGISTERED.with(|registered| {
        if registered.get() {
            return;
        }
        let mut properties = HashMap::new();
        for name in ["NumWords", "Vocabulary"] {
            properties.insert(name.to_string(), property_def(name));
        }
        runmat_builtins::register_class(ClassDef {
            name: WORD_ENCODING_CLASS.to_string(),
            parent: None,
            properties,
            methods: HashMap::new(),
        });
        registered.set(true);
    });
}

fn property_def(name: &str) -> PropertyDef {
    PropertyDef {
        name: name.to_string(),
        is_static: false,
        is_constant: false,
        is_dependent: false,
        get_access: Access::Public,
        set_access: Access::Public,
        default_value: None,
    }
}

enum EncodingSource {
    Documents(Vec<Vec<String>>),
    Words(Vec<String>),
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum EncodingOrder {
    FirstSeen,
    Frequency,
}

#[derive(Clone, Copy, Debug)]
struct WordEncodingOptions {
    order: EncodingOrder,
    max_num_words: Option<usize>,
}

impl Default for WordEncodingOptions {
    fn default() -> Self {
        Self {
            order: EncodingOrder::FirstSeen,
            max_num_words: None,
        }
    }
}

fn parse_word_encoding_args(
    args: Vec<Value>,
) -> BuiltinResult<(EncodingSource, WordEncodingOptions)> {
    if args.is_empty() {
        return Err(encoding_error(
            "wordEncoding",
            "wordEncoding: expected tokenizedDocument object or word vector",
        ));
    }
    if !(args.len() - 1).is_multiple_of(2) {
        return Err(encoding_error(
            "wordEncoding",
            "wordEncoding: name-value options must be paired",
        ));
    }
    let source = match &args[0] {
        Value::Object(object) if object.is_class(TOKENIZED_DOCUMENT_CLASS) => {
            EncodingSource::Documents(documents_from_object(object, "wordEncoding")?)
        }
        Value::Object(object) => {
            return Err(encoding_error(
                "wordEncoding",
                format!(
                    "wordEncoding: expected tokenizedDocument object or word vector, got {}",
                    object.class_name
                ),
            ));
        }
        value => EncodingSource::Words(word_input_from_value(value, "wordEncoding")?.words),
    };
    if matches!(source, EncodingSource::Words(_)) && args.len() > 1 {
        return Err(encoding_error(
            "wordEncoding",
            "wordEncoding: Order and MaxNumWords options are only supported for tokenizedDocument input",
        ));
    }
    let mut options = WordEncodingOptions::default();
    let mut idx = 1usize;
    while idx < args.len() {
        let name = scalar_text(&args[idx], "wordEncoding")
            .map_err(|err| encoding_error("wordEncoding", err.to_string()))?
            .to_ascii_lowercase();
        match name.as_str() {
            "order" => {
                let value = scalar_text(&args[idx + 1], "wordEncoding")
                    .map_err(|err| encoding_error("wordEncoding", err.to_string()))?
                    .to_ascii_lowercase();
                options.order = match value.as_str() {
                    "first-seen" => EncodingOrder::FirstSeen,
                    "frequency" => EncodingOrder::Frequency,
                    other => {
                        return Err(encoding_error(
                            "wordEncoding",
                            format!(
                                "wordEncoding: Order must be 'first-seen' or 'frequency', got '{other}'"
                            ),
                        ));
                    }
                };
            }
            "maxnumwords" => {
                options.max_num_words = parse_max_num_words(&args[idx + 1])?;
            }
            other => {
                return Err(encoding_error(
                    "wordEncoding",
                    format!("wordEncoding: unsupported option '{other}'"),
                ));
            }
        }
        idx += 2;
    }
    Ok((source, options))
}

fn build_word_encoding(
    source: EncodingSource,
    options: WordEncodingOptions,
) -> BuiltinResult<WordEncodingModel> {
    let words = match source {
        EncodingSource::Documents(documents) => documents.into_iter().flatten().collect::<Vec<_>>(),
        EncodingSource::Words(words) => words,
    };
    let mut counts = HashMap::<String, (usize, usize)>::new();
    for (pos, word) in words.into_iter().enumerate() {
        let entry = counts.entry(word).or_insert((0, pos));
        entry.0 += 1;
    }
    let mut ranked = counts
        .into_iter()
        .map(|(word, (count, first_pos))| (word, count, first_pos))
        .collect::<Vec<_>>();
    match options.order {
        EncodingOrder::FirstSeen => ranked.sort_by(|left, right| left.2.cmp(&right.2)),
        EncodingOrder::Frequency => {
            ranked.sort_by(|left, right| right.1.cmp(&left.1).then(left.2.cmp(&right.2)))
        }
    }
    if let Some(max) = options.max_num_words {
        ranked.truncate(max);
    }
    Ok(WordEncodingModel {
        vocabulary: ranked.into_iter().map(|(word, _, _)| word).collect(),
    })
}

#[derive(Clone, Copy, Debug, Default)]
struct LookupOptions {
    ignore_case: bool,
}

fn parse_word2ind_args(
    args: Vec<Value>,
) -> BuiltinResult<(ObjectInstance, WordInput, LookupOptions)> {
    if args.len() < 2 {
        return Err(encoding_error(
            "word2ind",
            "word2ind: expected word2ind(enc, words)",
        ));
    }
    let object = object_arg(&args[0], "word2ind", "wordEncoding")?;
    let words = word_input_from_value(&args[1], "word2ind")?;
    let options = parse_lookup_options(&args[2..], "word2ind")?;
    Ok((object, words, options))
}

fn parse_is_vocabulary_word_args(
    args: Vec<Value>,
) -> BuiltinResult<(ObjectInstance, WordInput, LookupOptions)> {
    if args.len() < 2 {
        return Err(encoding_error(
            "isVocabularyWord",
            "isVocabularyWord: expected isVocabularyWord(embOrEnc, words)",
        ));
    }
    let object = object_arg(
        &args[0],
        "isVocabularyWord",
        "wordEmbedding or wordEncoding",
    )?;
    let words = word_input_from_value(&args[1], "isVocabularyWord")?;
    let options = parse_lookup_options(&args[2..], "isVocabularyWord")?;
    Ok((object, words, options))
}

fn parse_ind2word_args(args: Vec<Value>) -> BuiltinResult<(ObjectInstance, NumericInput)> {
    if args.len() != 2 {
        return Err(encoding_error(
            "ind2word",
            "ind2word: expected ind2word(enc, M)",
        ));
    }
    let object = object_arg(&args[0], "ind2word", "wordEncoding")?;
    let indices = numeric_input_from_value(&args[1], "ind2word")?;
    Ok((object, indices))
}

fn object_arg(value: &Value, fn_name: &str, expected: &str) -> BuiltinResult<ObjectInstance> {
    match value {
        Value::Object(object) => Ok(object.clone()),
        other => Err(encoding_error(
            fn_name,
            format!("{fn_name}: expected {expected} object, got {other:?}"),
        )),
    }
}

fn parse_lookup_options(args: &[Value], fn_name: &str) -> BuiltinResult<LookupOptions> {
    if !args.len().is_multiple_of(2) {
        return Err(encoding_error(
            fn_name,
            format!("{fn_name}: name-value options must be paired"),
        ));
    }
    let mut options = LookupOptions::default();
    let mut idx = 0usize;
    while idx < args.len() {
        let name = scalar_text(&args[idx], fn_name)
            .map_err(|err| encoding_error(fn_name, err.to_string()))?
            .to_ascii_lowercase();
        match name.as_str() {
            "ignorecase" => options.ignore_case = parse_bool_scalar(&args[idx + 1], fn_name)?,
            other => {
                return Err(encoding_error(
                    fn_name,
                    format!("{fn_name}: unsupported option '{other}'"),
                ));
            }
        }
        idx += 2;
    }
    Ok(options)
}

struct WordInput {
    words: Vec<String>,
    shape: Vec<usize>,
}

fn word_input_from_value(value: &Value, fn_name: &str) -> BuiltinResult<WordInput> {
    match value {
        Value::String(text) => Ok(WordInput {
            words: vec![text.clone()],
            shape: vec![1, 1],
        }),
        Value::StringArray(array) => Ok(WordInput {
            words: array.data.clone(),
            shape: array.shape.clone(),
        }),
        Value::CharArray(array) if array.rows <= 1 => Ok(WordInput {
            words: vec![char_row_to_string(array)],
            shape: vec![1, 1],
        }),
        Value::CharArray(array) => {
            let mut words = Vec::with_capacity(array.rows);
            for row in 0..array.rows {
                let mut text = String::with_capacity(array.cols);
                for col in 0..array.cols {
                    text.push(array.data[row + col * array.rows]);
                }
                words.push(text.trim_end().to_string());
            }
            Ok(WordInput {
                words,
                shape: vec![array.rows, 1],
            })
        }
        Value::Cell(cell) => {
            let words = cell
                .data
                .iter()
                .map(|item| {
                    scalar_text(item, fn_name)
                        .map_err(|err| encoding_error(fn_name, err.to_string()))
                })
                .collect::<BuiltinResult<Vec<_>>>()?;
            Ok(WordInput {
                words,
                shape: cell.shape.clone(),
            })
        }
        other => Err(encoding_error(
            fn_name,
            format!("{fn_name}: expected string, character vector, or cell array of words, got {other:?}"),
        )),
    }
}

struct NumericInput {
    values: Vec<f64>,
    shape: Vec<usize>,
}

fn numeric_input_from_value(value: &Value, fn_name: &str) -> BuiltinResult<NumericInput> {
    match value {
        Value::Num(value) => Ok(NumericInput {
            values: vec![*value],
            shape: vec![1, 1],
        }),
        Value::Int(value) => Ok(NumericInput {
            values: vec![int_value_to_f64(value)],
            shape: vec![1, 1],
        }),
        Value::Tensor(tensor) => Ok(NumericInput {
            values: tensor.data.clone(),
            shape: tensor.shape.clone(),
        }),
        other => Err(encoding_error(
            fn_name,
            format!("{fn_name}: expected numeric positive integer indices, got {other:?}"),
        )),
    }
}

fn positive_index(value: f64, len: usize, fn_name: &str) -> BuiltinResult<usize> {
    if !value.is_finite() || value < 1.0 || value.fract() != 0.0 {
        return Err(encoding_error(
            fn_name,
            format!("{fn_name}: indices must be positive integers, got {value}"),
        ));
    }
    let idx = value as usize;
    if idx > len {
        return Err(encoding_error(
            fn_name,
            format!("{fn_name}: index {idx} exceeds vocabulary size {len}"),
        ));
    }
    Ok(idx - 1)
}

fn parse_max_num_words(value: &Value) -> BuiltinResult<Option<usize>> {
    let n = numeric_scalar(value, "wordEncoding", "MaxNumWords")?;
    if n.is_infinite() && n.is_sign_positive() {
        return Ok(None);
    }
    if !n.is_finite() || n < 1.0 || n.fract() != 0.0 {
        return Err(encoding_error(
            "wordEncoding",
            format!("wordEncoding: MaxNumWords must be a positive integer or Inf, got {n}"),
        ));
    }
    Ok(Some(n as usize))
}

fn numeric_scalar(value: &Value, fn_name: &str, option: &str) -> BuiltinResult<f64> {
    match value {
        Value::Num(value) => Ok(*value),
        Value::Int(value) => Ok(int_value_to_f64(value)),
        Value::Tensor(tensor) if tensor.data.len() == 1 => Ok(tensor.data[0]),
        other => Err(encoding_error(
            fn_name,
            format!("{fn_name}: {option} must be a numeric scalar, got {other:?}"),
        )),
    }
}

fn parse_bool_scalar(value: &Value, fn_name: &str) -> BuiltinResult<bool> {
    match value {
        Value::Bool(value) => Ok(*value),
        Value::Num(value) if *value == 0.0 || *value == 1.0 => Ok(*value != 0.0),
        Value::Tensor(tensor) if tensor.data.len() == 1 => match tensor.data[0] {
            0.0 => Ok(false),
            1.0 => Ok(true),
            other => Err(encoding_error(
                fn_name,
                format!("{fn_name}: logical scalar option must be true or false, got {other}"),
            )),
        },
        Value::LogicalArray(array) if array.data.len() == 1 => Ok(array.data[0] != 0),
        other => Err(encoding_error(
            fn_name,
            format!("{fn_name}: logical scalar option must be true or false, got {other:?}"),
        )),
    }
}

fn int_value_to_f64(value: &runmat_builtins::IntValue) -> f64 {
    match value {
        runmat_builtins::IntValue::I8(value) => *value as f64,
        runmat_builtins::IntValue::I16(value) => *value as f64,
        runmat_builtins::IntValue::I32(value) => *value as f64,
        runmat_builtins::IntValue::I64(value) => *value as f64,
        runmat_builtins::IntValue::U8(value) => *value as f64,
        runmat_builtins::IntValue::U16(value) => *value as f64,
        runmat_builtins::IntValue::U32(value) => *value as f64,
        runmat_builtins::IntValue::U64(value) => *value as f64,
    }
}

fn char_row_to_string(array: &CharArray) -> String {
    array.data.iter().collect()
}

fn encoding_error(fn_name: &str, message: impl Into<String>) -> crate::RuntimeError {
    let descriptor = match fn_name {
        "word2ind" => ERROR_WORD2IND_INVALID_INPUT,
        "ind2word" => ERROR_IND2WORD_INVALID_INPUT,
        "isVocabularyWord" => ERROR_IS_VOCABULARY_WORD_INVALID_INPUT,
        _ => ERROR_ENCODING_INVALID_INPUT,
    };
    let builder = build_runtime_error(message.into()).with_builtin(fn_name);
    match descriptor.identifier {
        Some(identifier) => builder.with_identifier(identifier).build(),
        None => builder.build(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use runmat_builtins::CellArray;

    fn tokenized_document_object(rows: Vec<Vec<&str>>) -> ObjectInstance {
        let values = rows
            .into_iter()
            .map(|row| {
                let len = row.len();
                Value::StringArray(
                    StringArray::new(
                        row.into_iter().map(str::to_string).collect::<Vec<_>>(),
                        vec![1, len],
                    )
                    .unwrap(),
                )
            })
            .collect::<Vec<_>>();
        let rows = values.len();
        let documents = CellArray::new(values, rows, 1).unwrap();
        let mut object = ObjectInstance::new(TOKENIZED_DOCUMENT_CLASS.to_string());
        object
            .properties
            .insert("Documents".to_string(), Value::Cell(documents));
        object
    }

    #[tokio::test]
    async fn word_encoding_builds_first_seen_and_frequency_vocabularies() {
        let documents = Value::Object(tokenized_document_object(vec![
            vec!["beta", "alpha", "beta"],
            vec!["gamma", "alpha", "beta"],
        ]));
        let first_seen = word_encoding_builtin(vec![documents.clone()])
            .await
            .unwrap();
        let Value::Object(first_seen) = first_seen else {
            panic!("expected object");
        };
        let model = word_encoding_from_object(&first_seen, "test").unwrap();
        assert_eq!(model.vocabulary, vec!["beta", "alpha", "gamma"]);

        let frequency = word_encoding_builtin(vec![
            documents,
            Value::String("Order".into()),
            Value::String("frequency".into()),
            Value::String("MaxNumWords".into()),
            Value::Num(2.0),
        ])
        .await
        .unwrap();
        let Value::Object(frequency) = frequency else {
            panic!("expected object");
        };
        let model = word_encoding_from_object(&frequency, "test").unwrap();
        assert_eq!(model.vocabulary, vec!["beta", "alpha"]);
    }

    #[tokio::test]
    async fn word_encoding_accepts_word_arrays_and_validates_options() {
        let words = Value::StringArray(
            StringArray::new(vec!["red".into(), "blue".into(), "red".into()], vec![1, 3]).unwrap(),
        );
        let enc = word_encoding_builtin(vec![words]).await.unwrap();
        let Value::Object(enc) = enc else {
            panic!("expected object");
        };
        assert_eq!(enc.properties.get("NumWords"), Some(&Value::Num(2.0)));

        let err = word_encoding_builtin(vec![
            Value::String("x".into()),
            Value::String("Order".into()),
            Value::String("frequency".into()),
        ])
        .await
        .unwrap_err();
        assert!(
            err.to_string()
                .contains("only supported for tokenizedDocument input"),
            "{err}"
        );
    }

    #[tokio::test]
    async fn word2ind_preserves_shape_and_supports_ignore_case() {
        let enc = word_encoding_builtin(vec![Value::StringArray(
            StringArray::new(vec!["Alpha".into(), "beta".into()], vec![1, 2]).unwrap(),
        )])
        .await
        .unwrap();
        let words = Value::StringArray(
            StringArray::new(
                vec![
                    "beta".into(),
                    "missing".into(),
                    "alpha".into(),
                    "Alpha".into(),
                ],
                vec![2, 2],
            )
            .unwrap(),
        );
        let out = word2ind_builtin(vec![
            enc,
            words,
            Value::String("IgnoreCase".into()),
            Value::Bool(true),
        ])
        .await
        .unwrap();
        let Value::Tensor(indices) = out else {
            panic!("expected tensor");
        };
        assert_eq!(indices.shape, vec![2, 2]);
        assert_eq!(indices.data[0], 2.0);
        assert!(indices.data[1].is_nan());
        assert_eq!(indices.data[2], 1.0);
        assert_eq!(indices.data[3], 1.0);
    }

    #[tokio::test]
    async fn ind2word_preserves_numeric_shape_and_rejects_bad_indices() {
        let enc = word_encoding_builtin(vec![Value::StringArray(
            StringArray::new(
                vec!["red".into(), "blue".into(), "green".into()],
                vec![1, 3],
            )
            .unwrap(),
        )])
        .await
        .unwrap();
        let out = ind2word_builtin(vec![
            enc.clone(),
            Value::Tensor(Tensor::new(vec![1.0, 3.0], vec![1, 2]).unwrap()),
        ])
        .await
        .unwrap();
        let Value::StringArray(words) = out else {
            panic!("expected string array");
        };
        assert_eq!(words.shape, vec![1, 2]);
        assert_eq!(words.data, vec!["red", "green"]);

        let err = ind2word_builtin(vec![enc, Value::Num(4.0)])
            .await
            .unwrap_err();
        assert!(err.to_string().contains("exceeds vocabulary"), "{err}");
    }

    #[tokio::test]
    async fn is_vocabulary_word_supports_word_encoding() {
        let enc = word_encoding_builtin(vec![Value::StringArray(
            StringArray::new(vec!["RunMat".into(), "GPU".into()], vec![1, 2]).unwrap(),
        )])
        .await
        .unwrap();
        let words = Value::StringArray(
            StringArray::new(vec!["runmat".into(), "cpu".into()], vec![1, 2]).unwrap(),
        );
        let out = is_vocabulary_word_builtin(vec![
            enc,
            words,
            Value::String("IgnoreCase".into()),
            Value::Bool(true),
        ])
        .await
        .unwrap();
        let Value::LogicalArray(mask) = out else {
            panic!("expected logical array");
        };
        assert_eq!(mask.shape, vec![1, 2]);
        assert_eq!(mask.data, vec![1, 0]);
    }
}