<commit_before><?php

/*
 * This file is part of Bens Penhorados, an undergraduate capstone project.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace App\Jobs\Extract;

use App\Extractors\Wrappers\VehicleExtractorWrapper;
use App\Jobs\Job;
use App\Models\Items\Item;
use App\Models\Items\Vehicle;

/**
 * This is the extract vehicle attributes job.
 *
 * @author <NAME> <<EMAIL>>
 */
class ExtractVehicleAttributesJob extends Job
{
    const REGEX_FLAG_YEAR = '/\bano\b|\bde\b/i';
    const REGEX_FLAG_ENGDISPL = '/[^\-]cc|cm[³3]\b/iu';
    const REGEX_FLAG_REGPLATECODE = '/matr\wcula/iu';
    const REGEX_FLAG_CONDITION = '/\bestado\b/i';
    const REGEX_FLAG_MAKE = '/\bmarca\b/i';
    const REGEX_FLAG_MODEL = '/\bmodelo\b/i';
    const REGEX_FLAG_COLOR = '/\bc\wr\b/iu';
    const REGEX_FLAG_FUEL = '/combust\wvel/iu';
    const REGEX_FLAG_CATEGORY = '/ve\wculo|categor\wa/iu';
    const REGEX_FLAG_TYPE = '/\btipo\b/i';

    /**
     * The vehicle extractor.
     *
     * @var VehicleExtractorWrapper
     */
    protected $extractor;

    /**
     * The attributes that should be extracted.
     *
     * @var array
     */
    protected $attributes = [
        'year'                => null,
        'engine_displacement' => null,
        'reg_plate_code'      => null,
        'is_good_condition'   => null,
        'make_id'             => null,
        'model_id'            => null,
        'color_id'            => null,
        'fuel_id'             => null,
        'category_id'         => null,
        'type_id'             => null,
    ];

    /**
     * The attributes that should be used on force extraction mode.
     *
     * @var string[]
     */
    protected $attrToForce = [
        'make_id', 'model_id', 'reg_plate_code', 'is_good_condition',
        'color_id', 'fuel_id', 'category_id', 'type_id',
    ];

    /**
     * The vehicle's code.
     *
     * @var string
     */
    protected $code;

    /**
     * The vehicle's description.
     *
     * @var array
     */
    protected $description;

    /**
     * Create a new job instance.
     *
     * @param string $code
     * @param array  $description
     */
    public function __construct($code, $description)
    {
        $this->code = $code;
        $this->description = $description;
        $this->extractor = new VehicleExtractorWrapper();
    }

    /**
     * Execute the job.
     *
     * @return mixed
     */
    public function handle()
    {
        print "\n > Extracting vehicle attributes of {$this->code} ... \n";

        // Start the normal extraction of attributes
        $this->extractAttributes();

        // Force extraction, if there are empty attributes
        if ($this->hasEmptyAttributes()) {
            $this->extractAttributes(true);
        }

        // Create a new vehicle
        $vehicle = Vehicle::create($this->attributes);

        // Set the polymorphic relationship
        Item::setPolymorphicRelation($this->code, $vehicle);

        // Update the item's title
        $vehicle->item->update([
            'title' => $this->generateTitle($vehicle),
        ]);
    }

    /**
     * Extract attributes from the vehicle's description.
     *
     * @return void
     */
    private function extractAttributes($flag_forceMode = false)
    {
        foreach ($this->description as $str) {
            foreach ($this->attributes as $attrKey => $attrVal) {

                // Check if the attribute value is null
                if (is_null($attrVal)) {
                    if ($flag_forceMode || preg_match($this->getRegexFlagPattern($attrKey), $str)) {

                        // Note: some extractors requires more than one input param
                        if (strcmp($attrKey, 'model_id')) {
                            if ($this->attributes['make_id']) {
                                $attrVal = $this->extractor->model($str, $this->attributes['make_id']);
                            }
                        } elseif (strcmp($attrKey, 'type_id')) {
                            $attrVal = $this->extractor->type($str, $flag_forceMode);
                        } else {
                            $attrVal = $this->extractor->{$this->getExtractorFuncName($attrKey)}($str);
                        }
                    }
                }
            }
        }
    }

