UpgradeWordPart.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. $delete = WordPart::where('id','>',0)->delete();
  39. #载入纸质词典数据
  40. $paper = UserDict::selectRaw('word,count(*)')->where("source",'_PAPER_')->groupBy('word')->cursor();
  41. $sql = "select
  42. count(*) from (
  43. select word, count(*) from user_dicts ud where source = '_PAPER_' group by word) as T";
  44. $count = DB::select($sql);
  45. $bar = $this->output->createProgressBar($count[0]->count);
  46. foreach ($paper as $key => $word) {
  47. $newWord = new WordPart;
  48. $newWord->word = $word->word;
  49. $newWord->weight = $word->count;
  50. $newWord->save();
  51. $bar->advance();
  52. }
  53. $bar->finish();
  54. #载入csv数据
  55. $csvFile = config("mint.path.dict_text") .'/system/part2.csv';
  56. if (($fp = fopen($csvFile, "r")) !== false) {
  57. Log::info("csv load:" . $csvFile);
  58. while (($data = fgetcsv($fp, 0, ',')) !== false) {
  59. WordPart::updateOrCreate(['word' => $data[0],],['weight' => $data[1],]);
  60. }
  61. fclose($fp);
  62. } else {
  63. $this->error( "can not open csv file. filename=" . $csvFile. PHP_EOL) ;
  64. Log::error( "can not open csv file. filename=" . $csvFile) ;
  65. }
  66. $this->info('ok');
  67. return 0;
  68. }
  69. }