You are on page 1of 5

EXPERIMENT NO : 9 Code: textmining.java //program for text mining import java.io.

*; public class textmining { public static void main(String[] args) throws IOException { String fpath,word; StringBuffer file=new StringBuffer(""); int i,count=0; FileInputStream fin;

BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the path of the file to be searched"); fpath=br.readLine(); System.out.println("Enter word to be searched"); word=br.readLine(); try{ fin=new FileInputStream(fpath); do { i=fin.read(); if(i!=-1) file.append((char)i); }while(i!=-1); int flen=file.length(); int wlen=word.length(); System.out.println("Total File Length=" +file.length()); for(int j=0; j<=(flen-wlen);j++) { if(file.substring(j,j+wlen).equals(word)) count++; }

if(count>0) System.out.println("Word Found " + count + " Times"); else System.out.println("Match Not Found"); fin.close(); } catch(FileNotFoundException e) { System.out.println("File not Found: " + e); } } } Output:-

EXPERIMENT NO :10 Code:- PageRanking.java //program for text mining import java.io.*; public class PageRanking { public static void main(String[] args) throws IOException { String fpath,word; int i; File root; int totalFiles=0; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the path of root folder"); fpath=br.readLine(); System.out.println("Enter word to be searched"); word=br.readLine(); root=new File(fpath); File[] rootFiles=root.listFiles(); totalFiles=rootFiles.length; System.out.println(rootFiles.length); int count[]=new int[totalFiles]; FileInputStream fin; for(int j=0;j<totalFiles;j++) { count[j]=0; } try{ for(int fc=0;fc< totalFiles;fc++) { count[fc]=0; String file=""; fin=new FileInputStream(rootFiles[fc]); do {

i=fin.read(); if(i!=-1) file=file + (char)i; }while(i!=-1); int flen=file.length(); int wlen=word.length(); for(int j=0; j<=(flen-wlen);j++) { if(file.substring(j,j+wlen).equals(word)) count[fc]++; } System.out.println("-------------------------------------------"); System.out.println("File Statistics:"); System.out.println("File Name: " + rootFiles[fc]); System.out.println("Total File Length=" +file.length()); if(count[fc]>0) System.out.println("Word Found " + count[fc] + " Times"); else System.out.println("Match Not Found"); System.out.println("-------------------------------------------"); fin.close(); } } catch(FileNotFoundException e) { System.out.println("File not Found: " + e); } //sorting file according to the no of match for(int j=0;j<totalFiles-1;j++) { for(int k=j+1; k<totalFiles; k++) { if(count[j]<count[k]) { int temp; File ftemp; temp=count[j];

count[j]=count[k]; count[k]=temp; //swapping files ftemp=rootFiles[j]; rootFiles[j]=rootFiles[k]; rootFiles[k]=ftemp; } } }

System.out.println("**********************************************"); System.out.println("Page Ranking For Word Searched: " + word); for(int j=0;j<rootFiles.length;j++) { System.out.println((j+1) +") File: " + rootFiles[j] + " Contains " + count[j] + " times"); }

System.out.println("**********************************************"); } } Output:-

You might also like