Skip to main content
Code Review

Return to Answer

added 55 characters in body
Source Link

Picking up the suggestion of @Tamoghna Chowdhury I wrote a sample implementation using Java8 Streams.

Note that the suggestions of @kraskevich aren't considered in my implementation. You should only consider this as a proof of concept of what is possible (in particular reducing the line count) by using the appropriate API of Java.

public class FrequencyCounter {
 private static Map<String, Long> countFrequency(Stream<String> valueStream) {
 return valueStream
 return valueStream.map(s -> s.split(" "))
 .flatMap(Arrays::stream)
 .map(FrequencyCounter::clean)
 .filter(s -> !s.equals(""))
 .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
 }
 public static void main(String[] args) throws IOException {
 JFileChooser fc = new JFileChooser();
 fc.setCurrentDirectory(new File(System.getProperty("user.dir")));
 if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
 Map<String, Long> map = countFrequency(
 Files.lines(Paths.get(fc.getSelectedFile().getAbsolutePath())));
 
 BufferedWriter writer = new BufferedWriter(new FileWriter(new File(fc.getSelectedFile().getAbsolutePath().replace(fc.getSelectedFile().getName(), "") + "result_" + fc.getSelectedFile().getName())));
 for (Entry<String, Long> ent : map.entrySet()) {
 writer.write(ent.getKey() + " " + ent.getValue() + "\n");
 writer.newLine();
 }
 writer.close();
 }
 }
 private static String clean(String word) {
 return word.replaceAll("[1234567—890!@#$%^&*()_+|\\-=~`{}\\[\\]:;\"<>,.?/]", "").toLowerCase(); 
 }
}

Picking up the suggestion of @Tamoghna Chowdhury I wrote a sample implementation using Java8 Streams.

Note that the suggestions of @kraskevich aren't considered in my implementation. You should only consider this as a proof of concept of what is possible (in particular reducing the line count) by using the appropriate API of Java.

public class FrequencyCounter {
 private static Map<String, Long> countFrequency(Stream<String> valueStream) {
 return valueStream.map(FrequencyCounter::clean)
 .filter(s -> !s.equals(""))
 .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
 }
 public static void main(String[] args) throws IOException {
 JFileChooser fc = new JFileChooser();
 fc.setCurrentDirectory(new File(System.getProperty("user.dir")));
 if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
 Map<String, Long> map = countFrequency(
 Files.lines(Paths.get(fc.getSelectedFile().getAbsolutePath())));
 
 BufferedWriter writer = new BufferedWriter(new FileWriter(new File(fc.getSelectedFile().getAbsolutePath().replace(fc.getSelectedFile().getName(), "") + "result_" + fc.getSelectedFile().getName())));
 for (Entry<String, Long> ent : map.entrySet()) {
 writer.write(ent.getKey() + " " + ent.getValue() + "\n");
 writer.newLine();
 }
 writer.close();
 }
 }
 private static String clean(String word) {
 return word.replaceAll("[1234567—890!@#$%^&*()_+|\\-=~`{}\\[\\]:;\"<>,.?/]", "").toLowerCase(); 
 }
}

Picking up the suggestion of @Tamoghna Chowdhury I wrote a sample implementation using Java8 Streams.

Note that the suggestions of @kraskevich aren't considered in my implementation. You should only consider this as a proof of concept of what is possible (in particular reducing the line count) by using the appropriate API of Java.

public class FrequencyCounter {
 private static Map<String, Long> countFrequency(Stream<String> valueStream) {
 return valueStream
 .map(s -> s.split(" "))
 .flatMap(Arrays::stream)
 .map(FrequencyCounter::clean)
 .filter(s -> !s.equals(""))
 .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
 }
 public static void main(String[] args) throws IOException {
 JFileChooser fc = new JFileChooser();
 fc.setCurrentDirectory(new File(System.getProperty("user.dir")));
 if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
 Map<String, Long> map = countFrequency(
 Files.lines(Paths.get(fc.getSelectedFile().getAbsolutePath())));
 
 BufferedWriter writer = new BufferedWriter(new FileWriter(new File(fc.getSelectedFile().getAbsolutePath().replace(fc.getSelectedFile().getName(), "") + "result_" + fc.getSelectedFile().getName())));
 for (Entry<String, Long> ent : map.entrySet()) {
 writer.write(ent.getKey() + " " + ent.getValue() + "\n");
 writer.newLine();
 }
 writer.close();
 }
 }
 private static String clean(String word) {
 return word.replaceAll("[1234567—890!@#$%^&*()_+|\\-=~`{}\\[\\]:;\"<>,.?/]", "").toLowerCase(); 
 }
}
Source Link

Picking up the suggestion of @Tamoghna Chowdhury I wrote a sample implementation using Java8 Streams.

Note that the suggestions of @kraskevich aren't considered in my implementation. You should only consider this as a proof of concept of what is possible (in particular reducing the line count) by using the appropriate API of Java.

public class FrequencyCounter {
 private static Map<String, Long> countFrequency(Stream<String> valueStream) {
 return valueStream.map(FrequencyCounter::clean)
 .filter(s -> !s.equals(""))
 .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
 }
 public static void main(String[] args) throws IOException {
 JFileChooser fc = new JFileChooser();
 fc.setCurrentDirectory(new File(System.getProperty("user.dir")));
 if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
 Map<String, Long> map = countFrequency(
 Files.lines(Paths.get(fc.getSelectedFile().getAbsolutePath())));
 
 BufferedWriter writer = new BufferedWriter(new FileWriter(new File(fc.getSelectedFile().getAbsolutePath().replace(fc.getSelectedFile().getName(), "") + "result_" + fc.getSelectedFile().getName())));
 for (Entry<String, Long> ent : map.entrySet()) {
 writer.write(ent.getKey() + " " + ent.getValue() + "\n");
 writer.newLine();
 }
 writer.close();
 }
 }
 private static String clean(String word) {
 return word.replaceAll("[1234567—890!@#$%^&*()_+|\\-=~`{}\\[\\]:;\"<>,.?/]", "").toLowerCase(); 
 }
}
lang-java

AltStyle によって変換されたページ (->オリジナル) /