Beispiel #1
0
int search_negated_elements (ft_doc_index *doc_idx, List *negated_elements)
{
	ListElement *cursor;
	void		*element;
	ft_word_info word_info;
	
	ASSERT (negated_elements);
	for (element = List_getFirst (negated_elements, &cursor); 
		 element; 
		 element = List_getNext (&cursor))
	{
		if (*(int *)element == FT_WORD_TYPE)
		{
			if (search_word (doc_idx, element, &word_info) == TMAN_FOUND)
			{
				return TMAN_FOUND;
			}
		}
		else if (*(int *)element == FT_PHRASE_TYPE)
		{
			if (search_phrase (doc_idx, element) == TMAN_FOUND)
			{
				return TMAN_FOUND;
			}
		}
	}

	return TMAN_NOT_FOUND;
}
Beispiel #2
0
void search_word(int start, int sub_index)
{
	int i;
	//printf("[%s] start %d sub_index %d\n", __FUNCTION__, start, sub_index);

	if(start > input_size)
	{	
		//printf("over input_size\n");
		return;
	}

	if(sub_index > sub_size)
	{	
		//printf("over sub_size\n");
		return;
	}
	
	for(i = start; i < input_size; i++)
	{
			if(input_str[i] == sub_str[sub_index])
			{
					if(sub_index == sub_size -1)
					{
		//					printf("match! start %d sub_index %d i %d\n", start, sub_index, i);
							count++;
					}
					else
					{
						search_word(i+1, sub_index + 1); 
					}
			}
	}
}
Beispiel #3
0
int asm_cmd_pop(FILE* strin, var* vars, double* codes, int* cur_code)
{
	if (strin == NULL) return ASM_STRIN_BAD;
	if (codes == NULL) return ASM_BAD_CODES_POINTER;
	if (cur_code == NULL) return ASM_BAD_CUR_CODE_POINTER;

	char pop_word[CMD_MAXLEN] = {};

	int cond = fscanf_s(strin, "%s", pop_word, CMD_MAXLEN);
	if (cond <= 0) return ASM_SCAN_FAILED;

	double reg = 0;
	int ret = asm_str_convert(_strlwr(pop_word), &reg);
	if (ret != ASM_OK) return ASM_BAD_POP_STR_TOKEN;

	if (reg == STR_var)
	{
		char pop_word_var[CMD_MAXLEN] = {};
		int cond = fscanf_s(strin, "%s", pop_word_var, CMD_MAXLEN);
		if (cond <= 0) return ASM_SCAN_FAILED;
		ret = search_word(vars, pop_word_var);
		if (ret < 0) return ASM_NO_SUCH_VAR;
		SEC_RUN_(asm_push_code(codes, cur_code, reg));
		SEC_RUN_(asm_push_code(codes, cur_code, ret));

		return ASM_OK;
	}
	

	SEC_RUN_(asm_push_code(codes, cur_code, reg));
	SEC_RUN_(asm_push_code(codes, cur_code, 0));
	return ASM_OK;
}
Beispiel #4
0
int main(int argc, char** argv) {
    init_dict_search();
    char * targets[] = {
        "yuppify", "bird-watcher", "arthritis", "ali, muhammad", "-monger",
        "yukon", "women's studies", "not exits", "go", "zoo"
    };
    for(int i = 0; i < 10; ++i) {
        char* result = search_word(targets[i]);
        printf("%s, %p\n",targets[i], result);
    }
}
static NJ_INT16 njd_search_word(NJ_CLASS *iwnn, NJ_CURSOR *cursor, NJ_UINT8 comp_flg,
                         NJ_UINT8 *exit_flag) {
    NJ_INT16 ret;


    ret = check_search_cursor(iwnn, cursor);
    if (ret != 0) {
        return ret;
    }
    
    return search_word(iwnn, cursor, comp_flg, exit_flag);
}
Beispiel #6
0
static double match_score_or_basic_pred (ft_doc_index *doc_idx, ft_basic_pred *ft_pred)
{
	int num_non_negated_words, i;
	double result;
	ft_word_info *non_negated_words_info;
	ft_word_info non_neg_words_info_static[10] = {'\0'};
	word_syntree *word;
	ft_phrase_info *phrase_info = NULL;
	ListElement *cursor;
	
	ASSERT (ft_pred);

	// There should be no negated word in this basic predicate.
	ASSERT (ft_pred->negated_query_elements == NULL);

	num_non_negated_words = va_get_size (ft_pred->non_negated_words);
	if (num_non_negated_words <= 10)
	{
		non_negated_words_info = non_neg_words_info_static;
	}
	else
	{
		non_negated_words_info = (ft_word_info *) tman_malloc (num_non_negated_words * sizeof(ft_word_info));
		memset (non_negated_words_info, '\0', num_non_negated_words * sizeof(ft_word_info));
	}

	for (i = 0; i < num_non_negated_words; i++)
	{
		word = (word_syntree *)va_get_element (ft_pred->non_negated_words, i);
		search_word (doc_idx, word, &(non_negated_words_info[i]));
	}

	// Do phrase matching.
	if (ft_pred->phrases_words_info)
	{
		for (phrase_info = List_getFirst (ft_pred->phrases_words_info, &cursor); phrase_info;
			 phrase_info = List_getNext (&cursor))
		{
			phrase_pos_match (doc_idx, phrase_info, non_negated_words_info, num_non_negated_words);
		}
	}

	// Score the basic predicate.
	result = calculate_score (non_negated_words_info, num_non_negated_words);

	if (num_non_negated_words > 10)
	{
		tman_free (non_negated_words_info);
	}
	
	return result;
}
/*
 * 缓存KEYS在idx中的偏移信息,以便加快search_word的搜索速度
 */
