Browse Source

用 DictApi 获取字典id

visuddhinanda 3 years ago
parent
commit
977c7d61b8

+ 17 - 13
app/Console/Commands/UpgradeCompound.php

@@ -7,6 +7,7 @@ use App\Models\WordIndex;
 use App\Models\WbwTemplate;
 use App\Models\UserDict;
 use App\Tools\TurboSplit;
+use App\Http\Api\DictApi;
 
 class UpgradeCompound extends Command
 {
@@ -24,7 +25,6 @@ class UpgradeCompound extends Command
      */
     protected $description = 'Command description';
 
-	protected $dict_id = 'c42980f0-5967-4833-b695-84183344f68f';
 
 
     /**
@@ -44,7 +44,12 @@ class UpgradeCompound extends Command
      */
     public function handle()
     {
-		
+        $dict_id = DictApi::getSysDict('robot_compound');
+        if(!$dict_id){
+            $this->error('没有找到 robot_compound 字典');
+            return 1;
+        }
+
 		$start = \microtime(true);
 
 		$_word = $this->argument('word');
@@ -62,6 +67,7 @@ class UpgradeCompound extends Command
 		//
 		if($this->option('test')){
 			//调试代码
+            $ts = new TurboSplit();
 			Storage::disk('local')->put("tmp/compound.md", "# Turbo Split");
 			//获取需要拆的词
 			$list = [
@@ -83,15 +89,13 @@ class UpgradeCompound extends Command
 						Storage::disk('local')->append("tmp/compound.md", "- `{$part['word']}`,{$part['factors']},{$part['confidence']}");
 					}
 				}
-			}		
-			$this->info("耗时:".\microtime(true)-$start);		
-			return 0;	
+			}
+			$this->info("耗时:".\microtime(true)-$start);
+			return 0;
 		}
 
 		//$words = WordIndex::where('final',0)->select('word')->orderBy('count','desc')->skip(72300)->cursor();
 		$words = WbwTemplate::select('real')
-						->where('book',118)
-						->whereBetween('paragraph',[1329,1367])
 						->where('type','<>','.ctl.')
 						->where('real','<>','')
 						->groupBy('real')->cursor();
@@ -99,7 +103,7 @@ class UpgradeCompound extends Command
 		foreach ($words as $key => $word) {
 			//先看目前字典里有没有
 			$isExists = UserDict::where('word',$word->real)
-								->where('dict_id',"<>",'8359757e-9575-455b-a772-cc6f036caea0')
+								->where('dict_id',"<>",$dict_id)
 								->exists();
 
 			if($isExists){
@@ -108,7 +112,7 @@ class UpgradeCompound extends Command
 			}
 			# code...
 			$count++;
-			$this->info("{$count}:{$word->real}"); 
+			$this->info("{$count}:{$word->real}");
 			$ts = new TurboSplit();
 			$parts = $ts->splitA($word->real);
 			foreach ($parts as $part) {
@@ -116,7 +120,7 @@ class UpgradeCompound extends Command
 					[
 						'word' => $part['word'],
 						'factors' => $part['factors'],
-						'dict_id' => $this->dict_id,
+						'dict_id' => $dict_id,
 					],
 					[
 						'id' => app('snowflake')->id(),
@@ -140,9 +144,9 @@ class UpgradeCompound extends Command
 			}
 		}
 		//删除旧数据
-		UserDict::where('dict_id',$this->dict_id)->where('flag',0)->delete();
-		UserDict::where('dict_id',$this->dict_id)->where('flag',1)->update(['flag'=>0]);
-	
+		UserDict::where('dict_id',$dict_id)->where('flag',0)->delete();
+		UserDict::where('dict_id',$dict_id)->where('flag',1)->update(['flag'=>0]);
+
         return 0;
     }
 }

+ 22 - 8
app/Console/Commands/UpgradeDictSysWbwExtract.php

@@ -1,5 +1,9 @@
 <?php
