Retrieval of Documents using Inverted Files -BE(IT) CLP-II Practical
Aim : To implement a program Retrieval of documents using inverted files.
Objective : To study Indexing , Inverted Files and searching with the help of inverted file in Java Language . Code written in Java to implement of the same with appropriate output.
Input to the Program of Inverted Files :
File 1 Contents : are you anil kumar
File 2 Contents : hello where are you.
Source Code for Inverted Files in Java Language :
package com.prac.prac;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class invertedfile
{
public static void displayIndex(ArrayListinvertedData,int[][] docno){
int i,j;
for(i=0;iSystem.out.print(invertedData.get(i)+"\t");
for(j=1;j<=docno[i][0];j++)
System.out.print(docno[i][j]+"\t");
System.out.print("\n");
}
}
public static void indexing(String fname,ArrayListinvertedData,int[][] docno,int fileno)
{
BufferedReader br;
try
{
br = new BufferedReader(new FileReader(fname));
String data = "", line = br.readLine();
while(line!=null)
{
data+=line+" ";
line=br.readLine();
}
String[] st=data.split("[ ,.]");
String currenttoken=null;
int i=0;
while(i{
currenttoken=st[i];
int indx=invertedData.indexOf(currenttoken);
if (indx==-1)
{
invertedData.add(currenttoken);
indx=invertedData.indexOf(currenttoken);
docno[indx][0]=1;
docno[indx][1]=fileno;
}
else
{
docno[indx][docno[indx][0]+1]=fileno;
docno[indx][0]+=1;
}
i+=1;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) throws NumberFormatException, IOException {
String fname="";
ArrayListinvertedData=new ArrayList ();
int docno[][]=new int[100][10];
InputStreamReader ins=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ins);
System.out.println("\nENTER TOTAL NO OF FILES:");
int no=Integer.parseInt(br.readLine());
int i=1;
while(i-1!=no)
{
System.out.println("\nENTER FILE "+i+" NAME:");
fname=br.readLine();
indexing(fname,invertedData,docno,i);
i+=1;
}
displayIndex(invertedData,docno);
}
}
Output for the Inverted Files Program:
ENTER TOTAL NO OF FILES:
2
ENTER FILE 1 NAME:
c:\anil1.txt
ENTER FILE 2 NAME:
c:\anil2.txt
hello 1
where 1
are 1 2
you 1 2
anil 2
kumar 2
Leave a Reply