void cache_idx(long *idx_cache)
{
    int i;
    long *p=idx_cache;
    unsigned int unused;

    rewind(pidx);
    for(i=0; i<MAX_KEYS; i++){
        if( search_word(KEY[i], &unused, &unused)){
            p[i]=ftell(pidx);
        }else p[i]=0;
    }
}
Beispiel #8
0
int main()
{
	int m,n,w,a,b,i,c,d,r,count;

	r = 0;

	//freopen("in6","r",stdin);
	while (scanf("%d%d%d",&m,&n,&w) != -1)
	{
		if( r++ != 0 )
			printf("\n");

		for( i = 0; i < m; i++ )
			scanf("%s",puzzle[i]);
		memset(info,0,sizeof(info));
		for( i = 0; i < w; i++ )
		{
			scanf("%s",words[i]);
			info[i].size = strlen(words[i]);
		}
		
		count = w;
		for( a = 0; a < m; a++ )
		for( b = 0; b < n; b++ )
		{
			if( count == 0 )
				goto escape;
			for( c = 0; c < w; c++ )
			{
				if( info[c].find == true )
					continue;
				if( (d = search_word(m,n,a,b,c)) >= 0 )
				{
					count -= 1;
					info[c].find = true;
					info[c].dir = direct[d];
					info[c].x = a;
					info[c].y = b;
				}
			}
		}
escape:
		for( i = 0; i < w; i++ )
			printf("%d %d %c\n",info[i].x,info[i].y,info[i].dir);
		
	}
	return 0;

}
Beispiel #9
0
//look for word in the given tree and saves result string to result
void search_word(TREE_NODE *root, W_TOKEN* word, wchar_t *result){
  if(!root){//word not found
    wcscpy(result, L"Consulta: ");
    wcscat(result, ( (W_TOKEN *)word )->word);
    wcscat(result, L" Palavra não encontrada\n");
    return;
  }
  else if(lex_order(( (W_TOKEN *)( root->data ) ), word) == 0){//found word
    wcscpy(result, L"Consulta: ");
    wcscat(result, ( (W_TOKEN *)word )->word);
    wcscat(result, L" Palavra encontrada nas linhas ");
    wchar_t line[10000];
    line[0] = '\0';
    int first_number = 1;//used to remove comma from last number
    LIST_NODE *node = ( (W_TOKEN *)( root->data ) )->list;
    while(node){//print all lines on which word was found
      if(!first_number)
        wcscat(result, L", ");
      swprintf(line, 1000, L"%d", *(int *)node->data);
      wcscat(result, line);
      node = node->next;
      first_number = 0;
    }
    wcscat(result, L"\n");
    return;
  }
  else if(lex_order(( (W_TOKEN *)( root->data ) ), word) < 0){//word bigger than root
    search_word(root->right, word, result);
  }
  else if(lex_order(( (W_TOKEN *)( root->data ) ), word) > 0){//word smaller than root
    search_word(root->left, word, result);
  }

  return;

}
Beispiel #10
0
void search(gdsl_bstree_t dict){
	char *buffer=NULL;
	size_t len;
	gdsl_element_t result;
	getchar();
	printf("Input a word to search: ");
	getline(&buffer,&len,stdin);
	result = search_word(dict,buffer);
	if(result==NULL){
		printf("Could not find word: %s",buffer);
	}else{
		printf("That word is in the dictionary.\n");
	}
	//getchar();


}
Beispiel #11
0
//search for all words given in file_name and save results to output_file_name
void search_words(TREE_NODE *root, char * file_name, char * output_file_name){
  FILE *file, *output_file;
  file = fopen(file_name,"r");
  output_file = fopen(output_file_name,"w");
  if(!file){
    printf("arquivo de entrada invalido\n");
    return;
  }
  if(!output_file){
    printf("arquivo de saida invalido\n");
    return;
  }

  wchar_t line [ 10000 ];

  wchar_t *result = malloc(sizeof(wchar_t) * 10000);

  //start benchmark
  double start_time, end_time, time_elapsed;
  start_time = (double)clock();

  while ( fgetws ( line, sizeof line, file ) != NULL ){//read line by line
    if(line[wcslen(line) -1] == '\n') line[wcslen(line) -1] = '\0';//remove new line char
    if(line[wcslen(line) -1] == ' ') line[wcslen(line) -1] = '\0';
    W_TOKEN *token = malloc(sizeof(W_TOKEN));
    str_to_lower(line);
    token->word = line;
    search_word(root, token, result);
    fwprintf(output_file, L"%ls",result);
    fputws ( result, stdout );
  }

  end_time = (double)clock();
  time_elapsed = ( end_time - start_time )/CLOCKS_PER_SEC;
  //end benchmark

  fwprintf(output_file, L"%s","\n");
  fwprintf(output_file, L"O tempo gasto na busca foi de %fms.\n", time_elapsed*1000);
  printf("\n");
  printf("O tempo gasto na busca foi de %fms.\n", time_elapsed*1000);

  fclose(file);
  fclose(output_file);

}
Beispiel #12
0
int main(){
	struct dictionary *d = NULL, *tmp;
	char *text = "Ciao a tutti ciao ciao a tutti tutti ciao ciao ciao tutti ciao a a";
	char *word;
	
	do{
		word = next_word(text);
		if(d==NULL)
			d=new_dictionary_element(word, 1);	
		if( (tmp = search_word(d, word)) != NULL){
			tmp->occurences++;
		} else {
			insert_word(d, word, 1);
		} 
		if(text=strchr(text, ' '))
			text++;
	}while( text != NULL );
	print_dictionary(d);
}
Beispiel #13
0
void search_word(gnode_t *pnode,int i,char tmp[])
{
	i++;
	while(pnode != NULL)
	{
		if(word_count >= win_x - 6 || word_count >= MAX_WORD)
			return;

		tmp[i - 1] = pnode->ch;
		
		if(pnode->pchinese != NULL)
		{
			memcpy(word[word_count],tmp,i);
			word_count++;			
		}

		search_word(pnode->psub,i,tmp);

		pnode = pnode->pnext;
	}
}
Beispiel #14
0
void show_word(dict_t *pdict,char *english)
{
	fill(6,2,win_x - 6,win_y / 3 - 2,' ');
	fill(6,win_y / 3 + 1,win_x - 6,win_y - win_y / 3 - 1,' ');

	gnode_t *pnode = search_node(pdict->phead,english);
	
	char tmp[MAX_CH];

	memset(word,0,MAX_WORD * MAX_CH);

	if(pnode != NULL)
	{
		word_count = 1;
		
		search_word(pnode->psub,0,tmp);

		show_english(english);
		
		show_chinese(pnode->pchinese,english);
	}
}
/*
 * 主要查询函数
 */
