Spaces:
Build error
Build error
File size: 4,274 Bytes
0ca462b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | #![allow(dead_code)]
//! Fast native content extractor for IntentForge v2
//!
//! Uses the 'scraper' crate for efficient HTML parsing and content identification
//! without the overhead of a Python microservice.
use scraper::{Html, Selector};
/// Result of a fast extraction
#[derive(Debug, Clone, Default)]
pub struct FastExtractResult {
pub title: String,
pub content: String,
pub description: Option<String>,
pub keywords: Vec<String>,
pub quality_score: f32,
}
pub struct FastExtractor;
impl FastExtractor {
/// Extract text content from HTML without microservice
pub fn extract(html: &str) -> FastExtractResult {
let fragment = Html::parse_document(html);
let mut result = FastExtractResult::default();
// 1. Title
let title_selector = Selector::parse("title").unwrap();
if let Some(title_node) = fragment.select(&title_selector).next() {
result.title = title_node.text().collect::<Vec<_>>().join(" ");
} else {
// Try H1 if title is missing
let h1_selector = Selector::parse("h1").unwrap();
if let Some(h1_node) = fragment.select(&h1_selector).next() {
result.title = h1_node.text().collect::<Vec<_>>().join(" ");
}
}
// 2. Description (Meta)
let desc_selector = Selector::parse("meta[name='description']").unwrap();
if let Some(desc_node) = fragment.select(&desc_selector).next() {
result.description = desc_node.value().attr("content").map(|s| s.to_string());
}
// 3. Content - prioritize main, article, or div.content
let content_selectors = [
Selector::parse("main").unwrap(),
Selector::parse("article").unwrap(),
Selector::parse("div#content").unwrap(),
Selector::parse("div.content").unwrap(),
Selector::parse("div.post-content").unwrap(),
Selector::parse("div.article-body").unwrap(),
];
let mut content_found = false;
for selector in &content_selectors {
if let Some(node) = fragment.select(selector).next() {
// Collect text while ignoring scripts/styles
result.content = Self::extract_clean_text(node);
content_found = true;
break;
}
}
if !content_found {
// Fallback to body
let body_selector = Selector::parse("body").unwrap();
if let Some(node) = fragment.select(&body_selector).next() {
result.content = Self::extract_clean_text(node);
}
}
// 4. Quality Score Calculation (Simple heuristic)
let mut q_score = 0.0f32;
if !result.title.is_empty() {
q_score += 0.2;
}
if result.content.len() > 500 {
q_score += 0.4;
} else if result.content.len() > 100 {
q_score += 0.2;
}
if result.description.is_some() {
q_score += 0.2;
}
// Penalize very short content
if result.content.len() < 50 {
q_score -= 0.3;
}
result.quality_score = q_score.clamp(0.0, 1.0);
result
}
fn extract_clean_text(node: scraper::ElementRef) -> String {
let mut text = String::new();
for element in node.children() {
if let Some(tag) = element.value().as_element() {
// Skip noise tags
let tag_name = tag.name();
if tag_name == "script"
|| tag_name == "style"
|| tag_name == "nav"
|| tag_name == "footer"
|| tag_name == "header"
{
continue;
}
}
if let Some(t) = element.value().as_text() {
let trimmed = t.trim();
if !trimmed.is_empty() {
text.push_str(trimmed);
text.push(' ');
}
} else if let Some(el) = scraper::ElementRef::wrap(element) {
text.push_str(&Self::extract_clean_text(el));
}
}
text.trim().to_string()
}
}
|