PageRenderTime 255ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/spelling/CorrectorLauncher.java

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