feat: initial text-deontic-extract v0.1.0 (text.deontic_extract@0.1.0)

Regex-based extraction of atomic deontic statements (Pflicht,
Verbot, Erlaubnis) from German legal text. Reuse-lens: contracts,
ISO standards, internal policies. Vorbild: Mercatus QuantGov /
RegData.

Pattern coverage in v0.1.0:

  - OBLIGATION:  muss, hat zu, ist verpflichtet, ist anzuzeigen,
                 aufzuzeichnen, muss eingehalten/geführt/...
  - PROHIBITION: darf nicht, dürfen nicht, ist untersagt, verboten
  - PERMISSION:  kann, können, darf

Each emitted Duty carries a confidence ∈ [0, 1]. Pattern hits
default to 0.85; ambiguous structures (bare "kann") drop to 0.70.
Anything below 0.6 is the surrounding flow's signal to re-run
through llm.chat for human-readable refinement and feed the result
to system.approval@^0 — that keeps deterministic extraction
separate from non-deterministic LLM judgment.

Pure in-WASM (regex), zero filesystem, zero network.

Reserved for next versions:

  - 0.2: EN / FR locale variants
  - 0.3: addressee + frequency + authority detection (Phase 1
         relies on flow-side LLM enrichment for those fields)

Signed-off-by: flemming-it <sf@flemming.it>
This commit is contained in:
flemming-it 2026-06-18 11:24:58 +02:00
commit 7dcdf7679f
6 changed files with 769 additions and 0 deletions

200
src/lib.rs Normal file
View file

