<commit_before>#!/usr/bin/env python3
# -*- coding: utf-8 -*-
try:
    import pyanisort.utilities as utilities
except ImportError:
    import utilities
    
import xml.etree.ElementTree as ET
import re
import difflib
import csv
import sys
import os
import io
import copy
import errno
import logging

logger = logging.getLogger(__name__)

# Set regex to a regex string or an array of regex strings to loop through
# Group one must be show name group two must be episode number
# Otherwise it uses the ones I made
# Matches Endings and openings
#'(?i)(?:[^]]*\][ _.]?)((?:(?!-?[ _.](?:En?D(?:ing)?|OP(?:ening)?)[ _.\d])[\w\s_-])+)(?:(?!\d|ED|OP).)*(OP(?:ening)?[ _.]?(?:\d{1,2})?|En?D(?:ing)?[ _.]?(?:\d{1,2})?)'
# Matches Files
#'(?i)(?:[^]]*\][ _.]?)((?:(?!-?[ _.]e?p?(?:isode[ _.]?)?\d{2,3})[!@#$%^&*()\\?<>;:\'\"{}\[\]|~`+=\w\s_-])+)(?:(?!\d).)*(\d{2,3})'
#(!@#$%^&*()\?<>;:'"{}[]|~`+=) all of these are valid characters on linux systems included for completion
# takes a filename and returns an array with the show name and episode number
# returns [show, ep]
def parseFilename(filename ,regex=None):
    path, file = os.path.split(filename)
    if (regex is None):
        regex = [
            "(?i)(?:[^]]*\][ _.]?)((?:(?!-?[ _.](?:En?D(?:ing)?|OP(?:ening)?)[ _.\d])[!@#$%^&*()\\?<>;:\'\"{}\[\]|~`+=\w\s_-])+)(?:(?!\d|ED|OP).)*(OP(?:ening)?[ _.]?(?:\d{1,2})?|En?D(?:ing)?[ _.]?(?:\d{1,2})?)",
            "(?i)(?:[^]]*\][ _.]?)((?:(?!-?[ _.]e?p?(?:isode[ _.]?)?\d{2,3})[!@#$%^&*()\\?<>;:\'\"{}\[\]|~`+=\w\s_-])+)(?:(?!\d).)*(\d{2,3})"
            ]
    else:
        if type(regex) is list:
            regex = regex
        elif type(regex) is str:
            regex = [regex]
        else:
            return
    index = 0
    while index < len(regex):
        reg = regex[index]
        m = re.match(reg, file)
        try:
            show = m.group(1)
            ep = m.group(2)
            break
        except AttributeError as e:
            logger.debug("{0}: Could not find match in file '{1}'".format(reg, file))
            if index < len(regex):
                index += 1
    else:
        logger.debug("Could not find match in file '{1}'".format(reg, file))
        return

    show = re.sub('[_.]', ' ', show)
    show = show.rstrip() # remove trailing spaces
    logger.debug("Found match in file '{0}': ({1}, {2})".format(file, show, ep))
    return [show, ep]

# set precision to a float between 0 and 1
# root is the root tag of the xmlfile
# the closer to 0 the less precise the match
# returns [[aid, title], [aid, title] ... ]
def findShowMatches(findMatch, root, precision=.95):<commit_msg>Modified regex to be more accurate<commit_after>#!/usr/bin/env python3
# -*- coding: utf-8 -*-
try:
    import pyanisort.utilities as utilities
except ImportError:
    import utilities
    
import xml.etree.ElementTree as ET
import re
import difflib
import csv
import sys
import os
import io
import copy
import errno
import logging

logger = logging.getLogger(__name__)

# Set regex to a regex string or an array of regex strings to loop through
# when creating new regex group one must be show name group two must be episode number
#(!@#$%^&*()\?<>;:'"{}[]|~`+=) all of these are valid characters on linux systems included for completion
# returns [show, ep]
def parseFilename(filename ,regex=None):
    path, file = os.path.split(filename)
    if (regex is None):
        regex = [
            "(?i)(?:[^]]*\][ _.]?)((?:(?!-?[ _.](?:En?D(?:ing)?|OP(?:ening)?|E?P?(?:isode[ _.]?)?\d{2,3}))[!@#$%^&*()\\?<>;:\'\"{}\[\]|~`+=\w\s._-])+)(?:(?!\d|En?D(?:ing)?|OP(?:ening)?|\[[\dA-F]{8}\]).)*(\d{2,3})", # Matches Show and episode (Requires Sub Group for Accuracy)
            "(?i)(?:[^]]*\][ _.]?)((?:(?!-?[ _.](?:En?D(?:ing)?|OP(?:ening)?|E?P?(?:isode[ _.]?)?\d{2,3}))[!@#$%^&*()\\?<>;:\'\"{}\[\]|~`+=\w\s._-])+)(?:(?!En?D(?:ing)?|OP(?:ening)?|\[[\dA-F]{8}\]).)*(OP(?:ening)?[ _.]?(?:\d{1,2})?|En?D(?:ing)?[ _.]?(?:\d{1,2})?)", # Matches Opening and Endings (Requires Sub Group for Accuracy)
            "(?i)((?:(?!-?[ _.](?:En?D(?:ing)?|OP(?:ening)?|E?P?(?:isode[ _.]?)?\d{2,3}))[!@#$%^&*()\\?<>;:\'\"{}\[\]|~`+=\w\s._-])+)(?:(?!\d|En?D(?:ing)?|OP(?:ening)?|\[[\dA-F]{8}\]).)*(\d{2,3})",# Matches Show and episode (Doesn't require sub group)
            "(?i)(?:[^]]*\][ _.]?)((?:(?!-?[ _.](?:En?D(?:ing)?|OP(?:ening)?|E?P?(?:isode[ _.]?)?\d{2,3}))[!@#$%^&*()\\?<>;:\'\"{}\[\]|~`+=\w\s._-])+)(?:(?!En?D(?:ing)?|OP(?:ening)?|\[[\dA-F]{8}\]).)*(OP(?:ening)?[ _.]?(?:\d{1,2})?|En?D(?:ing)?[ _.]?(?:\d{1,2})?)" # Matches Opening and Endings (Doesn't require sub group)
            ]
    else:
        if type(regex) is list:
            regex = regex
        elif type(regex) is str:
            regex = [regex]
        else:
            return
    index = 0
    while index < len(regex):
        reg = regex[index]
        m = re.match(reg, file)
        try:
            show = m.group(1)
            ep = m.group(2)
            break
        except AttributeError as e:
            logger.debug("Regex {0}: Could not find match in file '{1}'".format(index, file))
            if index < len(regex):
                index += 1
    else:
        logger.debug("Could not find match in file '{1}'".format(reg, file))
        return

    show = re.sub('[_.]', ' ', show)
    show = show.rstrip() # remove trailing spaces
    logger.debug("Regex {0}: Found match in file '{1}': ({2}, {3})".format(index, file, show, ep))
    return [show, ep]

# set precision to a float between 0 and 1
# root is the root tag of the xmlfile
# the closer to 0 the less precise the match
# returns [[aid, title], [aid, title] ... ]
def findShowMatches(findMatch, root, precision=.9):
{'fraction_non_alphanumeric': 0.27592886071491457, 'fraction_numerical': 0.013030463109702413, 'mean_word_length': 3.0141342756183747, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '2232372', 'n_tokens_mistral': 2172, 'n_tokens_neox': 2111, 'n_words': 513}