Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <inttypes.h>
- #include <string.h>
- #include <math.h>
- #include <ctype.h>
- #include "uthash/uthash.h"
- #define DEFAULT_TERM_NUM 5
- #define DEBUG 1
- /* query has a number of terms (strings) */
- typedef struct {
- char* term;
- float score;
- UT_hash_handle hhq;
- } query;
- query *query_dict = NULL;
- query* init_alloc_query() {
- query* query = malloc(sizeof(query));
- query->term = NULL;
- return query;
- }
- char* mystrdup(const char* str) {
- char* strdup = malloc(sizeof(char)*strlen(str)+1);
- if(strdup != NULL) {
- strcpy(strdup, str);
- }
- if(DEBUG)
- // printf("STRDUP: %s\n", strdup);
- return strdup;
- }
- void hash_query_entry(query* myq) {
- printf("hash q entry: key:%s leng:%f\n", myq->term, myq->score);
- HASH_ADD_KEYPTR(hhq, query_dict, myq->term, strlen(myq->term), myq);
- }
- void add_term_to_query(char* term) {
- // if query in hashtable, increase score:
- query* myq = NULL;
- if(DEBUG)
- printf("void add_term_to_query(query** query_dict, char* term, dictionary_entry* dict_entry): %s\n", term);
- if(query_dict != NULL) {
- if(DEBUG)
- printf("query_dict not null, term: %s\n", term);
- // HASH_FIND_STR(query_dict, term, myq);
- HASH_FIND(hhq, query_dict, term, strlen(term), myq);
- }
- if(myq == NULL) {
- myq = init_alloc_query();
- myq->term = mystrdup(term);
- myq->score = 1.0f;
- hash_query_entry(myq);
- } else {
- myq->score += 1.0f;
- }
- }
- void split_query_into_terms(char* querystr) {
- char* myquery = mystrdup(querystr);
- char* reentrant_saver;
- char* token;
- token = strtok_r(myquery, " \n\r\t", &reentrant_saver);
- while(token != NULL) {
- if(DEBUG)
- printf("found token in dictionary: %s\n", token);
- add_term_to_query(token);
- token = strtok_r(NULL, " \n\r\t", &reentrant_saver);
- }
- //free(myquery); // this crashes program?!
- }
- void print_query_struct() {
- query* entry;
- printf("query_dict:\n");
- for(entry=query_dict; entry != NULL; entry=entry->hhq.next) {
- printf("\t%s: %f\n", entry->term, entry->score);
- }
- }
- void search(char* querystr) {
- if(DEBUG)
- printf("given query: %s\n", querystr);
- split_query_into_terms(querystr);
- print_query_struct();
- //prefetch_tokens(&query_dict);
- //score_query(query_dict);
- }
- int main(int argc, char* argv[])
- {
- //build_dictionary("target/dictionary.txt");
- //print_dictionary();
- search(argv[1]);
- //print_dictionary();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement