Inverted Index Code (Short Version)
Inverted Index Algorithm Code (Short Version)
#include<stdio.h>
#include<string.h>
void main()
{
FILE *fptr1,*fptr2,*fptr3,*fptr4,*fptr5;
int count=1,len=0;
char ch[40],keyword[40],word[40],loc[40];
int flag=0;
// part 1 building the index
fptr1=fopen("temp.txt","r");
fptr2=fopen("temp1.txt","w");
while(fscanf(fptr1,"%s",ch)!=EOF)
{
fprintf(fptr2,"\n%s\t%d",ch,count);
len=strlen(ch);
count=count+len+1;
}
fclose(fptr1);
fclose(fptr2);
//part 2 the searching function
flag=0;
fptr3=fopen("temp1.txt","r");
printf("\n Enter a keyword to search");
scanf("%s",keyword);
while(fscanf(fptr3,"%s\t%s",word,loc)!=EOF)
{
if(strcmp(keyword,word)==0)
{
flag=1;
}
else
{
flag=0;
}
if(flag==1)
{
printf("Keyword found");
printf("\nkeyword:%slocation:%s",word,loc);
break;
}
}
if(flag==0)
{
printf("Keyword not found");
}
fclose(fptr3);
}
No comments:
Post a Comment