0

I'm attempting to count the number of trigrams, or three letter sequences, in a block of text. I have some code already that successfully counts the number of bigrams (2 letter sequence) using a 2d array, but I'm having some trouble altering it to accept trigrams.

#include <stdio.h>

int main(void) {
int count['z' - 'a' + 1]['z' - 'a' + 1] = {{ 0 }};
int c0 = EOF, c1;
FILE *plain = fopen("filename.txt", "r");

if (plain != NULL) {
    while ((c1 = getc(plain)) != EOF) {
        if (c1 >= 'a' && c1 <= 'z' && c0 >= 'a' && c0 <= 'z') {
            count[c0 - 'a'][c1 - 'a']++;
        }
        c0 = c1;
    }
    fclose(plain);
    for (c0 = 'a'; c0 <= 'z'; c0++) {
        for (c1 = 'a'; c1 <= 'z'; c1++) {
            int n = count[c0 - 'a'][c1 - 'a'];
            if (n) {
                printf("%c%c: %d\n", c0, c1, n);
            }
        }
    }
}
return 0;
}

Edit: Here's the code I've already tried. I was hoping to just expand the 2d array to a 3d array, but this returns nothing.

#include <stdio.h>

int main(void) {
int count['z' - 'a' + 1]['z' - 'a' + 1]['z' - 'a' + 1] = {{{ 0 }}};
int c0 = EOF, c1, c2;
FILE *plain = fopen("filename.txt", "r");

if (plain != NULL) {
    while ((c1 = getc(plain)) != EOF) {
        if (c1 >= 'a' && c1 <= 'z' && c0 >= 'a' && c0 <= 'z' && c2 >= 'a' && c2 <= 'z') {
            count[c0 - 'a'][c1 - 'a'][c2 - 'a']++;

        }
        c0 = c1;
        c1 = c2;
    }
    fclose(plain);
    for (c0 = 'a'; c0 <= 'z'; c0++) {
        for (c1 = 'a'; c1 <= 'z'; c1++) {
            for (c2 = 'a'; c2 <= 'z'; c2++) {
                    int n = count[c0 - 'a'][c1 - 'a'][c2 - 'a'];
                    if (n) {
                            printf("%c%c%c: %d\n", c0, c1, c2, n);
                    }
            }
        }
    }
}
return 0;
}

For example, this code prints all occurances of bigrams such as aa, ab, ac, etc. But I need it to count the occurances of aaa, aab, ... zzz. Any help would be greatly appreciated!

Edit 2: now it successfully prints the correct output, but it needs to be in descending order (most used trigram at the top)

nhlyoung
  • 67
  • 2
  • 8

1 Answers1

0

One needs more information if one is going to sort the bins; in this code, it's the order array-of-pointers that points to all the trigrams, one-to-one. However, with the original code, it's difficult to tell which trigram is with what index in O(1). Thus I have packed and flattened the array, thus ensuring that there is a bijection from index of array to the entire space of trigrams, (17576 of them.)

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

struct Tri { const char c0, c1, c2; };

static size_t tri_to_idx(const struct Tri tri) {
    return (tri.c0 - 'a') * ('z' - 'a' + 1) * ('z' - 'a' + 1)
        + (tri.c1 - 'a') * ('z' - 'a' + 1)
        + (tri.c2 - 'a');
}

static struct Tri idx_to_tri(const size_t idx) {
    struct Tri tri = {
        idx / (('z' - 'a' + 1) * ('z' - 'a' + 1)) + 'a',
        (idx % (('z' - 'a' + 1) * ('z' - 'a' + 1))) / ('z' - 'a' + 1) + 'a',
        idx % ('z' - 'a' + 1) + 'a' };
    assert(tri.c0 >= 'a' && tri.c0 <= 'z'
        && tri.c1 >= 'a' && tri.c1 <= 'z'
        && tri.c2 >= 'a' && tri.c2 <= 'z');
    return tri;
}

static const char *tri_to_str(const struct Tri tri) {
    static char str[4];
    str[0] = tri.c0, str[1] = tri.c1, str[2] = tri.c2, str[3] = '\0';
    return str;
}

static int int_reverse_cmp(const int *const a, const int *const b) {
    return (*a < *b) - (*b < *a);
}

static int compar(const void *a, const void *b) {
    return int_reverse_cmp(*(int **)a, *(int **)b);
}

int main(void) {
    const char *const fn = "filename.txt";
    int count[('z' - 'a' + 1) * ('z' - 'a' + 1) * ('z' - 'a' + 1)] = { 0 };
    int *order[('z' - 'a' + 1) * ('z' - 'a' + 1) * ('z' - 'a' + 1)];
    int c0 = EOF, c1 = EOF, c2;
    size_t i;
    FILE *plain = fopen(fn, "r");

    if(!plain) return perror(fn), EXIT_FAILURE;

    while ((c2 = getc(plain)) != EOF) {
        if (c1 >= 'a' && c1 <= 'z'
            && c0 >= 'a' && c0 <= 'z'
            && c2 >= 'a' && c2 <= 'z') {
            struct Tri tri = { c0, c1, c2 };
            count[tri_to_idx(tri)]++;
        }
        c0 = c1;
        c1 = c2;
    }
    fclose(plain);

    for (c0 = 'a'; c0 <= 'z'; c0++) {
        for (c1 = 'a'; c1 <= 'z'; c1++) {
            for (c2 = 'a'; c2 <= 'z'; c2++) {
                struct Tri tri = { c0, c1, c2 };
                order[tri_to_idx(tri)] = &count[tri_to_idx(tri)];
            }
        }
    }

    qsort(order, sizeof order / sizeof *order, sizeof *order, &compar);
    for(i = 0; i < ('z' - 'a' + 1) * ('z' - 'a' + 1) * ('z' - 'a' + 1)
        && *order[i]; i++) {
        printf("%s: %d\n", tri_to_str(idx_to_tri(order[i] - count)), *order[i]);
    }

    return EXIT_SUCCESS;
}

Now one can sort the pointers to the array and still have a bijection back into the trigrams by the index of the trigram, order[i] - count in the printf.

Neil
  • 1,767
  • 2
  • 16
  • 22