djunehor/laravel-grammar

View on GitHub
src/database/seeds/LaravelGrammarSeeder.php.stub

Summary

Maintainability
Test Coverage
<?php

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;

class LaravelGrammarSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $filePath = database_path('seeds/entries.csv');
        $table = config('laravel-grammar.table', 'entries');
        if (!File::exists($filePath) && !$this->unzip()) {
            echo "Failed to unzip csv. Check if [entries.csv.zip] exists in /database/seeds\n";
            return;
        }
        $rows = file($filePath);
        DB::table($table)->truncate();

        $i = 0;
        $insertArray = [];
        foreach ($rows as $row) {
            $array = explode(";", $row);
                $datum = [
                'word' => str_replace('"', '', $array[0]),
                'word_type' => json_encode($this->parts(str_replace('"', '', $array[1])))
                    ];

                $insertArray[] = $datum;
                $i++;

                // we want to insert 10000 rows per time for optimization
                if($i == 10000) {
                    DB::table($table)->insert($insertArray);
                    $i = 0;
                    $insertArray = [];
                }

        }

        echo "\e[0;31;42mYou can delete [entries.csv.zip] from /database/seeds\e[0m\n";
    }

    public function unzip()
    {
        $zip = new ZipArchive;
        $res = $zip->open(database_path('seeds/entries.csv.zip'));
        if ($res === TRUE) {
            $zip->extractTo(__DIR__);
            $zip->close();
            return true;
        } else {
            return false;
        }
    }

    private function parts($string)
    {
        $array = explode(' ', str_replace(",", "", $string));

        $parts = [];

        $partsOfSpeech = parts_of_speech();

        foreach ($array as $item) {
            if (array_key_exists($item, $partsOfSpeech)) {
                $parts[] = $partsOfSpeech[$item];
            }
        }

        return $parts;
    }
}