Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.*;
- import java.io.*;
- import java.util.regex.*;
- public class WordStatWords {
- public static void main(String[] args) throws Exception {
- Map<String, Integer> wordsAndCnt = new TreeMap<>();
- String regexp = "[\\p{L}\\p{Pd}']+";
- Pattern pattern = Pattern.compile(regexp);
- try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(args[0]), "UTF-8"))) {
- String line;
- while ((line = reader.readLine()) != null) {
- line = line.toLowerCase();
- Matcher matcher = pattern.matcher(line);
- while (matcher.find()) {
- String word = matcher.group();
- wordsAndCnt.putIfAbsent(word, 0);
- wordsAndCnt.put(word, wordsAndCnt.get(word) + 1);
- }
- }
- } catch (FileNotFoundException e) {
- System.err.println("File does not exsist");
- } catch (ArrayIndexOutOfBoundsException e) {
- System.err.println("Enter the name of the file");
- }
- try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(args[1]), "UTF-8"))) {
- for (Map.Entry e : wordsAndCnt.entrySet()) {
- writer.write(e.getKey() + " " + e.getValue() + "\r\n");
- }
- } catch (FileNotFoundException e) {
- System.err.println("File does not exsist");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement