ExportPaliSynonyms.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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
  14. * @var string
  15. */
  16. protected $signature = 'export:pali.synonyms';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = 'Command description';
  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. //irregular
  40. $dictId = ['4d3a0d92-0adc-4052-80f5-512a2603d0e8'];
  41. //regular
  42. $dictId[] = DictApi::getSysDict('system_regular');
  43. $path = storage_path('app/export/fts');
  44. if (!is_dir($path)) {
  45. $res = mkdir($path, 0700, true);
  46. if (!$res) {
  47. Log::error('mkdir fail path=' . $path);
  48. return 1;
  49. }
  50. }
  51. $filename = "/pali_synonyms.txt";
  52. $fp = fopen($path . $filename, 'w') or die("Unable to open file!");
  53. foreach ($dictId as $key => $dict) {
  54. $parents = UserDict::where('dict_id', $dict)
  55. ->select('parent')
  56. ->groupBy('parent')->cursor();
  57. foreach ($parents as $key => $parent) {
  58. $words = UserDict::where('dict_id', $dict)
  59. ->where('parent', $parent->parent)
  60. ->select('word')
  61. ->groupBy('word')->get();
  62. $wordsList = [];
  63. foreach ($words as $word) {
  64. $wordsList[$word->word] = 1;
  65. }
  66. $teams = DhammaTerm::where('word', $parent->parent)
  67. ->select(['meaning'])->get();
  68. foreach ($teams as $term) {
  69. $wordsList[$term->meaning] = 1;
  70. }
  71. $this->info("[{$parent->parent}] " . count($words) . " team=" . count($teams));
  72. // 合并 $parent->parent, $words->word, $team->meaning 为一个字符串数组
  73. $combinedArray = [];
  74. $combinedArray[] = $parent->parent;
  75. foreach ($wordsList as $word => $value) {
  76. $combinedArray[] = $word;
  77. }
  78. // 将 $combinedArray 写入 CSV 文件
  79. fputcsv($fp, $combinedArray);
  80. }
  81. }
  82. // 关闭文件
  83. fclose($fp);
  84. $this->info('done');
  85. return 0;
  86. }
  87. }