ExportAiPaliWordToken.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Http\Api\DictApi;
  5. use Illuminate\Support\Facades\Log;
  6. use App\Models\UserDict;
  7. class ExportAiPaliWordToken extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. * php artisan export:ai.pali.word.token
  12. * @var string
  13. */
  14. protected $signature = 'export:ai.pali.word.token {--format=gz : zip file format 7z,lzma,gz }';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'export ai pali word token';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return int
  34. */
  35. public function handle()
  36. {
  37. $this->info('export ai pali word token');
  38. Log::debug('export ai pali word token');
  39. if (\App\Tools\Tools::isStop()) {
  40. return 0;
  41. }
  42. $exportDir = storage_path('app/tmp/export/offline');
  43. if (!is_dir($exportDir)) {
  44. $res = mkdir($exportDir, 0755, true);
  45. if (!$res) {
  46. $this->error('mkdir fail path=' . $exportDir);
  47. return 1;
  48. } else {
  49. $this->info('make dir successful ' . $exportDir);
  50. }
  51. }
  52. $dict_id = DictApi::getSysDict('system_preference');
  53. if (!$dict_id) {
  54. $this->error('没有找到 system_preference 字典');
  55. return 1;
  56. }
  57. $filename = 'ai-pali-word-token-' . date("Y-m-d") . '.tsv';
  58. $exportFile = $exportDir . '/' . $filename;
  59. $fp = fopen($exportFile, 'w');
  60. if ($fp === false) {
  61. $this->error('无法创建文件');
  62. return 1;
  63. }
  64. $start = time();
  65. $total = UserDict::where('dict_id', $dict_id)->count();
  66. $words = UserDict::where('dict_id', $dict_id)
  67. ->select([
  68. 'word',
  69. 'factors',
  70. ])->cursor();
  71. foreach ($words as $key => $word) {
  72. $output = array($word->word, $word->factors);
  73. fwrite($fp, implode("\t", $output) . "\n");
  74. if ($key % 100 === 0) {
  75. $present = (int)($key * 100 / $total);
  76. $this->info("[{$present}%]-{$key}");
  77. }
  78. }
  79. fclose($fp);
  80. $this->info((time() - $start) . ' seconds');
  81. $this->call('export:zip', [
  82. 'id' => 'ai-pali-word-token',
  83. 'filename' => $exportFile,
  84. 'title' => 'ai pali word token',
  85. 'format' => $this->option('format'),
  86. ]);
  87. return 0;
  88. }
  89. }