Skip to content

Commit ed00d1d

Browse files
committed
Add stop words removal
1 parent ac2f199 commit ed00d1d

3 files changed

Lines changed: 12 additions & 5 deletions

File tree

src/lexer.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use rust_stemmers::{Algorithm, Stemmer};
22

3+
const STOP_WORDS: [&str; 4] = ["is", "as", "the", "a"];
4+
35
#[derive(Debug)]
46
pub struct Lexer<'a> {
57
content: &'a [char]
@@ -55,6 +57,13 @@ impl<'a> Lexer<'a> {
5557
if self.content[0].is_alphabetic() {
5658
let result = self.chop_while(|x| x.is_alphanumeric());
5759
let token = result.iter().collect::<String>();
60+
61+
// Remove stop words
62+
if STOP_WORDS.contains(&token.as_str()) {
63+
return None;
64+
}
65+
66+
// Stemming
5867
let stemmed_token = stemmer.stem(&token).into_owned().to_uppercase();
5968
return Some(stemmed_token);
6069
}

src/main.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,6 @@ fn append_folder_to_model(dir_path: &Path, model: Arc<Mutex<InMemoryModel>>, pro
207207

208208
/* Check the amount of files present in the main frequency table index */
209209
fn check_index(index_path: &str) -> Result<(), ()> {
210-
let a: f64 = 0.1;
211-
212210
let index_file = fs::File::open(index_path).map_err(|err| {
213211
eprintln!("{}: Could not open file {file} as \"{err}\"", "ERROR".bold().red(), file = index_path.bright_blue(), err = err.to_string().red());
214212
exit(1);

src/model.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub type GlobalTermFreq = HashMap::<String, usize>;
2626
#[derive(Serialize, Deserialize)]
2727
pub struct Doc {
2828
count: usize, // Total number of terms (tokens) present in this document.
29-
ft: FreqTable, // Frequency table mapping each term to the number of times it appears within this document
29+
ft: FreqTable, // Frequency table mapping each term to the number of times it appears within this document
3030
last_modified: SystemTime // The last time this document was modified on disk. Used to detect outdated indexes and trigger reindexing when needed.
3131
}
3232

@@ -81,8 +81,8 @@ fn tf(term: &str, doc: &Doc) -> f32 {
8181
}
8282
// For TF-IDF Ranking
8383
fn idf(term: &str, model: &InMemoryModel) -> f32 {
84-
let n = model.docs.len() as f32;
85-
let d = model.gtf.get(term).cloned().unwrap_or(1) as f32;
84+
let n = model.docs.len() as f32; // total docs in corpus
85+
let d = model.gtf.get(term).cloned().unwrap_or(1) as f32; // number of time the terms has appeared in the entire corpus
8686
f32::log10(n / d)
8787
}
8888

0 commit comments

Comments
 (0)