    /**
     * Get the name of the extractor function for a given attribute.<commit_msg>Fix stupid bugs ( need to sleep :sleeping: )<commit_after><?php

/*
 * This file is part of Bens Penhorados, an undergraduate capstone project.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace App\Jobs\Extract;

use App\Extractors\Wrappers\VehicleExtractorWrapper;
use App\Jobs\Job;
use App\Models\Items\Item;
use App\Models\Items\Vehicle;

/**
 * This is the extract vehicle attributes job.
 *
 * @author <NAME> <<EMAIL>>
 */
class ExtractVehicleAttributesJob extends Job
{
    const REGEX_FLAG_YEAR = '/\bano\b|\bde\b/i';
    const REGEX_FLAG_ENGDISPL = '/[^\-]cc|cm[³3]\b/iu';
    const REGEX_FLAG_REGPLATECODE = '/matr\wcula/iu';
    const REGEX_FLAG_CONDITION = '/\bestado\b/i';
    const REGEX_FLAG_MAKE = '/\bmarca\b/i';
    const REGEX_FLAG_MODEL = '/\bmodelo\b/i';
    const REGEX_FLAG_COLOR = '/\bc\wr\b/iu';
    const REGEX_FLAG_FUEL = '/combust\wvel/iu';
    const REGEX_FLAG_CATEGORY = '/ve\wculo|categor\wa/iu';
    const REGEX_FLAG_TYPE = '/\btipo\b/i';

    /**
     * The vehicle extractor.
     *
     * @var VehicleExtractorWrapper
     */
    protected $extractor;

    /**
     * The attributes that should be extracted.
     *
     * @var array
     */
    protected $attributes = [
        'year'                => null,
        'engine_displacement' => null,
        'reg_plate_code'      => null,
        'is_good_condition'   => null,
        'make_id'             => null,
        'model_id'            => null,
        'color_id'            => null,
        'fuel_id'             => null,
        'category_id'         => null,
        'type_id'             => null,
    ];

    /**
     * The attributes that should be used on force extraction mode.
     *
     * @var string[]
     */
    protected $attrToForce = [
        'make_id', 'model_id', 'reg_plate_code', 'is_good_condition',
        'color_id', 'fuel_id', 'category_id', 'type_id',
    ];

    /**
     * The vehicle's code.
     *
     * @var string
     */
    protected $code;

    /**
     * The vehicle's description.
     *
     * @var array
     */
    protected $description;

    /**
     * Create a new job instance.
     *
     * @param string $code
     * @param array  $description
     */
    public function __construct($code, $description)
    {
        $this->code = $code;
        $this->description = $description;
        $this->extractor = new VehicleExtractorWrapper();
    }

    /**
     * Execute the job.
     *
     * @return mixed
     */
    public function handle()
    {
        print "\n > Extracting vehicle attributes of {$this->code} ... \n";

        // Start the normal extraction of attributes
        $this->extractAttributes();

        // Force extraction, if there are empty attributes
        if ($this->hasEmptyAttributes()) {
            $this->extractAttributes(true);
        }

        // Create a new vehicle
        $vehicle = Vehicle::create($this->attributes);

        // Set the polymorphic relationship
        Item::setPolymorphicRelation($this->code, $vehicle);

        // Update the item's title
        $vehicle->item->update([
            'title' => $this->generateTitle($vehicle),
        ]);
    }

    /**
     * Extract attributes from the vehicle's description.
     *
     * @return void
     */
    private function extractAttributes($flag_forceMode = false)
    {
        foreach ($this->description as $str) {
            foreach ($this->attributes as $attrKey => $attrVal) {

                // Check if the attribute value is null
                if (is_null($attrVal)) {
                    if ($flag_forceMode || preg_match($this->getRegexFlagPattern($attrKey), $str)) {

                        // Note: some extractors requires more than one input param
                        if ($attrKey == 'model_id') {
                            if (isset($this->attributes['make_id'])) {
                                $this->attributes[$attrKey] = $this->extractor->model($str, $this->attributes['make_id']);
                            }
                        } elseif ($attrKey == 'type_id') {
                            $this->attributes[$attrKey] = $this->extractor->type($str, $flag_forceMode);
                        } else {
                            $this->attributes[$attrKey] = $this->extractor->{$this->getExtractorFuncName($attrKey)}($str);
                        }
                    }
                }
            }
        }
    }

    /**
     * Get the name of the extractor function for a given attribute.
{'fraction_non_alphanumeric': 0.13151853066579205, 'fraction_numerical': 0.00021865092380015305, 'mean_word_length': 1.7873248019500305, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 92, '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': '5443740', 'n_tokens_mistral': 2734, 'n_tokens_neox': 2641, 'n_words': 618}