provenant-cli 0.0.19

Rust-based ScanCode-compatible scanner for licenses, package metadata, SBOMs, and provenance data.
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
//! License index builder.
//!
//! This module implements the `build_index()` and `build_index_from_loaded()`
//! functions that construct all index data structures from rules and licenses.
//!
//! Based on the Python ScanCode Toolkit implementation at:
//! reference/scancode-toolkit/src/licensedcode/index.py (lines 381-577)

use std::collections::{HashMap, HashSet};

use crate::license_detection::automaton::{Automaton, AutomatonBuilder};
use crate::license_detection::hash_match::compute_hash;
use crate::license_detection::index::LicenseIndex;
use crate::license_detection::index::dictionary::{
    KnownToken, TokenDictionary, TokenId, TokenKind,
};
use crate::license_detection::models::{License, LoadedLicense, LoadedRule, Rule};
use crate::license_detection::rules::legalese;
use crate::license_detection::rules::thresholds::{
    SMALL_RULE, TINY_RULE, compute_thresholds_occurrences, compute_thresholds_unique,
};
use crate::license_detection::spdx_mapping::build_spdx_mapping;
use crate::license_detection::tokenize::{
    parse_required_phrase_spans, tokenize, tokenize_with_stopwords,
};
use crate::license_detection::{TokenMultiset, TokenSet};

const UNKNOWN_NGRAM_LENGTH: usize = 6;
const LICENSE_TOKEN_STRINGS: &[&str] = &["license", "licence", "licensed"];

const DEPRECATED_SPDX_SUBS: &[(&str, &str)] = &[
    ("ecos-2.0", "gpl-2.0-or-later with ecos-exception-2.0"),
    (
        "gpl-2.0-with-autoconf-exception",
        "gpl-2.0-only with autoconf-exception-2.0",
    ),
    (
        "gpl-2.0-with-bison-exception",
        "gpl-2.0-only with bison-exception-2.2",
    ),
    (
        "gpl-2.0-with-classpath-exception",
        "gpl-2.0-only with classpath-exception-2.0",
    ),
    (
        "gpl-2.0-with-font-exception",
        "gpl-2.0-only with font-exception-2.0",
    ),
    (
        "gpl-2.0-with-gcc-exception",
        "gpl-2.0-only with gcc-exception-2.0",
    ),
    (
        "gpl-3.0-with-autoconf-exception",
        "gpl-3.0-only with autoconf-exception-3.0",
    ),
    (
        "gpl-3.0-with-gcc-exception",
        "gpl-3.0-only with gcc-exception-3.1",
    ),
    (
        "wxwindows",
        "lgpl-2.0-or-later with wxwindows-exception-3.1",
    ),
];

fn add_deprecated_spdx_aliases(rid_by_spdx_key: &mut HashMap<String, usize>) {
    for (deprecated, replacement) in DEPRECATED_SPDX_SUBS {
        if let Some(&rid) = rid_by_spdx_key.get(*replacement) {
            rid_by_spdx_key.insert(deprecated.to_string(), rid);
        }
    }
}

fn populate_precomputed_rule_spdx(index: &mut LicenseIndex) {
    let licenses: Vec<_> = index.licenses_by_key.values().cloned().collect();
    let mapping = build_spdx_mapping(&licenses);

    for rule in &index.rules_by_rid {
        let Ok(license_expression_spdx) =
            mapping.expression_scancode_to_spdx(&rule.license_expression)
        else {
            continue;
        };

        index
            .rule_metadata_by_identifier
            .entry(rule.identifier.clone())
            .or_default()
            .license_expression_spdx = Some(license_expression_spdx);
    }
}

fn apply_loaded_rule_metadata(
    index: &mut LicenseIndex,
    loaded_rules: &[LoadedRule],
    with_deprecated: bool,
) {
    for loaded_rule in loaded_rules {
        if !with_deprecated && loaded_rule.is_deprecated {
            continue;
        }

        let metadata = index
            .rule_metadata_by_identifier
            .entry(loaded_rule.identifier.clone())
            .or_default();
        metadata.skip_for_required_phrase_generation =
            loaded_rule.skip_for_required_phrase_generation;
        metadata.replaced_by = loaded_rule.replaced_by.clone();
    }
}

fn prepare_rule_text(text: &str) -> String {
    text.lines()
        .map(|line| line.trim())
        .collect::<Vec<_>>()
        .join("\n")
}

#[cfg(test)]
pub(crate) fn generate_url_variants(
    text: &str,
    ignorable_urls: &Option<Vec<String>>,
) -> Vec<String> {
    let Some(urls) = ignorable_urls else {
        return vec![];
    };
    if urls.is_empty() {
        return vec![];
    }

    let mut variants = Vec::new();
    let current = text.to_string();

    for url in urls {
        let url_lower = url.to_lowercase();
        if url_lower.starts_with("https://") {
            let http_url = format!("http://{}", &url[8..]);
            if current.contains(url) {
                let variant = current.replace(url, &http_url);
                variants.push(variant);
            }
        } else if url_lower.starts_with("http://") {
            let https_url = format!("https://{}", &url[7..]);
            if current.contains(url) {
                let variant = current.replace(url, &https_url);
                variants.push(variant);
            }
        }
    }

    variants
}

