Selaa lähdekoodia

给自动拆分程序用的单词零件表

visuddhinanda 3 vuotta sitten
vanhempi
sitoutus
21ef3ee5d4

+ 57 - 0
app/Console/Commands/UpgradeWordPart.php

@@ -0,0 +1,57 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Models\WordPart;
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Log;
+
+class UpgradeWordPart extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'upgrade:wordpart';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Command description';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return int
+     */
+    public function handle()
+    {
+		#载入csv数据
+		$csvFile = config("app.path.dict_text") .'/system/part.csv';
+		if (($fp = fopen($csvFile, "r")) !== false) {
+			Log::info("csv load:" . $csvFile);
+			while (($data = fgetcsv($fp, 0, ',')) !== false) {
+				WordPart::updateOrCreate(['word' => $data[0],],['weight' => $data[1],]);
+			}
+			fclose($fp);
+		} else {
+			$this->error( "can not open csv file. filename=" . $csvFile. PHP_EOL) ;
+			Log::error( "can not open csv file. filename=" . $csvFile) ;
+		}
+		$this->info('ok');
+        return 0;
+    }
+}

+ 13 - 0
app/Models/WordPart.php

@@ -0,0 +1,13 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+
+class WordPart extends Model
+{
+    use HasFactory;
+	protected $fillable = ['word' , 'weight'];
+
+}

+ 33 - 0
database/migrations/2022_08_12_134209_create_word_parts_table.php

@@ -0,0 +1,33 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class CreateWordPartsTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('word_parts', function (Blueprint $table) {
+            $table->id();
+			$table->string('word',1024)->unique();
+			$table->bigInteger('weight');
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('word_parts');
+    }
+}