UpgradeCompound.php 2.8 KB

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