File tree Expand file tree Collapse file tree 6 files changed +61
-4
lines changed Expand file tree Collapse file tree 6 files changed +61
-4
lines changed Original file line number Diff line number Diff line change @@ -52,10 +52,10 @@ function check_exercise_existence() {
52
52
message " info" " These are the unimplemented practice exercises:
53
53
${unimplemented_exercises} "
54
54
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
+
59
59
exit 1
60
60
fi
61
61
}
Original file line number Diff line number Diff line change
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"
Original file line number Diff line number Diff line change
1
+ #! /usr/bin/env bash
2
+ cargo build --release && cp ./target/release/ngram ../../bin/generator-utils && rm -rf ./target
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments