<commit_before>package edu.jhu.thrax.util;

import java.util.Scanner;
import java.util.HashSet;

import java.io.IOException;
import java.io.File;

import edu.jhu.thrax.ThraxConfig;

public class TestSetFilter
{
    private static HashSet<String> testSentences;

    private static final String NT_REGEX = "\\[[^\\]]+?\\]";

    private static void getTestSentences(String filename) throws IOException
    {
        if (testSentences == null) {
            testSentences = new HashSet<String>();
        }
        Scanner scanner = new Scanner(new File(filename), "UTF-8");
        while (scanner.hasNextLine()) {
            testSentences.add(scanner.nextLine());
        }
    }

    public static String getPattern(String rule)
    {
        String [] parts = rule.split(ThraxConfig.DELIMITER_REGEX);
        if (parts.length != 4) {
            return "NOT-A-RULE";
        }
        String source = parts[1].trim();
        String pattern = source.replaceAll(NT_REGEX, "\\\\E.+\\\\Q");
        pattern = "(.+ )?\\Q" + pattern + "\\E( .+)?";
        return pattern;
    }

    private static boolean inTestSet(String rule)
    {
        String pattern = getPattern(rule);
        if (pattern.equals("NOT-A-RULE")) {
            System.err.printf("malformed rule: %s\n", rule);
            return false;
        }
//        System.err.println("pattern is " + pattern);
        for (String s : testSentences) {
            if (s.matches(pattern)) {
                return true;
            }
        }
        return false;
    }

    public static void main(String [] argv) throws IOException
    {
        // do some setup
        if (argv.length < 1) {
            System.err.println("usage: TestSetFilter <test set1> [test set2 ...]");
            return;
        }
        for (int i = 0; i < argv.length; i++) {
            getTestSentences(argv[i]);
        }

        Scanner scanner = new Scanner(System.in, "UTF-8");
        int rulesIn = 0;
        int rulesOut = 0;<commit_msg>better error handling in test set filter<commit_after>package edu.jhu.thrax.util;

import java.util.Scanner;
import java.util.HashSet;

import java.io.FileNotFoundException;
import java.io.File;

import edu.jhu.thrax.ThraxConfig;

public class TestSetFilter
{
    private static HashSet<String> testSentences;

    private static final String NT_REGEX = "\\[[^\\]]+?\\]";

    private static void getTestSentences(String filename)
    {
        try {
            Scanner scanner = new Scanner(new File(filename), "UTF-8");
            while (scanner.hasNextLine()) {
                testSentences.add(scanner.nextLine());
            }
        }
        catch (FileNotFoundException e) {
            System.err.printf("File not found: %s\n", e.getMessage());
        }
    }

    public static String getPattern(String rule)
    {
        String [] parts = rule.split(ThraxConfig.DELIMITER_REGEX);
        if (parts.length != 4) {
            return "NOT-A-RULE";
        }
        String source = parts[1].trim();
        String pattern = source.replaceAll(NT_REGEX, "\\\\E.+\\\\Q");
        pattern = "(.+ )?\\Q" + pattern + "\\E( .+)?";
        return pattern;
    }

    private static boolean inTestSet(String rule)
    {
        String pattern = getPattern(rule);
        if (pattern.equals("NOT-A-RULE")) {
            System.err.printf("malformed rule: %s\n", rule);
            return false;
        }
//        System.err.println("pattern is " + pattern);
        for (String s : testSentences) {
            if (s.matches(pattern)) {
                return true;
            }
        }
        return false;
    }

    public static void main(String [] argv)
    {
        // do some setup
        if (argv.length < 1) {
            System.err.println("usage: TestSetFilter <test set1> [test set2 ...]");
            return;
        }
        testSentences = new HashSet<String>();
        for (int i = 0; i < argv.length; i++) {
            getTestSentences(argv[i]);
        }

        Scanner scanner = new Scanner(System.in, "UTF-8");
        int rulesIn = 0;
        int rulesOut = 0;
{'fraction_non_alphanumeric': 0.13541921290637987, 'fraction_numerical': 0.0048887802493277925, 'mean_word_length': 1.9228571428571428, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, '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': '6070425', 'n_tokens_mistral': 1264, 'n_tokens_neox': 1213, 'n_words': 287}