<commit_before>package nl.bvs.srtfixer;

import nl.bvs.srtfixer.util.Constants;
import nl.bvs.srtfixer.util.FileFinder;

import java.io.*;
import java.util.Arrays;
import java.util.List;

/**
 * Rather hacky code to fix subtitle files, but it works well enough. It is
 * tailored to my TV, streaming apps and personal preference. It performs fine,
 * so I can't be bothered with optimising the code.
 *
 * SRT files are often ripped from images by OCR software. A lot of the time,
 * the upper case 'I' and lower case 'l' are mixed up by such processes. This
 * can cause annoyance when reading the subtitles.
 *
 * This little class tries to fix many of these issues. And usually it fixes way
 * more than it breaks, so yay!
 */
public class SrtFileFixer extends BaseFixer {
    /** Lines can start with various things such as dashes or quotes. */
    private static final String[] LINE_STARTS = {"", "\"", "-", "--", " -", " --", "- ", "-- ", " - ", " -- ",
            "-\"", "--\"", " -\"", " --\"", "- \"", "-- \"", " - \"", " -- \"", "[", " [", " [ ", "(", " (", " ( "};

    /** Some words to ignore while fixing. First 4 are the only English words that start with a double l. Wtf is a llano? */
    private static final List<String> IGNORE_LIST = Arrays.asList("llama", "llamas", "llano", "llanos", "llorar");

    /** Some words are wrongfully fixed. Starting with some Roman numerals. */
    private static final String[][] FIX_LIST = {{"ll", "II"}, {"Il", "II"}, {"IlI", "III"}, {"IlI:", "III:"}, {"Vll", "VII"}, {"VIlI", "VIII"},
            {"VllI", "VIII"}, {"Xll", "XII"}, {"XIlI", "XIII"}, {"XVll", "XVII"}, {"XVIlI", "XVIII"}, {"Nll", "NII"}, {"XXllI", "XXIII"},
            {"d'lsere", "d'Isere"}, {"lemand", "Iemand"}, {"Iets", "lets"}, {"ledere", "Iedere"}, {"Gls", "GIs"},
            {"KEllCHI", "KEIICHI"}, {"leyasu", "Ieyasu"}};

    /**
     * Go go gadget SrtFileFixer.
     * @param args if you enjoy passing about arguments that are never used, this is the place to do it!
     */
    public static void main(final String[] args) {
        System.out.println("Processing directory :: " + Constants.FILEFIXER_ROOT_DIR);
        new SrtFileFixer().process();
    }

    @Override
    protected void process() {
        // collect all srt files in the root dir
        final List<File> srtFiles = new FileFinder().collect(new File(Constants.FILEFIXER_ROOT_DIR), Constants.SRT_EXTENSION);

        // process each file, making a backup first if it is enabled
        for (final File srtFile : srtFiles) {
            System.out.println("Processing subtitle file :: " + srtFile.getName());

            if (Constants.MAKE_BACKUPS) {
                backupFile(srtFile);
            }

            fix(srtFile);
        }
    }

