add solutions for hw6:Base
This commit is contained in:
28
java/wspp/IntList.java
Normal file
28
java/wspp/IntList.java
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package wspp;
|
||||||
|
|
||||||
|
public class IntList {
|
||||||
|
private int[] array;
|
||||||
|
private int size;
|
||||||
|
|
||||||
|
public IntList() {
|
||||||
|
array = new int[10];
|
||||||
|
size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void add(int value) {
|
||||||
|
if (size >= array.length) {
|
||||||
|
int[] newArray = new int[array.length * 2];
|
||||||
|
System.arraycopy(array, 0, newArray, 0, size);
|
||||||
|
array = newArray;
|
||||||
|
}
|
||||||
|
array[size++] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int get(int index) {
|
||||||
|
return array[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
public int size() {
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
}
|
||||||
56
java/wspp/WordScanner.java
Normal file
56
java/wspp/WordScanner.java
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
package wspp;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
public class WordScanner {
|
||||||
|
private BufferedReader br;
|
||||||
|
private String line = null;
|
||||||
|
private int pos = 0;
|
||||||
|
|
||||||
|
public WordScanner(String fileName) throws IOException {
|
||||||
|
br = new BufferedReader(new InputStreamReader(
|
||||||
|
new FileInputStream(fileName), StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean hasNextLine() throws IOException {
|
||||||
|
if (line != null && pos < line.length()) return true;
|
||||||
|
line = br.readLine();
|
||||||
|
pos = 0;
|
||||||
|
return line != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isWordChar(char c) {
|
||||||
|
return Character.isLetter(c) || c == '\'' ||
|
||||||
|
Character.getType(c) == Character.DASH_PUNCTUATION;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasNextWord() throws IOException {
|
||||||
|
while (hasNextLine()) {
|
||||||
|
while (pos < line.length() && !isWordChar(line.charAt(pos))) {
|
||||||
|
pos++;
|
||||||
|
}
|
||||||
|
if (pos < line.length()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String nextWord() {
|
||||||
|
while (pos < line.length() && !isWordChar(line.charAt(pos))) {
|
||||||
|
pos++;
|
||||||
|
}
|
||||||
|
|
||||||
|
int start = pos;
|
||||||
|
while (pos < line.length() && isWordChar(line.charAt(pos))) {
|
||||||
|
pos++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return line.substring(start, pos).toLowerCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void close() throws IOException {
|
||||||
|
br.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
71
java/wspp/Wspp.java
Normal file
71
java/wspp/Wspp.java
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
package wspp;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class Wspp {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
if (args.length != 2) {
|
||||||
|
System.err.println("Usage: java Wspp inputFile outputFile");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String inputFile = args[0];
|
||||||
|
String outputFile = args[1];
|
||||||
|
|
||||||
|
try {
|
||||||
|
WordScanner scanner = new WordScanner(inputFile);
|
||||||
|
|
||||||
|
// LinkedHashMap сохраняет порядок вставки
|
||||||
|
Map<String, WordInfo> wordMap = new LinkedHashMap<>();
|
||||||
|
int wordPosition = 1;
|
||||||
|
|
||||||
|
while (scanner.hasNextWord()) {
|
||||||
|
String word = scanner.nextWord();
|
||||||
|
|
||||||
|
if (wordMap.containsKey(word)) {
|
||||||
|
WordInfo info = wordMap.get(word);
|
||||||
|
info.count++;
|
||||||
|
info.positions.add(wordPosition);
|
||||||
|
} else {
|
||||||
|
WordInfo info = new WordInfo();
|
||||||
|
info.count = 1;
|
||||||
|
info.positions = new IntList();
|
||||||
|
info.positions.add(wordPosition);
|
||||||
|
wordMap.put(word, info);
|
||||||
|
}
|
||||||
|
|
||||||
|
wordPosition++;
|
||||||
|
}
|
||||||
|
|
||||||
|
scanner.close();
|
||||||
|
|
||||||
|
// Запись результата
|
||||||
|
PrintWriter writer = new PrintWriter(
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
writer.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
writer.close();
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Error: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class WordInfo {
|
||||||
|
int count;
|
||||||
|
IntList positions;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user