Advertisement
Guest User

Untitled

a guest
Sep 25th, 2013
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.66 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <inttypes.h>
  5. #include <string.h>
  6. #include <math.h>
  7. #include <ctype.h>
  8. #include "uthash/uthash.h"
  9.  
  10. #define DEFAULT_TERM_NUM 5
  11. #define DEBUG 1
  12.  
  13. /* query has a number of terms (strings) */
  14. typedef struct {
  15.     char* term;
  16.     float score;
  17.     UT_hash_handle hhq;
  18. } query;
  19.  
  20. query *query_dict = NULL;
  21.  
  22. query* init_alloc_query() {
  23.     query* query = malloc(sizeof(query));
  24.     query->term = NULL;
  25.     return query;
  26. }
  27.  
  28. char* mystrdup(const char* str) {
  29.     char* strdup = malloc(sizeof(char)*strlen(str)+1);
  30.     if(strdup != NULL) {
  31.         strcpy(strdup, str);
  32.     }
  33.     if(DEBUG)
  34. //        printf("STRDUP: %s\n", strdup);
  35.     return strdup;
  36. }
  37.  
  38. void hash_query_entry(query* myq) {
  39.     printf("hash q entry: key:%s leng:%f\n", myq->term, myq->score);
  40.     HASH_ADD_KEYPTR(hhq, query_dict, myq->term, strlen(myq->term), myq);
  41. }
  42. void add_term_to_query(char* term) {
  43.     // if query in hashtable, increase score:
  44.     query* myq = NULL;
  45.  
  46.     if(DEBUG)
  47.         printf("void add_term_to_query(query** query_dict, char* term, dictionary_entry* dict_entry): %s\n", term);
  48.  
  49.     if(query_dict != NULL) {
  50.         if(DEBUG)
  51.             printf("query_dict not null, term: %s\n", term);
  52. //        HASH_FIND_STR(query_dict, term, myq);
  53.         HASH_FIND(hhq, query_dict, term, strlen(term), myq);
  54.     }
  55.     if(myq == NULL) {
  56.         myq = init_alloc_query();
  57.         myq->term = mystrdup(term);
  58.         myq->score = 1.0f;
  59.         hash_query_entry(myq);
  60.     } else {
  61.         myq->score += 1.0f;
  62.     }
  63. }
  64. void split_query_into_terms(char* querystr) {
  65.     char* myquery = mystrdup(querystr);
  66.    
  67.     char* reentrant_saver;
  68.    
  69.     char* token;
  70.     token = strtok_r(myquery, " \n\r\t", &reentrant_saver);
  71.    
  72.     while(token != NULL) {
  73.         if(DEBUG)
  74.             printf("found token in dictionary: %s\n", token);
  75.         add_term_to_query(token);
  76.         token = strtok_r(NULL, " \n\r\t", &reentrant_saver);
  77.     }
  78.     //free(myquery); // this crashes program?!
  79. }
  80.  
  81. void print_query_struct() {
  82.     query* entry;
  83.     printf("query_dict:\n");
  84.     for(entry=query_dict; entry != NULL; entry=entry->hhq.next) {
  85.         printf("\t%s: %f\n", entry->term, entry->score);
  86.     }
  87. }
  88.  
  89. void search(char* querystr) {
  90.    
  91.     if(DEBUG)
  92.         printf("given query: %s\n", querystr);
  93.    
  94.     split_query_into_terms(querystr);
  95.     print_query_struct();
  96.  
  97.     //prefetch_tokens(&query_dict);
  98.     //score_query(query_dict);
  99. }
  100.  
  101.  
  102. int main(int argc, char* argv[])
  103. {
  104.     //build_dictionary("target/dictionary.txt");
  105.     //print_dictionary();
  106.     search(argv[1]);
  107.    
  108.     //print_dictionary();
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement