<commit_before>package com.github.jleyba.dossier;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.Iterables.all;
import static com.google.common.collect.Iterables.isEmpty;
import static com.google.common.collect.Iterables.transform;
import static com.google.common.collect.Lists.newArrayList;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Ordering;

import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * Utilities for working with {@link Path paths}.
 */
class Paths {
  private Paths() {}

  /**
   * Computes the relative path {@code from} one path {@code to} another. The
   * origin path is assumed to be a file.
   */
  static Path getRelativePath(Path from, Path to) {
    from = from.toAbsolutePath().getParent();
    to = to.toAbsolutePath();

    Path root = getCommonPrefix(ImmutableSet.<Path>of(from, to));
    Path pathToRoot = from.relativize(root);
    Path pathFromRoot = root.relativize(to);

    return pathToRoot.resolve(pathFromRoot).normalize();
  }

  /**
   * Returns the {@link Path} that represents the longest common prefix for the provided
   * {@code paths}. All paths will be resolved and normalized relative to the current working
   * directory before computing a common prefix.
   */
  static Path getCommonPrefix(Iterable<Path> paths) {
    Path pwd = new File(".").getAbsoluteFile().toPath();
    return getCommonPrefix(pwd, paths);
  }

  /**
   * Returns the {@link Path} that represents the longest common prefix for the provided
   * {@code paths}. All paths will be resolved and normalized relative to the given {@code root}
   * directory before computing a common prefix.
   *
   * <p>If all of the provided {@code paths} do not designate
   */
  static Path getCommonPrefix(Path root, Iterable<Path> paths) {
    if (isEmpty(paths)) {
      return root;
    }
    root = root.toFile().getAbsoluteFile().toPath();
    paths = transform(paths, normalizeRelativeTo(root));

    Path prefix = root.getRoot();
    Path shortest = Ordering.from(length()).min(paths);
    Iterator<Path> parts = shortest.iterator();
    while (parts.hasNext()) {
      Path possiblePrefix = prefix.resolve(parts.next());<commit_msg>Clean up path handling code.<commit_after>package com.github.jleyba.dossier;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.Iterables.all;
import static com.google.common.collect.Iterables.isEmpty;
import static com.google.common.collect.Iterables.transform;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Ordering;

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

/**
 * Utilities for working with {@link Path paths}.
 */
class Paths {
  private Paths() {}

  /**
   * Computes the relative path {@code from} one path {@code to} another. The
   * origin path is assumed to be a file.
   */
  static Path getRelativePath(Path from, Path to) {
    from = from.toAbsolutePath().getParent();
    to = to.toAbsolutePath();

    Path root = getCommonPrefix(ImmutableSet.<Path>of(from, to));
    Path pathToRoot = from.relativize(root);
    Path pathFromRoot = root.relativize(to);

    return pathToRoot.resolve(pathFromRoot).normalize();
  }

  /**
   * Returns the {@link Path} that represents the longest common prefix for the provided
   * {@code paths}. All paths will be resolved and normalized relative to the current working
   * directory before computing a common prefix.
   */
  static Path getCommonPrefix(Iterable<Path> paths) {
    Path pwd = FileSystems.getDefault().getPath("").toAbsolutePath();
    return getCommonPrefix(pwd, paths);
  }

  /**
   * Returns the {@link Path} that represents the longest common prefix for the provided
   * {@code paths}. All paths will be resolved and normalized relative to the given {@code root}
   * directory before computing a common prefix.
   *
   * <p>If all of the provided {@code paths} do not designate
   */
  static Path getCommonPrefix(Path root, Iterable<Path> paths) {
    if (isEmpty(paths)) {
      return root;
    }
    root = root.toAbsolutePath();
    paths = transform(paths, normalizeRelativeTo(root));

    Path prefix = root.getRoot();
    Path shortest = Ordering.from(length()).min(paths);
    for (Path part : shortest) {
      Path possiblePrefix = prefix.resolve(part);
{'fraction_non_alphanumeric': 0.11416781292984869, 'fraction_numerical': 0.0, 'mean_word_length': 4.069721115537849, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4621018', 'n_tokens_mistral': 1478, 'n_tokens_neox': 1454, 'n_words': 430}