Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Team

I have a class that analyse the comments of customers. Now i have a method that does the count of comments. How do i improve this method by adding metrics such as allow to count for Question mark from the comments and if there is a sparm found from the folder it matches and print it?

What I have tried:

//CommentAnalyser.java

Java
<pre lang="Java">import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class CommentAnalyzer {
	
	private File file;
	
	public CommentAnalyzer(File file) {
		this.file = file;
	}
	
	public Map<String, Integer> analyze() {
		
		Map<String, Integer> resultsMap = new HashMap<>();
		
		try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
			
			String line = null;
			while ((line = reader.readLine()) != null) {
				
				if (line.length() < 15) {
					
					incOccurrence(resultsMap, "SHORTER_THAN_15");

				} else if (line.contains("Mover")) {

					incOccurrence(resultsMap, "MOVER_MENTIONS");
				
				} else if (line.contains("Shaker")) {

					incOccurrence(resultsMap, "SHAKER_MENTIONS");
				
				}
			}
			
		} catch (FileNotFoundException e) {
			System.out.println("File not found: " + file.getAbsolutePath());
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("IO Error processing file: " + file.getAbsolutePath());
			e.printStackTrace();
		}
		
		return resultsMap;
		
	}
	
	/**
	 * This method increments a counter by 1 for a match type on the countMap. Uninitialized keys will be set to 1
	 * @param countMap the map that keeps track of counts
	 * @param key the key for the value to increment
	 */
	private void incOccurrence(Map<String, Integer> countMap, String key) {
		
		countMap.putIfAbsent(key, 0);
		countMap.put(key, countMap.get(key) + 1);
	}

}


// Main.java


import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class Main {

public static void main(String[] args) {

Map<string, integer=""> totalResults = new HashMap<>();

File docPath = new File("docs");
File[] commentFiles = docPath.listFiles((d, n) -> n.BeginWith(".txt"));

for (File commentFile : commentFiles) {

CommentAnalyzer commentAnalyzer = new CommentAnalyzer(commentFile);
Map<string, integer=""> fileResults = commentAnalyzer.analyze();
addReportResults(<fileresults>, <totalresults>);

}

System.out.println("RESULTS\n=======");
totalResults.forEach((d,n) -> System.out.println(d + " : " + n));
}

/**
* This method adds the result counts from a source map to the target map
* @param source the source map
* @param target the target map
*/
private static void addReportResults(Map<string, integer=""> source, Map<string, integer=""> target) {

for (Map.Entry<string, integer=""> entry : source.entrySet()) {
target.put(entry.getKey(), entry.getValue());
}

}

}
Posted
Updated 12-Sep-22 15:41pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900