UpgradeCompound.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. var_dump($ts->splitA($_word));
  46. return 0;
  47. }
  48. //
  49. if($this->option('test')){
  50. //调试代码
  51. Storage::disk('local')->put("tmp/compound.md", "# Turbo Split");
  52. //获取需要拆的词
  53. $list = [
  54. [5,20,20],
  55. [21,30,20],
  56. [31,40,10],
  57. [41,60,10],
  58. ];
  59. foreach ($list as $take) {
  60. # code...
  61. $words = WordIndex::where('final',0)->whereBetween('len',[$take[0],$take[1]])->select('word')->take($take[2])->get();
  62. foreach ($words as $word) {
  63. $this->info($word->word);
  64. Storage::disk('local')->append("tmp/compound.md", "## {$word->word}");
  65. $parts = $ts->splitA($word->word);
  66. foreach ($parts as $part) {
  67. # code...
  68. $this->info("{$part['word']},{$part['factors']},{$part['confidence']}");
  69. Storage::disk('local')->append("tmp/compound.md", "- `{$part['word']}`,{$part['factors']},{$part['confidence']}");
  70. }
  71. }
  72. }
  73. $this->info("耗时:".\microtime(true)-$start);
  74. return 0;
  75. }
  76. $words = WordIndex::where('final',0)->select('word')->orderBy('count','desc')->skip(72300)->cursor();
  77. //$words = WbwTemplate::select('real')->where('type','<>','.ctl.')->where('real','<>','')->groupBy('real')->cursor();
  78. $count = 0;
  79. foreach ($words as $key => $word) {
  80. # code...
  81. $count++;
  82. $this->info("{$count}:{$word->word}");
  83. $ts = new TurboSplit();
  84. $parts = $ts->splitA($word->word);
  85. foreach ($parts as $part) {
  86. $new = UserDict::firstOrNew(
  87. [
  88. 'word' => $part['word'],
  89. 'factors' => $part['factors'],
  90. 'dict_id' => $dict_id,
  91. ],
  92. [
  93. 'id' => app('snowflake')->id(),
  94. 'source' => '_ROBOT_',
  95. 'create_time'=>(int)(microtime(true)*1000),
  96. ]
  97. );
  98. if(isset($part['type'])){
  99. $new->type = $part['type'];
  100. }else{
  101. $new->type = ".cp.";
  102. }
  103. $new->confidence = (int)(50*$part['confidence']);
  104. $new->language = 'cm';
  105. $new->creator_id = 1;
  106. $new->flag = 1;
  107. $new->save();
  108. }
  109. }
  110. //删除旧数据
  111. UserDict::where('dict_id',$dict_id)->where('flag',0)->delete();
  112. UserDict::where('dict_id',$dict_id)->where('flag',1)->update(['flag'=>0]);
  113. return 0;
  114. }
  115. }