Skip to content

Commit 208a9bc

Browse files
committed
Fix tokenization problem. Wrap the logic in a loop to correctly skip stop words and single digit number. Rishab MF.
1 parent 5c1a659 commit 208a9bc

1 file changed

Lines changed: 22 additions & 20 deletions

File tree

src/lexer.rs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,32 +48,34 @@ impl<'a> Lexer<'a> {
4848

4949
fn next_token(&mut self) -> Option<String> {
5050
let stemmer = Stemmer::create(Algorithm::English);
51-
self.trim_left();
51+
loop {
52+
self.trim_left();
5253

53-
if self.content.len() == 0 {
54-
return None;
55-
}
54+
if self.content.len() == 0 {
55+
return None;
56+
}
5657

57-
if self.content[0].is_numeric() {
58-
// Ignore single digit number
59-
let result = self.chop_while(|x| x.is_numeric());
60-
if result.len() == 1 { return None; }
61-
return Some(result.iter().collect());
62-
}
58+
if self.content[0].is_numeric() {
59+
// Ignore single digit number
60+
let result = self.chop_while(|x| x.is_numeric());
61+
if result.len() == 1 { continue; }
62+
return Some(result.iter().collect());
63+
}
6364

64-
if self.content[0].is_alphabetic() {
65-
let result = self.chop_while(|x| x.is_alphanumeric());
66-
let token = result.iter().collect::<String>();
65+
if self.content[0].is_alphabetic() {
66+
let result = self.chop_while(|x| x.is_alphanumeric());
67+
let token = result.iter().collect::<String>();
6768

68-
let stemmed = stemmer.stem(&token).into_owned().to_uppercase();
69-
if STOP_WORDS.contains(&stemmed.as_str()) {
70-
return None;
69+
let stemmed = stemmer.stem(&token).into_owned().to_uppercase();
70+
if STOP_WORDS.contains(&stemmed.as_str()) {
71+
continue;
72+
}
73+
return Some(stemmed);
7174
}
72-
return Some(stemmed);
75+
76+
let token = self.chop(1);
77+
return Some(token.iter().collect());
7378
}
74-
75-
let token = self.chop(1);
76-
return Some(token.iter().collect());
7779
}
7880
}
7981

0 commit comments

Comments
 (0)