From 6a197e023e3ec77f4b4a52bdbe75e8005ca29f38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vignesh=20Sarma=20K=20=28=E0=B4=B5=E0=B4=BF=E0=B4=98?= =?UTF-8?q?=E0=B5=8D=E0=B4=A8=E0=B5=87=E0=B4=B7=E0=B5=8D=20=E0=B4=B6?= =?UTF-8?q?=E0=B5=AA=E0=B4=AE=20=E0=B4=95=E0=B5=86=29?= Date: Tue, 28 Aug 2018 20:34:55 +0530 Subject: [PATCH] ran rustfmt --- src/snippet/mod.rs | 105 ++++++++++++++++++++++++++------------------- 1 file changed, 62 insertions(+), 43 deletions(-) diff --git a/src/snippet/mod.rs b/src/snippet/mod.rs index 344c44d82..26b6be0f6 100644 --- a/src/snippet/mod.rs +++ b/src/snippet/mod.rs @@ -1,12 +1,12 @@ -use tokenizer::{TokenStream, Tokenizer, Token}; -use std::collections::BTreeMap; -use Term; -use Document; -use Index; +use htmlescape::encode_minimal; use schema::FieldValue; use schema::Value; +use std::collections::BTreeMap; use tokenizer::BoxedTokenizer; -use htmlescape::encode_minimal; +use tokenizer::{Token, TokenStream, Tokenizer}; +use Document; +use Index; +use Term; #[derive(Debug)] pub struct HighlightSection { @@ -16,10 +16,7 @@ pub struct HighlightSection { impl HighlightSection { fn new(start: usize, stop: usize) -> HighlightSection { - HighlightSection { - start, - stop - } + HighlightSection { start, stop } } } @@ -33,18 +30,19 @@ pub struct FragmentCandidate { } impl FragmentCandidate { - /// Create a basic `FragmentCandidate` /// /// `score`, `num_chars` are set to 0 /// and `highlighted` is set to empty vec /// stop_offset is set to start_offset, which is taken as a param. fn new(start_offset: usize) -> FragmentCandidate { - FragmentCandidate{score: 0.0, - start_offset: start_offset, - stop_offset: start_offset, - num_chars: 0, - highlighted: vec![]} + FragmentCandidate { + score: 0.0, + start_offset: start_offset, + stop_offset: start_offset, + num_chars: 0, + highlighted: vec![], + } } /// Updates `score` and `highlighted` fields of the objects. @@ -57,8 +55,10 @@ impl FragmentCandidate { if let Some(score) = terms.get(&token.text.to_lowercase()) { self.score += score; - self.highlighted.push(HighlightSection{start: token.offset_from, - stop: token.offset_to}); + self.highlighted.push(HighlightSection { + start: token.offset_from, + stop: token.offset_to, + }); } } } @@ -69,11 +69,10 @@ pub struct Snippet { highlighted: Vec, } -const HIGHLIGHTEN_PREFIX:&str = ""; -const HIGHLIGHTEN_POSTFIX:&str = ""; +const HIGHLIGHTEN_PREFIX: &str = ""; +const HIGHLIGHTEN_POSTFIX: &str = ""; impl Snippet { - /// Returns a hignlightned html from the `Snippet`. pub fn to_html(&self) -> String { let mut html = String::new(); @@ -86,7 +85,9 @@ impl Snippet { html.push_str(HIGHLIGHTEN_POSTFIX); start_from = item.stop; } - html.push_str(&encode_minimal(&self.fragments[start_from..self.fragments.len()])); + html.push_str(&encode_minimal( + &self.fragments[start_from..self.fragments.len()], + )); html } } @@ -116,10 +117,11 @@ fn search_fragments<'a>( tokenizer: Box, text: &'a str, terms: BTreeMap, - max_num_chars: usize) -> Vec { + max_num_chars: usize, +) -> Vec { let mut token_stream = tokenizer.token_stream(text); let mut fragment = FragmentCandidate::new(0); - let mut fragments:Vec = vec![]; + let mut fragments: Vec = vec![]; while let Some(next) = token_stream.next() { if (next.offset_to - fragment.start_offset) > max_num_chars { @@ -141,24 +143,41 @@ fn search_fragments<'a>( /// /// Takes a vector of `FragmentCandidate`s and the text. /// Figures out the best fragment from it and creates a snippet. -fn select_best_fragment_combination<'a>(fragments: Vec, - text: &'a str,) -> Snippet { +fn select_best_fragment_combination<'a>( + fragments: Vec, + text: &'a str, +) -> Snippet { if let Some(init) = fragments.iter().nth(0) { - let fragment = fragments.iter().skip(1).fold(init, |acc, item| { - if item.score > acc.score { item } else { acc } - }); + let fragment = + fragments.iter().skip(1).fold( + init, + |acc, item| { + if item.score > acc.score { + item + } else { + acc + } + }, + ); let fragment_text = &text[fragment.start_offset..fragment.stop_offset]; - let highlighted = fragment.highlighted.iter().map(|item| { - HighlightSection{start: item.start-fragment.start_offset, - stop: item.stop-fragment.start_offset} - }).collect(); - Snippet{fragments: fragment_text.to_owned(), - highlighted: highlighted} + let highlighted = fragment + .highlighted + .iter() + .map(|item| HighlightSection { + start: item.start - fragment.start_offset, + stop: item.stop - fragment.start_offset, + }).collect(); + Snippet { + fragments: fragment_text.to_owned(), + highlighted: highlighted, + } } else { // when there no fragments to chose from, // for now create a empty snippet - Snippet{fragments: String::new(), - highlighted: vec![]} + Snippet { + fragments: String::new(), + highlighted: vec![], + } } } @@ -166,19 +185,19 @@ pub fn generate_snippet<'a>( doc: &'a [FieldValue], index: &Index, terms: Vec, - max_num_chars: usize) -> Snippet { + max_num_chars: usize, +) -> Snippet { unimplemented!(); } - #[cfg(test)] mod tests { - use tokenizer::{SimpleTokenizer, box_tokenizer}; - use std::iter::Iterator; - use std::collections::BTreeMap; use super::{search_fragments, select_best_fragment_combination}; + use std::collections::BTreeMap; + use std::iter::Iterator; + use tokenizer::{box_tokenizer, SimpleTokenizer}; - const TOKENIZER:SimpleTokenizer = SimpleTokenizer; + const TOKENIZER: SimpleTokenizer = SimpleTokenizer; #[test] fn test_snippet() {