Skip to content

Instantly share code, notes, and snippets.

@ykrkn
Created July 30, 2019 16:15
Show Gist options
  • Save ykrkn/7492c05cb436bc3eed0ee15ba7263a8f to your computer and use it in GitHub Desktop.
Save ykrkn/7492c05cb436bc3eed0ee15ba7263a8f to your computer and use it in GitHub Desktop.
count words in react project
package sx;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) throws Exception {
Path p = Paths.get(System.getProperty( "user.home" ), "src");
Map<String, Long> count = Files.walk(p)
.filter(Files::isRegularFile)
.filter(Main::filterExt)
.flatMap(Main::lines)
.map(Main::sanitizeLine)
.flatMap(Main::splitLine)
.collect(Collectors.groupingBy((s) -> s.toLowerCase(), Collectors.counting()));
count.entrySet().stream()
.sorted(Main.comparingByValue())
.limit(100)
.forEach(System.out::println);
}
static Comparator<Map.Entry<String, Long>> comparingByValue() {
return (c1, c2) -> c2.getValue().compareTo(c1.getValue());
}
static Stream<String> splitLine(String line) {
return Arrays.stream(line.split(" "))
.filter(s -> !s.isEmpty())
.filter(s -> !s.matches("^[0-9]+$"));
}
static String sanitizeLine(String line) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < line.length(); i++) {
char c = line.charAt(i);
if (0x30 <= c && c <= 0x39) sb.append(c);
else if (0x41 <= c && c <= 0x5a) sb.append(c);
else if (0x61 <= c && c <= 0x7a) sb.append(c);
else if (0x5f == c) sb.append(c);
else sb.append(' ');
}
return sb.toString();
}
static boolean filterExt(Path p) {
String s = p.toString();
if (s.endsWith(".ts")) return true;
if (s.endsWith(".js")) return true;
if (s.endsWith(".tsx")) return true;
if (s.endsWith(".jsx")) return true;
return false;
}
static Stream<String> lines(Path p) {
try {
return Files.readAllLines(p).stream();
} catch (IOException e) {
e.printStackTrace();
return Stream.empty();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment