parse_text.c
author viric@llimona
Wed, 29 Aug 2007 00:19:14 +0200
changeset 14 a961bb8806b9
child 15 17a66ceb774a
permissions -rw-r--r--
first 'zparsetext'.
viric@14
     1
#include <stdio.h>
viric@14
     2
#include "dictre.h"
viric@14
     3
viric@14
     4
static void give_accent_to_word(const char *tmp)
viric@14
     5
{
viric@14
     6
    char def[MAXDEF];
viric@14
     7
viric@14
     8
    find_def(tmp, def);
viric@14
     9
    if (def[0] != 0) /* found */
viric@14
    10
    {
viric@14
    11
        /* Print the word UNTIL a space.
viric@14
    12
         * the definition will have the form:
viric@14
    13
         *    ACCENTED_WORD NOMINATIVE1 NOMINATIVE2 ... \n */
viric@14
    14
        char *first_space;
viric@14
    15
        char *pos;
viric@14
    16
        first_space = strchr(def, ' ');
viric@14
    17
        if (first_space != 0) /* Space found */
viric@14
    18
            for(pos = def; pos < first_space; ++pos)
viric@14
    19
                putchar(*pos);
viric@14
    20
        return;
viric@14
    21
    }
viric@14
    22
viric@14
    23
    /* if first_space == 0 or word not found */
viric@14
    24
    printf("%s", tmp);
viric@14
    25
}
viric@14
    26
viric@14
    27
static void process_text(FILE *in, int pos, int length)
viric@14
    28
{
viric@14
    29
    unsigned char tmp[MAXWORD];
viric@14
    30
    int wordpos = 0;
viric@14
    31
    do
viric@14
    32
    {
viric@14
    33
        int c;
viric@14
    34
        /* Check pos only if length >= 0 */
viric@14
    35
        if (length >= 0 && pos >= length)
viric@14
    36
            break;
viric@14
    37
        c = fgetc(in);
viric@14
    38
        if (c == EOF)
viric@14
    39
            break;
viric@14
    40
        if (is_ASCII(c))
viric@14
    41
        {
viric@14
    42
            if (wordpos != 0)
viric@14
    43
            {
viric@14
    44
                tmp[wordpos] = 0;
viric@14
    45
                give_accent_to_word(tmp);
viric@14
    46
                wordpos = 0;
viric@14
    47
            }
viric@14
    48
            putchar(c);
viric@14
    49
        }
viric@14
    50
        else /* non-ASCII - we consider it russian */
viric@14
    51
        {
viric@14
    52
            tmp[wordpos++] = c;
viric@14
    53
        }
viric@14
    54
viric@14
    55
        pos += 1;
viric@14
    56
    } while(1);
viric@14
    57
}
viric@14
    58
viric@14
    59
int main()
viric@14
    60
{
viric@14
    61
    init_dictionary();
viric@14
    62
    process_text(stdin, 0, -1);
viric@14
    63
    end_dictionary();
viric@14
    64
}