UpgradeCompound.php 5.9 KB

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