Esempio n. 1
0
void destroy_trie (pinyin_trie* cur_node)
{
    int i = 0;

    if ( !cur_node ) {
        return ;
    }

    for (; i < EN_LEN; i++ ) {
        if ( !cur_node->next[i] ) {
            continue;
        }
        destroy_trie (cur_node->next[i]);
    }

    if ( cur_node->flag == 1 ) {
        cur_node->flag = 0;
        g_free (cur_node->ret_pos.pos);
        cur_node->ret_pos.pos = NULL;
    }

    g_free (cur_node);
    cur_node = NULL;

    return ;
}
Esempio n. 2
0
/** Destroys a word list after it is no longer needed. Every resources previously allocated for this list are freed.
   @param list Pointer to the list structure that shall be freed.
   @param free_data `LIST_FREE_NODE_DATA` if the freeing function for a node's data shall be called for each node while
      destroying the list; `LIST_NO_FREE_NODE_DATA` if nothing is to me done regarding each node's data.
   @warning This function cannot be called when other threads are possibly reading from or writing to the list.
   @warning If `free_data` is `LIST_FREE_NODE_DATA`, then it is assumed that `free_function` pointer previously passed
      to `init_word_list` is a valid pointer to a function.
 */
void destroy_word_list(Word_list_ptr list, int free_data)
{
	struct destroy_args_wrapper args;
	args.free_data = free_data;
	args.free_func = list->free_func;
	destroy_trie(list->trie, TRIE_FREE_DATA, (void*)&args);
	if (pthread_mutex_destroy(&list->mutex) != 0) {
		perror("::list.c:destroy_word_list(): Could not destroy mutex");
	}
}
Esempio n. 3
0
void destroy_trie(struct trie_node *root) {
	if (root == NULL) {
		return;
	}

	size_t i;
	for (i = 0; i < ALPHABET_SZ; i++) {
		destroy_trie(root->children[i]);
	}

	free(root);
}
Esempio n. 4
0
File: trie.c Progetto: GZShi/Trie-C
void destroy_trie(trie* root)
{
	trie* p = NULL;
	trie* t = NULL;
	if(root == NULL)
		return;

	p = root->next;

	// 横向遍历释放兄弟节点
	while(p)
	{
		t = p->next;
		destroy_trie(p);	
		p = t;
	}

	// 垂直遍历释放子孙节点
	t = root->children;
	free(root);
	destroy_trie(t);
}
Esempio n. 5
0
void remove_trie (pinyin_trie* cur_trie)
{
    g_print ("remove trie\n");

    if ( !cur_trie ) {
        g_warning ("args error in remove trie!\n");
        return ;
    }

    destroy_trie (cur_trie);
    g_print ("remove trie over\n");

    return ;
}
Esempio n. 6
0
void destroy_trie(TrieNode *root) {
  int len = strlen(root->childkeys);
  int i;
  for(i=0;i<len;i++) {
    destroy_trie(root->children[i]);
  }
  if(root->childkeys) {
    free(root->childkeys);
  }
  if(root->children) {
    free(root->children);
  }
  free(root);
}
Esempio n. 7
0
void destroy_trie(node* root){
    for(auto p : root->transitions){
        destroy_trie(p.second);
    }
    delete root;
}
Esempio n. 8
0
int main(int argc, char *argv[]) {
  if (argc != 2) {
    fprintf(stderr, "%s\n", "Usage: t9 [FILE]");
    return 1;
  }

  FILE *file = fopen(argv[1], "r");
  if (!file) {
    fprintf(stderr, "Error: file not found\n");
    return 1;
  }

  // insert dictionary into trie
  Tnode *trie = init_trie();
  char line[MAX_LINE_LENGTH];
  while (fgets(line, MAX_LINE_LENGTH, file)) {
    insert_word(trie, line);
  }

  fclose(file);

  // current position in trie
  Tnode *currentNode = trie;
  char input[MAX_LINE_LENGTH];

  printf("Enter \"exit\" to quit.\n");
  printf("Enter Key Sequence (or \"#\" for next word):\n");
  printf("> ");

  while (fgets(input, MAX_LINE_LENGTH, stdin)) {
    // remove newline
    char *trim;
    trim = strchr(input, '\n');
    if (trim) {
      *trim = '\0';
    }

    if (strcmp(input, "exit") == 0 || feof(stdin)) {
      break;
    }

    // check for #, else lookup word
    if (strcmp(input, "#") == 0) {
      if (currentNode && currentNode != trie) {
        printf("  %s\n", currentNode->word);
        currentNode = currentNode->nodes[8];
      } else {
        printf("  %s\n", "There are no more T9onyms");
      }
    } else {
      currentNode = lookup_word(trie, input);
      if (!currentNode || !currentNode->word) {
        printf("  %s\n", "Not found in current dictionary.");
      } else {
        printf("  \'%s\'\n", currentNode->word);
        currentNode = currentNode->nodes[8];
      }
    }

    printf("Enter Key Sequence (or \"#\" for next word):\n");
    printf("> ");
  }

  destroy_trie(trie);
  return 0;
}
Esempio n. 9
0
int main(void) {
	srand(time(NULL));

	printf("The available commands are:\n"
	       "I N - Inject an NxN board. This command will scan NxN letters to form a board\n"
	       "G N - Generates a new NxN board where a letter doesn't appear more than N times\n"
	       "P - Print the current board\n"
	       "W N word - Insert a word into the dictionary with score N\n"
	       "A N word - Insert a word and all prefixes in the dictionary. Non-proper prefixes get a score of 0\n"
	       "R word - Delete a word\n"
	       "S word - Search for a word and return its score (-1 if no such word exists)\n"
	       "D - Dump the dictionary with the corresponding scores\n"
	       "B - Find the best word (word with the highest score)\n"
	       "Q - Quit\n"
	       "> ");

	char **board = NULL;
	size_t board_dim = 0;
	trie = new_trie();

	char op;
	while (scanf(" %c", &op) == 1) {
		if (op == 'I') {
			destroy_board(board, board_dim);
			scanf("%zu", &board_dim);
			board = inject_board(board_dim);
		} else if (op == 'P') {
			if (board == NULL) {
				printf("No board at the moment\n");
			} else {
				print_board(board, board_dim);
			}
		} else if (op == 'G') {
			destroy_board(board, board_dim);
			scanf("%zu", &board_dim);
			board = generate_board(board_dim);
		} else if (op == 'W') {
			size_t word_score;
			scanf("%zu%s", &word_score, word_buff);
			insert_word(trie, word_buff, word_score);
		} else if (op == 'A') {
			size_t word_score;
			scanf("%zu%s", &word_score, word_buff);

			size_t i;
			for (i = 1; word_buff[i] != '\0'; i++) {
				char c = word_buff[i];
				word_buff[i] = '\0';
				insert_word(trie, word_buff, 0);
				word_buff[i] = c;
			}
			insert_word(trie, word_buff, word_score);

		} else if (op == 'R') {
			scanf("%s", word_buff);
			delete_word(trie, word_buff);
		} else if (op == 'S') {
			scanf("%s", word_buff);
			int s = word_score(trie, word_buff);
			if (s == -1) {
				printf("No such word: %s\n", word_buff);
			} else {
				printf("score(%s) = %d\n", word_buff, s);
			}
		} else if (op == 'D') {
			print_known_words(trie);
		} else if (op == 'B') {
			char *w = find_best_word(board, board_dim);
			if (w == NULL) {
				printf("No words found\n");
			} else {
				printf("Best word: %s\n", w);
			}
			free(w);
		} else if (op == 'Q') {
			break;
		} else {
			fprintf(stderr, "Unrecognized operation: %c\n", op);
		}

		printf("> ");
	}

	destroy_trie(trie);
	destroy_board(board, board_dim);

	return 0;
}