forked from harry2105/Selenium_Java_Batch_5
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecondCommonWord.java
More file actions
74 lines (53 loc) · 2.11 KB
/
SecondCommonWord.java
File metadata and controls
74 lines (53 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class SecondCommonWord {
public static void main (String[] args) {
String filePath = "Macbeth.txt";
getSecondFreqWord(filePath);
}
public static void getSecondFreqWord (String filePath) {
Map<String, Integer> freqMap = new HashMap<>();
try {
File file = new File(filePath);
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line;
String strWord="";
while((line = br.readLine()) != null){
String [] strLine=line.split(" ");
for(int i=0;i<strLine.length;i++){
if(strLine[i].length()>4){
if(strLine[i].contains("!") || strLine[i].contains(";") || strLine[i].contains(",") || strLine[i].contains(".") || strLine[i].contains(":")){
strWord=strLine[i].substring(0, strLine[i].length() - 1);
}else{
strWord=strLine[i];
}
if(freqMap.containsKey(strWord)){
freqMap.put(strWord, freqMap.get(strWord)+1);
}else{
freqMap.put(strWord,1);
}
}
}
}
} catch (IOException e) {
System.err.println("Issue in reading the file..!");
}
//System.out.println(freqMap);
List<Map.Entry<String, Integer>> list = new LinkedList<Map.Entry<String, Integer>>(freqMap.entrySet());
// Sort the list
Collections.sort(list, (o1, o2) -> (o2.getValue()).compareTo(o1.getValue()));
int i = 1, j = 2;
do {
System.out.println("Second Frequent Word : " + list.get(i).getKey() + " with occurences : " + list.get(i).getValue());
i=j; j++;
} while (list.get(i) == list.get(j));
}
}