ExportPaliSynonyms.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Http\Api\DictApi;
  5. use App\Models\UserDict;
  6. use App\Models\DhammaTerm;
  7. use Illuminate\Support\Facades\Redis;
  8. use Illuminate\Support\Facades\Log;
  9. class ExportPaliSynonyms extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. * php artisan export:pali.synonyms --output=
  14. * @var string
  15. */
  16. protected $signature = 'export:pali.synonyms {--output=}';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = '导出openSearch用的巴利语变格表';
  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 (!$this->option('output')) {
  40. $this->error('please set output file option --output=file');
  41. return 1;
  42. }
  43. //irregular
  44. $dictId = ['4d3a0d92-0adc-4052-80f5-512a2603d0e8'];
  45. //regular
  46. $dictId[] = DictApi::getSysDict('system_regular');
  47. $filename = $this->option('output');
  48. $fp = fopen($filename, 'w') or die("Unable to open file!");
  49. foreach ($dictId as $dict) {
  50. $parents = UserDict::where('dict_id', $dict)
  51. ->select('parent')
  52. ->groupBy('parent')->cursor();
  53. foreach ($parents as $parent) {
  54. $words = UserDict::where('dict_id', $dict)
  55. ->where('parent', $parent->parent)
  56. ->select('word')
  57. ->groupBy('word')->get();
  58. $wordsList = [];
  59. foreach ($words as $word) {
  60. $wordsList[$word->word] = 1;
  61. }
  62. $teams = DhammaTerm::where('word', $parent->parent)
  63. ->select(['meaning'])->get();
  64. foreach ($teams as $term) {
  65. $wordsList[$term->meaning] = 1;
  66. }
  67. $this->info("[{$parent->parent}] " . count($words) . " team=" . count($teams));
  68. // 合并 $parent->parent, $words->word, $team->meaning 为一个字符串数组
  69. $combinedArray = [];
  70. $combinedArray[] = $parent->parent;
  71. foreach ($wordsList as $word => $value) {
  72. $combinedArray[] = $word;
  73. }
  74. // 将 $combinedArray 写入 CSV 文件
  75. fputcsv($fp, $combinedArray);
  76. }
  77. }
  78. // 关闭文件
  79. fclose($fp);
  80. $this->info('done');
  81. return 0;
  82. }
  83. }