ExportPaliSynonyms.php 2.6 KB

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