void consult()
{
    char word[MAX_WORD+1];
    char *data=NULL;
    long idx[MAX_KEYS];
    unsigned int offset, length;

    cache_idx(idx);

    while(1){
        printf("INPUT A WORD OR PHRASE: ");
        if(get_input(word, MAX_WORD)){
            fseek(pidx,locate_idx(word,idx),SEEK_SET);
            if(search_word(word,&offset,&length))
            {
                data=get_data(offset,length);
                display_data(data,length);
                free(data);
                data=NULL;
            }else fprintf(stderr,"SORRY, '%s' CANNOT BE FOUND!\n", word);
            printf("\n----------------------------------------\n\n");
        }else break;
    }
}
Beispiel #16
0
int main(void)
{
	int num_case;

	scanf("%d", &num_case);
	while(num_case)
	{
		scanf("%s", &input_str);
		//printf("%s\n", input_str);
		scanf("%s", &sub_str);
	//	printf("%s %d\n", sub_str, strlen(sub_str));

		input_size = strlen(input_str);
		sub_size = strlen(sub_str);
		//algorithm
		count = 0;
		search_word(0, 0);
		

		printf("%d\n",count);
		num_case--;
	}
	return 0;
}
Beispiel #17
0
Datei: edit.c Projekt: vigna/ne
static int to_something(buffer *b, int (to_first)(int), int (to_rest)(int)) {
	assert_buffer(b);

	/* If we are after the end of the line, just return ERROR. */

	if (b->cur_line == b->num_lines -1 && b->cur_pos >= b->cur_line_desc->line_len) return ERROR;

	int64_t pos = b->cur_pos;
	int c;
	/* First of all, we search for the word start, if we're not over it. */
	if (pos >= b->cur_line_desc->line_len || !ne_isword(c = get_char(&b->cur_line_desc->line[pos], b->encoding), b->encoding))
		if (search_word(b, 1) != OK)
			return ERROR;

	bool changed = false;
	int64_t new_len = 0;
	pos = b->cur_pos;
	const int64_t cur_char = b->cur_char;
	const int cur_x = b->cur_x;
	/* Then, we compute the word position extremes, length of the result (which
		may change because of casing). */
	while (pos < b->cur_line_desc->line_len && ne_isword(c = get_char(&b->cur_line_desc->line[pos], b->encoding), b->encoding)) {
		const int new_c = new_len ? to_rest(c) : to_first(c);
		changed |= (c != new_c);

		if (b->encoding == ENC_UTF8) new_len += utf8seqlen(new_c);
		else new_len++;

		pos = next_pos(b->cur_line_desc->line, pos, b->encoding);
	}

	const int64_t len = pos - b->cur_pos;
	if (!len) {
		char_right(b);
		return OK;
	}

	if (changed) {
		/* We actually perform changes only if some character was case folded. */
		char * word = malloc(new_len * sizeof *word);
		if (!word) return OUT_OF_MEMORY;

		pos = b->cur_pos;
		new_len = 0;
		/* Second pass: we actually build the transformed word. */
		while (pos < b->cur_line_desc->line_len && ne_isword(c = get_char(&b->cur_line_desc->line[pos], b->encoding), b->encoding)) {
			if (b->encoding == ENC_UTF8) new_len += utf8str(new_len ? to_rest(c) : to_first(c), word + new_len);
			else {
				word[new_len] = new_len ? to_rest(c) : to_first(c);
				new_len++;
			}

			pos = next_pos(b->cur_line_desc->line, pos, b->encoding);
		}

		start_undo_chain(b);

		delete_stream(b, b->cur_line_desc, b->cur_line, b->cur_pos, len);
		if (new_len) insert_stream(b, b->cur_line_desc, b->cur_line, b->cur_pos, word, new_len);

		free(word);

		end_undo_chain(b);

		if (cur_char < b->attr_len) b->attr_len = cur_char;
		update_line(b, b->cur_line_desc, b->cur_y, cur_x, false);
		need_attr_update = true;
	}

	return search_word(b, 1);
}
Beispiel #18
0
static double match_score_accrue_basic_pred (ft_doc_index *doc_idx, ft_basic_pred *ft_pred)
{
	int num_non_negated_words = 0, status, i;
	double result;
	ft_word_info *non_negated_words_info = NULL;
	ft_word_info non_neg_words_info_static[10] = {'\0'};
	word_syntree *word;
	ft_phrase_info *phrase_info = NULL;
	ListElement *cursor;
	
	ASSERT (ft_pred);

	// First check for negated words, if any.
	if (ft_pred->negated_query_elements != NULL)
	{
		status = search_negated_elements (doc_idx, ft_pred->negated_query_elements);
		// If even one negated element was found, bail out.
		if (status == TMAN_FOUND)
		{
			result = 0.0;
			goto cleanup;
		}
	}

	num_non_negated_words = va_get_size (ft_pred->non_negated_words);
	if (num_non_negated_words <= 10)
	{
		non_negated_words_info = non_neg_words_info_static;
	}
	else
	{
		non_negated_words_info = (ft_word_info *) tman_malloc (num_non_negated_words * sizeof(ft_word_info));
		memset (non_negated_words_info, '\0', num_non_negated_words * sizeof(ft_word_info));
	}

	for (i = 0; i < num_non_negated_words; i++)
	{
		word = (word_syntree *)va_get_element (ft_pred->non_negated_words, i);
		search_word (doc_idx, word, &(non_negated_words_info[i]));
	}

	// Do phrase matching.
	if (ft_pred->phrases_words_info)
	{
		for (phrase_info = List_getFirst (ft_pred->phrases_words_info, &cursor); phrase_info;
			 phrase_info = List_getNext (&cursor))
		{
			phrase_pos_match (doc_idx, phrase_info, non_negated_words_info, num_non_negated_words);
		}
	}

	// Score the basic predicate.
	result = calculate_score (non_negated_words_info, num_non_negated_words);

cleanup:
	if (num_non_negated_words > 10)
	{
		tman_free (non_negated_words_info);
	}
	
	return result;
}
Beispiel #19
0
int main(void)
{
	FILE *fp = fopen("dict.txt", "r");      //以读的方式打开文件“dict.tx    t”
	if(fp == NULL)
	{
		perror("open file dict.txt\n");
		exit(0);
	}

	while(1)
	{
		char content[1000];
		char Ess[1000];
		char *token;
		link p = make_node();

		if(!fgets(content, 1000, fp))   //从文件读一行(单词),如果到末尾则跳出循环
			break;
		if(!fgets(Ess, 1000, fp))       //从文件读一行(内容),如果到末尾了跳出寻环
			break;

		if(head == NULL)        //初始化链表
		{
			head = p;
			tail = head;
		}
		else
		{
			tail -> next = p;
			tail = tail -> next;
		}

		//              fgets(content,100,fp);
		if(content[strlen(content) - 1] == '\n')        //如果数组到最后一个元素时候将‘\n’变为'\0'
			content[strlen(content) - 1] = '\0';

		strcpy(p -> word, content + 1);  //由于第一个为#所以把第二个以后的元素复制给p -> word
		//              printf("%s\n",p->word);
		//              fgets(Ess,100,fp);
		token = strtok(Ess, ":");       //截取:以前的内容
		//              printf("%s\n",token);

		p->count = 0;   //初始化

		while((token = strtok(NULL, "@"))!=NULL)
		{
			p -> Explain[p -> count] = (char *)malloc(strlen(token) + 1);
			strcpy(p -> Explain[p -> count], token);

			p->count++;
		}

		print_word(p);
	}

	fclose(fp);

	int i = 0;

	while(1)
	{
		printf("take a choice:\n0 printf_word\n1 add a new word\n2 delete a word\n3 search a word\n4 write a 2file\n5 read_2file\n6 exit\n");
		scanf("%d", &i);

		switch(i)
		{
			case 0 :
				printf_word(head);
				break;
			case  1:
				add_word(head);
				break;
			case  2:
				head = delete_word(head);
				break;
			case  3:
				search_word(head);
				break;
			case  4:
				write_2file(head);
				break;
			case  5:
				read_2file();
				break;
			case  6:
				printf("exit...\n");
				save_word(head);
				return 0;
				break;
			default:
				continue;
				break;
		}
	}

	return 0;
}
Beispiel #20
0
static int search_phrase (ft_doc_index *doc_idx, phrase_syntree *phrase)
{
	ft_word_info	*words_info;
	ft_word_info	words_info_static[10] = {'\0'};
	int				words_pos_static[10] = {0};
	ListElement		*cursor;
	word_syntree	*word;
	int				i;
	ft_phrase_info	phrase_info;
	int				result = TMAN_FOUND;
	
	ASSERT (phrase);

	phrase_info.num_words = phrase->num_words;
	phrase_info.type = FT_PHRASE_INFO_TYPE;
	if (phrase->num_words > 10)
	{
		words_info = (ft_word_info *)tman_malloc (phrase->num_words * sizeof(ft_word_info));
		memset (words_info, '\0', phrase->num_words * sizeof(ft_word_info));
		phrase_info.word_positions = (int *)tman_malloc (phrase->num_words * sizeof (int));
		memset (phrase_info.word_positions, 0, phrase->num_words * sizeof (int));
	}
	else
	{
		words_info = words_info_static;
		phrase_info.word_positions = words_pos_static;
	}
	
	// First search all the words in the phrase. If there is even one word that is not present in the document,
	// the search should fail.
	for (word = List_getFirst (phrase->word_list, &cursor), i = 0; word; word = List_getNext (&cursor), i++)
	{
		// Word positions are the same as they appear in the word list in phrase node.
		phrase_info.word_positions[i] = i;
		if (search_word (doc_idx, word, &(words_info[i])) == TMAN_NOT_FOUND)
		{
			result = TMAN_NOT_FOUND;
			break;
		}
	}

	if (result == TMAN_NOT_FOUND)
	{
		goto cleanup;
	}
	else
	{
		// If all the words are present in the document, search for their sequencing.
		if (phrase_pos_match (doc_idx, &phrase_info, words_info, phrase->num_words) == TMAN_NOMATCH)
		{
			result = TMAN_NOT_FOUND;
		}
	}

cleanup:
	if (phrase->num_words > 10)
	{
		tman_free (words_info);
		tman_free (phrase_info.word_positions);
	}

	return (result);
}