UpgradeWordPart.php 2.1 KB

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