Spaces:
Build error
Build error
Download src/crawler/extractor.rs from LuckyMod/intentforge: direct link, hf CLI and curl.
- Browser
- Download file 4.27 kB
-
https://huggingface.co/spaces/LuckyMod/intentforge/resolve/main/src/crawler/extractor.rs
- Command line
-
hf download hf://spaces/LuckyMod/intentforge/src/crawler/extractor.rs
-
curl -L -o extractor.rs https://huggingface.co/spaces/LuckyMod/intentforge/resolve/main/src/crawler/extractor.rs
4.27 kB
| //! 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 | |
| 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() | |
| } | |
| } | |