<commit_before><?php

namespace TuxOne\ValidationBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

/**
 * @Annotation
 */
class NotContainsBadWordsValidator extends ConstraintValidator
{
    private $dictionaryPath;

    public function __construct($dictionaryPath)
    {
        $this->dictionaryPath = $dictionaryPath;
        
    }

    public function validate($value, Constraint $constraint)
    {
        if (null === $value || '' === $value) {
            return;
        }

        if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
            throw new UnexpectedTypeException($value, 'string');
        }

        $stringValue = (string) $value;

        // Load blacklist
        $blacklist = $this->getBlackListArray();

        // Split input value into words
        $words = $this->getWordsArray($stringValue);

        // Search for bad words
        $match = array_intersect($words, $blacklist);

        if ( count($match) > 0 ) {
            $this->context->addViolation($constraint->message, array('%string%' => reset($match)));
        }
    }

    private function getBlackListArray()
    {
        return file(__DIR__."/../../Dictionaries/list.txt", FILE_IGNORE_NEW_LINES);
    }

    private function getWordsArray($text)
    {
        $text = strtolower($text);
        $text = preg_replace("/[^a-z0-9 ]/", ' ', $text);
        $words = explode(' ', $text);
        $words = array_unique($words);
        return $words;
    }
}<commit_msg>Replace the hard coded dictionary path with a variable.<commit_after><?php

namespace TuxOne\ValidationBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

/**
 * @Annotation
 */
class NotContainsBadWordsValidator extends ConstraintValidator
{
    private $dictionaryPath;

    public function __construct($dictionaryPath)
    {
        $this->dictionaryPath = $dictionaryPath;
        
    }

    public function validate($value, Constraint $constraint)
    {
        if (null === $value || '' === $value) {
            return;
        }

        if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
            throw new UnexpectedTypeException($value, 'string');
        }

        $stringValue = (string) $value;

        // Load blacklist
        $blacklist = $this->getBlackListArray();

        // Split input value into words
        $words = $this->getWordsArray($stringValue);

        // Search for bad words
        $match = array_intersect($words, $blacklist);

        if ( count($match) > 0 ) {
            $this->context->addViolation($constraint->message, array('%string%' => reset($match)));
        }
    }

    private function getBlackListArray()
    {
        return file($this->dictionaryPath, FILE_IGNORE_NEW_LINES);
    }

    private function getWordsArray($text)
    {
        $text = strtolower($text);
        $text = preg_replace("/[^a-z0-9 ]/", ' ', $text);
        $words = explode(' ', $text);
        $words = array_unique($words);
        return $words;
    }
}
{'fraction_non_alphanumeric': 0.13834586466165413, 'fraction_numerical': 0.0018045112781954887, 'mean_word_length': 2.7328843995510663, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 20, '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': '2270907', 'n_tokens_mistral': 951, 'n_tokens_neox': 910, 'n_words': 184}