How to find repeated words of a given file
This program is written how to count repeated words in given file . Here we are using
hash map to store the words and count as string and int key value pairs. then we use
BufferReader to read file using split function all file words . we also use comparator
to repeat count using separate function . Please go through the code try to run it on your
own system . You will get it easily .
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.regex.Pattern;
import java.util.Scanner;
/***
*
*
*
* @author Mukesh Jha
*
*/
public class Maxwordcount {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your file path String--");
String pathentered = scanner.nextLine();
System.out.println("Your file path is: " + pathentered);
Map wordMap = buildWordMap(pathentered);
List> list = sortByValueInDecreasingOrder(wordMap);
System.out.println("List of repeated word from file and their count");
for (Map.Entry entry : list) { if (entry.getValue() > 1)
{ System.out.println(entry.getKey() + " => " + entry.getValue());
}
}
}
public static Map buildWordMap(String fileName)
{ // Using diamond operator for clean code
Map wordMap = new HashMap<>();
// Using try-with-resource statement for automatic resource management
try {
FileInputStream fis = new FileInputStream(fileName);
DataInputStream dis = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(dis));
Pattern pattern = Pattern.compile("\\s+");
String line =null;
while ((line = br.readLine()) != null)
{
line = line.toLowerCase();
String[] words = pattern.split(line);
for (String word : words)
{
if (wordMap.containsKey(word))
{
wordMap.put(word, (wordMap.get(word) + 1)); }
else {
wordMap.put(word, 1);
}
}
}
}
catch(IOException ioex)
{
ioex.printStackTrace();
}
return wordMap;
}
public static List> sortByValueInDecreasingOrder(Map wordMap)
{
Set> entries = wordMap.entrySet();
List> list = new ArrayList<>(entries);
Collections.sort(list, new Comparator
{
@Override
public int compare(Map.Entry o1, Map.Entry o2) {
return (o2.getValue()).compareTo(o1.getValue());
}
});
return list;
}
}
Programm OutPut
Programm OutPut
Enter your file path String--
enter your file path you want to read
How to find repeated words of a given file
Reviewed by Mukesh Jha
on
12:14 AM
Rating:
No comments:
Add your comment