Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.*;
- import java.io.*;
- public class WordStatWords {
- public static void main(String[] args) throws Exception {
- Map <String, Integer> wordsAndCnt = new TreeMap<>();
- ArrayList <String> lines = new ArrayList<>();
- ArrayList <String> words = new ArrayList<>();
- try {
- BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(args[0]), "UTF-8"));
- String line;
- while ((line=reader.readLine()) != null) {
- lines.add(line);
- }
- reader.close();
- } catch (FileNotFoundException e){
- System.out.print("File does not exsist");
- }
- BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(args[1]), "UTF-8"));
- for (int i = 0; i < lines.size(); i++) {
- String line = lines.get(i);
- line = line.toLowerCase();
- line = line.replaceAll("[^\\p{L}\\p{Pd} ']", " ");
- String[] arrWords = line.split("\\s+");
- for (int j = 0; j < arrWords.length; j++) {
- if (arrWords[j]!= null && !arrWords[j].isEmpty()){
- words.add(arrWords[j]);
- }
- }
- }
- for (int i = 0; i < words.size(); i++) {
- if (wordsAndCnt.containsKey(words.get(i))) {
- int x = wordsAndCnt.get(words.get(i));
- wordsAndCnt.put(words.get(i), x+1);
- } else {
- wordsAndCnt.put(words.get(i), 1);
- }
- }
- try {
- for (Map.Entry e : wordsAndCnt.entrySet()) {
- writer.write(e.getKey() + " " + e.getValue() + "\r\n");
- }
- writer.close();
- } catch (FileNotFoundException e) {
- System.out.print("File does not exsist");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement