AngelPn/Inverted-Search-Engine

build_entry_index(): Add MatchType options support

Closed this issue · 0 comments

build_entry_index() should check given MatchType and select the appropriate matching words method. As discussed in our previous call, this can be done using a pointer to function:

/* pointer to function that does the keyword matching */
typedef int (*MatchingFunc)(const char* word1, const char* word2);

The above function pointer should be added as an argument to BK_tree_insert() and replace compare_words():

ErrorCode BK_tree_insert(BK_treenode* root, BK_treenode new_node, MatchingFunc matching) {
   ...
   int dist = matching(get_entry_word((*root)->item), get_entry_word(new_node->item));
   ...
}

Finally, if statements should determine the matching method in build_entry_index():

ErrorCode build_entry_index(const entry_list el, MatchType type, BK_tree* ix) {

    if (((*ix) = (BK_tree)malloc(sizeof(struct tree))) == NULL)
        return EC_FAIL;
    (*ix)->size = get_number_entries(el);
    (*ix)->root = NULL;

    MatchingFunc matching;
    if (type == MT_EXACT_MATCH)
        matching = exact_match;
    else if (type == MT_HAMMING_DIST)
        matching = hamming_dist;
    else if (type == MT_EDIT_DIST)
        matching = edit_dist;
    else return EC_FAIL;

    /* Insert all entry_list's items to the tree iteratively */
    ListNode current = get_first_node(el);
    for(int i = 0; i < (*ix)->size; i++) {
        if (BK_tree_insert(&((*ix)->root), make_treenode(get_node_item(current)), matching) == EC_FAIL)
            return EC_FAIL;
        current = get_next_node(current);
    }
    return EC_SUCCESS;
}

This requires the matching methods to be implemented:

int exact_match(const char* word1, const char* word2);
int hamming_dist(const char* word1, const char* word2);
int edit_dist(const char* word1, const char* word2);

Hamming and Edit Distance are implemented by sigmod2013.