Skip to content

armgit / mwmbl

Public Imported from GitHub
0

An open source, non-profit web search engine

Apply heuristic majority-terms filter in Rust LTR predict

Daoud Clarke2a2de6ca3251
diff --git a/mwmbl_rank/src/features.rs b/mwmbl_rank/src/features.rs
index 49d33be..070e4d7 100644
--- a/mwmbl_rank/src/features.rs
+++ b/mwmbl_rank/src/features.rs
@@ -78,6 +78,12 @@ pub const FEATURE_NAMES: &[&str] = &[
pub const NUM_FEATURES: usize = FEATURE_NAMES.len(); // 50
+/// Index of `num_terms` (number of query terms) in the feature vector.
+pub const NUM_TERMS_INDEX: usize = 42;
+/// Index of `match_terms` (max distinct terms matched over title/extract/domain/
+/// domain_tokenized/path) in the feature vector.
+pub const MATCH_TERMS_INDEX: usize = NUM_FEATURES - 1;
+
/// Result of matching query terms against a text part.
#[derive(Debug, Default)]
pub struct MatchFeatures {
@@ -357,6 +363,12 @@ mod tests {
assert_eq!(NUM_FEATURES, 50);
}
+ #[test]
+ fn test_filter_feature_indices() {
+ assert_eq!(FEATURE_NAMES[NUM_TERMS_INDEX], "num_terms");
+ assert_eq!(FEATURE_NAMES[MATCH_TERMS_INDEX], "match_terms");
+ }
+
#[test]
fn test_get_features_length() {
let terms = vec!["rust", "programming"];
diff --git a/mwmbl_rank/src/pipeline.rs b/mwmbl_rank/src/pipeline.rs
index f365149..0e2c4d1 100644
--- a/mwmbl_rank/src/pipeline.rs
+++ b/mwmbl_rank/src/pipeline.rs
@@ -6,7 +6,7 @@
use xgb::{parameters, Booster, DMatrix};
-use crate::features::{get_features_with_regex, NUM_FEATURES};
+use crate::features::{get_features_with_regex, MATCH_TERMS_INDEX, NUM_FEATURES, NUM_TERMS_INDEX};
use crate::text::{tokenize, build_query_regex};
/// A single document record passed from Python.
@@ -252,9 +252,20 @@ impl XGBPipeline {
let dmat = DMatrix::from_dense(&flat_features, n_rows)
.map_err(|e| format!("Failed to create DMatrix: {}", e))?;
- let predictions = booster.predict(&dmat)
+ let mut predictions = booster.predict(&dmat)
.map_err(|e| format!("XGBoost prediction failed: {}", e))?;
+ // Replicate the heuristic ranker's majority-terms filter: drop any result that
+ // matches no more than half the query terms. match_terms (the max matched terms
+ // over fields) and num_terms are already in the extracted feature matrix, so this
+ // reuses the computed features rather than recomputing them.
+ for (i, pred) in predictions.iter_mut().enumerate() {
+ let row = &flat_features[i * NUM_FEATURES..(i + 1) * NUM_FEATURES];
+ if row[MATCH_TERMS_INDEX] <= row[NUM_TERMS_INDEX] / 2.0 {
+ *pred = 0.0;
+ }
+ }
+
Ok(predictions)
}
@@ -331,6 +342,41 @@ mod tests {
}
}
+ #[test]
+ fn test_predict_filters_too_few_match_terms() {
+ // Train on records that all match the query, then predict on a mix of
+ // matching and non-matching documents. Non-matching docs (match_terms <=
+ // num_terms / 2) must be forced to 0.0 regardless of the model output.
+ let records = make_records(20);
+ let labels: Vec<f32> = vec![1.0; 20];
+ let mut pipeline = XGBPipeline::new(0.0, 0.1, 2.0, 10);
+ pipeline.fit(&records, &labels).expect("fit should succeed");
+
+ let test = vec![
+ // Matches both query terms ("rust programming") -> kept.
+ DocumentRecord {
+ query: "rust programming".to_string(),
+ url: "https://example.com/rust".to_string(),
+ title: "Rust Programming".to_string(),
+ extract: "rust programming language".to_string(),
+ score: 1.0,
+ },
+ // Matches no query terms -> filtered to 0.0.
+ DocumentRecord {
+ query: "rust programming".to_string(),
+ url: "https://example.com/cooking".to_string(),
+ title: "Cooking Recipes".to_string(),
+ extract: "how to bake bread".to_string(),
+ score: 1.0,
+ },
+ ];
+
+ let preds = pipeline.predict(&test).expect("predict should succeed");
+ assert_eq!(preds.len(), 2);
+ assert!(preds[0] > 0.0, "matching doc should keep its prediction");
+ assert_eq!(preds[1], 0.0, "non-matching doc should be filtered to 0.0");
+ }
+
#[test]
fn test_pipeline_predict_without_fit() {
let records = make_records(5);