UpgradeRegular.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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';
  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. $words = UserDict::where('type','.n:base.')
  71. ->orWhere('type','.v:base.')
  72. ->orWhere('type','.adj:base.')
  73. ->orWhere('type','.ti:base.')
  74. ->select(['word','type','grammar'])
  75. ->groupBy(['word','type','grammar'])
  76. ->orderBy('word');
  77. $query = "
  78. select count(*) from (select count(*) from user_dicts ud where
  79. \"type\" = '.v:base.' or
  80. \"type\" = '.n:base.' or
  81. \"type\" = '.ti:base.' or
  82. \"type\" = '.adj:base.'
  83. group by word,type,grammar) as t;
  84. ";
  85. $count = DB::select($query);
  86. $bar = $this->output->createProgressBar($count[0]->count);
  87. /*
  88. $words = UserDict::where('word','ābandhattalakkhaṇa')
  89. ->select(['word','type','grammar'])
  90. ->groupBy(['word','type','grammar']);
  91. $bar = $this->output->createProgressBar(1);
  92. */
  93. foreach ($words->cursor() as $word) {
  94. # code...
  95. switch($word->type){
  96. case ".v:base.":
  97. $casetable=$verbEnding;
  98. break;
  99. case ".n:base.":
  100. $casetable = $nounEnding;
  101. break;
  102. case ".ti:base.":
  103. case ".adj:base.":
  104. $casetable = $adjEnding;
  105. break;
  106. case "":
  107. $casetable=false;
  108. break;
  109. default:
  110. $casetable=false;
  111. break;
  112. }
  113. if($casetable === false){
  114. continue;
  115. }
  116. //$this->info("{$word->word}:{$word->type}");
  117. foreach($casetable as $thiscase){
  118. if($word->type==".v:base."){
  119. $endLen = (int)$thiscase[0];
  120. $head = mb_substr($word->word,0,(0-$endLen),"UTF-8");//原词剩余的部分
  121. $newEnding = $thiscase[1];
  122. $newGrammar = $thiscase[2];
  123. $newword=$head.$thiscase[1];
  124. //动词不做符合规则判定
  125. $isMatch = true;
  126. }else{
  127. $endLen = (int)$thiscase[5];
  128. $end = mb_substr($word->word,0-$endLen,NULL,"UTF-8");//原词被切下来的部分
  129. $head = mb_substr($word->word,0,(0-$endLen),"UTF-8");//原词剩余的部分
  130. $newEnding = $thiscase[3];
  131. $newGrammar = $thiscase[4];
  132. $newword=$head.$thiscase[2];
  133. if($word->type==".n:base."){
  134. //名词
  135. if($thiscase[0]==$word->grammar && $thiscase[1]==$end){
  136. //符合规则判定成功
  137. $isMatch = true;
  138. }else{
  139. $isMatch = false;
  140. }
  141. }else{
  142. //形容词
  143. if($thiscase[1]==$end){
  144. //符合规则判定成功
  145. $isMatch = true;
  146. }else{
  147. $isMatch = false;
  148. }
  149. }
  150. }
  151. if($isMatch){
  152. //$this->error($newword.':match');
  153. //查询这个词是否在三藏存在
  154. $exist = Cache::remember('palicanon/word/exists/'.$newword, 10 , function() use($newword) {
  155. return WbwTemplate::where('real',$newword)->exists();
  156. });
  157. if($exist){
  158. $new = UserDict::firstOrNew(
  159. [
  160. 'word' => $newword,
  161. 'type' => \str_replace(':base','',$word->type),
  162. 'grammar' => $newGrammar,
  163. 'parent' => $word->word,
  164. 'factors' => "{$word->word}+[{$newEnding}]",
  165. 'dict_id' => $this->dict_id,
  166. ],
  167. [
  168. 'id' => app('snowflake')->id(),
  169. 'source' => '_ROBOT_',
  170. 'create_time'=>(int)(microtime(true)*1000)
  171. ]
  172. );
  173. $new->confidence = 80;
  174. $new->language = 'cm';
  175. $new->creator_id = 1;
  176. $new->flag = 1;
  177. $new->save();
  178. }
  179. }
  180. }
  181. $bar->advance();
  182. }
  183. $bar->finish();
  184. //删除旧数据
  185. UserDict::where('dict_id',$this->dict_id)->where('flag',0)->delete();
  186. UserDict::where('dict_id',$this->dict_id)->where('flag',1)->update(['flag'=>0]);
  187. return 0;
  188. }
  189. }