Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.nio.charset.StandardCharsets;
- import java.util.ArrayList;
- import java.util.Map;
- import java.util.TreeMap;
- public class WordStatLineIndex {
- public static void main(String[] args) throws IOException {
- Map <String, ArrayList <AdditionalPair <Integer, Integer>>> wordStatistic
- = new TreeMap <>();
- try (FastScanner reader = new FastScanner(
- new FileInputStream(args[0]), "UTF-8")) {
- int lineCounter = 0;
- while (reader.hasNext()) {
- int inLineWordsCounter = 0;
- lineCounter++;
- while (reader.onLine()) {
- String currentWord = reader.nextWord();
- if (!currentWord.isEmpty()) {
- inLineWordsCounter++;
- if (!wordStatistic.containsKey(currentWord)) {
- wordStatistic.put(currentWord, new ArrayList <>());
- }
- wordStatistic.get(currentWord).add(new AdditionalPair <>(lineCounter, inLineWordsCounter));
- }
- }
- }
- try (BufferedWriter writer = new BufferedWriter(
- new OutputStreamWriter(
- new FileOutputStream(args[1]),
- "UTF-8"))) {
- for (Map.Entry <String, ArrayList <AdditionalPair <Integer, Integer>>> currentWordStatisticPair
- : wordStatistic.entrySet()) {
- writer.write(currentWordStatisticPair.getKey() + " ");
- writer.write(currentWordStatisticPair.getValue().size() + "");
- for (AdditionalPair <Integer, Integer> currentWordStatistic
- : currentWordStatisticPair.getValue()) {
- writer.write(" " + currentWordStatistic.getFirstElement());
- writer.write(":" + currentWordStatistic.getSecondElement());
- }
- writer.write("\n");
- }
- } catch (IOException ex) {
- System.out.println("sorry, but program can't output result \n Try to check args correctness");
- }
- } catch (FileNotFoundException ex) {
- System.out.println("sorry, but program can't find the input file OwO");
- } catch (IndexOutOfBoundsException ex) {
- System.out.println("pls, enter args correctly (Example: \"input.txt\" \"output.txt\")");
- }
- }
- }
- // Also we can use String ArrayList instead of AdditionalPair, but AdditionalPair solution is more pretty, I think.
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class FastScanner implements AutoCloseable {
- private char currentChar;
- private final InputStreamReader stream;
- private static final byte END_OF_FILE = -1;
- private static final char NEW_LINE = '\n';
- private static final char APOSTROPHE = '\'';
- FastScanner(FileInputStream stream, String charsetName) throws IOException {
- this.stream = new InputStreamReader(stream, charsetName);
- }
- private void readNextChar() throws IOException {
- currentChar = (char) stream.read();
- }
- boolean hasNext() throws IOException {
- readNextChar();
- return (byte) currentChar != END_OF_FILE;
- }
- boolean onLine() {
- return currentChar != NEW_LINE
- && (byte) currentChar != END_OF_FILE;
- }
- String nextWord() throws IOException {
- StringBuilder currentWord = new StringBuilder();
- while (!charIsPartOfWord(currentChar)) {
- readNextChar();
- if (!onLine()) {
- break;
- }
- }
- while (charIsPartOfWord(currentChar)) {
- currentWord.append(currentChar);
- readNextChar();
- }
- return currentWord.toString().toLowerCase();
- }
- private boolean charIsPartOfWord(char symbol) {
- return Character.isLetter(symbol)
- || Character.getType(symbol) == Character.DASH_PUNCTUATION
- || symbol == APOSTROPHE;
- }
- public void close(){}
- }
- class AdditionalPair<FirstDataType, SecondDataType> {
- private final FirstDataType first;
- private final SecondDataType second;
- AdditionalPair(FirstDataType first, SecondDataType second) {
- this.first = first;
- this.second = second;
- }
- FirstDataType getFirstElement() {
- return first;
- }
- SecondDataType getSecondElement() {
- return second;
- }
- }
- //pair in javaFX 2.0 +
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement