<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using NadekoBot.Extensions;

// THANKS @ShoMinamimoto for suggestions and coding help
namespace NadekoBot.Modules.Games.Common.Trivia
{
    public class TriviaQuestion
    {
        //represents the min size to judge levDistance with
        private static readonly HashSet<Tuple<int, int>> strictness = new HashSet<Tuple<int, int>> {
            new Tuple<int, int>(9, 0),
            new Tuple<int, int>(14, 1),
            new Tuple<int, int>(19, 2),
            new Tuple<int, int>(22, 3),
        };
        public static int maxStringLength = 22;

        public string Category { get; set; }
        public string Question { get; set; }
        public string ImageUrl { get; set; }
        public string AnswerImageUrl { get; set; }
        public string Answer { get; set; }

        public TriviaQuestion(string q, string a, string c, string img = null, string answerImage = null)
        {
            this.Question = q;
            this.Answer = a;
            this.Category = c;
            this.ImageUrl = img;
            this.AnswerImageUrl = answerImage ?? img;
        }

        public string GetHint() => Scramble(Answer);

        public bool IsAnswerCorrect(string guess)
        {
            guess = CleanGuess(guess);
            if (Answer.Equals(guess))
            {
                return true;
            }
            Answer = CleanGuess(Answer);
            guess = CleanGuess(guess);
            if (Answer.Equals(guess))
            {
                return true;
            }

            int levDistance = Answer.LevenshteinDistance(guess);
            return JudgeGuess(Answer.Length, guess.Length, levDistance);
        }

        private bool JudgeGuess(int guessLength, int answerLength, int levDistance)
        {
            foreach (Tuple<int, int> level in strictness)
            {
                if (guessLength <= level.Item1 || answerLength <= level.Item1)
                {
                    if (levDistance <= level.Item2)
                        return true;
                    else
                        return false;
                }
            }
            return false;
        }

        private string CleanGuess(string str)
        {
            str = " " + str.ToLower() + " ";<commit_msg>Possible trivia weirdness fix, #1522<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using NadekoBot.Extensions;

// THANKS @ShoMinamimoto for suggestions and coding help
namespace NadekoBot.Modules.Games.Common.Trivia
{
    public class TriviaQuestion
    {
        //represents the min size to judge levDistance with
        private static readonly HashSet<Tuple<int, int>> strictness = new HashSet<Tuple<int, int>> {
            new Tuple<int, int>(9, 0),
            new Tuple<int, int>(14, 1),
            new Tuple<int, int>(19, 2),
            new Tuple<int, int>(22, 3),
        };
        public static int maxStringLength = 22;

        public string Category { get; set; }
        public string Question { get; set; }
        public string ImageUrl { get; set; }
        public string AnswerImageUrl { get; set; }
        public string Answer { get; set; }
        public string CleanAnswer { get; }

        public TriviaQuestion(string q, string a, string c, string img = null, string answerImage = null)
        {
            this.Question = q;
            this.Answer = a;
            this.CleanAnswer = Clean(a);
            this.Category = c;
            this.ImageUrl = img;
            this.AnswerImageUrl = answerImage ?? img;
        }

        public string GetHint() => Scramble(Answer);

        public bool IsAnswerCorrect(string guess)
        {
            if (Answer.Equals(guess))
            {
                return true;
            }
            var cleanGuess = Clean(guess);
            if (CleanAnswer.Equals(cleanGuess))
            {
                return true;
            }

            int levDistanceClean = CleanAnswer.LevenshteinDistance(cleanGuess);
            int levDistanceNormal = Answer.LevenshteinDistance(guess);
            return JudgeGuess(CleanAnswer.Length, cleanGuess.Length, levDistanceClean)
                || JudgeGuess(Answer.Length, guess.Length, levDistanceNormal);
        }

        private bool JudgeGuess(int guessLength, int answerLength, int levDistance)
        {
            foreach (Tuple<int, int> level in strictness)
            {
                if (guessLength <= level.Item1 || answerLength <= level.Item1)
                {
                    if (levDistance <= level.Item2)
                        return true;
                    else
                        return false;
                }
            }
            return false;
        }

        private string Clean(string str)
        {
            str = " " + str.ToLower() + " ";
{'fraction_non_alphanumeric': 0.08948320933038408, 'fraction_numerical': 0.007239091091896239, 'mean_word_length': 1.6727565824825363, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 23, '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': '5449451', 'n_tokens_mistral': 1374, 'n_tokens_neox': 1289, 'n_words': 359}