fn build_rule_from_license(license: &License) -> Option<Rule> {
    let has_stored_minimum_coverage = license.minimum_coverage.is_some();

    let text = if license.text.is_empty() {
        "unknown-spdx license identifier".to_string()
    } else {
        prepare_rule_text(&license.text)
    };

    Some(Rule {
        identifier: format!("{}.LICENSE", license.key),
        license_expression: license.key.clone(),
        text,
        tokens: vec![],
        rule_kind: crate::license_detection::models::RuleKind::Text,
        is_false_positive: false,
        is_required_phrase: false,
        is_from_license: true,
        relevance: 100,
        minimum_coverage: license.minimum_coverage,
        has_stored_minimum_coverage,
        is_continuous: false,
        required_phrase_spans: vec![],
        stopwords_by_pos: HashMap::new(),
        referenced_filenames: None,
        ignorable_urls: license.ignorable_urls.clone(),
        ignorable_emails: license.ignorable_emails.clone(),
        ignorable_copyrights: license.ignorable_copyrights.clone(),
        ignorable_holders: license.ignorable_holders.clone(),
        ignorable_authors: license.ignorable_authors.clone(),
        language: None,
        notes: license.notes.clone(),
        length_unique: 0,
        high_length_unique: 0,
        high_length: 0,
        min_matched_length: 0,
        min_high_matched_length: 0,
        min_matched_length_unique: 0,
        min_high_matched_length_unique: 0,
        is_small: false,
        is_tiny: false,
        starts_with_license: false,
        ends_with_license: false,
        is_deprecated: license.is_deprecated,
        spdx_license_key: license.spdx_license_key.clone(),
        other_spdx_license_keys: license.other_spdx_license_keys.clone(),
    })
}

fn build_rules_from_licenses(licenses: &[License]) -> Vec<Rule> {
    licenses
        .iter()
        .filter_map(build_rule_from_license)
        .collect()
}

fn get_essential_spdx_tokens() -> &'static [&'static str] {
    &["spdx", "license", "licence", "identifier", "licenseref"]
}

fn collect_spdx_tokens(licenses: &[License]) -> HashSet<String> {
    let mut tokens: HashSet<String> = HashSet::new();
    for &tok in get_essential_spdx_tokens() {
        tokens.insert(tok.to_string());
    }
    for license in licenses {
        if let Some(ref spdx_key) = license.spdx_license_key {
            for token in tokenize(spdx_key) {
                tokens.insert(token);
            }
        }
        for spdx_key in &license.other_spdx_license_keys {
            for token in tokenize(spdx_key) {
                tokens.insert(token);
            }
        }
    }
    tokens
}

const MARKERS: &[&str] = &[
    "copyright",
    "c",
    "copyrights",
    "rights",
    "reserved",
    "trademark",
    "foundation",
    "government",
    "institute",
    "university",
    "inc",
    "corp",
    "co",
    "author",
    "com",
    "org",
    "net",
    "uk",
    "fr",
    "be",
    "de",
    "http",
    "https",
    "www",
];

pub fn is_good_tokens_ngram(tokens_ngram: &[String], known_tokens_ngram: &[KnownToken]) -> bool {
    const MIN_GOOD: usize = 3;

    let digit_count = tokens_ngram
        .iter()
        .filter(|t| t.chars().all(|c| c.is_ascii_digit()))
        .count();
    if digit_count >= MIN_GOOD {
        return false;
    }

    let year_count = tokens_ngram
        .iter()
        .filter(|t| t.len() == 4 && t.chars().all(|c| c.is_ascii_digit()))
        .count();
    if year_count > 0 {
        return false;
    }

    let single_char_count = tokens_ngram.iter().filter(|t| t.len() == 1).count();
    if single_char_count >= MIN_GOOD {
        return false;
    }

    let unique_tids = TokenSet::from_token_ids(known_tokens_ngram.iter().map(|token| token.id));
    if unique_tids.len() <= 2 {
        return false;
    }

    let has_high_token = known_tokens_ngram
        .iter()
        .any(|token| token.kind == TokenKind::Legalese);
    if !has_high_token {
        return false;
    }

    let has_marker = tokens_ngram.iter().any(|t| MARKERS.contains(&t.as_str()));
    if has_marker {
        return false;
    }

    true
}

pub fn compute_is_approx_matchable(rule: &Rule) -> bool {
    !(rule.is_false_positive
        || rule.is_required_phrase
        || rule.is_tiny
        || rule.is_continuous
        || (rule.is_small && (rule.is_license_reference() || rule.is_license_tag())))
}

