Upload files to "java/wordStat"
This commit is contained in:
117
java/wordStat/WordStatLengthAffix.java
Normal file
117
java/wordStat/WordStatLengthAffix.java
Normal file
@@ -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 <inputFile> <outputFile>"
|
||||
);
|
||||
}
|
||||
|
||||
String inputFileName = args[0];
|
||||
String outputFileName = args[1];
|
||||
try {
|
||||
BufferedReader r = new BufferedReader(
|
||||
new FileReader(inputFileName)
|
||||
);
|
||||
|
||||
Map<String, WordInfo> 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<WordInfo> 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
93
java/wordStat/WordStatLengthMiddle.java
Normal file
93
java/wordStat/WordStatLengthMiddle.java
Normal file
@@ -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 <inputFile> <outputFile>"
|
||||
);
|
||||
}
|
||||
|
||||
String inputFileName = args[0];
|
||||
String outputFileName = args[1];
|
||||
try {
|
||||
BufferedReader r = new BufferedReader(
|
||||
new FileReader(inputFileName)
|
||||
);
|
||||
|
||||
Map<String, WordInfo> 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<WordInfo> 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
96
java/wordStat/WordStatLengthPrefix.java
Normal file
96
java/wordStat/WordStatLengthPrefix.java
Normal file
@@ -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 <inputFile> <outputFile>"
|
||||
);
|
||||
}
|
||||
|
||||
String inputFileName = args[0];
|
||||
String outputFileName = args[1];
|
||||
try {
|
||||
BufferedReader r = new BufferedReader(
|
||||
new FileReader(inputFileName)
|
||||
);
|
||||
|
||||
Map<String, WordInfo> 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<WordInfo> 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
95
java/wordStat/WordStatLengthSuffix.java
Normal file
95
java/wordStat/WordStatLengthSuffix.java
Normal file
@@ -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 <inputFile> <outputFile>"
|
||||
);
|
||||
}
|
||||
|
||||
String inputFileName = args[0];
|
||||
String outputFileName = args[1];
|
||||
try {
|
||||
BufferedReader r = new BufferedReader(
|
||||
new FileReader(inputFileName)
|
||||
);
|
||||
|
||||
Map<String, WordInfo> 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<WordInfo> 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
91
java/wordStat/WordStatTest.java
Normal file
91
java/wordStat/WordStatTest.java
Normal file
@@ -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 <a href="https://www.kgeorgiy.info/courses/prog-intro/homeworks.html#wordstat">Word Statistics</a> homework
|
||||
* of <a href="https://www.kgeorgiy.info/courses/prog-intro/">Introduction to Programming</a> course.
|
||||
*
|
||||
* @author Georgiy Korneev (kgeorgiy@kgeorgiy.info)
|
||||
*/
|
||||
public final class WordStatTest {
|
||||
|
||||
// === Base
|
||||
private static final Named<Function<String, Stream<String>>> 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<Function<String, Stream<String>>> MIDDLE = size(
|
||||
"Middle",
|
||||
SIZE * 2 + 1,
|
||||
s -> Stream.of(s.substring(SIZE, s.length() - SIZE))
|
||||
);
|
||||
|
||||
static Named<Function<String, Stream<String>>> size(
|
||||
final String name,
|
||||
final int length,
|
||||
final Function<String, Stream<String>> f
|
||||
) {
|
||||
return Named.of(name, s ->
|
||||
s.length() >= length ? f.apply(s) : Stream.empty()
|
||||
);
|
||||
}
|
||||
|
||||
// === 3839
|
||||
private static final Named<Function<String, Stream<String>>> 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<Function<String, Stream<String>>> SUFFIX = size(
|
||||
"Suffix",
|
||||
2,
|
||||
s -> Stream.of(s.substring(s.length() - s.length() / 2))
|
||||
);
|
||||
|
||||
// === 4749
|
||||
private static final Named<Function<String, Stream<String>>> 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user