Exemple #1
0
void
search_trie(trie_t *curr, char *word, char *prefix)
{
	int	indx;
	int	match_len = 0;
	int	word_len;
	int	prefix_len;
	char	next_ch;

	if (!curr) {
		return;
	}

	word_len = strlen(word);
	prefix_len = strlen(prefix);

	match_len = find_longest_match(curr, word, word + word_len - 1);
	printf("longest match in %s for %s is %d\n", curr->str, word, match_len);	

	if (match_len == word_len) {
		print_all_words(curr, prefix);
		return;
	}

	if (match_len < word_len) {
		next_ch = word[match_len];
		indx = tolower(next_ch) - 'a';
		strncat(prefix, curr->str, match_len);		
		search_trie(curr->child[indx], word + match_len, prefix);
		return;
	}
}
Exemple #2
0
gchar* get_ret_via_keys (const gchar* keys, const gchar* str_md5)
{
    int i = 0;
    int k = 0;
    int len = 0;
    gchar* ret = NULL;
    gchar* tmp = NULL;
    gchar** data_array = NULL;
    struct pos_array* pos_buf = NULL;
    pinyin_trie* cur_trie = NULL;

    if ( !keys || !str_md5 ) {
        g_warning ("args error in get ret via keys!\n");
        return NULL;
    }

    /*print_flag ((gchar*)str_md5);*/
    g_print ("str md5: %s\n", str_md5);
    cur_trie = (pinyin_trie*)g_hash_table_lookup (trie_table, str_md5);
    /*g_print ("first char: %d\n", cur_trie->next[ch_to_num('w')]->flag);*/
    if ( !cur_trie ) {
        g_warning ("string md5 error in get pinyin trie!\n");
        return NULL;
    }

    data_array = (gchar**) g_hash_table_lookup (data_table, str_md5);
    if ( !data_array ) {
        g_warning ("string md5 error in get data array!\n");
        return NULL;
    }

    pos_buf = search_trie (keys, cur_trie);
    if ( !pos_buf ) {
        g_warning ("get pos array failed!\n");
        return NULL;
    }

    len = pos_buf->cnt;
    for (; i < len; i++ ) {
        k = pos_buf->pos[i];
        /*fprintf (stderr, "data %d: %s\n", k, data_array[k]);*/

        if ( i == 0 ) {
            ret = g_strdup (data_array[k]);
        } else {
            tmp = g_strdup_printf ("%s;%s", ret, data_array[k]);
            g_free (ret);
            ret = g_strdup (tmp);
            g_free (tmp);
        }
    }
    /*g_free (pos_buf);*/
    /*pos_buf = NULL;*/

    return ret;
}
Exemple #3
0
void
search()
{
	char	readline[80];
	char	prefix[512];

	while (gets(readline)) {
		prefix[0] = '\0';
		search_trie(&g_root, readline, prefix);
		printf(".....\n.....\n");
	}
}
    void search_trie(TrieNode* Root, int I, int J, vector<vector<char>>& Board, unordered_set<size_t>& Visited, vector<string>& Solution, string& CurrentWord)
    {
        size_t N = Board.size();
        size_t M = Board[0].size();
        size_t index = I * M + J;
        if (I < 0 || I >= N || J < 0 || J >= M)
        {
            return;
        }
        
        char c = Board[I][J];

        auto& nextNode = Root->Edges[c - 'a'];
        if (!nextNode)
        {
            return;
        }
        
        if (Visited.count(index) != 0)
        {
            return;
        }
        
        Visited.insert(index);
        CurrentWord += c;
        
        if (nextNode->IsWordEnd && !nextNode->FoundAlready)
        {
            Solution.push_back(CurrentWord);
            nextNode->FoundAlready = true;
        }
        
        search_trie(nextNode.get(), I - 1, J, Board, Visited, Solution, CurrentWord);
        search_trie(nextNode.get(), I, J - 1, Board, Visited, Solution, CurrentWord);
        search_trie(nextNode.get(), I, J + 1, Board, Visited, Solution, CurrentWord);
        search_trie(nextNode.get(), I + 1, J, Board, Visited, Solution, CurrentWord);
        
        CurrentWord.pop_back();
        Visited.erase(index);
    }
 vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
     auto root = buildTrie(words);
     vector<string> solution;
     
     for (size_t i = 0; i < board.size(); ++i)
     {
         for (size_t j = 0; j < board[i].size(); ++j)
         {
             unordered_set<size_t> visited;
             string currentWord;
             search_trie(root.get(), i, j, board, visited, solution, currentWord);
         }
     }
     
     return solution;
 }
