I want to search for a query from a file named a.java. If my query is String name I want to get the frequency of a string individually from the query from the text file. First I have to count the frequency of String and then name individually and then add the frequency both. how can I implement this program in java platform?
public class Tf2 {
Integer k;
int totalword = 0;
int totalfile, containwordfile = 0;
Map<String, Integer> documentToCount = new HashMap<>();
File file = new File("H:/java");
File[] files = file.listFiles();
public void Count(String word) {
File[] files = file.listFiles();
Integer count = 0;
for (File f : files) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(f));
count = documentToCount.get(word);
documentToCount.clear();
String line;
while ((line = br.readLine()) != null) {
String term[] = line.trim().replaceAll("[^a-zA-Z0-9 ]", " ").toLowerCase().split(" ");
for (String terms : term) {
totalword++;
if (count == null) {
count = 0;
}
if (documentToCount.containsKey(word)) {
count = documentToCount.get(word);
documentToCount.put(terms, count + 1);
} else {
documentToCount.put(terms, 1);
}
}
}
k = documentToCount.get(word);
if (documentToCount.get(word) != null) {
containwordfile++;
System.out.println("" + k);
}
} catch (Exception e) {
e.printStackTrace();
}
}
} public static void main(String[] args) throws IOException {Tf2 ob = new Tf2();String query="String name";ob.Count(query);
}}
I tried this with hashmap. but it cannot count the frequency of the query individually.