src/sentence.c
changeset 0 06dd3b8d90ad
equal deleted inserted replaced
-1:000000000000 0:06dd3b8d90ad
       
     1 /**
       
     2  *  $Id: sentence.c 53 2008-01-10 00:19:41Z mbroeker $
       
     3  * $URL: http://localhost/svn/c/VirtualReader/trunk/src/sentence.c $
       
     4  */
       
     5 
       
     6 #include <sentence.h>
       
     7 
       
     8 int words (char *ptr)
       
     9 {
       
    10     char *buffer = NULL;
       
    11     char *token;
       
    12     int count = 0;
       
    13 
       
    14     if (ptr != NULL)
       
    15         buffer = strdup (ptr);
       
    16 
       
    17     if (buffer == NULL)
       
    18         return -1;
       
    19 
       
    20     token = strtok (buffer, " ");
       
    21     while (token != NULL) {
       
    22         token = strtok (NULL, " ");
       
    23         count++;
       
    24     }
       
    25 
       
    26     if (buffer != NULL)
       
    27         free (buffer);
       
    28 
       
    29     return count;
       
    30 }
       
    31 
       
    32 int *parse (char *buf)
       
    33 {
       
    34     int count = 0;
       
    35     int i;
       
    36     int *s_ptr = NULL;
       
    37 
       
    38     s_ptr = malloc (2 * sizeof (int));
       
    39     s_ptr[count++] = 0;
       
    40 
       
    41     for (i = 0; i < strlen (buf); i++)
       
    42         switch (buf[i]) {
       
    43         case '\n':
       
    44         case '\t':
       
    45         case '"':
       
    46         case '\'':
       
    47             buf[i] = ' ';
       
    48             break;
       
    49         case '.':
       
    50         case '!':
       
    51         case '?':
       
    52             s_ptr = realloc (s_ptr, (count + 2) * sizeof (int));
       
    53             if (s_ptr == NULL) {
       
    54                 printf ("Not enough memory\n");
       
    55                 return NULL;
       
    56             }
       
    57             s_ptr[count++] = ++i;
       
    58             break;
       
    59         default:
       
    60             break;
       
    61         }
       
    62 
       
    63     s_ptr[count] = 0;
       
    64 
       
    65     return s_ptr;
       
    66 }
       
    67 
       
    68 char *readbuffer (char *fname)
       
    69 {
       
    70     FILE *f;
       
    71     char *text = NULL;
       
    72     char buffer[201];
       
    73 
       
    74     if ((f = fopen (fname, "r")) == NULL) {
       
    75         fprintf (stderr, "ERROR: Cannot open %s for reading!\n", fname);
       
    76         return NULL;
       
    77     }
       
    78 
       
    79     text = malloc (2);
       
    80     *text = 0;
       
    81 
       
    82     while (fgets (buffer, 200, f) != 0) {
       
    83         if ((text = realloc (text, strlen (text) + strlen (buffer) + 1)) == NULL) {
       
    84             printf ("Not enough memory\n");
       
    85             return NULL;
       
    86         };
       
    87         strcat (text, buffer);
       
    88         *buffer = 0;
       
    89     }
       
    90 
       
    91     fclose (f);
       
    92     return text;
       
    93 }
       
    94 
       
    95 char *getSentence (char *text, int start, int end)
       
    96 {
       
    97     int i;
       
    98     char *value = NULL;
       
    99 
       
   100     if ((i = end - start) < 0)
       
   101         return NULL;
       
   102 
       
   103     value = malloc (i + 1);
       
   104     if (value == NULL)
       
   105         return NULL;
       
   106 
       
   107     i = 1;
       
   108     while (i) {
       
   109         switch (text[start]) {
       
   110         case '.':
       
   111         case ',':
       
   112         case '!':
       
   113         case '?':
       
   114         case '(':
       
   115         case ')':
       
   116         case '\n':
       
   117         case ' ':
       
   118             start++;
       
   119             break;
       
   120         default:
       
   121             i = 0;
       
   122             break;
       
   123         }
       
   124     }
       
   125 
       
   126     for (i = start; i < end; i++) {
       
   127         value[i - start] = text[i];
       
   128     }
       
   129 
       
   130     value[i - start] = 0;
       
   131 
       
   132     return value;
       
   133 }
       
   134 
       
   135 char **getstrings (char *Text, int *value)
       
   136 {
       
   137     char *ptr;
       
   138     int i = 0;
       
   139     int count = 0;
       
   140     char **sentences = NULL;
       
   141     int *s_ptr;
       
   142 
       
   143     if ((s_ptr = parse (Text)) == NULL)
       
   144         return NULL;
       
   145 
       
   146     while (s_ptr[i + 1] > 0) {
       
   147         if ((ptr = getSentence (Text, s_ptr[i], s_ptr[i + 1])) == NULL)
       
   148             return NULL;
       
   149         if (strlen (ptr) > 0) {
       
   150             if ((sentences = realloc (sentences, (count + 1) * sizeof (char *) + 1)) == NULL)
       
   151                 return NULL;
       
   152             sentences[count++] = strdup (ptr);
       
   153         }
       
   154         if (ptr != NULL)
       
   155             free (ptr);
       
   156         i++;
       
   157     }
       
   158 
       
   159     if (s_ptr)
       
   160         free (s_ptr);
       
   161 
       
   162     *value = count;
       
   163     return sentences;
       
   164 }