UpgradeCompound.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Storage;
  5. use App\Models\WordIndex;
  6. use App\Models\WbwTemplate;
  7. use App\Models\UserDict;
  8. use App\Tools\TurboSplit;
  9. use App\Http\Api\DictApi;
  10. use Illuminate\Support\Facades\DB;
  11. use Illuminate\Support\Facades\Log;
  12. class UpgradeCompound extends Command
  13. {
  14. /**
  15. * The name and signature of the console command.
  16. *
  17. * @var string
  18. */
  19. protected $signature = 'upgrade:compound {word?} {--book=} {--debug} {--test} {--continue}';
  20. /**
  21. * The console command description.
  22. *
  23. * @var string
  24. */
  25. protected $description = 'auto split compound word';
  26. /**
  27. * Create a new command instance.
  28. *
  29. * @return void
  30. */
  31. public function __construct()
  32. {
  33. parent::__construct();
  34. }
  35. /**
  36. * Execute the console command.
  37. *
  38. * @return int
  39. */
  40. public function handle()
  41. {
  42. if(file_exists(base_path('.stop'))){
  43. $this->info('.stop exists');
  44. return 0;
  45. }
  46. $dict_id = DictApi::getSysDict('robot_compound');
  47. if(!$dict_id){
  48. $this->error('没有找到 robot_compound 字典');
  49. return 1;
  50. }
  51. $start = \microtime(true);
  52. //
  53. if($this->option('test')){
  54. //调试代码
  55. $ts = new TurboSplit();
  56. Storage::disk('local')->put("tmp/compound.md", "# Turbo Split");
  57. //获取需要拆的词
  58. $list = [
  59. [5,20,20],
  60. [21,30,20],
  61. [31,40,10],
  62. [41,60,10],
  63. ];
  64. foreach ($list as $take) {
  65. # code...
  66. $words = WordIndex::where('final',0)
  67. ->whereBetween('len',[$take[0],$take[1]])
  68. ->select('word')
  69. ->take($take[2])->get();
  70. foreach ($words as $word) {
  71. $this->info($word->word);
  72. Storage::disk('local')->append("tmp/compound.md", "## {$word->word}");
  73. $parts = $ts->splitA($word->word);
  74. foreach ($parts as $part) {
  75. # code...
  76. $info = "`{$part['word']}`,{$part['factors']},{$part['confidence']}";
  77. $this->info($info);
  78. Storage::disk('local')->append("tmp/compound.md", "- {$info}");
  79. }
  80. }
  81. }
  82. $this->info("耗时:".\microtime(true)-$start);
  83. return 0;
  84. }
  85. $_word = $this->argument('word');
  86. if(!empty($_word)){
  87. $words = array((object)array('real'=>$_word));
  88. $count[] = (object)array('count'=>1);
  89. }else if($this->option('book')){
  90. $words = WbwTemplate::select('real')
  91. ->where('book',$this->option('book'))
  92. ->where('type','<>','.ctl.')
  93. ->where('real','<>','')
  94. ->orderBy('real')
  95. ->groupBy('real')->cursor();
  96. $count = DB::select('SELECT count(*) from (
  97. SELECT "real" from wbw_templates where book = ? and type <> ? and real <> ? group by real) T',
  98. [$this->option('book'),'.ctl.','']);
  99. }else{
  100. $words = WbwTemplate::select('real')
  101. ->where('type','<>','.ctl.')
  102. ->where('real','<>','')
  103. ->orderBy('real')
  104. ->groupBy('real')->cursor();
  105. $count = DB::select('SELECT count(*) from (
  106. SELECT "real" from wbw_templates where type <> ? and real <> ? group by real) T',
  107. ['.ctl.','']);
  108. }
  109. $bar = $this->output->createProgressBar($count[0]->count);
  110. foreach ($words as $key => $word) {
  111. $bar->advance();
  112. if($this->option('continue')){
  113. //先看目前字典里有没有已经拆过的这个词
  114. $isExists = UserDict::where('word',$word->real)
  115. ->where('dict_id',$dict_id)
  116. ->where('flag',1)
  117. ->exists();
  118. if($isExists){
  119. continue;
  120. }
  121. }
  122. //删除该词旧数据
  123. UserDict::where('word',$word->real)
  124. ->where('dict_id',$dict_id)
  125. ->delete();
  126. $ts = new TurboSplit();
  127. if($this->option('debug')){
  128. $ts->debug(true);
  129. }
  130. $parts = $ts->splitA($word->real);
  131. if(!empty($_word)){
  132. Storage::disk('local')->put("tmp/compound1.csv", "word,type,grammar,parent,factors");
  133. }
  134. $count = 0;
  135. foreach ($parts as $part) {
  136. if(isset($part['type']) && $part['type'] === ".v."){
  137. continue;
  138. }
  139. $count++;
  140. $new = new UserDict;
  141. $new->id = app('snowflake')->id();
  142. $new->word = $part['word'];
  143. $new->factors = $part['factors'];
  144. $new->dict_id = $dict_id;
  145. $new->source = '_ROBOT_';
  146. $new->create_time = (int)(microtime(true)*1000);
  147. if(isset($part['type'])){
  148. $new->type = $part['type'];
  149. }else{
  150. $new->type = ".cp.";
  151. }
  152. if(isset($part['grammar'])){
  153. $new->grammar = $part['grammar'];
  154. }
  155. if(isset($part['parent'])){
  156. $new->parent = $part['parent'];
  157. }
  158. $new->confidence = 50*$part['confidence'];
  159. $new->note = $part['confidence'];
  160. $new->language = 'cm';
  161. $new->creator_id = 1;
  162. $new->flag = 1;//标记为维护状态
  163. $new->save();
  164. if(!empty($_word)){
  165. $output = "{$part['word']},{$part['type']},{$part['grammar']},{$part['parent']},{$part['factors']},{$part['confidence']}";
  166. $this->info($count);
  167. $this->info($output);
  168. Storage::disk('local')->append("tmp/compound1.csv", $output);
  169. }
  170. }
  171. }
  172. //维护状态数据改为正常状态
  173. UserDict::where('dict_id',$dict_id)->where('flag',1)->update(['flag'=>0]);
  174. $bar->finish();
  175. return 0;
  176. }
  177. }