UpgradeRegular.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\UserDict;
  4. use App\Models\WbwTemplate;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Facades\Cache;
  7. use Illuminate\Support\Facades\Log;
  8. use Illuminate\Support\Facades\DB;
  9. class UpgradeRegular extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'upgrade:regular {word?} {--debug}';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = 'Command description';
  23. protected $dict_id = '57afac99-0887-455c-b18e-67c8682158b0';
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return int
  37. */
  38. public function handle()
  39. {
  40. $nounEnding = array();
  41. $rowCount=0;
  42. if(($handle=fopen(public_path('app/public/ending/noun.csv'),'r'))!==FALSE){
  43. while(($data=fgetcsv($handle,0,','))!==FALSE){
  44. $rowCount++;
  45. if($rowCount==1) continue;//忽略首行
  46. array_push($nounEnding,$data);
  47. }
  48. }
  49. fclose($handle);
  50. $adjEnding = array();
  51. $rowCount=0;
  52. if(($handle=fopen(public_path('app/public/ending/adj.csv'),'r'))!==FALSE){
  53. while(($data=fgetcsv($handle,0,','))!==FALSE){
  54. $rowCount++;
  55. if($rowCount==1) continue;//忽略首行
  56. array_push($adjEnding,$data);
  57. }
  58. }
  59. fclose($handle);
  60. $verbEnding = array();
  61. $rowCount=0;
  62. if(($handle=fopen(public_path('app/public/ending/verb.csv'),'r'))!==FALSE){
  63. while(($data=fgetcsv($handle,0,','))!==FALSE){
  64. $rowCount++;
  65. if($rowCount==1) continue;//忽略首行
  66. array_push($verbEnding,$data);
  67. }
  68. }
  69. fclose($handle);
  70. if(empty($this->argument('word'))){
  71. $words = UserDict::where('type','.n:base.')
  72. ->orWhere('type','.v:base.')
  73. ->orWhere('type','.adj:base.')
  74. ->orWhere('type','.ti:base.');
  75. }else{
  76. $words = UserDict::where('word',$this->argument('word'))
  77. ->where(function($query) {
  78. $query->where('type','.n:base.')
  79. ->orWhere('type','.v:base.')
  80. ->orWhere('type','.adj:base.')
  81. ->orWhere('type','.ti:base.');
  82. });
  83. }
  84. $words = $words->select(['word','type','grammar'])
  85. ->groupBy(['word','type','grammar'])
  86. ->orderBy('word');
  87. $query = "
  88. select count(*) from (select count(*) from user_dicts ud where
  89. \"type\" = '.v:base.' or
  90. \"type\" = '.n:base.' or
  91. \"type\" = '.ti:base.' or
  92. \"type\" = '.adj:base.'
  93. group by word,type,grammar) as t;
  94. ";
  95. $count = DB::select($query);
  96. $bar = $this->output->createProgressBar($count[0]->count);
  97. /*
  98. $words = UserDict::where('word','ābandhattalakkhaṇa')
  99. ->select(['word','type','grammar'])
  100. ->groupBy(['word','type','grammar']);
  101. $bar = $this->output->createProgressBar(1);
  102. */
  103. foreach ($words->cursor() as $word) {
  104. # code...
  105. switch($word->type){
  106. case ".v:base.":
  107. $casetable=$verbEnding;
  108. break;
  109. case ".n:base.":
  110. $casetable = $nounEnding;
  111. break;
  112. case ".ti:base.":
  113. case ".adj:base.":
  114. $casetable = $adjEnding;
  115. break;
  116. case "":
  117. $casetable=false;
  118. break;
  119. default:
  120. $casetable=false;
  121. break;
  122. }
  123. if($casetable === false){
  124. continue;
  125. }
  126. if($this->option('debug')) $this->info("{$word->word}:{$word->type}");
  127. foreach($casetable as $thiscase){
  128. if($word->type==".v:base."){
  129. $endLen = (int)$thiscase[0];
  130. $head = mb_substr($word->word,0,(0-$endLen),"UTF-8");//原词剩余的部分
  131. $newEnding = $thiscase[1];
  132. $newGrammar = $thiscase[2];
  133. $newword=$head.$thiscase[1];
  134. //动词不做符合规则判定
  135. $isMatch = true;
  136. }else{
  137. $endLen = (int)$thiscase[5];
  138. $end = mb_substr($word->word,0-$endLen,NULL,"UTF-8");//原词被切下来的部分
  139. $head = mb_substr($word->word,0,(0-$endLen),"UTF-8");//原词剩余的部分
  140. $newEnding = $thiscase[3];
  141. $newGrammar = $thiscase[4];
  142. $newword=$head.$thiscase[2];
  143. if($word->type==".n:base."){
  144. //名词
  145. if($thiscase[0]==$word->grammar && $thiscase[1]==$end){
  146. //符合规则判定成功
  147. $isMatch = true;
  148. }else{
  149. $isMatch = false;
  150. }
  151. }else{
  152. //形容词
  153. if($thiscase[1]==$end){
  154. //符合规则判定成功
  155. $isMatch = true;
  156. }else{
  157. $isMatch = false;
  158. }
  159. }
  160. }
  161. if($isMatch){
  162. if($this->option('debug')) $this->error($newword.':match');
  163. //查询这个词是否在三藏存在
  164. $exist = Cache::remember('palicanon/word/exists/'.$newword, 10 , function() use($newword) {
  165. return WbwTemplate::where('real',$newword)->exists();
  166. });
  167. if($exist){
  168. if($this->option('debug')) $this->info('exist');
  169. $new = UserDict::firstOrNew(
  170. [
  171. 'word' => $newword,
  172. 'type' => \str_replace(':base','',$word->type),
  173. 'grammar' => $newGrammar,
  174. 'parent' => $word->word,
  175. 'factors' => "{$word->word}+[{$newEnding}]",
  176. 'dict_id' => $this->dict_id,
  177. ],
  178. [
  179. 'id' => app('snowflake')->id(),
  180. 'source' => '_ROBOT_',
  181. 'create_time'=>(int)(microtime(true)*1000)
  182. ]
  183. );
  184. $new->confidence = 80;
  185. $new->language = 'cm';
  186. $new->creator_id = 1;
  187. $new->flag = 1;
  188. $new->save();
  189. }else{
  190. if($this->option('debug')) $this->info('not exist');
  191. }
  192. }
  193. }
  194. $bar->advance();
  195. }
  196. $bar->finish();
  197. //删除旧数据
  198. UserDict::where('dict_id',$this->dict_id)->where('flag',0)->delete();
  199. UserDict::where('dict_id',$this->dict_id)->where('flag',1)->update(['flag'=>0]);
  200. return 0;
  201. }
  202. }