Skip to content

Instantly share code, notes, and snippets.

@archer884
Created November 29, 2014 23:52
Show Gist options
  • Save archer884/c5456d7488a338fec186 to your computer and use it in GitHub Desktop.
Save archer884/c5456d7488a338fec186 to your computer and use it in GitHub Desktop.
Checks files for spelling errors.
#![feature(phase)]
#[phase(plugin)]
extern crate regex_macros;
extern crate regex;
use regex::Regex;
use std::ascii::AsciiExt;
use std::collections::HashSet;
use std::io::{BufferedReader, File};
use std::io::fs::PathExtensions;
static WORD_PATTERN: Regex = regex!(r"(\w)+('\w+)?([.])?");
fn main() {
let dict_set = get_dict();
let content = get_content();
let mut has_errors = false;
let mut line_counter = 0i;
for line in content.iter() {
line_counter += 1;
for word in get_words(line.as_slice()).iter() {
if !dict_set.contains(word) && !dict_set.contains(&word.replace(".", "")) {
has_errors = true;
println!("{}: {}", line_counter.to_string(), word);
}
}
}
if !has_errors { println!("Good!"); }
}
fn get_words(text: &str) -> Vec<String> {
WORD_PATTERN.captures_iter(text).map(|cap| cap.at(0).to_ascii_upper()).collect()
}
fn get_content() -> Vec<String> {
let args = std::os::args();
if args.len() > 1 {
BufferedReader::new(File::open(&Path::new(&args[1]))).lines().map(|line| line.unwrap()).collect()
} else {
std::io::stdin().lines().map(|line| line.unwrap()).collect()
}
}
fn get_dict() -> HashSet<String> {
let std_word_path = Path::new("/usr/share/dict/words");
let spc_word_path = Path::new("./.spelling");
let mut dict_set: HashSet<String> = HashSet::new();
if std_word_path.exists() {
populate_hash(&mut dict_set, &std_word_path);
}
if spc_word_path.exists() {
populate_hash(&mut dict_set, &spc_word_path);
}
dict_set
}
fn populate_hash(dict_set: &mut HashSet<String>, path: &Path) {
for word in BufferedReader::new(File::open(path)).lines().map(|line| line.unwrap()) {
if !dict_set.contains(&word) {
dict_set.insert(word.trim().to_ascii_upper());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment