Saturday, 26 September 2020

Find frequency of keyword in Java

 This program checks the frequency of a keyword in any .txt file

  • Start your IDE 
  • Type in a class named Frequency :
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class Frequency {

public static void main(String[] args) throws FileNotFoundException {
Scanner scan2=new Scanner(System.in);
String src="";
System.out.println("Enter main file path (Only .txt files approved)");
for (;;){
src=scan2.nextLine();
if (new File(src).exists()==true){break;}
System.out.println("Your file doesn't exist. Please try again.");
}System.out.println("-----------------------------------------------------------------------");
System.out.println("Welcome to Frequency");
System.out.println("-----------------------------------------------------------------------");
for (;;){
Scanner scan=new Scanner(new File(src));
System.out.println("Enter the word that you want to find: (Type $END$ to terminate program.)");
String chr=scan2.next().replace("\"", "");
if (chr.equals("$END$")){System.out.println("Terminating program...");System.out.println("Terminated");break;}
chr=chr.replace("?", "");
chr=chr.replace(";", "");
chr=chr.replace("!", "");
chr=chr.replace(",", "");
chr=chr.replace("'", "");
chr=chr.replace("’", "");
chr=chr.replace(".", "");
chr=chr.replace(":", "");
chr=chr.replace("(", "");
chr=chr.replace(")", "");
chr=chr.replace("]", "");
chr=chr.replace("[", "");
chr=chr.replace("{", "");
chr=chr.replace("}", "");
chr=chr.replace("-", "");
chr=chr.replace("@", "");
chr=chr.replace("%", "");
chr=chr.replace("*", "");
chr=chr.replace("#", "");
chr=chr.replace("`", "");
chr=chr.replace("~", "");
chr=chr.replace("/", "");
chr=chr.replace("\\", "");
chr=chr.replace(">", "");
chr=chr.replace("<", "");
double frq=0;
double total=0;
String y="";
while (scan.hasNext()){
y=scan.next().replace("\"", "");
y=y.replace("?", "");
y=y.replace(";", "");
y=y.replace("!", "");
y=y.replace(",", "");
y=y.replace("’", "");
y=y.replace("'", "");
y=y.replace(".", "");
y=y.replace(":", "");
y=y.replace("(", "");
y=y.replace(")", "");
y=y.replace("]", "");
y=y.replace("[", "");
y=y.replace("{", "");
y=y.replace("}", "");
y=y.replace("-", "");
y=y.replace("@", "");
y=y.replace("%", "");
y=y.replace("*", "");
y=y.replace("#", "");
y=y.replace("`", "");
y=y.replace("~", "");
y=y.replace("/", "");
y=y.replace("\\", "");
y=y.replace(">", "");
y=y.replace("<", "");
//System.out.println(y); Just to see result
total++;
if (y.equalsIgnoreCase(chr)){frq++;}
}
System.out.println("Word appears "+frq+" times");
System.out.println("The word has an appearance frequency in the text of "+frq/total*100+"%");
System.out.println("-----------------------------------------------------------------------");}

}

}



  • Execute the code

No comments:

Post a Comment

Algorithmic Pixel Art: The Beauty of Aliasing

Discussed  here More images in  Google drive Some outputs: Forward to anyone who says aliasing is ugly.