Guest User

Untitled

a guest
May 10th, 2016
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.18 KB | None | 0 0
  1. /**
  2.  * speller.c
  3.  *
  4.  * Computer Science 50
  5.  * Problem Set 5
  6.  *
  7.  * Implements a spell-checker.
  8.  */
  9.  
  10. #include <ctype.h>
  11. #include <stdio.h>
  12. #include <sys/resource.h>
  13. #include <sys/time.h>
  14.  
  15. #include "dictionary.h"
  16. #undef calculate
  17. #undef getrusage
  18.  
  19. // default dictionary
  20. #define DICTIONARY "dictionaries/large"
  21.  
  22. // prototype
  23. double calculate(const struct rusage* b, const struct rusage* a);
  24.  
  25. int main(int argc, char* argv[])
  26. {
  27.     // check for correct number of args
  28.     if (argc != 2 && argc != 3)
  29.     {
  30.         printf("Usage: speller [dictionary] text\n");
  31.         return 1;
  32.     }
  33.  
  34.     // structs for timing data
  35.     struct rusage before, after;
  36.  
  37.     // benchmarks
  38.     double time_load = 0.0, time_check = 0.0, time_size = 0.0, time_unload = 0.0;
  39.  
  40.     // determine dictionary to use
  41.     char* dictionary = (argc == 3) ? argv[1] : DICTIONARY;
  42.  
  43.     // load dictionary
  44.     getrusage(RUSAGE_SELF, &before);
  45.     bool loaded = load(dictionary);
  46.     getrusage(RUSAGE_SELF, &after);
  47.  
  48.     // abort if dictionary not loaded
  49.     if (!loaded)
  50.     {
  51.         printf("Could not load %s.\n", dictionary);
  52.         return 1;
  53.     }
  54.  
  55.     // calculate time to load dictionary
  56.     time_load = calculate(&before, &after);
  57.  
  58.     // try to open text
  59.     char* text = (argc == 3) ? argv[2] : argv[1];
  60.     FILE* fp = fopen(text, "r");
  61.     if (fp == NULL)
  62.     {
  63.         printf("Could not open %s.\n", text);
  64.         unload();
  65.         return 1;
  66.     }
  67.  
  68.     // prepare to report misspellings
  69.     printf("\nMISSPELLED WORDS\n\n");
  70.  
  71.     // prepare to spell-check
  72.     int index = 0, misspellings = 0, words = 0;
  73.     char word[LENGTH+1];
  74.  
  75.     // spell-check each word in text
  76.     for (int c = fgetc(fp); c != EOF; c = fgetc(fp))
  77.     {
  78.         // allow only alphabetical characters and apostrophes
  79.         if (isalpha(c) || (c == '\'' && index > 0))
  80.         {
  81.             // append character to word
  82.             word[index] = c;
  83.             index++;
  84.  
  85.             // ignore alphabetical strings too long to be words
  86.             if (index > LENGTH)
  87.             {
  88.                 // consume remainder of alphabetical string
  89.                 while ((c = fgetc(fp)) != EOF && isalpha(c));
  90.  
  91.                 // prepare for new word
  92.                 index = 0;
  93.             }
  94.         }
  95.  
  96.         // ignore words with numbers (like MS Word can)
  97.         else if (isdigit(c))
  98.         {
  99.             // consume remainder of alphanumeric string
  100.             while ((c = fgetc(fp)) != EOF && isalnum(c));
  101.  
  102.             // prepare for new word
  103.             index = 0;
  104.         }
  105.  
  106.         // we must have found a whole word
  107.         else if (index > 0)
  108.         {
  109.             // terminate current word
  110.             word[index] = '\0';
  111.  
  112.             // update counter
  113.             words++;
  114.  
  115.             // check word's spelling
  116.             getrusage(RUSAGE_SELF, &before);
  117.             bool misspelled = !check(word);
  118.             getrusage(RUSAGE_SELF, &after);
  119.  
  120.             // update benchmark
  121.             time_check += calculate(&before, &after);
  122.  
  123.             // print word if misspelled
  124.             if (misspelled)
  125.             {
  126.                 printf("%s\n", word);
  127.                 misspellings++;
  128.             }
  129.  
  130.             // prepare for next word
  131.             index = 0;
  132.         }
  133.     }
  134.  
  135.     // check whether there was an error
  136.     if (ferror(fp))
  137.     {
  138.         fclose(fp);
  139.         printf("Error reading %s.\n", text);
  140.         unload();
  141.         return 1;
  142.     }
  143.  
  144.     // close text
  145.     fclose(fp);
  146.  
  147.     // determine dictionary's size
  148.     getrusage(RUSAGE_SELF, &before);
  149.     unsigned int n = size();
  150.     getrusage(RUSAGE_SELF, &after);
  151.  
  152.     // calculate time to determine dictionary's size
  153.     time_size = calculate(&before, &after);
  154.  
  155.     // unload dictionary
  156.     getrusage(RUSAGE_SELF, &before);
  157.     bool unloaded = unload();
  158.     getrusage(RUSAGE_SELF, &after);
  159.  
  160.     // abort if dictionary not unloaded
  161.     if (!unloaded)
  162.     {
  163.         printf("Could not unload %s.\n", dictionary);
  164.         return 1;
  165.     }
  166.  
  167.     // calculate time to unload dictionary
  168.     time_unload = calculate(&before, &after);
  169.  
  170.     // report benchmarks
  171.     printf("\nWORDS MISSPELLED:     %d\n", misspellings);
  172.     printf("WORDS IN DICTIONARY:  %d\n", n);
  173.     printf("WORDS IN TEXT:        %d\n", words);
  174.     printf("TIME IN load:         %.2f\n", time_load);
  175.     printf("TIME IN check:        %.2f\n", time_check);
  176.     printf("TIME IN size:         %.2f\n", time_size);
  177.     printf("TIME IN unload:       %.2f\n", time_unload);
  178.     printf("TIME IN TOTAL:        %.2f\n\n",
  179.      time_load + time_check + time_size + time_unload);
  180.  
  181.     // that's all folks
  182.     return 0;
  183. }
  184.  
  185. /**
  186.  * Returns number of seconds between b and a.
  187.  */
  188. double calculate(const struct rusage* b, const struct rusage* a)
  189. {
  190.     if (b == NULL || a == NULL)
  191.     {
  192.         return 0.0;
  193.     }
  194.     else
  195.     {
  196.         return ((((a->ru_utime.tv_sec * 1000000 + a->ru_utime.tv_usec) -
  197.                  (b->ru_utime.tv_sec * 1000000 + b->ru_utime.tv_usec)) +
  198.                 ((a->ru_stime.tv_sec * 1000000 + a->ru_stime.tv_usec) -
  199.                  (b->ru_stime.tv_sec * 1000000 + b->ru_stime.tv_usec)))
  200.                 / 1000000.0);
  201.     }
  202. }
Add Comment
Please, Sign In to add comment