add solution for hw6:3233
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -3,4 +3,4 @@ package wspp;
|
||||
public class WordInfo {
|
||||
int count;
|
||||
IntList positions;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Integer, Integer> 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<String, WordInfo> wordMap = new LinkedHashMap<>();
|
||||
int wordPosition = 1;
|
||||
Map<Integer, Integer> 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<String, WordInfo> 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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user