Skip to content

Commit 363996c

Browse files
committed
Try stemming
1 parent f1bf7fb commit 363996c

5 files changed

Lines changed: 25 additions & 6 deletions

File tree

.gitignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
target/
22
docs.gl/
33

4-
index.json
5-
index.db
6-
index-big.json
4+
index*
5+
6+
.[dD]S_Store

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edition = "2021"
55

66
[dependencies]
77
colored = "3.0.0"
8+
rust-stemmers = "1.2.0"
89
serde = { version = "1.0.219" , features = ["derive"] }
910
serde_json = "1.0.140"
1011
sqlite = "0.30.3"

src/lexer.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use rust_stemmers::{Algorithm, Stemmer};
2+
13
#[derive(Debug)]
24
pub struct Lexer<'a> {
35
// Lifetimes implemented as content is not owned
@@ -38,6 +40,7 @@ impl<'a> Lexer<'a> {
3840
}
3941

4042
fn next_token(&mut self) -> Option<String> {
43+
let stemmer = Stemmer::create(Algorithm::English);
4144
self.trim_left();
4245

4346
if self.content.len() == 0 {
@@ -53,7 +56,10 @@ impl<'a> Lexer<'a> {
5356

5457
if self.content[0].is_alphabetic() {
5558
let result = self.chop_while(|x| x.is_alphanumeric());
56-
return Some(result.iter().map(|x| x.to_ascii_uppercase()).collect());
59+
let token = result.iter().collect::<String>();
60+
let stemmed_token = stemmer.stem(&token).into_owned().to_uppercase();
61+
println!("Before: {}, After: {}", token, stemmed_token);
62+
return Some(stemmed_token);
5763
}
5864

5965
// // Ignore single-character unwanted punctuation

src/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ fn parse_xml_file(file_path: &Path) -> Result<String, ()> {
1919
})?;
2020
let er = EventReader::new(BufReader::new(file));
2121
let mut content = String::new();
22+
2223
for event in er.into_iter() {
2324
let event = event.map_err(|err| {
2425
let TextPosition {row, column} = err.position();
@@ -166,7 +167,7 @@ fn fetch_model(index_path: &str) -> Result<InMemoryModel, ()> {
166167

167168
return Ok(model);
168169
}
169-
170+
170171
fn usage(program: &String) {
171172
eprintln!("{}: {program} [SUBCOMMAND] [OPTIONS]", "USAGE".bold().cyan(), program = program.bright_blue());
172173
eprintln!("Subcommands:");
@@ -195,7 +196,7 @@ fn entry() -> Result<(), ()> {
195196

196197
let subcommand = subcommand.ok_or_else(|| {
197198
usage(&program);
198-
eprintln!("ERROR: no subcommand is provided");
199+
eprintln!("{}: no subcommand is provided", "ERROR".red().bold());
199200
})?;
200201

201202
match subcommand.as_str() {

0 commit comments

Comments
 (0)