pub fn tokens_to_bytes(tokens: &[TokenId]) -> Vec<u8> {
    tokens.iter().flat_map(|t| t.to_le_bytes()).collect()
}

pub fn ngrams<T: Clone>(items: &[T], ngram_length: usize) -> Vec<Vec<T>> {
    if items.len() < ngram_length {
        return Vec::new();
    }
    items
        .windows(ngram_length)
        .map(|window| window.to_vec())
        .collect()
}

pub fn build_index(rules: Vec<Rule>, licenses: Vec<License>) -> LicenseIndex {
    let legalese_words = legalese::get_legalese_words();
    let mut dictionary = TokenDictionary::new_with_legalese(&legalese_words);
    let len_legalese = dictionary.legalese_count();

    // Pre-assign SPDX tokens before processing rules (Python: index.py:301-314)
    // This ensures SPDX tokens get consistent IDs matching Python
    {
        let spdx_tokens = collect_spdx_tokens(&licenses);
        let mut sorted_tokens: Vec<&String> = spdx_tokens.iter().collect();
        sorted_tokens.sort();
        for token in sorted_tokens {
            if dictionary.lookup(token).is_none() {
                let _ = dictionary.intern(token);
            }
        }
    }

    let license_token_ids = TokenSet::from_token_ids(
        LICENSE_TOKEN_STRINGS
            .iter()
            .filter_map(|&token| dictionary.lookup(token).map(|token| token.id)),
    );

    let mut rid_by_hash: HashMap<[u8; 20], usize> = HashMap::new();
    let mut rules_by_rid: Vec<Rule> = Vec::with_capacity(rules.len());
    let mut tids_by_rid: Vec<Vec<TokenId>> = Vec::with_capacity(rules.len());
    let mut sets_by_rid: HashMap<usize, TokenSet> = HashMap::new();
    let mut msets_by_rid: HashMap<usize, TokenMultiset> = HashMap::new();
    let mut high_sets_by_rid: HashMap<usize, TokenSet> = HashMap::new();
    let mut high_postings_by_rid: HashMap<usize, HashMap<TokenId, Vec<usize>>> = HashMap::new();
    let mut false_positive_rids: HashSet<usize> = HashSet::new();
    let mut approx_matchable_rids: HashSet<usize> = HashSet::new();
    let mut rids_by_high_tid: HashMap<TokenId, HashSet<usize>> = HashMap::new();

    let mut pattern_to_rids: HashMap<Vec<u8>, Vec<usize>> = HashMap::with_capacity(rules.len());
    let mut unknown_automaton_patterns: Vec<Vec<u8>> = Vec::new();

    let mut licenses_by_key: HashMap<String, License> = HashMap::new();
    let mut sorted_licenses: Vec<License> = licenses;
    sorted_licenses.sort_by(|a, b| a.key.cmp(&b.key));
    for license in &sorted_licenses {
        licenses_by_key.insert(license.key.clone(), license.clone());
    }

    let mut license_rules_vec: Vec<License> = licenses_by_key.values().cloned().collect();
    license_rules_vec.sort_by(|a, b| a.key.cmp(&b.key));
    let license_rules = build_rules_from_licenses(&license_rules_vec);

    let mut all_rules: Vec<Rule> = license_rules.into_iter().chain(rules).collect();
    all_rules.sort();

    let mut rid_by_spdx_key: HashMap<String, usize> = HashMap::new();
    let mut unknown_spdx_rid: Option<usize> = None;

    for (rid, mut rule) in all_rules.into_iter().enumerate() {
        rule.required_phrase_spans = parse_required_phrase_spans(&rule.text);
        let (rule_tokens, stopwords_by_pos) = tokenize_with_stopwords(&rule.text);
        rule.stopwords_by_pos = stopwords_by_pos;
        let mut known_rule_tokens: Vec<KnownToken> = Vec::with_capacity(rule_tokens.len());
        let mut rule_token_ids: Vec<TokenId> = Vec::with_capacity(rule_tokens.len());

        let mut is_weak = true;
        for rts in &rule_tokens {
            let known_token = dictionary.intern(rts);
            if is_weak && known_token.kind == TokenKind::Legalese {
                is_weak = false;
            }
            known_rule_tokens.push(known_token);
            rule_token_ids.push(known_token.id);
        }

        let rule_length = rule_token_ids.len();
        rule.tokens = rule_token_ids.clone();

        rule.starts_with_license = rule_token_ids
            .first()
            .map(|&tid| license_token_ids.contains_token_id(tid))
            .unwrap_or(false);
        rule.ends_with_license = rule_token_ids
            .last()
            .map(|&tid| license_token_ids.contains_token_id(tid))
            .unwrap_or(false);

        let rule_hash = compute_hash(&rule_token_ids);

        // Only add non-empty patterns to the automaton
        // Empty patterns (from non-ASCII text like Japanese) would match everywhere
        if !rule_token_ids.is_empty() {
            let pattern = tokens_to_bytes(&rule_token_ids);
            pattern_to_rids.entry(pattern).or_default().push(rid);
        }

        if rule.is_false_positive {
            false_positive_rids.insert(rid);
            rules_by_rid.push(rule);
            tids_by_rid.push(rule_token_ids);
            continue;
        }

        rid_by_hash.insert(rule_hash, rid);

        // Match Python indexing order: approx-matchable membership is decided
        // before compute_thresholds() later derives final is_small/is_tiny flags.
        let is_approx_matchable = compute_is_approx_matchable(&rule);

        if rule_length >= UNKNOWN_NGRAM_LENGTH {
            let known_ngrams = ngrams(&known_rule_tokens, UNKNOWN_NGRAM_LENGTH);
            let toks_ngrams = ngrams(&rule_tokens, UNKNOWN_NGRAM_LENGTH);
            for (known_ngram, toks_ngram) in known_ngrams.iter().zip(toks_ngrams.iter()) {
                if is_good_tokens_ngram(toks_ngram, known_ngram) {
                    let token_ids: Vec<TokenId> =
                        known_ngram.iter().map(|token| token.id).collect();
                    unknown_automaton_patterns.push(tokens_to_bytes(&token_ids));
                }
            }
        }

        if is_approx_matchable && !is_weak {
            approx_matchable_rids.insert(rid);

            let mut postings: HashMap<TokenId, Vec<usize>> = HashMap::new();
            for (pos, token) in known_rule_tokens.iter().enumerate() {
                if token.kind == TokenKind::Legalese {
                    postings.entry(token.id).or_default().push(pos);
                }
            }
            if !postings.is_empty() {
                high_postings_by_rid.insert(rid, postings);
            }
        }

        let tids_set = TokenSet::from_token_ids(rule_token_ids.iter().copied());
        let mset = TokenMultiset::from_token_ids(&rule_token_ids);

        sets_by_rid.insert(rid, tids_set.clone());
        msets_by_rid.insert(rid, mset.clone());

        let tids_set_high = tids_set.high_subset(&dictionary);
        let mset_high = mset.high_subset(&dictionary);

        if !tids_set_high.is_empty() {
            high_sets_by_rid.insert(rid, tids_set_high.clone());
        }

        // Build inverted index: map high-value tokens to rules containing them
        if approx_matchable_rids.contains(&rid) {
            for tid in tids_set_high.iter() {
                rids_by_high_tid
                    .entry(TokenId::new(tid))
                    .or_default()
                    .insert(rid);
            }
        }

        rule.length_unique = tids_set.len();
        rule.high_length_unique = tids_set_high.len();
        rule.high_length = mset_high.total_count();

        let (updated_coverage, min_matched_length, min_high_matched_length) =
            compute_thresholds_occurrences(rule.minimum_coverage, rule_length, rule.high_length);
        if !rule.has_stored_minimum_coverage {
            rule.minimum_coverage = updated_coverage;
        }
        rule.min_matched_length = min_matched_length;
        rule.min_high_matched_length = min_high_matched_length;

        let (min_matched_length_unique, min_high_matched_length_unique) = compute_thresholds_unique(
            rule.minimum_coverage,
            rule_length,
            rule.length_unique,
            rule.high_length_unique,
        );
        rule.min_matched_length_unique = min_matched_length_unique;
        rule.min_high_matched_length_unique = min_high_matched_length_unique;
        rule.is_small = rule_length < SMALL_RULE;
        rule.is_tiny = rule_length < TINY_RULE;

        if let Some(ref spdx_key) = rule.spdx_license_key {
            rid_by_spdx_key.insert(spdx_key.to_lowercase(), rid);
        }
        for alias in &rule.other_spdx_license_keys {
            rid_by_spdx_key.insert(alias.to_lowercase(), rid);
        }

        if rule.license_expression == "unknown-spdx" {
            unknown_spdx_rid = Some(rid);
        }

        rules_by_rid.push(rule);
        tids_by_rid.push(rule_token_ids);
    }

    add_deprecated_spdx_aliases(&mut rid_by_spdx_key);

    // Build unique patterns list sorted for deterministic ordering
    let mut unique_patterns: Vec<&[u8]> = pattern_to_rids.keys().map(|p| p.as_slice()).collect();
    unique_patterns.sort();

    let mut rules_builder = AutomatonBuilder::new();
    for pattern in &unique_patterns {
        rules_builder.add_pattern(pattern);
    }
    let rules_automaton = rules_builder.build();

    // Build pattern_id_to_rid mapping from unique patterns
    let pattern_id_to_rid: Vec<Vec<usize>> = unique_patterns
        .iter()
        .map(|pattern| pattern_to_rids.get(*pattern).cloned().unwrap_or_default())
        .collect();

    let unknown_automaton = if unknown_automaton_patterns.is_empty() {
        AutomatonBuilder::new().build()
    } else {
        let mut unique_patterns: Vec<Vec<u8>> = unknown_automaton_patterns.into_iter().collect();
        unique_patterns.sort();
        let mut builder = AutomatonBuilder::new();
        for pattern in &unique_patterns {
            builder.add_pattern(pattern);
        }
        builder.build()
    };

    let mut index = LicenseIndex {
        dictionary,
        len_legalese,
        rid_by_hash,
        rules_by_rid,
        tids_by_rid,
        rules_automaton,
        unknown_automaton,
        sets_by_rid,
        rule_metadata_by_identifier: HashMap::new(),
        msets_by_rid,
        high_sets_by_rid,
        high_postings_by_rid,
        false_positive_rids,
        approx_matchable_rids,
        licenses_by_key,
        pattern_id_to_rid,
        rid_by_spdx_key,
        unknown_spdx_rid,
        rids_by_high_tid,
        spdx_license_list_version: None,
    };
    populate_precomputed_rule_spdx(&mut index);
    index
}

/// Convert a `LoadedRule` to a runtime `Rule`.
///
/// This is a build-stage operation that creates the initial runtime `Rule`
/// with default values for runtime-computed fields.
pub fn loaded_rule_to_rule(loaded: LoadedRule) -> Rule {
    Rule {
        identifier: loaded.identifier,
        license_expression: loaded.license_expression,
        text: loaded.text,
        tokens: vec![],
        rule_kind: loaded.rule_kind,
        is_false_positive: loaded.is_false_positive,
        is_required_phrase: loaded.is_required_phrase,
        is_from_license: false,
        relevance: loaded.relevance.unwrap_or(100),
        minimum_coverage: loaded.minimum_coverage,
        has_stored_minimum_coverage: loaded.has_stored_minimum_coverage,
        is_continuous: loaded.is_continuous,
        required_phrase_spans: vec![],
        stopwords_by_pos: HashMap::new(),
        referenced_filenames: loaded.referenced_filenames,
        ignorable_urls: loaded.ignorable_urls,
        ignorable_emails: loaded.ignorable_emails,
        ignorable_copyrights: loaded.ignorable_copyrights,
        ignorable_holders: loaded.ignorable_holders,
        ignorable_authors: loaded.ignorable_authors,
        language: loaded.language,
        notes: loaded.notes,
        length_unique: 0,
        high_length_unique: 0,
        high_length: 0,
        min_matched_length: 0,
        min_high_matched_length: 0,
        min_matched_length_unique: 0,
        min_high_matched_length_unique: 0,
        is_small: false,
        is_tiny: false,
        starts_with_license: false,
        ends_with_license: false,
        is_deprecated: loaded.is_deprecated,
        spdx_license_key: None,
        other_spdx_license_keys: vec![],
    }
}

/// Convert a `LoadedLicense` to a runtime `License`.
///
/// This is a build-stage operation that creates the runtime `License`.
pub fn loaded_license_to_license(loaded: LoadedLicense) -> License {
    License {
        key: loaded.key,
        short_name: loaded.short_name,
        name: loaded.name,
        language: loaded.language,
        spdx_license_key: loaded.spdx_license_key,
        other_spdx_license_keys: loaded.other_spdx_license_keys,
        category: loaded.category,
        owner: loaded.owner,
        homepage_url: loaded.homepage_url,
        text: loaded.text,
        reference_urls: loaded.reference_urls,
        osi_license_key: loaded.osi_license_key,
        text_urls: loaded.text_urls,
        osi_url: loaded.osi_url,
        faq_url: loaded.faq_url,
        other_urls: loaded.other_urls,
        notes: loaded.notes,
        is_deprecated: loaded.is_deprecated,
        is_exception: loaded.is_exception,
        is_unknown: loaded.is_unknown,
        is_generic: loaded.is_generic,
        replaced_by: loaded.replaced_by,
        minimum_coverage: loaded.minimum_coverage,
        standard_notice: loaded.standard_notice,
        ignorable_copyrights: loaded.ignorable_copyrights,
        ignorable_holders: loaded.ignorable_holders,
        ignorable_authors: loaded.ignorable_authors,
        ignorable_urls: loaded.ignorable_urls,
        ignorable_emails: loaded.ignorable_emails,
    }
}

/// Build a `LicenseIndex` from loaded rules and licenses.
///
/// This is the primary build-stage entry point that:
/// 1. Filters deprecated entries if `with_deprecated` is false
/// 2. Converts `LoadedRule` to `Rule`
/// 3. Converts `LoadedLicense` to `License`
/// 4. Synthesizes license-derived rules from the filtered license set
/// 5. Calls `build_index()` to construct the final index
///
/// # Arguments
/// * `loaded_rules` - Rules loaded from the loader stage
/// * `loaded_licenses` - Licenses loaded from the loader stage
/// * `with_deprecated` - If false, filter out deprecated entries before building
///
/// # Returns
/// A fully constructed `LicenseIndex`
pub fn build_index_from_loaded(
    loaded_rules: Vec<LoadedRule>,
    loaded_licenses: Vec<LoadedLicense>,
    with_deprecated: bool,
) -> LicenseIndex {
    let rule_metadata = loaded_rules.clone();
    let rules: Vec<Rule> = loaded_rules
        .into_iter()
        .filter(|r| with_deprecated || !r.is_deprecated)
        .map(loaded_rule_to_rule)
        .collect();

    let licenses: Vec<License> = loaded_licenses
        .into_iter()
        .filter(|l| with_deprecated || !l.is_deprecated)
        .map(loaded_license_to_license)
        .collect();

    let mut index = build_index(rules, licenses);
    apply_loaded_rule_metadata(&mut index, &rule_metadata, with_deprecated);
    index
}

/// Build a `LicenseIndex` from loaded rules and licenses.
///
/// This is the common path used by both directory loading and the compact embedded
/// loader snapshot.
///
/// # Arguments
/// * `loaded_rules` - Rules loaded from the loader stage
/// * `loaded_licenses` - Licenses loaded from the loader stage
/// * `with_deprecated` - If false, filter out deprecated entries before building
/// * `pattern_id_to_rid` - Pre-built pattern ID to rule ID mapping
///
/// # Returns
/// A fully constructed `LicenseIndex`
#[allow(dead_code)]
pub fn build_index_from_loaded_with_automatons(
    loaded_rules: Vec<LoadedRule>,
    loaded_licenses: Vec<LoadedLicense>,
    with_deprecated: bool,
    rules_automaton: Automaton,
    unknown_automaton: Automaton,
    pattern_id_to_rid: Vec<Vec<usize>>,
) -> LicenseIndex {
    let rule_metadata = loaded_rules.clone();
    let rules: Vec<Rule> = loaded_rules
        .into_iter()
        .filter(|r| with_deprecated || !r.is_deprecated)
        .map(loaded_rule_to_rule)
        .collect();

    let licenses: Vec<License> = loaded_licenses
        .into_iter()
        .filter(|l| with_deprecated || !l.is_deprecated)
        .map(loaded_license_to_license)
        .collect();

    let mut index = build_index_with_automatons(
        rules,
        licenses,
        rules_automaton,
        unknown_automaton,
        pattern_id_to_rid,
    );
    apply_loaded_rule_metadata(&mut index, &rule_metadata, with_deprecated);
    index
}

