use std::collections::HashMap;
use crate::error::{Error, Result};
pub trait Classifier {
fn id(&self, feature: &str) -> Option<u32>;
fn score(&self, features: &[u32]) -> f64;
}
impl<C: Classifier + ?Sized> Classifier for &C {
fn id(&self, feature: &str) -> Option<u32> {
(**self).id(feature)
}
fn score(&self, features: &[u32]) -> f64 {
(**self).score(features)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct LinearClassifier {
ids: HashMap<String, u32>,
bias: f64,
weights: Vec<f64>,
pairs: HashMap<(u32, u32), f64>,
}
impl LinearClassifier {
pub fn new(
bias: f64,
features: HashMap<String, f64>,
pairs: HashMap<(String, String), f64>,
) -> Result<Self> {
if !bias.is_finite() {
return Err(Error::Parser(format!("the bias {bias} is not finite")));
}
let mut names: Vec<&str> = features
.keys()
.map(String::as_str)
.chain(
pairs
.keys()
.flat_map(|(one, other)| [one.as_str(), other.as_str()]),
)
.collect();
names.sort_unstable();
names.dedup();
let ids: HashMap<String, u32> = names
.iter()
.enumerate()
.map(|(id, name)| (name.to_string(), id as u32))
.collect();
let mut weights = vec![0.0; names.len()];
for (name, weight) in &features {
if !weight.is_finite() {
return Err(Error::Parser(format!(
"the weight {weight} of {name:?} is not finite"
)));
}
weights[ids[name] as usize] = *weight;
}
let mut by_id = HashMap::new();
for ((one, other), weight) in &pairs {
if !weight.is_finite() {
return Err(Error::Parser(format!(
"the weight {weight} of the pair {one:?} and {other:?} is not finite"
)));
}
if one == other {
return Err(Error::Parser(format!(
"{one:?} is paired with itself: give its weight as a feature"
)));
}
let (low, high) = (ids[one].min(ids[other]), ids[one].max(ids[other]));
if by_id.insert((low, high), *weight).is_some() {
return Err(Error::Parser(format!(
"the pair {one:?} and {other:?} is given in both orders"
)));
}
}
Ok(Self {
ids,
bias,
weights,
pairs: by_id,
})
}
pub fn feature_count(&self) -> usize {
self.weights.len()
}
pub fn pair_count(&self) -> usize {
self.pairs.len()
}
}
impl Classifier for LinearClassifier {
fn id(&self, feature: &str) -> Option<u32> {
self.ids.get(feature).copied()
}
fn score(&self, features: &[u32]) -> f64 {
let mut score = self.bias;
for (index, &one) in features.iter().enumerate() {
score += self.weights.get(one as usize).copied().unwrap_or(0.0);
if self.pairs.is_empty() {
continue;
}
for &other in &features[index + 1..] {
let pair = (one.min(other), one.max(other));
score += self.pairs.get(&pair).copied().unwrap_or(0.0);
}
}
score
}
}
#[cfg(test)]
mod tests {
use super::*;
fn classifier() -> LinearClassifier {
let features = HashMap::from([
("a".to_string(), 1.0),
("b".to_string(), -2.0),
("c".to_string(), 4.0),
]);
let pairs = HashMap::from([(("c".to_string(), "a".to_string()), 8.0)]);
LinearClassifier::new(0.5, features, pairs).unwrap()
}
fn ids(classifier: &LinearClassifier, names: &[&str]) -> Vec<u32> {
let mut ids: Vec<u32> = names
.iter()
.map(|name| classifier.id(name).unwrap())
.collect();
ids.sort_unstable();
ids
}
#[test]
fn the_score_adds_the_bias_the_features_and_the_pairs() {
let classifier = classifier();
assert_eq!(classifier.score(&[]), 0.5);
assert_eq!(classifier.score(&ids(&classifier, &["a"])), 1.5);
assert_eq!(classifier.score(&ids(&classifier, &["a", "b"])), -0.5);
assert_eq!(classifier.score(&ids(&classifier, &["a", "c"])), 13.5);
assert_eq!(classifier.score(&ids(&classifier, &["a", "b", "c"])), 11.5);
}
#[test]
fn an_unknown_feature_has_no_number() {
let classifier = classifier();
assert_eq!(classifier.id("d"), None);
assert_eq!(classifier.feature_count(), 3);
assert_eq!(classifier.pair_count(), 1);
}
#[test]
fn a_feature_named_in_a_pair_only_has_a_number() {
let pairs = HashMap::from([(("x".to_string(), "y".to_string()), 2.0)]);
let classifier = LinearClassifier::new(0.0, HashMap::new(), pairs).unwrap();
assert_eq!(classifier.feature_count(), 2);
assert_eq!(classifier.score(&ids(&classifier, &["x"])), 0.0);
assert_eq!(classifier.score(&ids(&classifier, &["x", "y"])), 2.0);
}
#[test]
fn the_numbers_follow_the_names() {
let classifier = classifier();
assert_eq!(classifier.id("a"), Some(0));
assert_eq!(classifier.id("b"), Some(1));
assert_eq!(classifier.id("c"), Some(2));
}
#[test]
fn weights_that_cannot_be_used_are_refused() {
let feature = |weight: f64| HashMap::from([("a".to_string(), weight)]);
let pair = |one: &str, other: &str, weight: f64| {
HashMap::from([((one.to_string(), other.to_string()), weight)])
};
let mut both = pair("a", "b", 1.0);
both.insert(("b".to_string(), "a".to_string()), 1.0);
let refused = [
(
"bias",
LinearClassifier::new(f64::NAN, HashMap::new(), HashMap::new()),
),
(
"feature",
LinearClassifier::new(0.0, feature(f64::INFINITY), HashMap::new()),
),
(
"pair",
LinearClassifier::new(0.0, HashMap::new(), pair("a", "b", f64::NAN)),
),
(
"itself",
LinearClassifier::new(0.0, HashMap::new(), pair("a", "a", 1.0)),
),
(
"both orders",
LinearClassifier::new(0.0, HashMap::new(), both),
),
];
let accepted: Vec<&str> = refused
.iter()
.filter(|(_, result)| !matches!(result, Err(Error::Parser(_))))
.map(|(name, _)| *name)
.collect();
assert!(accepted.is_empty(), "not refused: {accepted:?}");
}
}