Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,5 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

View file

@ -0,0 +1,36 @@
public static void main(String[] args) throws IOException {
Map<Integer, Integer> frequencies = frequencies("src/LetterFrequency.java");
System.out.println(print(frequencies));
}
static String print(Map<Integer, Integer> frequencies) {
StringBuilder string = new StringBuilder();
int key;
for (Map.Entry<Integer, Integer> entry : frequencies.entrySet()) {
key = entry.getKey();
string.append("%,-8d".formatted(entry.getValue()));
/* display the hexadecimal value for non-printable characters */
if ((key >= 0 && key < 32) || key == 127) {
string.append("%02x%n".formatted(key));
} else {
string.append("%s%n".formatted((char) key));
}
}
return string.toString();
}
static Map<Integer, Integer> frequencies(String path) throws IOException {
try (InputStreamReader reader = new InputStreamReader(new FileInputStream(path))) {
/* key = character, and value = occurrences */
Map<Integer, Integer> map = new HashMap<>();
int value;
while ((value = reader.read()) != -1) {
if (map.containsKey(value)) {
map.put(value, map.get(value) + 1);
} else {
map.put(value, 1);
}
}
return map;
}
}

View file

@ -0,0 +1,26 @@
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
public class LetterFreq {
public static int[] countLetters(String filename) throws IOException{
int[] freqs = new int[26];
BufferedReader in = new BufferedReader(new FileReader(filename));
String line;
while((line = in.readLine()) != null){
line = line.toUpperCase();
for(char ch:line.toCharArray()){
if(Character.isLetter(ch)){
freqs[ch - 'A']++;
}
}
}
in.close();
return freqs;
}
public static void main(String[] args) throws IOException{
System.out.println(Arrays.toString(countLetters("filename.txt")));
}
}

View file

@ -0,0 +1,15 @@
public static int[] countLetters(String filename) throws IOException{
int[] freqs = new int[26];
try(BufferedReader in = new BufferedReader(new FileReader(filename))){
String line;
while((line = in.readLine()) != null){
line = line.toUpperCase();
for(char ch:line.toCharArray()){
if(Character.isLetter(ch)){
freqs[ch - 'A']++;
}
}
}
}
return freqs;
}

View file

@ -0,0 +1,7 @@
public static Map<Integer, Long> countLetters(String filename) throws IOException {
return Files.lines(Paths.get(filename))
.flatMapToInt(String::chars)
.filter(Character::isLetter)
.boxed()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
}