コード例 #1
0
ファイル: trie.c プロジェクト: yuchenz-cmu/lameasr
void trie_add_word(Trie *trie, char* target, char* lex) {
    // assume word is separated by space
    if (trie->head_node == NULL) {
        trie->head_node = init_trie_node(trie->alphabet_size);
    }

    char word[LEX_LEN];
    strncpy(word, target, LEX_LEN);
    char curr_letter[LETTER_LEN];
    int aidx = 0;
    TrieNode *curr_node = trie->head_node;
    TrieNode *prev_node = NULL;
    char *token = strtok(word, " ");

    while (token != NULL) {
        // fprintf(stderr, "%s\n", token);
        aidx = 0;
        while (aidx < trie->alphabet_size) {
            if (!strcmp(token, trie->alphabet[aidx])) {
                break;
            }
            aidx++;
        }
        assert(aidx < trie->alphabet_size);

        if (curr_node->next[aidx] == NULL) {
            curr_node->next[aidx] = init_trie_node(trie->alphabet_size);
        }

        prev_node = curr_node;
        curr_node = curr_node->next[aidx];
        token = strtok(NULL, " ");
    }

    prev_node->is_word = 1;
    strncpy(prev_node->lex, lex, LEX_LEN);
    fprintf(stderr, "Added %s ... \n", prev_node->lex);
}
コード例 #2
0
ファイル: api-lazytrie.c プロジェクト: Maxime2/ccan
static int lazytrie_edge_info(const struct aga_graph *g,
			      const struct aga_node *n,
			      ptrint_t *e,
			      struct aga_edge_info *ei)
{
	struct trie_node *tn = container_of(n, struct trie_node, agan);
	struct trie_node *next;
	int index = ptr2int(e);

	assert((index >= 1) && (index <= NLETTERS));

	next = tn->next[index - 1];

	if (!next) {
		int depth = strlen(tn->prefix);
		int start, end;

		start = tn->start;
		while (start < tn->end) {
			if (wordarray[start][depth] >= LETTERS[index - 1])
				break;
			start++;
		}

		end = start;
		while (end < tn->end) {
			if (wordarray[end][depth] > LETTERS[index - 1])
				break;
			end++;
		}

		if (end > start) {
			char plus[2] = { LETTERS[index - 1], '\0' };
			next = tal(tn, struct trie_node);
			init_trie_node(next, start, end,
				       tal_strcat(next, tn->prefix, plus));
		}