<commit_before>﻿using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace anime_downloader.Classes
{
    public static class Methods
    {
        /// <summary>
        ///     Compute the distance between two strings.
        /// </summary>
        public static int LevenshteinDistance(string s, string t)
        {
            var n = s.Length;
            var m = t.Length;
            var d = new int[n + 1, m + 1];
            if (n == 0)
                return m;
            if (m == 0)
                return n;
            for (var i = 0; i <= n; d[i, 0] = i++)
            { }
            for (var j = 0; j <= m; d[0, j] = j++)
            { }
            for (var i = 1; i <= n; i++)
            {
                for (var j = 1; j <= m; j++)
                {
                    var cost = t[j - 1] == s[i - 1] ? 0 : 1;
                    d[i, j] = Math.Min(
                        Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1),
                        d[i - 1, j - 1] + cost);
                }
            }
            return d[n, m];
        }

        /// <summary>
        ///     Strip the entire path of extraneous information (subgroups, resolution, etc).
        /// </summary>
        /// <param name="filename">
        /// A file name, not a filepath.
        /// </param>
        /// <param name="removeEpisode">
        ///  A flag for also removing the episode number
        /// </param>
        public static string Strip(string filename, bool removeEpisode = false)
        {
            var text = filename;

            // Remove brackets and parenthesis
            var phrases = Regex.Matches(text, @"\s?\[(.*?)\]|\((.*?)\)\s*")
                .Cast<Match>()
                .Select(match => match.Groups[0].Value)
                .ToList();
            text = new[] { ".mkv", ".mp4", ".avi" }.Union(phrases).Aggregate(text, (current, s) => current.Replace(s, ""));

            // _ and . can be used for spaces for some subgroups, replace with spaces instead
            text = new[] {"_", "."}.Aggregate(text, (current, s) => current.Replace(s, " "));

            if (removeEpisode)
            {
                var regularEpisodePattern = Regex.Matches(text, @"\-\s[0-9]{1,}\s");            // Name {- #}
                var namedEpisodePattern = Regex.Matches(text, @"[e|E]pisode\s[0-9]{1,}");       // Name {Episode #}

                if (regularEpisodePattern.Count > 0)
                {
                    var split = text.Split('-');
                    text = string.Join("-", split.Take(split.Length - 1));
                }

                else if (namedEpisodePattern.Count > 0)
                {
                    var value = namedEpisodePattern.Cast<Match>().Select(match => match.Groups[0].Value).ToList().First();
                    text = text.Replace(value, "");
                }
                
            }

            return Regex.Replace(text.Trim(), @"\s+", " ");
        }

        /// <summary>
        ///     Completely clear focus from an element.
        /// </summary>
        public static void ClearFocusFrom(FrameworkElement element)
        {
            var parent = (FrameworkElement) element.Parent;
            while (parent != null && !((IInputElement) parent).Focusable)<commit_msg>Fix regex pattern for stripping episode from nyaa<commit_after>﻿using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace anime_downloader.Classes
{
    public static class Methods
    {
        /// <summary>
        ///     Compute the distance between two strings.
        /// </summary>
        public static int LevenshteinDistance(string s, string t)
        {
            var n = s.Length;
            var m = t.Length;
            var d = new int[n + 1, m + 1];
            if (n == 0)
                return m;
            if (m == 0)
                return n;
            for (var i = 0; i <= n; d[i, 0] = i++)
            { }
            for (var j = 0; j <= m; d[0, j] = j++)
            { }
            for (var i = 1; i <= n; i++)
            {
                for (var j = 1; j <= m; j++)
                {
                    var cost = t[j - 1] == s[i - 1] ? 0 : 1;
                    d[i, j] = Math.Min(
                        Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1),
                        d[i - 1, j - 1] + cost);
                }
            }
            return d[n, m];
        }

        /// <summary>
        ///     Strip the entire path of extraneous information (subgroups, resolution, etc).
        /// </summary>
        /// <param name="filename">
        /// A file name, not a filepath.
        /// </param>
        /// <param name="removeEpisode">
        ///  A flag for also removing the episode number
        /// </param>
        public static string Strip(string filename, bool removeEpisode = false)
        {
            var text = filename;

            // Remove brackets and parenthesis
            var phrases = Regex.Matches(text, @"\s?\[(.*?)\]|\((.*?)\)\s*")
                .Cast<Match>()
                .Select(match => match.Groups[0].Value)
                .ToList();
            text = new[] { ".mkv", ".mp4", ".avi" }.Union(phrases).Aggregate(text, (current, s) => current.Replace(s, ""));

            // _ and . can be used for spaces for some subgroups, replace with spaces instead
            text = new[] {"_", "."}.Aggregate(text, (current, s) => current.Replace(s, " "));

            if (removeEpisode)
            {
                var regularEpisodePattern = Regex.Matches(text, @"\-\s[0-9]{1,}");       // Name {- #}
                var namedEpisodePattern = Regex.Matches(text, @"[e|E]pisode\s[0-9]{1,}");       // Name {Episode #}

                if (regularEpisodePattern.Count > 0)
                {
                    var split = text.Split('-');
                    text = string.Join("-", split.Take(split.Length - 1));
                }

                else if (namedEpisodePattern.Count > 0)
                {
                    var value = namedEpisodePattern.Cast<Match>().Select(match => match.Groups[0].Value).ToList().First();
                    text = text.Replace(value, "");
                }
                
            }

            return Regex.Replace(text.Trim(), @"\s+", " ");
        }

        /// <summary>
        ///     Completely clear focus from an element.
        /// </summary>
        public static void ClearFocusFrom(FrameworkElement element)
        {
            var parent = (FrameworkElement) element.Parent;
            while (parent != null && !((IInputElement) parent).Focusable)
{'fraction_non_alphanumeric': 0.1498894620486367, 'fraction_numerical': 0.009432571849668387, 'mean_word_length': 1.3097345132743363, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5610342', 'n_tokens_mistral': 2017, 'n_tokens_neox': 1891, 'n_words': 518}