-
+/**
+ * 将用户词典中的数据进行汇总。
+ * 算法:
+ * 同样词性的合并为一条记录。意思按照出现的次数排序
+ */
 namespace App\Console\Commands;
 
 use Illuminate\Console\Command;
@@ -38,7 +42,17 @@ class UpgradeDictSysWbwExtract extends Command
      */
     public function handle()
     {
-		$dict  = UserDict::select('word')->where('word','!=','')->where('dict_id','ef620a93-a55d-4756-89c5-e188ab009e45')->groupBy('word');
+        $user_dict_id = DictApi::getSysDict('community');
+        if(!$user_dict_id){
+            $this->error('没有找到 community 字典');
+            return 1;
+        }
+        $user_dict_extract_id = DictApi::getSysDict('community_extract');
+        if(!$user_dict_extract_id){
+            $this->error('没有找到 community_extract 字典');
+            return 1;
+        }
+		$dict  = UserDict::select('word')->where('word','!=','')->where('dict_id',$user_dict_id)->groupBy('word');
 		$bar = $this->output->createProgressBar($dict->count());
 		foreach ($dict->cursor() as  $word) {
 			# code...
@@ -50,7 +64,7 @@ class UpgradeDictSysWbwExtract extends Command
 
 			$case = UserDict::selectRaw('type,grammar, sum(confidence)')
 					->where('word',$word->word)
-					->where('dict_id','ef620a93-a55d-4756-89c5-e188ab009e45')
+					->where('dict_id',$user_dict_id)
 					->where('type','!=','.part.')
 					->where('type','<>','')
 					->whereNotNull('type')
@@ -59,13 +73,13 @@ class UpgradeDictSysWbwExtract extends Command
 					->first();
 			if($case){
 				$wordtype = $case->type;
-				$wordgrammar = $case->grammar;			
+				$wordgrammar = $case->grammar;
 			}
 
 			//parent
 			$parent = UserDict::selectRaw('parent, sum(confidence)')
 					->where('word',$word->word)
-					->where('dict_id','ef620a93-a55d-4756-89c5-e188ab009e45')
+					->where('dict_id',$user_dict_id)
 					->where('type','!=','.part.')
 					->where('parent','!=','')
 					->whereNotNull('parent')
@@ -73,14 +87,14 @@ class UpgradeDictSysWbwExtract extends Command
 					->orderBy('sum','desc')
 					->first();
 			if($parent){
-				$wordparent = $parent->parent;			
+				$wordparent = $parent->parent;
 			}
 
 
 				//factors
 				$factor = UserDict::selectRaw('factors, sum(confidence)')
 						->where('word',$word->word)
-						->where('dict_id','ef620a93-a55d-4756-89c5-e188ab009e45')
+						->where('dict_id',$user_dict_id)
 						->where('type','!=','.part.')
 						->where('factors','<>','')
 						->whereNotNull('factors')
@@ -97,7 +111,7 @@ class UpgradeDictSysWbwExtract extends Command
 						'grammar' => $wordgrammar,
 						'parent' => $wordparent,
 						'factors' => $wordfactors,
-						'dict_id' => '85dcc61c-c9e1-4ae0-9b44-cd6d9d9f0d01', 
+						'dict_id' => $user_dict_extract_id,
 					],
 					[
 						'id' => app('snowflake')->id(),

+ 23 - 14
app/Console/Commands/UpgradeRegular.php

@@ -1,5 +1,9 @@
 <?php
-
+/**
+ * 生成系统规则变形词典
+ * 算法: 扫描字典里的所有单词。根据语尾表变形。
+ * 并在词库中查找是否在三藏中出现。出现的保存。
+ */
 namespace App\Console\Commands;
 
 use App\Models\UserDict;
@@ -8,6 +12,7 @@ use Illuminate\Console\Command;
 use Illuminate\Support\Facades\Cache;
 use Illuminate\Support\Facades\Log;
 use Illuminate\Support\Facades\DB;
+use App\Http\Api\DictApi;
 
 class UpgradeRegular extends Command
 {
@@ -23,8 +28,7 @@ class UpgradeRegular extends Command
      *
      * @var string
      */
-    protected $description = 'Command description';
-	protected $dict_id = '57afac99-0887-455c-b18e-67c8682158b0';
+    protected $description = 'upgrade regular';
     /**
      * Create a new command instance.
      *
@@ -42,6 +46,11 @@ class UpgradeRegular extends Command
      */
     public function handle()
     {
+        $dict_id = DictApi::getSysDict('system_regular');
+        if(!$dict_id){
+            $this->error('没有找到 system_regular 字典');
+            return 1;
+        }
 		$nounEnding = array();
 		$rowCount=0;
 		if(($handle=fopen(public_path('app/public/ending/noun.csv'),'r'))!==FALSE){
@@ -79,7 +88,7 @@ class UpgradeRegular extends Command
 			$words = UserDict::where('type','.n:base.')
 							->orWhere('type','.v:base.')
 							->orWhere('type','.adj:base.')
-							->orWhere('type','.ti:base.');	
+							->orWhere('type','.ti:base.');
 		}else{
 			$words = UserDict::where('word',$this->argument('word'))
 							->where(function($query) {
@@ -87,16 +96,16 @@ class UpgradeRegular extends Command
 								->orWhere('type','.v:base.')
 								->orWhere('type','.adj:base.')
 								->orWhere('type','.ti:base.');
-							});		
+							});
 		}
 		$words = $words->select(['word','type','grammar'])
 						->groupBy(['word','type','grammar'])
 						->orderBy('word');
 		$query = "
-		select count(*) from (select count(*) from user_dicts ud where 
-			\"type\" = '.v:base.' or 
-			\"type\" = '.n:base.' or 
-			\"type\" = '.ti:base.' or 
+		select count(*) from (select count(*) from user_dicts ud where
+			\"type\" = '.v:base.' or
+			\"type\" = '.n:base.' or
+			\"type\" = '.ti:base.' or
 			\"type\" = '.adj:base.'
 			group by word,type,grammar) as t;
 		";
@@ -148,7 +157,7 @@ class UpgradeRegular extends Command
 					$head = mb_substr($word->word,0,(0-$endLen),"UTF-8");//原词剩余的部分
 					$newEnding = $thiscase[3];
 					$newGrammar = $thiscase[4];
-					$newword=$head.$thiscase[2];					
+					$newword=$head.$thiscase[2];
 					if($word->type==".n:base."){
 						//名词
 						if($thiscase[0]==$word->grammar  && $thiscase[1]==$end){
@@ -156,7 +165,7 @@ class UpgradeRegular extends Command
 							$isMatch = true;
 						}else{
 							$isMatch = false;
-						}						
+						}
 					}else{
 						//形容词
 						if($thiscase[1]==$end){
@@ -164,7 +173,7 @@ class UpgradeRegular extends Command
 							$isMatch = true;
 						}else{
 							$isMatch = false;
-						}							
+						}
 					}
 
 				}
@@ -184,7 +193,7 @@ class UpgradeRegular extends Command
 								'grammar' => $newGrammar,
 								'parent' => $word->word,
 								'factors' => "{$word->word}+[{$newEnding}]",
-								'dict_id' => $this->dict_id, 
+								'dict_id' => $dict_id,
 							],
 							[
 								'id' => app('snowflake')->id(),
@@ -206,7 +215,7 @@ class UpgradeRegular extends Command
 		}
 		$bar->finish();
 		//删除旧数据
-		$delOld = UserDict::where('dict_id',$this->dict_id);
+		$delOld = UserDict::where('dict_id',$dict_id);
 		if(!empty($this->argument('word'))){
 			$delOld = $delOld->where('word',$this->argument('word'));
 		}