Skip to content

Commit e82278f

Browse files
committed
Add source for ngram
1 parent 01fe817 commit e82278f

File tree

6 files changed

+61
-4
lines changed

6 files changed

+61
-4
lines changed

bin/generator-utils/ngram

0 Bytes
Binary file not shown.

bin/generator-utils/utils.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ function check_exercise_existence() {
5252
message "info" "These are the unimplemented practice exercises:
5353
${unimplemented_exercises}"
5454

55-
# this is a compiled, tiny Rust program
56-
# this fn uses the ngram algorithm to find the closest match to an exercise
57-
# I'm using the ngrammatic crate to achieve this
58-
bin/generator-utils/ngram "${unimplemented_exercises}" "$slug"
55+
# Find closest match to typed-in not-found slug
56+
# see util/ngram for source
57+
echo "${YELLOW}$(bin/generator-utils/ngram "${unimplemented_exercises}" "$slug")${RESET}"
58+
5959
exit 1
6060
fi
6161
}

util/ngram/Cargo.lock

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

util/ngram/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "ngram"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
ngrammatic = "0.4.0"

util/ngram/build

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env bash
2+
cargo build --release && cp ./target/release/ngram ../../bin/generator-utils && rm -rf ./target

util/ngram/src/main.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use ngrammatic::{CorpusBuilder, Pad};
2+
3+
fn main() {
4+
let mut args = std::env::args();
5+
let exercises = args.nth(1).expect("Missing exercises argument");
6+
let slug = args.nth(0).expect("Missing slug argument");
7+
let exercises: Vec<&str> = exercises
8+
.split(|c: char| c.is_whitespace() || c == '\n')
9+
.collect();
10+
let mut corpus = CorpusBuilder::new().arity(2).pad_full(Pad::Auto).finish();
11+
12+
for exercise in exercises.iter() {
13+
corpus.add_text(exercise);
14+
}
15+
16+
if let Some(top_result) = corpus.search(&slug, 0.25).first() {
17+
if top_result.similarity > 0.99 {
18+
println!("{}", top_result.text);
19+
} else {
20+
println!(
21+
"{} - There is an exercise with a similar name: '{}' [{:.0}% match]",
22+
slug,
23+
top_result.text,
24+
top_result.similarity * 100.0
25+
);
26+
}
27+
} else {
28+
println!("{}", slug);
29+
}
30+
}

0 commit comments

Comments
 (0)