feat: Alpha-ID-SE corpus parser with code normalization

This commit is contained in:
2026-05-18 16:20:21 +02:00
parent 176aa87c0b
commit 3e8284bfc0
2 changed files with 72 additions and 1 deletions
+43 -1
View File
@@ -1 +1,43 @@
// implemented in a later task
use crate::model::{AlphaIdEntry, AppError};
/// Normalize an ICD code field by removing classification marks.
pub fn normalize_code(raw: &str) -> String {
raw.trim().trim_end_matches(['+', '*', '!']).trim().to_string()
}
pub fn parse_line(line: &str) -> Option<AlphaIdEntry> {
let f: Vec<&str> = line.split('|').collect();
if f.len() < 8 { return None; }
Some(AlphaIdEntry {
valid: f[0].trim() == "1",
alpha_id: f[1].trim().to_string(),
icd_primary: normalize_code(f[2]),
icd_star: normalize_code(f[3]),
icd_addon: normalize_code(f[4]),
icd_primary2: normalize_code(f[5]),
orpha: f[6].trim().to_string(),
text: f[7].trim().to_string(),
})
}
pub fn load(path: &str) -> Result<Vec<AlphaIdEntry>, AppError> {
let raw = std::fs::read_to_string(path)
.map_err(|e| AppError::Io(format!("{path}: {e}")))?;
let entries: Vec<AlphaIdEntry> = raw
.lines()
.filter(|l| !l.trim().is_empty())
.filter_map(parse_line)
.collect();
if entries.is_empty() {
return Err(AppError::Parse(format!("no entries parsed from {path}")));
}
Ok(entries)
}
/// The primary ICD code a matched entry contributes (falls back to star/addon).
pub fn primary_code(e: &AlphaIdEntry) -> Option<&str> {
for c in [&e.icd_primary, &e.icd_primary2, &e.icd_star, &e.icd_addon] {
if !c.is_empty() { return Some(c); }
}
None
}