Example #1
0
/**
 * Reads all the N-grams in the given N-gram file into the array of strings.
 *
 * args:
 * ngrams_file - the N-gram file to read N-grams from
 * ngrams - the array of string to read N-grams into
 *
 * returns: the number of ngrams read
 */
int read_ngrams(FILE *fp,
                char **ngrams, 
                s3lmwid_t wid[][MAX_WORDS_PER_NGRAM], 
                int32 nwords[],
                int max_lines, 
                lm_t *lm)
{
    char line_read[MAX_STRLEN];
    int n, length;
    
    n = 0;

    /* read each line in the file into the ngrams array */
    while (fgets(line_read, MAX_STRLEN, fp) != NULL) {
      if (str_cmp("<END_UTT>\n", line_read) == 0) {
	break;
      }
      if (n < max_lines) {
	length = strlen(line_read);
	line_read[length-1] = '\0';

	ngrams[n] = (char *) ckd_calloc(length, sizeof(char));
	strncpy(ngrams[n], line_read, length-1);

	nwords[n] = ngram2wid(line_read, length, wid[n], lm);
	n++;
      } else {
	break;
      }
    }

    return n;
}
Example #2
0
/**
 * Reads all the N-grams in the given N-gram file into the array of strings.
 *
 * args:
 * ngrams_file - the N-gram file to read N-grams from
 * ngrams - the array of string to read N-grams into
 *
 * returns: the number of ngrams read
 */
int
read_ngrams(char *ngrams_file,
            char **ngrams,
            s3lmwid32_t * wid[], int32 nwords[], int max_lines, lm_t * lm)
{
    FILE *fp;
    char line_read[MAX_STRLEN];
    int n, length;

    if ((fp = fopen(ngrams_file, "r")) == NULL) {
        E_FATAL("Unable to open N-gram file %s\n", ngrams_file);
    }

    n = 0;

    /* read each line in the file into the ngrams array */
    while (fgets(line_read, MAX_STRLEN, fp) != NULL) {
        if (n < max_lines) {
            length = strlen(line_read);
            line_read[length - 1] = '\0';
            ngrams[n] = (char *) ckd_calloc(length, sizeof(char));
            strncpy(ngrams[n], line_read, length - 1);
            wid[n] = (s3lmwid32_t *) ckd_calloc(3, sizeof(s3lmwid32_t));
            nwords[n] = ngram2wid(line_read, length, wid[n], lm);
            n++;
        }
        else {
            break;
        }
    }

    return n;
}