Pure Programmer
Blue Matrix


Cluster Map

Project: Spell Checker

Write a spell checking program. The program should take two filenames on the command line. The first is the file containing correctly spelled dictionary words (one per line). The second is the text file to check. The program should print misspelled words one per line without duplicates to the console.

See Webster's Second International for a file that contains US English words (235k words from 1934 edition) or the open source file [[English Words]] on GitHub (466k words). You can download ebooks in text format from [[Project Gutenberg]] to test your spell checker.

Output
$ rustc SpellChecker.rs error[E0432]: unresolved import `regex` --> SpellChecker.rs:8:5 | 8 | use regex::Regex; | ^^^^^ maybe a missing crate `regex`? | = help: consider adding `extern crate regex` to use the `regex` crate error[E0425]: cannot find value `ifh` in this scope --> SpellChecker.rs:17:11 | 17 | utf8mode(ifh); | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> SpellChecker.rs:19:21 | 19 | while getline(line,ifh) { | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> SpellChecker.rs:22:8 | 22 | close(ifh); | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> SpellChecker.rs:26:11 | 26 | utf8mode(ifh); | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> SpellChecker.rs:29:21 | 29 | while getline(line,ifh) { | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> SpellChecker.rs:44:8 | 44 | close(ifh); | ^^^ not found in this scope error[E0425]: cannot find function `exit` in this scope --> SpellChecker.rs:51:3 | 51 | exit(1); | ^^^^ not found in this scope | help: consider importing this function | 8 + use std::process::exit; | error[E0425]: cannot find function `utf8mode` in this scope --> SpellChecker.rs:17:2 | 17 | utf8mode(ifh); | ^^^^^^^^ not found in this scope error[E0425]: cannot find function `getline` in this scope --> SpellChecker.rs:19:8 | 19 | while getline(line,ifh) { | ^^^^^^^ not found in this scope error[E0425]: cannot find function `tolower` in this scope --> SpellChecker.rs:20:14 | 20 | dictionary[tolower(line)] = true; | ^^^^^^^ not found in this scope error[E0425]: cannot find function `close` in this scope --> SpellChecker.rs:22:2 | 22 | close(ifh); | ^^^^^ not found in this scope error[E0308]: mismatched types --> SpellChecker.rs:16:41 | 16 | fn readDictionaryFile(filespec:&str) -> Result<(), Arc<dyn error::Error>> { | ------------------ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Result<(), Arc<dyn Error>>`, found `()` | | | implicitly returns `()` as its body has no tail or `return` expression | = note: expected enum `Result<(), Arc<(dyn std::error::Error + 'static)>>` found unit type `()` error[E0425]: cannot find function `utf8mode` in this scope --> SpellChecker.rs:26:2 | 26 | utf8mode(ifh); | ^^^^^^^^ not found in this scope error[E0425]: cannot find function `getline` in this scope --> SpellChecker.rs:29:8 | 29 | while getline(line,ifh) { | ^^^^^^^ not found in this scope error[E0425]: cannot find function `findAll` in this scope --> SpellChecker.rs:30:33 | 30 | let mut matches:Vec<String> = findAll(line,Regex::new(r"\\w+").unwrap()); | ^^^^^^^ not found in this scope error[E0425]: cannot find function `defined` in this scope --> SpellChecker.rs:31:6 | 31 | if defined(matches) { | ^^^^^^^ not found in this scope error[E0425]: cannot find function `tolower` in this scope --> SpellChecker.rs:33:25 | 33 | let slower:String = tolower(s); | ^^^^^^^ not found in this scope error[E0425]: cannot find function `mapContains` in this scope --> SpellChecker.rs:34:9 | 34 | if !mapContains(dictionary,slower) { | ^^^^^^^^^^^ not found in this scope error[E0425]: cannot find function `sortedKeys` in this scope --> SpellChecker.rs:40:31 | 40 | let sortedkeys:Vec<String> = sortedKeys(misspelledWords); | ^^^^^^^^^^ not found in this scope error[E0425]: cannot find function `close` in this scope --> SpellChecker.rs:44:2 | 44 | close(ifh); | ^^^^^ not found in this scope error[E0308]: mismatched types --> SpellChecker.rs:25:37 | 25 | fn spellcheckFile(filespec:&str) -> Result<(), Arc<dyn error::Error>> { | -------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Result<(), Arc<dyn Error>>`, found `()` | | | implicitly returns `()` as its body has no tail or `return` expression | = note: expected enum `Result<(), Arc<(dyn std::error::Error + 'static)>>` found unit type `()` error[E0433]: failed to resolve: use of undeclared crate or module `io` --> SpellChecker.rs:63:59 | 63 | if let Some(ex) = utils::isCustomErrorType(ex.clone(), io::Error("".to_string())){ | ^^ | | | use of undeclared crate or module `io` | help: a builtin type with a similar name exists: `i8` error: aborting due to 23 previous errors Some errors have detailed explanations: E0308, E0425, E0432, E0433. For more information about an error, try `rustc --explain E0308`. $ rustc SpellChecker.rs error[E0432]: unresolved import `regex` --> SpellChecker.rs:8:5 | 8 | use regex::Regex; | ^^^^^ maybe a missing crate `regex`? | = help: consider adding `extern crate regex` to use the `regex` crate error[E0425]: cannot find value `ifh` in this scope --> SpellChecker.rs:17:11 | 17 | utf8mode(ifh); | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> SpellChecker.rs:19:21 | 19 | while getline(line,ifh) { | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> SpellChecker.rs:22:8 | 22 | close(ifh); | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> SpellChecker.rs:26:11 | 26 | utf8mode(ifh); | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> SpellChecker.rs:29:21 | 29 | while getline(line,ifh) { | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> SpellChecker.rs:44:8 | 44 | close(ifh); | ^^^ not found in this scope error[E0425]: cannot find function `exit` in this scope --> SpellChecker.rs:51:3 | 51 | exit(1); | ^^^^ not found in this scope | help: consider importing this function | 8 + use std::process::exit; | error[E0425]: cannot find function `utf8mode` in this scope --> SpellChecker.rs:17:2 | 17 | utf8mode(ifh); | ^^^^^^^^ not found in this scope error[E0425]: cannot find function `getline` in this scope --> SpellChecker.rs:19:8 | 19 | while getline(line,ifh) { | ^^^^^^^ not found in this scope error[E0425]: cannot find function `tolower` in this scope --> SpellChecker.rs:20:14 | 20 | dictionary[tolower(line)] = true; | ^^^^^^^ not found in this scope error[E0425]: cannot find function `close` in this scope --> SpellChecker.rs:22:2 | 22 | close(ifh); | ^^^^^ not found in this scope error[E0308]: mismatched types --> SpellChecker.rs:16:41 | 16 | fn readDictionaryFile(filespec:&str) -> Result<(), Arc<dyn error::Error>> { | ------------------ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Result<(), Arc<dyn Error>>`, found `()` | | | implicitly returns `()` as its body has no tail or `return` expression | = note: expected enum `Result<(), Arc<(dyn std::error::Error + 'static)>>` found unit type `()` error[E0425]: cannot find function `utf8mode` in this scope --> SpellChecker.rs:26:2 | 26 | utf8mode(ifh); | ^^^^^^^^ not found in this scope error[E0425]: cannot find function `getline` in this scope --> SpellChecker.rs:29:8 | 29 | while getline(line,ifh) { | ^^^^^^^ not found in this scope error[E0425]: cannot find function `findAll` in this scope --> SpellChecker.rs:30:33 | 30 | let mut matches:Vec<String> = findAll(line,Regex::new(r"\\w+").unwrap()); | ^^^^^^^ not found in this scope error[E0425]: cannot find function `defined` in this scope --> SpellChecker.rs:31:6 | 31 | if defined(matches) { | ^^^^^^^ not found in this scope error[E0425]: cannot find function `tolower` in this scope --> SpellChecker.rs:33:25 | 33 | let slower:String = tolower(s); | ^^^^^^^ not found in this scope error[E0425]: cannot find function `mapContains` in this scope --> SpellChecker.rs:34:9 | 34 | if !mapContains(dictionary,slower) { | ^^^^^^^^^^^ not found in this scope error[E0425]: cannot find function `sortedKeys` in this scope --> SpellChecker.rs:40:31 | 40 | let sortedkeys:Vec<String> = sortedKeys(misspelledWords); | ^^^^^^^^^^ not found in this scope error[E0425]: cannot find function `close` in this scope --> SpellChecker.rs:44:2 | 44 | close(ifh); | ^^^^^ not found in this scope error[E0308]: mismatched types --> SpellChecker.rs:25:37 | 25 | fn spellcheckFile(filespec:&str) -> Result<(), Arc<dyn error::Error>> { | -------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Result<(), Arc<dyn Error>>`, found `()` | | | implicitly returns `()` as its body has no tail or `return` expression | = note: expected enum `Result<(), Arc<(dyn std::error::Error + 'static)>>` found unit type `()` error[E0433]: failed to resolve: use of undeclared crate or module `io` --> SpellChecker.rs:63:59 | 63 | if let Some(ex) = utils::isCustomErrorType(ex.clone(), io::Error("".to_string())){ | ^^ | | | use of undeclared crate or module `io` | help: a builtin type with a similar name exists: `i8` error: aborting due to 23 previous errors Some errors have detailed explanations: E0308, E0425, E0432, E0433. For more information about an error, try `rustc --explain E0308`. $ rustc SpellChecker.rs error[E0432]: unresolved import `regex` --> SpellChecker.rs:8:5 | 8 | use regex::Regex; | ^^^^^ maybe a missing crate `regex`? | = help: consider adding `extern crate regex` to use the `regex` crate error[E0425]: cannot find value `ifh` in this scope --> SpellChecker.rs:17:11 | 17 | utf8mode(ifh); | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> SpellChecker.rs:19:21 | 19 | while getline(line,ifh) { | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> SpellChecker.rs:22:8 | 22 | close(ifh); | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> SpellChecker.rs:26:11 | 26 | utf8mode(ifh); | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> SpellChecker.rs:29:21 | 29 | while getline(line,ifh) { | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> SpellChecker.rs:44:8 | 44 | close(ifh); | ^^^ not found in this scope error[E0425]: cannot find function `exit` in this scope --> SpellChecker.rs:51:3 | 51 | exit(1); | ^^^^ not found in this scope | help: consider importing this function | 8 + use std::process::exit; | error[E0425]: cannot find function `utf8mode` in this scope --> SpellChecker.rs:17:2 | 17 | utf8mode(ifh); | ^^^^^^^^ not found in this scope error[E0425]: cannot find function `getline` in this scope --> SpellChecker.rs:19:8 | 19 | while getline(line,ifh) { | ^^^^^^^ not found in this scope error[E0425]: cannot find function `tolower` in this scope --> SpellChecker.rs:20:14 | 20 | dictionary[tolower(line)] = true; | ^^^^^^^ not found in this scope error[E0425]: cannot find function `close` in this scope --> SpellChecker.rs:22:2 | 22 | close(ifh); | ^^^^^ not found in this scope error[E0308]: mismatched types --> SpellChecker.rs:16:41 | 16 | fn readDictionaryFile(filespec:&str) -> Result<(), Arc<dyn error::Error>> { | ------------------ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Result<(), Arc<dyn Error>>`, found `()` | | | implicitly returns `()` as its body has no tail or `return` expression | = note: expected enum `Result<(), Arc<(dyn std::error::Error + 'static)>>` found unit type `()` error[E0425]: cannot find function `utf8mode` in this scope --> SpellChecker.rs:26:2 | 26 | utf8mode(ifh); | ^^^^^^^^ not found in this scope error[E0425]: cannot find function `getline` in this scope --> SpellChecker.rs:29:8 | 29 | while getline(line,ifh) { | ^^^^^^^ not found in this scope error[E0425]: cannot find function `findAll` in this scope --> SpellChecker.rs:30:33 | 30 | let mut matches:Vec<String> = findAll(line,Regex::new(r"\\w+").unwrap()); | ^^^^^^^ not found in this scope error[E0425]: cannot find function `defined` in this scope --> SpellChecker.rs:31:6 | 31 | if defined(matches) { | ^^^^^^^ not found in this scope error[E0425]: cannot find function `tolower` in this scope --> SpellChecker.rs:33:25 | 33 | let slower:String = tolower(s); | ^^^^^^^ not found in this scope error[E0425]: cannot find function `mapContains` in this scope --> SpellChecker.rs:34:9 | 34 | if !mapContains(dictionary,slower) { | ^^^^^^^^^^^ not found in this scope error[E0425]: cannot find function `sortedKeys` in this scope --> SpellChecker.rs:40:31 | 40 | let sortedkeys:Vec<String> = sortedKeys(misspelledWords); | ^^^^^^^^^^ not found in this scope error[E0425]: cannot find function `close` in this scope --> SpellChecker.rs:44:2 | 44 | close(ifh); | ^^^^^ not found in this scope error[E0308]: mismatched types --> SpellChecker.rs:25:37 | 25 | fn spellcheckFile(filespec:&str) -> Result<(), Arc<dyn error::Error>> { | -------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Result<(), Arc<dyn Error>>`, found `()` | | | implicitly returns `()` as its body has no tail or `return` expression | = note: expected enum `Result<(), Arc<(dyn std::error::Error + 'static)>>` found unit type `()` error[E0433]: failed to resolve: use of undeclared crate or module `io` --> SpellChecker.rs:63:59 | 63 | if let Some(ex) = utils::isCustomErrorType(ex.clone(), io::Error("".to_string())){ | ^^ | | | use of undeclared crate or module `io` | help: a builtin type with a similar name exists: `i8` error: aborting due to 23 previous errors Some errors have detailed explanations: E0308, E0425, E0432, E0433. For more information about an error, try `rustc --explain E0308`.

Solution