PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/starter/src/cs276/pe1/spell/SpellingScorer.java

https://github.com/rioleo/huangenius
Java | 96 lines | 66 code | 11 blank | 19 comment | 12 complexity | df5f8e58fa310122b48fdf0362764449 MD5 | raw file
  1. package cs276.pe1.spell;
  2. import java.io.File;
  3. import java.util.Arrays;
  4. import java.util.LinkedHashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import cs276.util.IOUtils;
  8. /**
  9. * Scorer for evaluating a SpellingCorrector. Reads a file
  10. * that contains correctly spelled words and, for each, one or
  11. * more mis-spellings. A SpellingCorrector class is then
  12. * instantiated (the specific type is passed as an argument
  13. * and it is assumed to have a no-arg constructor).
  14. *
  15. * @author dramage
  16. */
  17. public class SpellingScorer {
  18. /**
  19. * Prints usage information, a status message, and an optional exit
  20. * reason before exiting with status -1.
  21. */
  22. public static void usage(String message, Throwable cause) {
  23. System.err.println("CS276 PE1 Scoring Script");
  24. System.err.println();
  25. System.err.println("Usage: ");
  26. System.err.println(" (java-cmd) SpellingScorer cs276.pe1.spell.YourSpellingCorrectorClass");
  27. System.err.println();
  28. System.err.println(message);
  29. if (cause != null) {
  30. cause.printStackTrace();
  31. }
  32. System.exit(-1);
  33. }
  34. /**
  35. * Default usage information with no extra reasons provided.
  36. */
  37. public static void usage() {
  38. usage("",null);
  39. }
  40. public static void main(String[] argv) {
  41. if (argv.length < 1 || argv.length > 2) {
  42. usage();
  43. }
  44. // instantiate scorer
  45. SpellingCorrector scorer = null;
  46. try {
  47. Class<?> type = Class.forName(argv[0]);
  48. scorer = (SpellingCorrector)type.newInstance();
  49. } catch (Exception e) {
  50. usage("Error while instantiating corrector " + argv[0]
  51. + ": does it have a public no-args constructor?", e);
  52. }
  53. // load words file
  54. File wordsFile = new File(argv.length == 2 ? argv[1]
  55. : "/afs/ir/class/cs276/pe1-2011/spelltest.txt");
  56. if (!wordsFile.exists() || !wordsFile.canRead()) {
  57. usage();
  58. }
  59. Map<String,List<String>> words = new LinkedHashMap<String,List<String>>();
  60. try {
  61. for (String line : IOUtils.readLines(IOUtils.openFile(wordsFile))) {
  62. List<String> split = Arrays.asList(line.split("\\s+"));
  63. words.put(split.get(0), split.subList(1, split.size()));
  64. }
  65. } catch (Exception e) {
  66. usage("Error while loading words file from " + wordsFile, e);
  67. }
  68. // score words
  69. final long startTime = System.currentTimeMillis();
  70. int nCorrect = 0;
  71. int nTotal = 0;
  72. for (Map.Entry<String, List<String>> entry : words.entrySet()) {
  73. final String word = entry.getKey();
  74. for (String misspelling : entry.getValue()) {
  75. List<String> corrections = scorer.corrections(misspelling);
  76. if (!corrections.isEmpty() && corrections.get(0).equals(word)) {
  77. nCorrect += 1;
  78. }
  79. nTotal += 1;
  80. }
  81. }
  82. System.out.println(argv[0] + ": " + nCorrect + "/" + nTotal + " in "
  83. + (System.currentTimeMillis() - startTime + "ms"));
  84. }
  85. }