From 0d6b168b1e76dca60e7a1aeaa66d1b6113789feb Mon Sep 17 00:00:00 2001 From: codejava Date: Mon, 13 Apr 2026 10:42:12 +0300 Subject: [PATCH] Upload files to "java/wordStat" --- java/wordStat/WordStatLengthAffix.java | 117 ++++++++++++++++++++++++ java/wordStat/WordStatLengthMiddle.java | 93 +++++++++++++++++++ java/wordStat/WordStatLengthPrefix.java | 96 +++++++++++++++++++ java/wordStat/WordStatLengthSuffix.java | 95 +++++++++++++++++++ java/wordStat/WordStatTest.java | 91 ++++++++++++++++++ 5 files changed, 492 insertions(+) create mode 100644 java/wordStat/WordStatLengthAffix.java create mode 100644 java/wordStat/WordStatLengthMiddle.java create mode 100644 java/wordStat/WordStatLengthPrefix.java create mode 100644 java/wordStat/WordStatLengthSuffix.java create mode 100644 java/wordStat/WordStatTest.java diff --git a/java/wordStat/WordStatLengthAffix.java b/java/wordStat/WordStatLengthAffix.java new file mode 100644 index 0000000..e4f53c2 --- /dev/null +++ b/java/wordStat/WordStatLengthAffix.java @@ -0,0 +1,117 @@ +import java.io.*; +import java.util.*; + +/** + * @author Nikita Doschennikov (me@fymio.us) + */ +public class WordStatLengthAffix { + + public static void main(String[] args) { + if (args.length != 2) { + System.err.println("incorrect input!"); + System.err.println( + "usage: java WordStatLengthAffix " + ); + } + + String inputFileName = args[0]; + String outputFileName = args[1]; + try { + BufferedReader r = new BufferedReader( + new FileReader(inputFileName) + ); + + Map wordMap = new HashMap<>(); + StringBuilder sb = new StringBuilder(); + int wordIndex = 0; + + int data = r.read(); + while (data != -1) { + char c = (char) data; + + if ( + Character.getType(c) == Character.DASH_PUNCTUATION || + Character.isLetter(c) || + c == '\'' + ) { + sb.append(c); + } else { + if (sb.length() > 0) { + String word = sb.toString().toLowerCase(); + if (word.length() != 1) { + String prefix = word.substring( + 0, + word.length() / 2 + ); + String suffix = word.substring( + word.length() - word.length() / 2 + ); + if (wordMap.containsKey(prefix)) { + wordMap.get(prefix).count++; + } else { + wordMap.put( + prefix, + new WordInfo(prefix, 1, wordIndex) + ); + wordIndex++; + } + if (wordMap.containsKey(suffix)) { + wordMap.get(suffix).count++; + } else { + wordMap.put( + suffix, + new WordInfo(suffix, 1, wordIndex) + ); + wordIndex++; + } + } + sb.setLength(0); + } + } + + data = r.read(); + } + + if (sb.length() > 0) { + String word = sb.toString().toLowerCase(); + if (word.length() != 1) { + String prefix = word.substring(0, word.length() / 2); + String suffix = word.substring( + word.length() - word.length() / 2 + ); + if (wordMap.containsKey(prefix)) { + wordMap.get(prefix).count++; + } else { + wordMap.put(prefix, new WordInfo(prefix, 1, wordIndex)); + wordIndex++; + } + if (wordMap.containsKey(suffix)) { + wordMap.get(suffix).count++; + } else { + wordMap.put(suffix, new WordInfo(suffix, 1, wordIndex)); + wordIndex++; + } + } + } + + r.close(); + + List sortedWords = new ArrayList<>(wordMap.values()); + sortedWords.sort( + Comparator.comparingInt((WordInfo w) -> + w.word.length() + ).thenComparingInt(w -> w.firstIndex) + ); + + PrintWriter writer = new PrintWriter(outputFileName, "UTF-8"); + + for (WordInfo info : sortedWords) { + writer.println(info.word + " " + info.count); + } + + writer.close(); + } catch (Exception ex) { + System.err.println("An error occured: " + ex.getMessage()); + } + } +} diff --git a/java/wordStat/WordStatLengthMiddle.java b/java/wordStat/WordStatLengthMiddle.java new file mode 100644 index 0000000..4063ecb --- /dev/null +++ b/java/wordStat/WordStatLengthMiddle.java @@ -0,0 +1,93 @@ +import java.io.*; +import java.util.*; + +/** + * @author Nikita Doschennikov (me@fymio.us) + */ +public class WordStatLengthMiddle { + + public static void main(String[] args) { + if (args.length != 2) { + System.err.println("incorrect input!"); + System.err.println( + "usage: java WordStatLengthMiddle " + ); + } + + String inputFileName = args[0]; + String outputFileName = args[1]; + try { + BufferedReader r = new BufferedReader( + new FileReader(inputFileName) + ); + + Map wordMap = new HashMap<>(); + StringBuilder sb = new StringBuilder(); + int wordIndex = 0; + + int data = r.read(); + while (data != -1) { + char c = (char) data; + + if ( + Character.getType(c) == Character.DASH_PUNCTUATION || + Character.isLetter(c) || + c == '\'' + ) { + sb.append(c); + } else { + if (sb.length() > 0) { + String word = sb.toString().toLowerCase(); + if (word.length() >= 7) { + word = word.substring(3, word.length() - 3); + if (wordMap.containsKey(word)) { + wordMap.get(word).count++; + } else { + wordMap.put( + word, + new WordInfo(word, 1, wordIndex) + ); + wordIndex++; + } + } + sb.setLength(0); + } + } + + data = r.read(); + } + + if (sb.length() > 0) { + String word = sb.toString().toLowerCase(); + if (word.length() >= 7) { + word = word.substring(3, word.length() - 3); + if (wordMap.containsKey(word)) { + wordMap.get(word).count++; + } else { + wordMap.put(word, new WordInfo(word, 1, wordIndex)); + wordIndex++; + } + } + } + + r.close(); + + List sortedWords = new ArrayList<>(wordMap.values()); + sortedWords.sort( + Comparator.comparingInt((WordInfo w) -> + w.word.length() + ).thenComparingInt(w -> w.firstIndex) + ); + + PrintWriter writer = new PrintWriter(outputFileName, "UTF-8"); + + for (WordInfo info : sortedWords) { + writer.println(info.word + " " + info.count); + } + + writer.close(); + } catch (Exception ex) { + System.err.println("An error occured: " + ex.getMessage()); + } + } +} diff --git a/java/wordStat/WordStatLengthPrefix.java b/java/wordStat/WordStatLengthPrefix.java new file mode 100644 index 0000000..ee56651 --- /dev/null +++ b/java/wordStat/WordStatLengthPrefix.java @@ -0,0 +1,96 @@ +import java.io.*; +import java.util.*; + +/** + * @author Nikita Doschennikov (me@fymio.us) + */ +public class WordStatLengthPrefix { + + public static void main(String[] args) { + if (args.length != 2) { + System.err.println("incorrect input!"); + System.err.println( + "usage: java WordStatLengthPrefix " + ); + } + + String inputFileName = args[0]; + String outputFileName = args[1]; + try { + BufferedReader r = new BufferedReader( + new FileReader(inputFileName) + ); + + Map wordMap = new HashMap<>(); + StringBuilder sb = new StringBuilder(); + int wordIndex = 0; + + int data = r.read(); + while (data != -1) { + char c = (char) data; + + if ( + Character.getType(c) == Character.DASH_PUNCTUATION || + Character.isLetter(c) || + c == '\'' + ) { + sb.append(c); + } else { + if (sb.length() > 0) { + String word = sb.toString().toLowerCase(); + if (word.length() != 1) { + String prefix = word.substring( + 0, + word.length() / 2 + ); + if (wordMap.containsKey(prefix)) { + wordMap.get(prefix).count++; + } else { + wordMap.put( + prefix, + new WordInfo(prefix, 1, wordIndex) + ); + wordIndex++; + } + } + sb.setLength(0); + } + } + + data = r.read(); + } + + if (sb.length() > 0) { + String word = sb.toString().toLowerCase(); + if (word.length() != 1) { + String prefix = word.substring(0, word.length() / 2); + if (wordMap.containsKey(prefix)) { + wordMap.get(prefix).count++; + } else { + wordMap.put(prefix, new WordInfo(prefix, 1, wordIndex)); + wordIndex++; + } + } + } + + r.close(); + + List sortedWords = new ArrayList<>(wordMap.values()); + sortedWords.sort( + Comparator.comparingInt((WordInfo w) -> + w.word.length() + ).thenComparingInt(w -> w.firstIndex) + ); + + PrintWriter writer = new PrintWriter(outputFileName, "UTF-8"); + + for (WordInfo info : sortedWords) { + writer.println(info.word + " " + info.count); + } + + writer.close(); + } catch (Exception ex) { + System.err.println("An error occured: " + ex.getMessage()); + } + } +} diff --git a/java/wordStat/WordStatLengthSuffix.java b/java/wordStat/WordStatLengthSuffix.java new file mode 100644 index 0000000..bc1bcf0 --- /dev/null +++ b/java/wordStat/WordStatLengthSuffix.java @@ -0,0 +1,95 @@ +import java.io.*; +import java.util.*; + +/** + * @author Nikita Doschennikov (me@fymio.us) + */ +public class WordStatLengthSuffix { + + public static void main(String[] args) { + if (args.length != 2) { + System.err.println("incorrect input!"); + System.err.println( + "usage: java WordStatLengthSuffix " + ); + } + + String inputFileName = args[0]; + String outputFileName = args[1]; + try { + BufferedReader r = new BufferedReader( + new FileReader(inputFileName) + ); + + Map wordMap = new HashMap<>(); + StringBuilder sb = new StringBuilder(); + int wordIndex = 0; + + int data = r.read(); + while (data != -1) { + char c = (char) data; + + if ( + Character.getType(c) == Character.DASH_PUNCTUATION || + Character.isLetter(c) || + c == '\'' + ) { + sb.append(c); + } else { + if (sb.length() > 0) { + String word = sb.toString().toLowerCase(); + if (word.length() != 1) { + word = word.substring( + word.length() - word.length() / 2 + ); + if (wordMap.containsKey(word)) { + wordMap.get(word).count++; + } else { + wordMap.put( + word, + new WordInfo(word, 1, wordIndex) + ); + wordIndex++; + } + } + sb.setLength(0); + } + } + + data = r.read(); + } + + if (sb.length() > 0) { + String word = sb.toString().toLowerCase(); + if (word.length() != 1) { + word = word.substring(word.length() - word.length() / 2); + if (wordMap.containsKey(word)) { + wordMap.get(word).count++; + } else { + wordMap.put(word, new WordInfo(word, 1, wordIndex)); + wordIndex++; + } + } + } + + r.close(); + + List sortedWords = new ArrayList<>(wordMap.values()); + sortedWords.sort( + Comparator.comparingInt((WordInfo w) -> + w.word.length() + ).thenComparingInt(w -> w.firstIndex) + ); + + PrintWriter writer = new PrintWriter(outputFileName, "UTF-8"); + + for (WordInfo info : sortedWords) { + writer.println(info.word + " " + info.count); + } + + writer.close(); + } catch (Exception ex) { + System.err.println("An error occured: " + ex.getMessage()); + } + } +} diff --git a/java/wordStat/WordStatTest.java b/java/wordStat/WordStatTest.java new file mode 100644 index 0000000..e99f4a6 --- /dev/null +++ b/java/wordStat/WordStatTest.java @@ -0,0 +1,91 @@ +package wordStat; + +import base.Named; +import base.Selector; +import java.util.Comparator; +import java.util.function.Function; +import java.util.stream.Stream; + +/** + * Tests for Word Statistics homework + * of Introduction to Programming course. + * + * @author Georgiy Korneev (kgeorgiy@kgeorgiy.info) + */ +public final class WordStatTest { + + // === Base + private static final Named>> ID = Named.of( + "", + Stream::of + ); + private static final WordStatTester.Variant BASE = + new WordStatTester.Variant("", false, Comparator.comparingInt(p -> 0)); + + // === 3637 + public static final int SIZE = 3; + private static final WordStatTester.Variant LENGTH = + new WordStatTester.Variant( + "Length", + false, + Comparator.comparingInt(p -> p.first().length()) + ); + private static final Named>> MIDDLE = size( + "Middle", + SIZE * 2 + 1, + s -> Stream.of(s.substring(SIZE, s.length() - SIZE)) + ); + + static Named>> size( + final String name, + final int length, + final Function> f + ) { + return Named.of(name, s -> + s.length() >= length ? f.apply(s) : Stream.empty() + ); + } + + // === 3839 + private static final Named>> AFFIX = size( + "Affix", + 2, + s -> + Stream.of( + s.substring(0, s.length() / 2), + s.substring(s.length() - s.length() / 2) + ) + ); + + // === 3536 + private static final Named>> SUFFIX = size( + "Suffix", + 2, + s -> Stream.of(s.substring(s.length() - s.length() / 2)) + ); + + // === 4749 + private static final Named>> PREFIX = size( + "Prefix", + 2, + s -> Stream.of(s.substring(0, s.length() / 2)) + ); + + // === Common + public static final Selector SELECTOR = new Selector(WordStatTester.class) + .variant("Base", BASE.with(ID)) + .variant("3637", LENGTH.with(MIDDLE)) + .variant("3839", LENGTH.with(AFFIX)) + .variant("3435", LENGTH.with(SUFFIX)) + .variant("3233", LENGTH.with(ID)) + .variant("4142", LENGTH.with(MIDDLE)) + .variant("4749", LENGTH.with(PREFIX)); + + private WordStatTest() { + // Utility class + } + + public static void main(final String... args) { + SELECTOR.main(args); + } +}