/// Build a `LicenseIndex`.
///
/// This is the core implementation that builds all index structures except
/// the automatons, which are provided from embedded data.
///
/// IMPORTANT: The rid assignment must match the xtask's build_automatons function.
/// The xtask assigns rids in order: rules first (sorted by identifier), then
/// licenses (sorted by key). This function must follow the same ordering for
/// the pattern_id_to_rid mapping to be correct.
#[allow(dead_code)]
fn build_index_with_automatons(
    rules: Vec<Rule>,
    licenses: Vec<License>,
    rules_automaton: Automaton,
    unknown_automaton: Automaton,
    pattern_id_to_rid: Vec<Vec<usize>>,
) -> LicenseIndex {
    let legalese_words = legalese::get_legalese_words();
    let mut dictionary = TokenDictionary::new_with_legalese(&legalese_words);
    let len_legalese = dictionary.legalese_count();

    {
        let spdx_tokens = collect_spdx_tokens(&licenses);
        let mut sorted_tokens: Vec<&String> = spdx_tokens.iter().collect();
        sorted_tokens.sort();
        for token in sorted_tokens {
            if dictionary.lookup(token).is_none() {
                let _ = dictionary.intern(token);
            }
        }
    }

    let license_token_ids = TokenSet::from_token_ids(
        LICENSE_TOKEN_STRINGS
            .iter()
            .filter_map(|&token| dictionary.lookup(token).map(|token| token.id)),
    );

    let mut rid_by_hash: HashMap<[u8; 20], usize> = HashMap::new();
    let mut rules_by_rid: Vec<Rule> = Vec::with_capacity(rules.len() + licenses.len());
    let mut tids_by_rid: Vec<Vec<TokenId>> = Vec::with_capacity(rules.len() + licenses.len());
    let mut sets_by_rid: HashMap<usize, TokenSet> = HashMap::new();
    let mut msets_by_rid: HashMap<usize, TokenMultiset> = HashMap::new();
    let mut high_sets_by_rid: HashMap<usize, TokenSet> = HashMap::new();
    let mut high_postings_by_rid: HashMap<usize, HashMap<TokenId, Vec<usize>>> = HashMap::new();
    let mut false_positive_rids: HashSet<usize> = HashSet::new();
    let mut approx_matchable_rids: HashSet<usize> = HashSet::new();
    let mut rids_by_high_tid: HashMap<TokenId, HashSet<usize>> = HashMap::new();

    let mut licenses_by_key: HashMap<String, License> = HashMap::new();
    let mut sorted_licenses: Vec<License> = licenses;
    sorted_licenses.sort_by(|a, b| a.key.cmp(&b.key));
    for license in &sorted_licenses {
        licenses_by_key.insert(license.key.clone(), license.clone());
    }

    let mut rid_by_spdx_key: HashMap<String, usize> = HashMap::new();
    let mut unknown_spdx_rid: Option<usize> = None;

    let mut sorted_rules: Vec<Rule> = rules;
    sorted_rules.sort_by(|a, b| a.identifier.cmp(&b.identifier));

    let mut rid: usize = 0;

    for mut rule in sorted_rules {
        rule.required_phrase_spans = parse_required_phrase_spans(&rule.text);
        let (rule_tokens, stopwords_by_pos) = tokenize_with_stopwords(&rule.text);
        rule.stopwords_by_pos = stopwords_by_pos;
        let mut known_rule_tokens: Vec<KnownToken> = Vec::with_capacity(rule_tokens.len());
        let mut rule_token_ids: Vec<TokenId> = Vec::with_capacity(rule_tokens.len());

        let mut is_weak = true;
        for rts in &rule_tokens {
            let known_token = dictionary.intern(rts);
            if is_weak && known_token.kind == TokenKind::Legalese {
                is_weak = false;
            }
            known_rule_tokens.push(known_token);
            rule_token_ids.push(known_token.id);
        }

        let rule_length = rule_token_ids.len();
        rule.tokens = rule_token_ids.clone();

        rule.starts_with_license = rule_token_ids
            .first()
            .map(|&tid| license_token_ids.contains_token_id(tid))
            .unwrap_or(false);
        rule.ends_with_license = rule_token_ids
            .last()
            .map(|&tid| license_token_ids.contains_token_id(tid))
            .unwrap_or(false);

        let rule_hash = compute_hash(&rule_token_ids);

        if rule.is_false_positive {
            false_positive_rids.insert(rid);
            rules_by_rid.push(rule);
            tids_by_rid.push(rule_token_ids);
            rid += 1;
            continue;
        }

        rid_by_hash.insert(rule_hash, rid);

        let is_approx_matchable = compute_is_approx_matchable(&rule);

        if is_approx_matchable && !is_weak {
            approx_matchable_rids.insert(rid);

            let mut postings: HashMap<TokenId, Vec<usize>> = HashMap::new();
            for (pos, token) in known_rule_tokens.iter().enumerate() {
                if token.kind == TokenKind::Legalese {
                    postings.entry(token.id).or_default().push(pos);
                }
            }
            if !postings.is_empty() {
                high_postings_by_rid.insert(rid, postings);
            }
        }

        let tids_set = TokenSet::from_token_ids(rule_token_ids.iter().copied());
        let mset = TokenMultiset::from_token_ids(&rule_token_ids);

        sets_by_rid.insert(rid, tids_set.clone());
        msets_by_rid.insert(rid, mset.clone());

        let tids_set_high = tids_set.high_subset(&dictionary);
        let mset_high = mset.high_subset(&dictionary);

        if !tids_set_high.is_empty() {
            high_sets_by_rid.insert(rid, tids_set_high.clone());
        }

        if approx_matchable_rids.contains(&rid) {
            for tid in tids_set_high.iter() {
                rids_by_high_tid
                    .entry(TokenId::new(tid))
                    .or_default()
                    .insert(rid);
            }
        }

        rule.length_unique = tids_set.len();
        rule.high_length_unique = tids_set_high.len();
        rule.high_length = mset_high.total_count();

        let (updated_coverage, min_matched_length, min_high_matched_length) =
            compute_thresholds_occurrences(rule.minimum_coverage, rule_length, rule.high_length);
        if !rule.has_stored_minimum_coverage {
            rule.minimum_coverage = updated_coverage;
        }
        rule.min_matched_length = min_matched_length;
        rule.min_high_matched_length = min_high_matched_length;

        let (min_matched_length_unique, min_high_matched_length_unique) = compute_thresholds_unique(
            rule.minimum_coverage,
            rule_length,
            rule.length_unique,
            rule.high_length_unique,
        );
        rule.min_matched_length_unique = min_matched_length_unique;
        rule.min_high_matched_length_unique = min_high_matched_length_unique;
        rule.is_small = rule_length < SMALL_RULE;
        rule.is_tiny = rule_length < TINY_RULE;

        if let Some(ref spdx_key) = rule.spdx_license_key {
            rid_by_spdx_key.insert(spdx_key.to_lowercase(), rid);
        }
        for alias in &rule.other_spdx_license_keys {
            rid_by_spdx_key.insert(alias.to_lowercase(), rid);
        }

        rules_by_rid.push(rule);
        tids_by_rid.push(rule_token_ids);
        rid += 1;
    }

    for license in sorted_licenses {
        let Some(mut rule) = build_rule_from_license(&license) else {
            continue;
        };
        rule.required_phrase_spans = parse_required_phrase_spans(&rule.text);
        let (rule_tokens, stopwords_by_pos) = tokenize_with_stopwords(&rule.text);
        rule.stopwords_by_pos = stopwords_by_pos;
        let mut known_rule_tokens: Vec<KnownToken> = Vec::with_capacity(rule_tokens.len());
        let mut rule_token_ids: Vec<TokenId> = Vec::with_capacity(rule_tokens.len());

        let mut is_weak = true;
        for rts in &rule_tokens {
            let known_token = dictionary.intern(rts);
            if is_weak && known_token.kind == TokenKind::Legalese {
                is_weak = false;
            }
            known_rule_tokens.push(known_token);
            rule_token_ids.push(known_token.id);
        }

        let rule_length = rule_token_ids.len();
        rule.tokens = rule_token_ids.clone();

        rule.starts_with_license = rule_token_ids
            .first()
            .map(|&tid| license_token_ids.contains_token_id(tid))
            .unwrap_or(false);
        rule.ends_with_license = rule_token_ids
            .last()
            .map(|&tid| license_token_ids.contains_token_id(tid))
            .unwrap_or(false);

        let rule_hash = compute_hash(&rule_token_ids);

        if rule.is_false_positive {
            false_positive_rids.insert(rid);
            rules_by_rid.push(rule);
            tids_by_rid.push(rule_token_ids);
            rid += 1;
            continue;
        }

        rid_by_hash.insert(rule_hash, rid);

        let is_approx_matchable = compute_is_approx_matchable(&rule);

        if is_approx_matchable && !is_weak {
            approx_matchable_rids.insert(rid);

            let mut postings: HashMap<TokenId, Vec<usize>> = HashMap::new();
            for (pos, token) in known_rule_tokens.iter().enumerate() {
                if token.kind == TokenKind::Legalese {
                    postings.entry(token.id).or_default().push(pos);
                }
            }
            if !postings.is_empty() {
                high_postings_by_rid.insert(rid, postings);
            }
        }

        let tids_set = TokenSet::from_token_ids(rule_token_ids.iter().copied());
        let mset = TokenMultiset::from_token_ids(&rule_token_ids);

        sets_by_rid.insert(rid, tids_set.clone());
        msets_by_rid.insert(rid, mset.clone());

        let tids_set_high = tids_set.high_subset(&dictionary);
        let mset_high = mset.high_subset(&dictionary);

        if !tids_set_high.is_empty() {
            high_sets_by_rid.insert(rid, tids_set_high.clone());
        }

        if approx_matchable_rids.contains(&rid) {
            for tid in tids_set_high.iter() {
                rids_by_high_tid
                    .entry(TokenId::new(tid))
                    .or_default()
                    .insert(rid);
            }
        }

        rule.length_unique = tids_set.len();
        rule.high_length_unique = tids_set_high.len();
        rule.high_length = mset_high.total_count();

        let (updated_coverage, min_matched_length, min_high_matched_length) =
            compute_thresholds_occurrences(rule.minimum_coverage, rule_length, rule.high_length);
        if !rule.has_stored_minimum_coverage {
            rule.minimum_coverage = updated_coverage;
        }
        rule.min_matched_length = min_matched_length;
        rule.min_high_matched_length = min_high_matched_length;

        let (min_matched_length_unique, min_high_matched_length_unique) = compute_thresholds_unique(
            rule.minimum_coverage,
            rule_length,
            rule.length_unique,
            rule.high_length_unique,
        );
        rule.min_matched_length_unique = min_matched_length_unique;
        rule.min_high_matched_length_unique = min_high_matched_length_unique;
        rule.is_small = rule_length < SMALL_RULE;
        rule.is_tiny = rule_length < TINY_RULE;

        if let Some(ref spdx_key) = rule.spdx_license_key {
            rid_by_spdx_key.insert(spdx_key.to_lowercase(), rid);
        }
        for alias in &rule.other_spdx_license_keys {
            rid_by_spdx_key.insert(alias.to_lowercase(), rid);
        }

        if rule.license_expression == "unknown-spdx" {
            unknown_spdx_rid = Some(rid);
        }

        rules_by_rid.push(rule);
        tids_by_rid.push(rule_token_ids);
        rid += 1;
    }

    add_deprecated_spdx_aliases(&mut rid_by_spdx_key);

    let mut index = LicenseIndex {
        dictionary,
        len_legalese,
        rid_by_hash,
        rules_by_rid,
        tids_by_rid,
        rules_automaton,
        unknown_automaton,
        sets_by_rid,
        rule_metadata_by_identifier: HashMap::new(),
        msets_by_rid,
        high_sets_by_rid,
        high_postings_by_rid,
        false_positive_rids,
        approx_matchable_rids,
        licenses_by_key,
        pattern_id_to_rid,
        rid_by_spdx_key,
        unknown_spdx_rid,
        rids_by_high_tid,
        spdx_license_list_version: None,
    };
    populate_precomputed_rule_spdx(&mut index);
    index
}

#[cfg(test)]
mod tests;