add solutions for hw6:Base
This commit is contained in:
6
java/wspp/WordInfo.java
Normal file
6
java/wspp/WordInfo.java
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package wspp;
|
||||||
|
|
||||||
|
public class WordInfo {
|
||||||
|
int count;
|
||||||
|
IntList positions;
|
||||||
|
}
|
||||||
@@ -22,7 +22,8 @@ public class WordScanner {
|
|||||||
|
|
||||||
private boolean isWordChar(char c) {
|
private boolean isWordChar(char c) {
|
||||||
return Character.isLetter(c) || c == '\'' ||
|
return Character.isLetter(c) || c == '\'' ||
|
||||||
Character.getType(c) == Character.DASH_PUNCTUATION;
|
Character.getType(c) == Character.DASH_PUNCTUATION ||
|
||||||
|
c == '$' || c == '_';
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasNextWord() throws IOException {
|
public boolean hasNextWord() throws IOException {
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ public class Wspp {
|
|||||||
try {
|
try {
|
||||||
WordScanner scanner = new WordScanner(inputFile);
|
WordScanner scanner = new WordScanner(inputFile);
|
||||||
|
|
||||||
// LinkedHashMap сохраняет порядок вставки
|
|
||||||
Map<String, WordInfo> wordMap = new LinkedHashMap<>();
|
Map<String, WordInfo> wordMap = new LinkedHashMap<>();
|
||||||
int wordPosition = 1;
|
int wordPosition = 1;
|
||||||
|
|
||||||
@@ -41,7 +40,6 @@ public class Wspp {
|
|||||||
|
|
||||||
scanner.close();
|
scanner.close();
|
||||||
|
|
||||||
// Запись результата
|
|
||||||
PrintWriter writer = new PrintWriter(
|
PrintWriter writer = new PrintWriter(
|
||||||
new OutputStreamWriter(
|
new OutputStreamWriter(
|
||||||
new FileOutputStream(outputFile), StandardCharsets.UTF_8));
|
new FileOutputStream(outputFile), StandardCharsets.UTF_8));
|
||||||
@@ -63,9 +61,4 @@ public class Wspp {
|
|||||||
System.err.println("Error: " + e.getMessage());
|
System.err.println("Error: " + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static class WordInfo {
|
|
||||||
int count;
|
|
||||||
IntList positions;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
68
java/wspp/WsppPos.java
Normal file
68
java/wspp/WsppPos.java
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
package wspp;
|
||||||
|
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStreamWriter;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class WsppPos {
|
||||||
|
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);
|
||||||
|
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user