<commit_before><?php

namespace Spatie\Emoji\Generator;

final class Parser
{
    /** @var string */
    private $body;

    /** @var string */
    private $group;

    /** @var string */
    private $subgroup;

    /** @var array */
    private $emojis = [];

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

    public function parse()
    {
        $lines = explode("\n", $this->body);

        foreach($lines as $line) {
            $this->parseLine(trim($line));
        }
    }

    public function getEmojis(): array
    {
        return $this->emojis;
    }

    private function parseLine(string $line)
    {
        if (strlen($line) === 0) {
            return;
        }

        if(strpos($line, '# group:') === 0) {
            $this->group = trim(str_replace('# group:', '', $line));
            return;
        }

        if(strpos($line, '# subgroup:') === 0) {
            $this->subgroup = trim(str_replace('# subgroup:', '', $line));
            return;
        }

        if(strpos($line, '#') === 0) {
            return;
        }

        preg_match('/^([A-Z\d ]+)\s+;\s+(fully-qualified|component)\s+# [^a-z ]* ([\w\d:\-\*\# ]+)$/', $line, $matches);
        if(count($matches) === 4) {
            list(, $code, , $name) = $matches;

            $this->addEmoji(trim($code), trim($name));
        }
    }

    private function addEmoji(string $code, string $name)
    {
        if(!isset($this->emojis[$this->group])) {
            $this->emojis[$this->group] = [];
        }

        if(!isset($this->emojis[$this->group][$this->subgroup])) {
            $this->emojis[$this->group][$this->subgroup] = [];
        }

        $name = str_replace([
            '*',
            '#',
        ], [
            'asterisk',
            'hash',
        ], $name);
        $cleanName = strtolower(preg_replace("/\s+/", " ", preg_replace("/[^\w]+/", " ", $name)));
        $const = 'CHARACTER_'.strtoupper(preg_replace("/\s+/", "", preg_replace('/(.)(?= [a-z])/', '$1_', $cleanName)));
        $method = lcfirst(preg_replace("/\s+/", "", ucwords($cleanName)));

        $this->emojis[$this->group][$this->subgroup][$code] = [
            'name' => $name,
            'const' => $const,
            'method' => $method,
            'code' => '\u{'.implode('}\u{', explode(' ', $code)).'}',
        ];
    }
}<commit_msg>replace 1st, 2nd, 3rd<commit_after><?php

namespace Spatie\Emoji\Generator;

final class Parser
{
    /** @var string */
    private $body;

    /** @var string */
    private $group;

    /** @var string */
    private $subgroup;

    /** @var array */
    private $emojis = [];

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

    public function parse()
    {
        $lines = explode("\n", $this->body);

        foreach($lines as $line) {
            $this->parseLine(trim($line));
        }
    }

    public function getEmojis(): array
    {
        return $this->emojis;
    }

    private function parseLine(string $line)
    {
        if (strlen($line) === 0) {
            return;
        }

        if(strpos($line, '# group:') === 0) {
            $this->group = trim(str_replace('# group:', '', $line));
            return;
        }

        if(strpos($line, '# subgroup:') === 0) {
            $this->subgroup = trim(str_replace('# subgroup:', '', $line));
            return;
        }

        if(strpos($line, '#') === 0) {
            return;
        }

        preg_match('/^([A-Z\d ]+)\s+;\s+(fully-qualified|component)\s+# [^a-z ]* ([\w\d:\-\*\# ]+)$/', $line, $matches);
        if(count($matches) === 4) {
            list(, $code, , $name) = $matches;

            $this->addEmoji(trim($code), trim($name));
        }
    }

    private function addEmoji(string $code, string $name)
    {
        if(!isset($this->emojis[$this->group])) {
            $this->emojis[$this->group] = [];
        }

        if(!isset($this->emojis[$this->group][$this->subgroup])) {
            $this->emojis[$this->group][$this->subgroup] = [];
        }

        $name = str_replace([
            '*',
            '#',
            '1st',
            '2nd',
            '3rd',
        ], [
            'asterisk',
            'hash',
            'first',
            'second',
            'third',
        ], $name);
        $cleanName = strtolower(preg_replace("/\s+/", " ", preg_replace("/[^\w]+/", " ", $name)));
        $const = 'CHARACTER_'.strtoupper(preg_replace("/\s+/", "", preg_replace('/(.)(?= [a-z])/', '$1_', $cleanName)));
        $method = lcfirst(preg_replace("/\s+/", "", ucwords($cleanName)));

        $this->emojis[$this->group][$this->subgroup][$code] = [
            'name' => $name,
            'const' => $const,
            'method' => $method,
            'code' => '\u{'.implode('}\u{', explode(' ', $code)).'}',
        ];
    }
}
{'fraction_non_alphanumeric': 0.22413439868879328, 'fraction_numerical': 0.0036877688998156115, 'mean_word_length': 1.783352337514253, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 51, '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': '2183895', 'n_tokens_mistral': 1653, 'n_tokens_neox': 1604, 'n_words': 257}