package cn.edu.bjut.textmining.chapter7; import java.io.BufferedReader; import java.io.IOException; import java.io.UncheckedIOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; /** * 第7章数据与输出路径工具。 */ public final class Chapter7Data { private Chapter7Data() { } public static Path dataPath(String name) { return Paths.get("data", "chapter7", name); } public static Path outputPath(String name) { try { Path directory = Paths.get("output", "chapter7"); Files.createDirectories(directory); return directory.resolve(name); } catch (IOException e) { throw new UncheckedIOException("创建输出目录失败", e); } } public static String display(Path path) { return path.normalize().toString().replace('\\', '/'); } public static List readNonBlankLines(String name) { try { List lines = new ArrayList(); try (BufferedReader reader = Files.newBufferedReader(dataPath(name), StandardCharsets.UTF_8)) { String line; while ((line = reader.readLine()) != null) { line = line.trim(); if (!line.isEmpty() && !line.startsWith("#")) { lines.add(line); } } } return lines; } catch (IOException e) { throw new UncheckedIOException("读取第7章数据失败", e); } } }