Upload files to "java/wspp"
This commit is contained in:
44
java/wspp/IntList.java
Normal file
44
java/wspp/IntList.java
Normal file
@@ -0,0 +1,44 @@
|
||||
package wspp;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* @author Nikita Doschennikov (me@fymio.us)
|
||||
*/
|
||||
public class IntList {
|
||||
|
||||
protected int[] list = new int[8];
|
||||
protected int idx = 0;
|
||||
|
||||
public IntList() {}
|
||||
|
||||
public void put(int val) {
|
||||
if (idx >= list.length) {
|
||||
list = Arrays.copyOf(list, list.length * 2);
|
||||
}
|
||||
|
||||
list[idx++] = val;
|
||||
}
|
||||
|
||||
public int getLength() {
|
||||
return idx;
|
||||
}
|
||||
|
||||
public int get(int index) {
|
||||
return list[index];
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
for (int i = 0; i < idx; i++) {
|
||||
if (i == idx - 1) {
|
||||
sb.append(String.valueOf(list[i]) + "\n");
|
||||
} else {
|
||||
sb.append(String.valueOf(list[i]) + " ");
|
||||
}
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
25
java/wspp/WordInfo.java
Normal file
25
java/wspp/WordInfo.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package wspp;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author Nikita Doschennikov (me@fymio.us)
|
||||
*/
|
||||
public class WordInfo {
|
||||
|
||||
final String word;
|
||||
int firstOccurrence;
|
||||
|
||||
IntList occurrences = new IntList();
|
||||
|
||||
Map<Integer, IntList> lineOccurrences = new HashMap<>();
|
||||
|
||||
public WordInfo(String word) {
|
||||
this.word = word;
|
||||
}
|
||||
|
||||
public WordInfo(String word, int firstOccurrence) {
|
||||
this.word = word;
|
||||
this.firstOccurrence = firstOccurrence;
|
||||
}
|
||||
}
|
||||
80
java/wspp/Wspp.java
Normal file
80
java/wspp/Wspp.java
Normal file
@@ -0,0 +1,80 @@
|
||||
package wspp;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author Nikita Doschennikov (me@fymio.us)
|
||||
*/
|
||||
public class Wspp {
|
||||
|
||||
public static void main(String[] args) {
|
||||
if (args.length != 2) {
|
||||
System.err.println(
|
||||
"usage: java Wspp <inputFilePath> <outputFilePath>"
|
||||
);
|
||||
}
|
||||
|
||||
final String inputFileName = args[0];
|
||||
final String outputFileName = args[1];
|
||||
|
||||
Map<String, WordInfo> words = new LinkedHashMap<>();
|
||||
|
||||
try (
|
||||
BufferedReader br = new BufferedReader(
|
||||
new FileReader(inputFileName)
|
||||
);
|
||||
FileWriter fw = new FileWriter(outputFileName)
|
||||
) {
|
||||
String line;
|
||||
int wordPos = 1;
|
||||
while ((line = br.readLine()) != null) {
|
||||
line = line.toLowerCase();
|
||||
StringBuilder word = new StringBuilder();
|
||||
|
||||
for (char c : line.toCharArray()) {
|
||||
if (
|
||||
Character.isLetter(c) ||
|
||||
c == '\'' ||
|
||||
Character.getType(c) == Character.DASH_PUNCTUATION
|
||||
) {
|
||||
word.append(c);
|
||||
} else {
|
||||
if (!word.isEmpty()) {
|
||||
if (words.containsKey(word.toString())) {
|
||||
words
|
||||
.get(word.toString())
|
||||
.occurrences.put(wordPos++);
|
||||
} else {
|
||||
WordInfo info = new WordInfo(word.toString());
|
||||
info.occurrences.put(wordPos++);
|
||||
words.put(word.toString(), info);
|
||||
}
|
||||
}
|
||||
|
||||
word = new StringBuilder();
|
||||
}
|
||||
}
|
||||
|
||||
if (!word.isEmpty()) {
|
||||
if (words.containsKey(word.toString())) {
|
||||
words.get(word.toString()).occurrences.put(wordPos++);
|
||||
} else {
|
||||
WordInfo info = new WordInfo(word.toString());
|
||||
info.occurrences.put(wordPos++);
|
||||
words.put(word.toString(), info);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (String word : words.keySet()) {
|
||||
WordInfo info = words.get(word);
|
||||
int count = info.occurrences.getLength();
|
||||
String occurencies = info.occurrences.toString();
|
||||
fw.write(word + " " + count + " " + occurencies);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error reading file.");
|
||||
}
|
||||
}
|
||||
}
|
||||
127
java/wspp/WsppLast.java
Normal file
127
java/wspp/WsppLast.java
Normal file
@@ -0,0 +1,127 @@
|
||||
package wspp;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author Nikita Doschennikov (me@fymio.us)
|
||||
*/
|
||||
public class WsppLast {
|
||||
|
||||
public static void main(String[] args) {
|
||||
if (args.length != 2) {
|
||||
System.err.println(
|
||||
"usage: java WsppPosition <inputFilePath> <outputFilePath>"
|
||||
);
|
||||
}
|
||||
|
||||
final String inputFileName = args[0];
|
||||
final String outputFileName = args[1];
|
||||
|
||||
Map<String, WordInfo> words = new LinkedHashMap<>();
|
||||
|
||||
try (
|
||||
BufferedReader br = new BufferedReader(
|
||||
new FileReader(inputFileName)
|
||||
);
|
||||
FileWriter fw = new FileWriter(outputFileName)
|
||||
) {
|
||||
String line;
|
||||
int wordPos = 1;
|
||||
int lineNumber = 1;
|
||||
while ((line = br.readLine()) != null) {
|
||||
line = line.toLowerCase();
|
||||
StringBuilder word = new StringBuilder();
|
||||
|
||||
for (char c : line.toCharArray()) {
|
||||
if (
|
||||
Character.isLetter(c) ||
|
||||
c == '\'' ||
|
||||
Character.getType(c) == Character.DASH_PUNCTUATION ||
|
||||
Character.isDigit(c) ||
|
||||
c == '$' ||
|
||||
c == '_'
|
||||
) {
|
||||
word.append(c);
|
||||
} else {
|
||||
if (!word.isEmpty()) {
|
||||
if (words.containsKey(word.toString())) {
|
||||
var lO = words.get(
|
||||
word.toString()
|
||||
).lineOccurrences;
|
||||
if (lO.containsKey(lineNumber)) {
|
||||
lO.get(lineNumber).put(wordPos++);
|
||||
} else {
|
||||
var intList = new IntList();
|
||||
intList.put(wordPos++);
|
||||
lO.put(lineNumber, intList);
|
||||
}
|
||||
} else {
|
||||
WordInfo info = new WordInfo(
|
||||
word.toString(),
|
||||
wordPos
|
||||
);
|
||||
var intList = new IntList();
|
||||
intList.put(wordPos++);
|
||||
info.lineOccurrences.put(lineNumber, intList);
|
||||
words.put(word.toString(), info);
|
||||
}
|
||||
}
|
||||
|
||||
word = new StringBuilder();
|
||||
}
|
||||
}
|
||||
|
||||
if (!word.isEmpty()) {
|
||||
if (words.containsKey(word.toString())) {
|
||||
var lO = words.get(word.toString()).lineOccurrences;
|
||||
if (lO.containsKey(lineNumber)) {
|
||||
lO.get(lineNumber).put(wordPos++);
|
||||
} else {
|
||||
var intList = new IntList();
|
||||
intList.put(wordPos++);
|
||||
lO.put(lineNumber, intList);
|
||||
}
|
||||
} else {
|
||||
WordInfo info = new WordInfo(word.toString(), wordPos);
|
||||
var intList = new IntList();
|
||||
intList.put(wordPos++);
|
||||
info.lineOccurrences.put(lineNumber, intList);
|
||||
words.put(word.toString(), info);
|
||||
}
|
||||
}
|
||||
|
||||
lineNumber++;
|
||||
}
|
||||
|
||||
List<WordInfo> sortedWords = new ArrayList<>(words.values());
|
||||
sortedWords.sort(
|
||||
Comparator.comparingInt((WordInfo w) ->
|
||||
w.word.length()
|
||||
).thenComparingInt(w -> w.firstOccurrence)
|
||||
);
|
||||
|
||||
for (WordInfo info : sortedWords) {
|
||||
int totalNumberOfOccurrences = 0;
|
||||
var lO = info.lineOccurrences;
|
||||
String word = info.word;
|
||||
for (int key : lO.keySet()) {
|
||||
totalNumberOfOccurrences += lO.get(key).getLength();
|
||||
}
|
||||
|
||||
fw.write(word + " " + totalNumberOfOccurrences);
|
||||
|
||||
for (int key : lO.keySet()) {
|
||||
var occurrences = lO.get(key);
|
||||
|
||||
fw.write(
|
||||
" " + occurrences.get(occurrences.getLength() - 1)
|
||||
);
|
||||
}
|
||||
fw.write("\n");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error reading file.");
|
||||
}
|
||||
}
|
||||
}
|
||||
7
java/wspp/package-info.java
Normal file
7
java/wspp/package-info.java
Normal file
@@ -0,0 +1,7 @@
|
||||
/**
|
||||
* Tests for <a href="https://www.kgeorgiy.info/courses/prog-intro/homeworks.html#wspp">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)
|
||||
*/
|
||||
package wspp;
|
||||
Reference in New Issue
Block a user