Files
tools/graph/src/language.rs
T

100 lines
3.8 KiB
Rust
Raw Normal View History

2026-08-20 21:27:38 -04:00
use std::env::var;
2026-08-20 20:43:18 -04:00
use oxigraph::model::{Literal, TermRef};
2026-07-01 17:42:23 -04:00
use oxilangtag::LanguageTag;
2026-08-08 23:50:04 -04:00
use std::sync::LazyLock;
2026-08-20 20:43:18 -04:00
use spargebra::algebra::{Expression, Function, GraphPattern};
use spargebra::term::{NamedNode, NamedNodePattern, TermPattern, TriplePattern, Variable};
use tracing::debug;
2026-07-01 17:42:23 -04:00
pub const ENGLISH_PRIMARY: &str = "en";
pub static ENGLISH_OR_UNTAGGED: LazyLock<LanguageCondition> = LazyLock::new(|| {
2026-08-08 23:50:04 -04:00
LanguageCondition::ExactMatchOrUntagged(
LanguageTag::parse(ENGLISH_PRIMARY.to_string()).unwrap(),
)
2026-07-01 17:42:23 -04:00
});
2026-08-20 20:43:18 -04:00
#[derive(Clone, Debug)]
2026-07-01 17:42:23 -04:00
pub enum LanguageCondition {
ExactMatchOnly(LanguageTag<String>),
ExactMatchOrUntagged(LanguageTag<String>),
UntaggedOnly,
AnyOrNone,
}
impl LanguageCondition {
2026-08-05 11:21:25 -04:00
pub fn primary_matches_term<'a>(&self, term: impl Into<TermRef<'a>>) -> bool {
if let TermRef::Literal(literal) = term.into() {
2026-08-08 23:50:04 -04:00
let tag = literal
.language()
2026-07-01 17:42:23 -04:00
.map(LanguageTag::parse_and_normalize)
.and_then(Result::ok);
match (tag, self) {
2026-08-08 23:50:04 -04:00
(Some(language), LanguageCondition::ExactMatchOnly(expectation))
| (Some(language), LanguageCondition::ExactMatchOrUntagged(expectation)) => {
language.primary_language() == expectation.primary_language()
}
2026-07-01 17:42:23 -04:00
(None, LanguageCondition::ExactMatchOrUntagged(_)) => true,
(None, LanguageCondition::UntaggedOnly) => true,
(_, LanguageCondition::AnyOrNone) => true,
_ => false,
}
} else {
false
}
}
2026-08-05 11:21:25 -04:00
pub fn primary_language(&self) -> Option<&str> {
match self {
LanguageCondition::ExactMatchOnly(tag) => Some(tag.primary_language()),
LanguageCondition::ExactMatchOrUntagged(tag) => Some(tag.primary_language()),
_ => None,
}
}
2026-08-20 20:43:18 -04:00
pub fn filter(conditions: impl IntoIterator<Item = (String, Self)>) -> String {
2026-08-20 21:27:38 -04:00
let exact_expression = |variable: String, language| Expression::FunctionCall(
2026-08-20 20:43:18 -04:00
Function::LangMatches, vec![
Expression::FunctionCall(Function::Lang, vec![
Expression::Variable(Variable::new_unchecked(variable))
]),
Expression::Literal(Literal::new_simple_literal(language))
],
);
let untagged_expression = |variable| Expression::Not(
Box::new(Expression::FunctionCall(Function::HasLang, vec![
Expression::Variable(Variable::new_unchecked(variable))
]))
);
let condition_to_expression = |variable, condition| match condition {
LanguageCondition::ExactMatchOnly(language) =>
2026-08-20 21:27:38 -04:00
Some(exact_expression(variable, language.to_string())),
2026-08-20 20:43:18 -04:00
LanguageCondition::ExactMatchOrUntagged(language) => {
Some(Expression::Or(
2026-08-20 21:27:38 -04:00
Box::new(exact_expression(variable.clone(), language.to_string())),
2026-08-20 20:43:18 -04:00
Box::new(untagged_expression(variable)),
))
}
LanguageCondition::UntaggedOnly => Some(untagged_expression(variable)),
LanguageCondition::AnyOrNone => None,
};
let mut iter = conditions.into_iter();
let first_expression = iter.next()
.and_then(|(variable, condition)| condition_to_expression(variable, condition));
2026-08-20 21:27:38 -04:00
if let Some(expression) = first_expression {
let expr = iter.filter_map(|(variable, condition)| condition_to_expression(variable, condition))
.fold(expression, |acc, next_exp| Expression::Or(Box::new(acc), Box::new(next_exp)));
GraphPattern::Filter {
expr,
inner: Box::new(GraphPattern::Bgp { patterns: vec![] }),
}.to_string()
} else {
String::default()
2026-07-01 17:42:23 -04:00
}
}
2026-08-08 23:50:04 -04:00
}