Exemple #6
0
int main(int argc, char** argv) {
	// ----------------------------
	// Initialize
	// ----------------------------
	
	// Open file, print errors if appropriate.
	FILE* my_dict = fopen(argv[1], "r");
	if(my_dict == NULL) {
		fputs("Invalid file. Terminating.\n",stderr);
		return 1;
	}
	
	// Initialize the root
	struct Node* root = make_node();
	
	// Build the trie
	root = make_trie(root, my_dict);
	
	// ----------------------------
	// User interaction
	// ----------------------------
	
	// Make a node to go through the tree
	struct Node* tracker = root;

	// Get user input, print results
	printf("Enter \"exit\" to quit.\n");
	while(1) {		
		// Prompt		
		printf("Enter Key Sequence (or \"#\" for next word):\n");

		// Make a buffer and capture user input.
		char input[MAX_WORD_LENGTH];
		fgets(input, MAX_WORD_LENGTH, stdin);

		// Break if given exit command
		if (strstr(input,"exit") != NULL) {
			break;
		}

		// If user enters one or more #'s, keep old tracker so we can
		// iterate further. Otherwise, restart our tracker from the root.
		if (input[0] != '#') {
			tracker = root;
		}

		// Process the input to remove \n
		input[strlen(input)-1] = '\0'; 
		//make_lower_case(input);
		
		// Make a flag for checking for out of bounds attemps (looking for too many #'s)
		int oob_flag = 0;
		int* oob_ptr = &oob_flag;
		
		// search for the input
		tracker = search_trie(input, tracker, oob_ptr);
		
		// Print results
		// If we got a NULL back (went too far for a word that wasn't in the trie) or
		// the word is NULL (user didn't enter anything), word not found.
		// If we ran out of words while looking for '#', no more T9onyms.
		// Else, print the word. Have a cookie.
		if (tracker == NULL || tracker->word == NULL) {
			printf("\tNot found in current dictionary.\n");
		} else if (oob_flag) {
			printf("\tThere are no more T9onyms\n");
			oob_flag = 0;
		} else {
			printf("\t%s\n",tracker->word);
		}
	}
	
	// ----------------------------
	// Clean up
	// ----------------------------
	
	// Free the trie
	free_node(root);
	
	// Close file to prevent any related memory leaks
	fclose(my_dict);
	
	return 0;
}
Exemple #7
0
int main(int argc, char** argv) {
	// ----------------------------
	// Initialize
	// ----------------------------
	
	// Open file, print errors if appropriate.
	FILE* my_dict = fopen(argv[1], "r");
	if(my_dict == NULL) {
		fputs("Invalid file. Terminating.\n",stderr);
		return 1;
	}
	
	// Initialize the root
	struct Node* root = make_node();
	
	// Build the trie
	root = make_trie(root, my_dict);
	
	// ----------------------------
	// User interaction
	// ----------------------------
	
	// Make a node and buffers to go through the tree
	struct Node* tracker = root;
	char input[MAX_WORD_LENGTH];
	char output[MAX_WORD_LENGTH];
	
	// Get user input, print results
	printf("Enter \"exit\" to quit.\n");
	while(1) {		
		// Prompt		
		printf("Enter Key Sequence (or \"#\" for next word):\n");

		fgets(input, MAX_WORD_LENGTH, stdin);

		// If user just hit enter, skip the rest of this loop
		if (input[0] == '\n') {
			printf("\tUser did not enter anything.\n");
			continue;
		}
		
		// Break if given exit command
		if (strstr(input,"exit") != NULL) {
			break;
		}

		// If user enters one or more #'s, keep old tracker so we can
		// iterate further. Otherwise, restart our tracker from the root.
		if (input[0] != '#') {
			tracker = root;
		}
		
		// Process the input to remove \n
		input[strlen(input)-1] = '\0'; 
		
		// search for the input and save our progress in tracker in case the user wants more #'s
		tracker = search_trie(input, tracker, output);
		
		printf("\t%s\n",output);
	}
	
	// ----------------------------
	// Clean up
	// ----------------------------
	
	// Free the trie
	free_node(root);
	
	// Close file to prevent any related memory leaks
	fclose(my_dict);
	
	return 0;
}