UpgradeCompound.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace App\Console\Commands;
  3. require_once __DIR__."/../../../public/app/dict/turbo_split.php";
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Facades\Storage;
  6. use App\Models\WordIndex;
  7. use App\Models\WbwTemplate;
  8. use App\Models\UserDict;
  9. use App\Tools\TurboSplit;
  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. protected $dict_id = 'c42980f0-5967-4833-b695-84183344f68f';
  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. $start = \microtime(true);
  42. $_word = $this->argument('word');
  43. if(!empty($_word)){
  44. $ts = new TurboSplit();
  45. $results = $ts->splitA($_word);
  46. Storage::disk('local')->put("tmp/compound1.csv", "word,type,grammar,parent,factors");
  47. foreach ($results as $key => $value) {
  48. # code...
  49. Storage::disk('local')->append("tmp/compound1.csv", "{$value['word']},{$value['type']},{$value['grammar']},{$value['parent']},{$value['factors']}");
  50. }
  51. return 0;
  52. }
  53. //
  54. if($this->option('test')){
  55. //调试代码
  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)->whereBetween('len',[$take[0],$take[1]])->select('word')->take($take[2])->get();
  67. foreach ($words as $word) {
  68. $this->info($word->word);
  69. Storage::disk('local')->append("tmp/compound.md", "## {$word->word}");
  70. $parts = $ts->splitA($word->word);
  71. foreach ($parts as $part) {
  72. # code...
  73. $this->info("{$part['word']},{$part['factors']},{$part['confidence']}");
  74. Storage::disk('local')->append("tmp/compound.md", "- `{$part['word']}`,{$part['factors']},{$part['confidence']}");
  75. }
  76. }
  77. }
  78. $this->info("耗时:".\microtime(true)-$start);
  79. return 0;
  80. }
  81. //$words = WordIndex::where('final',0)->select('word')->orderBy('count','desc')->skip(72300)->cursor();
  82. $words = WbwTemplate::select('real')
  83. ->where('book',118)
  84. ->whereBetween('paragraph',[1329,1367])
  85. ->where('type','<>','.ctl.')
  86. ->where('real','<>','')
  87. ->groupBy('real')->cursor();
  88. $count = 0;
  89. foreach ($words as $key => $word) {
  90. //先看目前字典里有没有
  91. $isExists = UserDict::where('word',$word->real)
  92. ->whereIn('dict_id',[
  93. '57afac99-0887-455c-b18e-67c8682158b0',
  94. '4d3a0d92-0adc-4052-80f5-512a2603d0e8'
  95. ])
  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' => $this->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',$this->dict_id)->where('flag',0)->delete();
  136. UserDict::where('dict_id',$this->dict_id)->where('flag',1)->update(['flag'=>0]);
  137. return 0;
  138. }
  139. }