ExportPaliSynonyms.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. /*
  42. //irregular
  43. $dictId = ['4d3a0d92-0adc-4052-80f5-512a2603d0e8'];
  44. //regular
  45. $dictId[] = DictApi::getSysDict('system_regular');
  46. $dictId[] = DictApi::getSysDict('robot_compound');
  47. */
  48. $filename = $this->option('output');
  49. $fp = fopen($filename, 'w') or die("Unable to open file!");
  50. $parents = UserDict::select('parent')
  51. ->whereNotNull('parent')
  52. ->where('parent', '<>', '')
  53. ->groupBy('parent')->cursor();
  54. foreach ($parents as $parent) {
  55. if (str_contains($parent->parent, ' ')) {
  56. continue;
  57. }
  58. $words = UserDict::where('parent', $parent->parent)
  59. ->select('word')
  60. ->groupBy('word')->get();
  61. $wordsList = [];
  62. foreach ($words as $word) {
  63. $wordsList[$word->word] = 1;
  64. }
  65. $teams = DhammaTerm::where('word', $parent->parent)
  66. ->select(['meaning'])->get();
  67. foreach ($teams as $term) {
  68. $wordsList[$term->meaning] = 1;
  69. }
  70. $this->info("[{$parent->parent}] " . count($words) . " team=" . count($teams));
  71. // 合并 $parent->parent, $words->word, $team->meaning 为一个字符串数组
  72. $combinedArray = [];
  73. $combinedArray[] = $parent->parent;
  74. foreach ($wordsList as $word => $value) {
  75. $combinedArray[] = $word;
  76. }
  77. // 将 $combinedArray 写入 CSV 文件
  78. fputcsv($fp, $combinedArray);
  79. }
  80. // 关闭文件
  81. fclose($fp);
  82. $this->info('done');
  83. return 0;
  84. }
  85. }