webpage_quality_analyzer 1.0.2

High-performance webpage quality analyzer with 115 comprehensive metrics - Rust library with WASM, C++, and Python bindings
Documentation
//! # Custom Profile Creation Examples
//!
//! This example demonstrates various ways to create and customize scoring profiles
//! using the ProfileBuilder API. It shows:
//!
//! 1. Creating profiles from scratch with ProfileBuilder
//! 2. Using content type presets
//! 3. Setting custom metric weights
//! 4. Setting custom thresholds
//! 5. Using ProfileTemplates
//!
//! Run with: `cargo run --example custom_profile_creation`

use std::collections::HashMap;
use webpage_quality_analyzer::config::profile_builder::{ContentType, ProfileBuilder};
use webpage_quality_analyzer::config::ProfileTemplates;
use webpage_quality_analyzer::metrics::ThresholdSet;

fn main() {
    println!("🎨 Custom Profile Creation Examples\n");
    println!("{}", "=".repeat(80));

    // Example 1: Basic Profile
    example_1_basic_profile();

    // Example 2: Content Type Presets
    example_2_content_type_presets();

    // Example 3: Custom Metric Weights
    example_3_custom_weights();

    // Example 4: Custom Thresholds
    example_4_custom_thresholds();

    // Example 5: Using ProfileTemplates
    example_5_profile_templates();

    // Example 6: Advanced Customization
    example_6_advanced_custom();

    println!("\n✅ All examples completed successfully!");
}

/// Example 1: Basic Profile Creation
fn example_1_basic_profile() {
    println!("\n📊 Example 1: Basic Profile Creation");
    println!("{}", "-".repeat(80));

    let profile = ProfileBuilder::new("my_custom_profile")
        .with_description("A simple custom profile for testing")
        .build()
        .expect("Failed to build profile");

    println!("✓ Profile Name: {}", profile.metadata.name);
    println!("✓ Description: {}", profile.metadata.description);
    println!("✓ Category Weights:");
    for (category, weight) in &profile.category_weights {
        println!("  - {}: {:.2}", category, weight);
    }
}

/// Example 2: Using Content Type Presets
fn example_2_content_type_presets() {
    println!("\n📊 Example 2: Content Type Presets");
    println!("{}", "-".repeat(80));

    // Long-form article preset
    let article_profile = ProfileBuilder::new("tech_blog")
        .for_content_type(ContentType::LongFormArticle)
        .with_description("Profile for technical blog posts")
        .build()
        .expect("Failed to build article profile");

    println!("✓ Long-Form Article Profile:");
    println!(
        "  - Content weight: {:.2}",
        article_profile
            .category_weights
            .get("content")
            .unwrap_or(&0.0)
    );

    // News article preset
    let news_profile = ProfileBuilder::new("news_site")
        .for_content_type(ContentType::NewsArticle)
        .with_description("Profile for news articles")
        .build()
        .expect("Failed to build news profile");

    println!("✓ News Article Profile:");
    println!(
        "  - Content weight: {:.2}",
        news_profile.category_weights.get("content").unwrap_or(&0.0)
    );
    println!(
        "  - SEO weight: {:.2}",
        news_profile.category_weights.get("seo").unwrap_or(&0.0)
    );
}

/// Example 3: Custom Metric Weights
fn example_3_custom_weights() {
    println!("\n📊 Example 3: Custom Metric Weights");
    println!("{}", "-".repeat(80));

    let profile = ProfileBuilder::new("content_first")
        .for_content_type(ContentType::LongFormArticle)
        .with_description("Extremely content-focused profile")
        .with_metric_weight("word_count", 0.60)
        .expect("Failed to set word_count weight")
        .with_metric_weight("paragraph_count", 0.30)
        .expect("Failed to set paragraph_count weight")
        .with_metric_weight("title_len", 0.10)
        .expect("Failed to set title_len weight")
        .build()
        .expect("Failed to build profile");

    println!("✓ Custom Metric Weights:");
    for (metric, override_cfg) in &profile.metric_overrides {
        println!("  - {}: {:.2}", metric, override_cfg.weight);
    }
}

/// Example 4: Custom Thresholds
fn example_4_custom_thresholds() {
    println!("\n📊 Example 4: Custom Thresholds");
    println!("{}", "-".repeat(80));

    // Define custom thresholds for word count
    let word_count_thresholds = ThresholdSet {
        excellent: 3000.0, // Very long articles
        good: 2000.0,
        fair: 1000.0,
        poor: 500.0,
    };

    let profile = ProfileBuilder::new("long_form_strict")
        .for_content_type(ContentType::LongFormArticle)
        .with_description("Strict long-form article requirements")
        .with_custom_threshold("word_count", word_count_thresholds)
        .expect("Failed to set custom thresholds")
        .build()
        .expect("Failed to build profile");

    if let Some(override_cfg) = profile.metric_overrides.get("word_count") {
        if let Some(thresholds) = &override_cfg.thresholds {
            println!("✓ Custom Word Count Thresholds:");
            println!("  - Excellent: {} words", thresholds.excellent);
            println!("  - Good: {} words", thresholds.good);
            println!("  - Fair: {} words", thresholds.fair);
            println!("  - Poor: {} words", thresholds.poor);
        }
    }
}

/// Example 5: Using ProfileTemplates
fn example_5_profile_templates() {
    println!("\n📊 Example 5: Using ProfileTemplates");
    println!("{}", "-".repeat(80));

    // List all available templates
    let templates = ProfileTemplates::list_templates();
    println!("✓ Available Templates:");
    for template in &templates {
        println!("  - {}", template);
    }
    println!();

    // Get a specific template
    let template = ProfileTemplates::get_template("content_article")
        .expect("Failed to get content_article template");

    println!("✓ Content Article Template:");
    println!("  - Name: {}", template.metadata.name);
    println!("  - Description: {}", template.metadata.description);
    println!(
        "  - Content weight: {:.2}",
        template.category_weights.get("content").unwrap_or(&0.0)
    );
}

/// Example 6: Advanced Customization
fn example_6_advanced_custom() {
    println!("\n📊 Example 6: Advanced Customization");
    println!("{}", "-".repeat(80));

    let profile = ProfileBuilder::new("advanced_custom")
        .with_description("Fully customized profile with multiple features")
        .for_content_type(ContentType::LongFormArticle)
        .with_metric_weight("word_count", 0.40)
        .expect("Failed to set weight")
        .with_metric_weight("readability_fk", 0.30)
        .expect("Failed to set weight")
        .with_metric_weight("title_len", 0.30)
        .expect("Failed to set weight")
        .with_metric_enabled("meta_desc_len", false)
        .build()
        .expect("Failed to build profile");

    println!("✓ Advanced profile created:");
    println!("  - Name: {}", profile.metadata.name);
    println!("  - Content Type: Long Form Article");
    println!(
        "  - Total metric overrides: {}",
        profile.metric_overrides.len()
    );
    println!(
        "  - Enabled metrics: {}",
        profile
            .metric_overrides
            .values()
            .filter(|m| m.enabled)
            .count()
    );
    println!(
        "  - Disabled metrics: {}",
        profile
            .metric_overrides
            .values()
            .filter(|m| !m.enabled)
            .count()
    );
}