UpgradeCompound.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. class UpgradeCompound extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'upgrade:compound {word?} {--test}';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = 'Command description';
  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. $dict_id = DictApi::getSysDict('robot_compound');
  41. if(!$dict_id){
  42. $this->error('没有找到 robot_compound 字典');
  43. return 1;
  44. }
  45. $start = \microtime(true);
  46. $_word = $this->argument('word');
  47. if(!empty($_word)){
  48. $ts = new TurboSplit();
  49. $results = $ts->splitA($_word);
  50. Storage::disk('local')->put("tmp/compound1.csv", "word,type,grammar,parent,factors");
  51. foreach ($results as $key => $value) {
  52. # code...
  53. Storage::disk('local')->append("tmp/compound1.csv", "{$value['word']},{$value['type']},{$value['grammar']},{$value['parent']},{$value['factors']}");
  54. }
  55. return 0;
  56. }
  57. //
  58. if($this->option('test')){
  59. //调试代码
  60. $ts = new TurboSplit();
  61. Storage::disk('local')->put("tmp/compound.md", "# Turbo Split");
  62. //获取需要拆的词
  63. $list = [
  64. [5,20,20],
  65. [21,30,20],
  66. [31,40,10],
  67. [41,60,10],
  68. ];
  69. foreach ($list as $take) {
  70. # code...
  71. $words = WordIndex::where('final',0)->whereBetween('len',[$take[0],$take[1]])->select('word')->take($take[2])->get();
  72. foreach ($words as $word) {
  73. $this->info($word->word);
  74. Storage::disk('local')->append("tmp/compound.md", "## {$word->word}");
  75. $parts = $ts->splitA($word->word);
  76. foreach ($parts as $part) {
  77. # code...
  78. $this->info("{$part['word']},{$part['factors']},{$part['confidence']}");
  79. Storage::disk('local')->append("tmp/compound.md", "- `{$part['word']}`,{$part['factors']},{$part['confidence']}");
  80. }
  81. }
  82. }
  83. $this->info("耗时:".\microtime(true)-$start);
  84. return 0;
  85. }
  86. //$words = WordIndex::where('final',0)->select('word')->orderBy('count','desc')->skip(72300)->cursor();
  87. $words = WbwTemplate::select('real')
  88. ->where('type','<>','.ctl.')
  89. ->where('real','<>','')
  90. ->groupBy('real')->cursor();
  91. $count = 0;
  92. foreach ($words as $key => $word) {
  93. //先看目前字典里有没有
  94. $isExists = UserDict::where('word',$word->real)
  95. ->where('dict_id',"<>",$dict_id)
  96. ->exists();
  97. if($isExists){
  98. $this->info("found:{$word->real}");
  99. continue;
  100. }
  101. # code...
  102. $count++;
  103. $this->info("{$count}:{$word->real}");
  104. $ts = new TurboSplit();
  105. $parts = $ts->splitA($word->real);
  106. foreach ($parts as $part) {
  107. $new = UserDict::firstOrNew(
  108. [
  109. 'word' => $part['word'],
  110. 'factors' => $part['factors'],
  111. 'dict_id' => $dict_id,
  112. ],
  113. [
  114. 'id' => app('snowflake')->id(),
  115. 'source' => '_ROBOT_',
  116. 'create_time'=>(int)(microtime(true)*1000),
  117. ]
  118. );
  119. if(isset($part['type'])){
  120. $new->type = $part['type'];
  121. }else{
  122. $new->type = ".cp.";
  123. }
  124. if(isset($part['grammar'])) $new->parent = $part['grammar'];
  125. if(isset($part['parent'])) $new->parent = $part['parent'];
  126. $new->confidence = 50*$part['confidence'];
  127. $new->note = $part['confidence'];
  128. $new->language = 'cm';
  129. $new->creator_id = 1;
  130. $new->flag = 1;
  131. $new->save();
  132. }
  133. }
  134. //删除旧数据
  135. UserDict::where('dict_id',$dict_id)->where('flag',0)->delete();
  136. UserDict::where('dict_id',$dict_id)->where('flag',1)->update(['flag'=>0]);
  137. return 0;
  138. }
  139. }