@ -0,0 +1,200 @@
//! `text.deontic_extract` — regex-based duty extraction from
//! German legal text.
//!
//! Reuse-lens: same shape for contracts (AGB), ISO standards,
//! internal policies. The pattern list is German-language; an
//! `EN`-/`FR`-locale variant is a 0.2 task.
//!
//! Vorbild: Mercatus QuantGov / RegData.
//!
//! # Confidence
//!
//! Each Duty carries `confidence ∈ [0, 1]`. Pattern hits get
//! 0.85; ambiguous structures get 0.5. Anything below 0.6 the
//! surrounding flow re-runs through `llm.chat` for human-readable
//! refinement, then back through a Juristen-approval step.
#![allow(clippy::result_large_err)]
use chain_module_sdk::prelude::*;
use regex::Regex;
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize)]
struct InputNorm {
eli: Option<String>,
jurabk: Option<String>,
paragraph: Option<String>,
paragraphs: Vec<InputParagraph>,
}
#[derive(Debug, Deserialize)]
struct InputParagraph {
number: String,
text: String,
}
#[derive(Debug, Serialize)]
struct Duty {
modality: &'static str,
action: String,
source_norm: String,
paragraph: String,
confidence: f32,
}
#[derive(Debug, Serialize)]
struct Output {
duties: Vec<Duty>,
avg_confidence: f32,
}
const MOD_OBLIGATION: &str = "OBLIGATION";
const MOD_PROHIBITION: &str = "PROHIBITION";
const MOD_PERMISSION: &str = "PERMISSION";
#[fai_module]
pub fn invoke(_ctx: Context, inputs: Inputs) -> Result<Outputs, ModuleError> {
let norm: InputNorm = inputs
.require_json("norm")
.map_err(|e| ModuleError::invalid_input(format!("norm JSON shape: {e}")))?;
let source = format!(
"{} {}",
norm.jurabk.as_deref().unwrap_or("?"),
norm.paragraph.as_deref().unwrap_or(""),
);
let mut duties = Vec::new();
let patterns = compile_patterns()?;
for p in &norm.paragraphs {
for sentence in split_sentences(&p.text) {
for (modality, conf, re) in &patterns {
if re.is_match(sentence) {
duties.push(Duty {
modality,
action: sentence.trim().to_string(),
source_norm: source.clone(),
paragraph: p.number.clone(),
confidence: *conf,
});
break; // first matching modality wins per sentence
}
}
}
}
let avg = if duties.is_empty() {
0.0
} else {
duties.iter().map(|d| d.confidence).sum::<f32>() / duties.len() as f32
};
let out = Output { duties, avg_confidence: avg };
Outputs::new().with_json("duties", &out)
}
fn compile_patterns() -> Result<Vec<(&'static str, f32, Regex)>, ModuleError> {
let raw = [
(MOD_OBLIGATION, 0.85, r"\bmuss(?:t|en|te)?\b"),
(MOD_OBLIGATION, 0.85, r"\bhat\s+zu\s+\w+en\b"),
(MOD_OBLIGATION, 0.85, r"\bist\s+verpflichtet\b"),
(MOD_OBLIGATION, 0.85, r"\bsind\s+verpflichtet\b"),
(MOD_OBLIGATION, 0.80, r"\b(?:ist|sind)\s+anzuzeigen\b"),
(MOD_OBLIGATION, 0.80, r"\baufzuzeichnen\b"),
(MOD_OBLIGATION, 0.80, r"\b(?:muss|müssen)\s+\w+\s+(?:eingehalten|gef[uü]hrt|gemeldet|nachgewiesen)\b"),
(MOD_PROHIBITION, 0.85, r"\bdarf\s+(?:nicht|kein)\b"),
(MOD_PROHIBITION, 0.85, r"\bd[uü]rfen\s+(?:nicht|kein)\b"),
(MOD_PROHIBITION, 0.85, r"\b(?:ist|sind)\s+untersagt\b"),
(MOD_PROHIBITION, 0.85, r"\b(?:ist|sind)\s+verboten\b"),
(MOD_PERMISSION, 0.70, r"\bkann\b"),
(MOD_PERMISSION, 0.70, r"\bk[oö]nnen\b"),
(MOD_PERMISSION, 0.70, r"\bdarf\b"),
];
let mut out = Vec::with_capacity(raw.len());
for (m, c, src) in raw {
let re = Regex::new(src).map_err(|e| {
ModuleError::invalid_input(format!("internal regex compile: {e}"))
})?;
out.push((m, c, re));
}
Ok(out)
}
fn split_sentences(text: &str) -> Vec<&str> {
text.split_terminator(|c: char| c == '.' || c == ';' || c == '\n')
.map(str::trim)
.filter(|s| !s.is_empty() && s.len() > 4)
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
fn norm(paragraphs: Vec<(&str, &str)>) -> serde_json::Value {
serde_json::json!({
"eli": "eli/bund/gewo/14",
"jurabk": "GewO",
"paragraph": "§ 14",
"paragraphs": paragraphs
.into_iter()
.map(|(n, t)| serde_json::json!({"number": n, "text": t}))
.collect::<Vec<_>>(),
})
}
fn extract(v: serde_json::Value) -> Output {
let p: InputNorm = serde_json::from_value(v).expect("valid");
let mut duties = Vec::new();
let patterns = compile_patterns().expect("regex ok");
for para in &p.paragraphs {
for sentence in split_sentences(&para.text) {
for (m, c, re) in &patterns {
if re.is_match(sentence) {
duties.push(Duty {
modality: m,
action: sentence.trim().to_string(),
source_norm: "GewO § 14".into(),
paragraph: para.number.clone(),
confidence: *c,
});
break;
}
}
}
}
let avg = if duties.is_empty() {
0.0
} else {
duties.iter().map(|d| d.confidence).sum::<f32>() / duties.len() as f32
};
Output { duties, avg_confidence: avg }
}
#[test]
fn finds_obligation_in_gewo_14_text() {
let result = extract(norm(vec![(
"(1)",
"Wer den selbstständigen Betrieb anfängt, muss dies anzeigen.",
)]));
assert!(!result.duties.is_empty());
assert_eq!(result.duties[0].modality, "OBLIGATION");
}
#[test]
fn finds_prohibition() {
let result = extract(norm(vec![(
"(1)",
"Personenbezogene Daten dürfen nicht ohne Einwilligung verarbeitet werden.",
)]));
assert_eq!(result.duties[0].modality, "PROHIBITION");
}
#[test]
fn no_duty_means_zero_avg() {
let result = extract(norm(vec![("(1)", "Dieser Satz enthält nichts Deontisches.")]));
assert_eq!(result.duties.len(), 0);
assert_eq!(result.avg_confidence, 0.0);
}
}