UpgradeWordPart.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\UserDict;
  4. use App\Models\WordPart;
  5. use App\Tools\Tools;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Facades\Log;
  9. class UpgradeWordPart extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'upgrade:word.part';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = 'Command description';
  23. /**
  24. * Create a new command instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. }
  32. /**
  33. * Execute the console command.
  34. *
  35. * @return int
  36. */
  37. public function handle()
  38. {
  39. if (Tools::isStop()) {
  40. return 0;
  41. }
  42. $delete = WordPart::where('id', '>', 0)->delete();
  43. // 载入纸质词典数据
  44. $paper = UserDict::selectRaw('word,count(*)')->where('source', '_PAPER_')->groupBy('word')->cursor();
  45. $sql = "select
  46. count(*) from (
  47. select word, count(*) from user_dicts ud where source = '_PAPER_' group by word) as T";
  48. $count = DB::select($sql);
  49. $bar = $this->output->createProgressBar($count[0]->count);
  50. foreach ($paper as $key => $word) {
  51. $newWord = new WordPart;
  52. $newWord->word = $word->word;
  53. $newWord->weight = $word->count;
  54. $newWord->save();
  55. $bar->advance();
  56. }
  57. $bar->finish();
  58. // 载入csv数据
  59. $csvFile = config('mint.path.dict_text').'/system/part2.csv';
  60. if (($fp = fopen($csvFile, 'r')) !== false) {
  61. Log::info('csv load:'.$csvFile);
  62. while (($data = fgetcsv($fp, 0, ',')) !== false) {
  63. WordPart::updateOrCreate(['word' => $data[0]], ['weight' => $data[1]]);
  64. }
  65. fclose($fp);
  66. } else {
  67. $this->error('can not open csv file. filename='.$csvFile.PHP_EOL);
  68. Log::error('can not open csv file. filename='.$csvFile);
  69. }
  70. $this->info('ok');
  71. return 0;
  72. }
  73. }