UpgradeCompound.php 6.2 KB

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