KjetilIN / rs-search-engine

A custom search engine built with Rust. It parses HTML files and utilizes TF-IDF scoring to rank document relevance based on search queries. The project includes a Rust-based backend server and vanilla HTML/CSS for the web frontend.
MIT License
0 stars 0 forks source link

Description for each search result #7

Open KjetilIN opened 2 months ago

KjetilIN commented 2 months ago

What?

Create a paragraph for each search result

Current attempt:

This code was implemented, but it requires to re-read the file and get the line that has the keyword. Another way would make it a static and not make the description include the keyword

/// Creates the paragraph text under the search result
///
/// Uses the document content that is already parsed from the html parser
/// NOTE: This is not working that well. It is very slow.
/// Need another way to get the paragraph from
pub fn get_key_word_paragraph(document_path:&str, terms: &String) -> String {

    let content = parse_file_html(document_path).expect(&format!("File not found {}", document_path));

    let mut paragraph = String::new();
    let terms_list: Vec<&str> = terms.split_whitespace().collect();
    let mut paragraph_lines: usize = 0;

    for line in content.lines() {
        let mut found = false;
        let mut highlighted_line = String::new();

        // Split the line into words and check each word
        for word in line.split_whitespace() {
            if terms_list.contains(&word) {
                found = true;
                highlighted_line.push_str(&format!("<strong>{}</strong> ", word));
            } else {
                highlighted_line.push_str(&format!("{} ", word));
            }
        }

        // If any term is found in the line, add the highlighted line to the paragraph
        if found && paragraph_lines < MAX_PARAGRAPH_LINES_COUNTER {
            paragraph.push_str(&highlighted_line.trim_end());
            paragraph.push('\n');
            paragraph_lines += 1;
        }
    }

    paragraph
}