瀏覽代碼

查找parent

visuddhinanda 1 年之前
父節點
當前提交
2eaf2b964d
共有 1 個文件被更改,包括 58 次插入31 次删除
  1. 58 31
      api-v8/app/Console/Commands/UpgradeDictSysPreference.php

+ 58 - 31
api-v8/app/Console/Commands/UpgradeDictSysPreference.php

@@ -6,6 +6,7 @@ use Illuminate\Console\Command;
 use App\Http\Api\DictApi;
 use App\Models\UserDict;
 use App\Models\WordIndex;
+use Carbon\Carbon;
 
 class UpgradeDictSysPreference extends Command
 {
@@ -41,7 +42,7 @@ class UpgradeDictSysPreference extends Command
      */
     public function handle()
     {
-        if(\App\Tools\Tools::isStop()){
+        if (\App\Tools\Tools::isStop()) {
             return 0;
         }
         $this->info("start");
@@ -54,14 +55,16 @@ class UpgradeDictSysPreference extends Command
         $dict_id = array();
         foreach ($dictList as $key => $value) {
             $dict_id[$value] = DictApi::getSysDict($value);
-            if(!$dict_id[$value]){
+            if (!$dict_id[$value]) {
                 $this->error("没有找到 {$value} 字典");
                 return 1;
-            }else{
+            } else {
                 $this->info("{$value} :{$dict_id[$value]}");
             }
         }
-
+        if (!$this->confirm('继续吗?')) {
+            return 0;
+        }
         //搜索顺序
         $order = [
             '4d3a0d92-0adc-4052-80f5-512a2603d0e8',/* system irregular */
@@ -74,43 +77,67 @@ class UpgradeDictSysPreference extends Command
         $found = 0;
         foreach ($words as $key => $word) {
             if (preg_match('/\d/', $word->word)) {
+                //不处理带数字的
                 continue;
             }
             $rows++;
-            $preference = null;
+            $factors = null;
+            $parent = null;
+            $confidence = 0;
             foreach ($order as $key => $dict) {
-                $preference = UserDict::where('word', $word->word)
-                                ->where('dict_id', $dict)
-                                ->whereNotNull('factors')
-                                ->where('factors','<>','')
-                                ->orderBy('confidence', 'desc')
-                                ->first();                # code...
-                if($preference){
-                    break;
+                //找到第一个有拆分的
+                $preWords = UserDict::where('word', $word->word)
+                    ->where('dict_id', $dict)
+                    ->orderBy('confidence', 'desc')
+                    ->get();
+                foreach ($preWords as $key => $value) {
+                    if (!$factors && !empty($value->factors)) {
+                        $factors = $value->factors;
+                        if ($value->confidence > $confidence) {
+                            $confidence = $value->confidence;
+                        }
+                    }
+                    if (!$parent && !empty($value->parent)) {
+                        $parent = $value->parent;
+                        if ($value->confidence > $confidence) {
+                            $confidence = $value->confidence;
+                        }
+                    }
+                    if ($parent && $factors) {
+                        break;
+                    }
                 }
             }
-            if($preference){
-                $userDict = UserDict::firstOrNew([
-                    'word'=>$word->word,
-                    'dict_id'=>$dict_id['system_preference']
-                ],
-                [
-                    'id' => app('snowflake')->id(),
-                    'source' => '_ROBOT_',
-                    'create_time'=>(int)(microtime(true)*1000)
-                ]);
-                $userDict->factors = $preference->factors;
-                $userDict->parent = $preference->parent;
-                $userDict->confidence = $preference->confidence;
-                $userDict->language = 'cm';
-                $userDict->creator_id = 1;
-                $userDict->save();
+            if ($parent || $factors) {
+                $prefWord = UserDict::where('word', $word->word)
+                    ->where('dict_id', $dict_id['system_preference'])
+                    ->first();
+                if (!$prefWord) {
+                    $prefWord = new UserDict();
+                    $prefWord->word = $word->word;
+                    $prefWord->dict_id = $dict_id['system_preference'];
+                    $prefWord->id = app('snowflake')->id();
+                    $prefWord->source = '_ROBOT_';
+                    $prefWord->create_time = (int)(microtime(true) * 1000);
+                    $prefWord->language = 'cm';
+                    $prefWord->creator_id = 1;
+                } else {
+                    if (Carbon::parse($prefWord->updated_at) > Carbon::parse($prefWord->created_at)) {
+                        //跳过已经被编辑过的。
+                        $this->info('跳过已经被编辑过的' . $word->word);
+                        continue;
+                    }
+                }
+                $prefWord->factors = $factors;
+                $prefWord->parent = $parent;
+                $prefWord->confidence = $confidence;
+                $prefWord->save();
                 $found++;
             }
-            if($rows % 100 == 0){
+            if ($rows % 100 == 0) {
                 $output = "[{$rows}] {$word->word} found:{$found}";
                 $this->info($output);
-                $found=0;
+                $found = 0;
             }
         }
         return 0;