From d8154da7a9718ef77e699b90ed42db82e89c3492 Mon Sep 17 00:00:00 2001 From: me Date: Mon, 2 Feb 2026 14:27:24 +0300 Subject: [PATCH] add solution for hw6:3233 --- .gitea/workflows/wspp.yml | 2 +- java/wspp/WordInfo.java | 2 +- java/wspp/WordScanner.java | 23 ++++++++++++-- java/wspp/WsppPos.java | 62 ++++++++++++++++++++++++++------------ 4 files changed, 64 insertions(+), 25 deletions(-) diff --git a/.gitea/workflows/wspp.yml b/.gitea/workflows/wspp.yml index c31ed23..9d8215a 100644 --- a/.gitea/workflows/wspp.yml +++ b/.gitea/workflows/wspp.yml @@ -19,4 +19,4 @@ jobs: - name: Run Word Stat++ tests run: | - java -ea -cp out wspp.WsppTest Base + java -ea -cp out wspp.WsppTest Base 3233 diff --git a/java/wspp/WordInfo.java b/java/wspp/WordInfo.java index 522e98e..a5c9e0d 100644 --- a/java/wspp/WordInfo.java +++ b/java/wspp/WordInfo.java @@ -3,4 +3,4 @@ package wspp; public class WordInfo { int count; IntList positions; -} \ No newline at end of file +} diff --git a/java/wspp/WordScanner.java b/java/wspp/WordScanner.java index b13551e..a49c800 100644 --- a/java/wspp/WordScanner.java +++ b/java/wspp/WordScanner.java @@ -7,6 +7,7 @@ public class WordScanner { private BufferedReader br; private String line = null; private int pos = 0; + private int lineNumber = 0; public WordScanner(String fileName) throws IOException { br = new BufferedReader(new InputStreamReader( @@ -16,14 +17,17 @@ public class WordScanner { private boolean hasNextLine() throws IOException { if (line != null && pos < line.length()) return true; line = br.readLine(); + if (line != null) { + lineNumber++; + } pos = 0; return line != null; } private boolean isWordChar(char c) { - return Character.isLetter(c) || c == '\'' || - Character.getType(c) == Character.DASH_PUNCTUATION || - c == '$' || c == '_'; + return Character.isLetter(c) || Character.isDigit(c) || + c == '\'' || c == '$' || c == '_' || + Character.getType(c) == Character.DASH_PUNCTUATION; } public boolean hasNextWord() throws IOException { @@ -51,7 +55,20 @@ public class WordScanner { return line.substring(start, pos).toLowerCase(); } + public int getLineNumber() { + return lineNumber; + } + public void close() throws IOException { br.close(); } + + public void reset(String fileName) throws IOException { + br.close(); + br = new BufferedReader(new InputStreamReader( + new FileInputStream(fileName), StandardCharsets.UTF_8)); + line = null; + pos = 0; + lineNumber = 0; + } } diff --git a/java/wspp/WsppPos.java b/java/wspp/WsppPos.java index debef0d..163468d 100644 --- a/java/wspp/WsppPos.java +++ b/java/wspp/WsppPos.java @@ -1,17 +1,11 @@ -package wspp; - -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStreamWriter; -import java.io.PrintWriter; +import java.io.*; import java.nio.charset.StandardCharsets; -import java.util.LinkedHashMap; -import java.util.Map; +import java.util.*; public class WsppPos { public static void main(String[] args) { if (args.length != 2) { - System.err.println("Usage: java Wspp inputFile outputFile"); + System.err.println("Usage: java WsppPos inputFile outputFile"); return; } @@ -19,42 +13,64 @@ public class WsppPos { String outputFile = args[1]; try { + // Первый проход: считаем количество слов в каждой строке WordScanner scanner = new WordScanner(inputFile); + Map wordsPerLine = new HashMap<>(); + + while (scanner.hasNextWord()) { + scanner.nextWord(); + int line = scanner.getLineNumber(); + wordsPerLine.put(line, wordsPerLine.getOrDefault(line, 0) + 1); + } + scanner.close(); + // Второй проход: собираем статистику + scanner = new WordScanner(inputFile); Map wordMap = new LinkedHashMap<>(); - int wordPosition = 1; + Map currentPosInLine = new HashMap<>(); while (scanner.hasNextWord()) { String word = scanner.nextWord(); - + int lineNum = scanner.getLineNumber(); + + // Позиция с начала строки + int posInLine = currentPosInLine.getOrDefault(lineNum, 0) + 1; + currentPosInLine.put(lineNum, posInLine); + + // Пересчитываем в позицию с конца строки + int totalWordsInLine = wordsPerLine.get(lineNum); + int posFromEnd = totalWordsInLine - posInLine + 1; + if (wordMap.containsKey(word)) { WordInfo info = wordMap.get(word); info.count++; - info.positions.add(wordPosition); + info.lineNumbers.add(lineNum); + info.positions.add(posFromEnd); } else { WordInfo info = new WordInfo(); info.count = 1; + info.lineNumbers = new IntList(); info.positions = new IntList(); - info.positions.add(wordPosition); + info.lineNumbers.add(lineNum); + info.positions.add(posFromEnd); wordMap.put(word, info); } - - wordPosition++; } scanner.close(); + // Запись результата PrintWriter writer = new PrintWriter( - new OutputStreamWriter( - new FileOutputStream(outputFile), StandardCharsets.UTF_8)); + new OutputStreamWriter( + new FileOutputStream(outputFile), StandardCharsets.UTF_8)); for (Map.Entry entry : wordMap.entrySet()) { String word = entry.getKey(); WordInfo info = entry.getValue(); - + writer.print(word + " " + info.count); - for (int i = 0; i < info.positions.size(); i++) { - writer.print(" " + info.positions.get(i)); + for (int i = 0; i < info.lineNumbers.size(); i++) { + writer.print(" " + info.lineNumbers.get(i) + ":" + info.positions.get(i)); } writer.println(); } @@ -65,4 +81,10 @@ public class WsppPos { System.err.println("Error: " + e.getMessage()); } } + + static class WordInfo { + int count; + IntList lineNumbers; + IntList positions; + } }