<commit_before><?php

namespace DJEM\Crosslinks;

class Crosslinks
{
    private $tokens = [];
    private $phrases = [];
    private $stemmers = [];

    private $candidates = [];
    private $buffer = [];

    private function __construct()
    {
    }

    private function addStemmers($stemmers)
    {
        $this->stemmers = $stemmers;
    }

    private function addText($text)
    {
        $this->tokens = Tokenizer::parse($text);
    }

    private function addPhrases($phrases)
    {
        $this->phrases = [];
        if (! is_array($phrases)) {
            return;
        }

        foreach ($phrases as $phrase => $link) {
            $this->phrases[] = [
                'phrase' => array_map(function ($word) {
                    return $this->normalizeWord($word);
                }, preg_split('/\s+/', $phrase)),
                'link' => $link,
            ];
        }
    }

    private function normalizeWord($word)
    {
        foreach ($this->stemmers as $func) {
            if (function_exists($func)) {
                $word = $func($word);
            }
        }

        return $word;
    }

    private function findWord($word, $arr)
    {
        $word = $this->normalizeWord($word);

        return count(array_filter($arr, function ($entry) use ($word) {
            return $entry == $word;
        })) != 0;
    }

    private function fillCandidates($word)
    {
        // remove invalid candidates
        $this->candidates = array_filter($this->candidates, function ($entry) use ($word) {
            return $this->findWord($word, $entry['phrase']);
        });

        // insert new candidates
        foreach ($this->phrases as $entry) {
            if ($this->findWord($word, $entry['phrase'])) {
                $entry['position'] = count($this->buffer);
                $this->candidates[] = $entry;
            }
        }
    }

    private function getStartToken($link)
    {
        return (object) [
            'type' => 'CrosslinkStart',
            'text' => '<a href="'.htmlentities($link).'">',
        ];
    }<commit_msg>style fix<commit_after><?php

namespace DJEM\Crosslinks;

class Crosslinks
{
    private $tokens = [];
    private $phrases = [];
    private $stemmers = [];

    private $candidates = [];
    private $buffer = [];

    private function __construct()
    {
    }

    private function addStemmers($stemmers)
    {
        $this->stemmers = $stemmers;
    }

    private function addText($text)
    {
        $this->tokens = Tokenizer::parse($text);
    }

    private function addPhrases($phrases)
    {
        $this->phrases = [];
        if (! is_array($phrases)) {
            return;
        }

        foreach ($phrases as $phrase => $link) {
            $this->phrases[] = [
                'phrase' => array_map(function ($word) {
                    return $this->normalizeWord($word);
                }, preg_split('/\s+/', $phrase)),
                'link' => $link,
            ];
        }
    }

    private function normalizeWord($word)
    {
        foreach ($this->stemmers as $func) {
            if (function_exists($func)) {
                $word = $func($word);
            }
        }

        return $word;
    }

    private function findWord($word, $arr)
    {
        return array_search($this->normalizeWord($word), $arr) !== false;
    }

    private function fillCandidates($word)
    {
        // remove invalid candidates
        $this->candidates = array_filter($this->candidates, function ($entry) use ($word) {
            return $this->findWord($word, $entry['phrase']);
        });

        // insert new candidates
        foreach ($this->phrases as $entry) {
            if ($this->findWord($word, $entry['phrase'])) {
                $entry['position'] = count($this->buffer);
                $this->candidates[] = $entry;
            }
        }
    }

    private function getStartToken($link)
    {
        return (object) [
            'type' => 'CrosslinkStart',
            'text' => '<a href="'.htmlentities($link).'">',
        ];
    }
{'fraction_non_alphanumeric': 0.15609636184857423, 'fraction_numerical': 0.0002458210422812193, 'mean_word_length': 1.7586440677966102, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 43, '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': '6842136', 'n_tokens_mistral': 1213, 'n_tokens_neox': 1148, 'n_words': 229}