    /**
     * Tries to fix as many issues in the file, hopefully without introducing any new problems.
     */
    @Override
    protected void fixFile(final BufferedReader reader, final BufferedWriter writer) throws Exception {
        // init
        int lineCounter = 1;
        boolean lastLineWasEmpty = false;

        String line;
        while ((line = reader.readLine()) != null) {
            final String fixedLine = fixLine(line);

            if (fixedLine != null && !"".equals(fixedLine.trim())) {
                if (lastLineWasEmpty && lineIsNumberAboveZero(fixedLine)) {
                    writer.newLine();
                    lineCounter++;
                    writer.write(lineCounter);
                } else {
                    lastLineWasEmpty = false;
                    writer.write(fixedLine);
                }
                writer.newLine();
            } else {
                lastLineWasEmpty = true;
            }
        }
    }
<commit_msg>Seems like I broke the counter. It's fixed now.<commit_after>package nl.bvs.srtfixer;

import nl.bvs.srtfixer.util.Constants;
import nl.bvs.srtfixer.util.FileFinder;

import java.io.*;
import java.util.Arrays;
import java.util.List;

/**
 * Rather hacky code to fix subtitle files, but it works well enough. It is
 * tailored to my TV, streaming apps and personal preference. It performs fine,
 * so I can't be bothered with optimising the code.
 *
 * SRT files are often ripped from images by OCR software. A lot of the time,
 * the upper case 'I' and lower case 'l' are mixed up by such processes. This
 * can cause annoyance when reading the subtitles.
 *
 * This little class tries to fix many of these issues. And usually it fixes way
 * more than it breaks, so yay!
 */
public class SrtFileFixer extends BaseFixer {
    /** Lines can start with various things such as dashes or quotes. */
    private static final String[] LINE_STARTS = {"", "\"", "-", "--", " -", " --", "- ", "-- ", " - ", " -- ",
            "-\"", "--\"", " -\"", " --\"", "- \"", "-- \"", " - \"", " -- \"", "[", " [", " [ ", "(", " (", " ( "};

    /** Some words to ignore while fixing. First 4 are the only English words that start with a double l. Wtf is a llano? */
    private static final List<String> IGNORE_LIST = Arrays.asList("llama", "llamas", "llano", "llanos", "llorar");

    /** Some words are wrongfully fixed. Starting with some Roman numerals. */
    private static final String[][] FIX_LIST = {{"ll", "II"}, {"Il", "II"}, {"IlI", "III"}, {"IlI:", "III:"}, {"Vll", "VII"}, {"VIlI", "VIII"},
            {"VllI", "VIII"}, {"Xll", "XII"}, {"XIlI", "XIII"}, {"XVll", "XVII"}, {"XVIlI", "XVIII"}, {"Nll", "NII"}, {"XXllI", "XXIII"},
            {"d'lsere", "d'Isere"}, {"lemand", "Iemand"}, {"Iets", "lets"}, {"ledere", "Iedere"}, {"Gls", "GIs"},
            {"KEllCHI", "KEIICHI"}, {"leyasu", "Ieyasu"}};

    /**
     * Go go gadget SrtFileFixer.
     * @param args if you enjoy passing about arguments that are never used, this is the place to do it!
     */
    public static void main(final String[] args) {
        System.out.println("Processing directory :: " + Constants.FILEFIXER_ROOT_DIR);
        new SrtFileFixer().process();
    }

    @Override
    protected void process() {
        // collect all srt files in the root dir
        final List<File> srtFiles = new FileFinder().collect(new File(Constants.FILEFIXER_ROOT_DIR), Constants.SRT_EXTENSION);

        // process each file, making a backup first if it is enabled
        for (final File srtFile : srtFiles) {
            System.out.println("Processing subtitle file :: " + srtFile.getName());

            if (Constants.MAKE_BACKUPS) {
                backupFile(srtFile);
            }

            fix(srtFile);
        }
    }

    /**
     * Tries to fix as many issues in the file, hopefully without introducing any new problems.
     */
    @Override
    protected void fixFile(final BufferedReader reader, final BufferedWriter writer) throws Exception {
        // init
        int lineCounter = 1;
        boolean lastLineWasEmpty = false;

        String line;
        while ((line = reader.readLine()) != null) {
            final String fixedLine = fixLine(line);

            if (fixedLine != null && !"".equals(fixedLine.trim())) {
                if (lastLineWasEmpty && lineIsNumberAboveZero(fixedLine)) {
                    writer.newLine();
                    lineCounter++;
                    writer.write("" + lineCounter);
                } else {
                    lastLineWasEmpty = false;
                    writer.write(fixedLine);
                }
                writer.newLine();
            } else {
                lastLineWasEmpty = true;
            }
        }
    }

{'fraction_non_alphanumeric': 0.15376415722851433, 'fraction_numerical': 0.0005329780146568954, 'mean_word_length': 2.316836058329651, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '4151792', 'n_tokens_mistral': 2269, 'n_tokens_neox': 2209, 'n_words': 713}