26 lines
919 B
Rust
26 lines
919 B
Rust
//! Ludus concept ids for vocabulary: `w-<lang>-<16 base32 chars of sha256(lemma)>` (lowercase ASCII, Ludus rule).
|
|
|
|
use crate::{Lang, fold};
|
|
use sha2::{Digest, Sha256};
|
|
|
|
const ALPHABET: &[u8; 32] = b"abcdefghijklmnopqrstuvwxyz234567";
|
|
|
|
/// The concept id of a lemma. The lemma is folded first (lowercase, NFC; Latin `j`/`v` → `i`/`u`), so every form of
|
|
/// one lemma shares one concept.
|
|
pub fn concept_id(lang: Lang, lemma: &str) -> String {
|
|
let digest = Sha256::digest(fold(lemma, lang).as_bytes());
|
|
// 16 base32 characters are exactly 80 bits: the first 10 bytes.
|
|
let mut bits: u128 = 0;
|
|
for &b in &digest[..10] {
|
|
bits = (bits << 8) | u128::from(b);
|
|
}
|
|
let mut out = String::with_capacity(21);
|
|
out.push_str("w-");
|
|
out.push_str(lang.code());
|
|
out.push('-');
|
|
for i in (0..16).rev() {
|
|
out.push(ALPHABET[((bits >> (i * 5)) & 31) as usize] as char);
|
|
}
|
|
out
|
|
}
|