/src/spelling/CorrectorLauncher.java
Java | 70 lines | 61 code | 8 blank | 1 comment | 8 complexity | 2212ce0e32c0bf4d9c0cb181ca557a3a MD5 | raw file
1package spelling;
2
3import java.io.File;
4import java.io.FileNotFoundException;
5import java.io.IOException;
6import java.util.Locale;
7import java.util.Scanner;
8
9import spelling.corrector.CorrectorBuilder;
10import spelling.corrector.SpellingCorrector;
11import spelling.dictionary.Dictionary;
12
13public class CorrectorLauncher {
14
15 public static void main(String[] args) throws IOException {
16 try {
17 SpellingCorrector sc = handleCommandLine(args);
18 Scanner inputScanner = new Scanner(System.in);
19 System.out.print("Please enter a word (anything else will quit the application):");
20 while(inputScanner.hasNext("[a-zA-Z]+")){
21 String toCorrect = inputScanner.next("[a-zA-Z]+");
22 String correction = sc.correct(toCorrect.toLowerCase());
23 System.out.println(toCorrect+ ":\t did you mean "+ correction);
24 System.out.print("\nPlease enter a word:");
25 }
26
27 } catch (FileNotFoundException e) {
28 System.out.println(e.getMessage());
29 }
30 }
31 private static SpellingCorrector handleCommandLine(String[] args) throws IOException {
32 boolean hasCommandFlag = args.length > 0;
33 String filename = "holmes.txt";
34 boolean isVerbose = false;
35 if(hasCommandFlag){
36 for(int index=0;index<args.length;index++){
37 if("-h".equalsIgnoreCase(args[index])){
38 showHelp();
39 }else if("-f".equalsIgnoreCase(args[index])){
40 if(index+1 > args.length){
41 showHelp();
42 }else{
43 if(args[index+1].startsWith("-")){
44 showHelp();
45 }else{
46 filename = args[index+1];
47 index++;
48 }
49 }
50 }else if("-v".equalsIgnoreCase(args[index])){
51 if(index+1 > args.length){
52 //do nothing
53 }else{
54 isVerbose = true;
55 }
56 }
57
58 }
59 }
60 Dictionary.load(Locale.ENGLISH, new File(filename));
61
62 SpellingCorrector corrector = CorrectorBuilder.buildCorrector(isVerbose);
63 return corrector;
64 }
65
66 private static void showHelp() {
67 System.out.println("Usage SpellingCorrector [-h -f filename -v] \n\t-h : Show this help\n\t-f filename : Use this file to train the corrector\n\t-v [all] : Print statistic for methods if \"all\" then print statistic for every methods");
68 System.exit(0);
69 }
70}