UpgradeDictDefaultMeaning.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * 刷新字典单词的默认意思
  4. * 目标:
  5. * 可以查询到某个单词某种语言的首选意思
  6. * 算法:
  7. * 1. 某种语言会有多个字典。按照字典重要程度人工排序
  8. * 2. 按照顺序搜索这些字典。找到第一个意思就停止。
  9. */
  10. namespace App\Console\Commands;
  11. use App\Models\UserDict;
  12. use App\Tools\Tools;
  13. use Illuminate\Console\Command;
  14. use Illuminate\Support\Facades\Cache;
  15. use Illuminate\Support\Facades\Log;
  16. class UpgradeDictDefaultMeaning extends Command
  17. {
  18. /**
  19. * The name and signature of the console command.
  20. *
  21. * @var string
  22. */
  23. protected $signature = 'upgrade:dict.default.meaning {word?}';
  24. protected $dict = [
  25. 'zh-Hans' => [
  26. '8833de18-0978-434c-b281-a2e7387f69be', /* 巴汉字典明法尊者修订版 */
  27. 'f364d3dc-b611-471b-9a4f-531286b8c2c3', /* 《巴汉词典》Mahāñāṇo Bhikkhu编著 */
  28. '0e4dc5c8-a228-4693-92ba-7d42918d8a91', /* 汉译パーリ语辞典-黃秉榮 */
  29. '6aa9ec8b-bba4-4bcd-abd2-9eae015bad2b', /* 汉译パーリ语辞典-李瑩 */
  30. 'eb99f8b4-c3e5-43af-9102-6a93fcb97db6', /* パーリ语辞典--勘误表 */
  31. '0d79e8e8-1430-4c99-a0f1-b74f2b4b26d8', /* 《巴汉词典》增订 */
  32. ],
  33. 'zh-Hant' => [
  34. '3acf0c0f-59a7-4d25-a3d9-bf394a266ebd', /* 汉译パーリ语辞典-黃秉榮 */
  35. '5293ffb9-887e-4cf2-af78-48bf52a85304', /* 巴利詞根 */
  36. ],
  37. 'jp' => [
  38. '91d3ec93-3811-4973-8d84-ced99179a0aa', /* パーリ语辞典 */
  39. '6d6c6812-75e7-457d-874f-5b049ad4b6de', /* パーリ语辞典-增补 */
  40. ],
  41. 'en' => [
  42. 'c6e70507-4a14-4687-8b70-2d0c7eb0cf21', /* Concise P-E Dict */
  43. 'eae9fd6f-7bac-4940-b80d-ad6cd6f433bf', /* Concise P-E Dict */
  44. '2f93d0fe-3d68-46ee-a80b-11fa445a29c6', /* unity */
  45. 'b9163baf-2bca-41a5-a936-5a0834af3945', /* Pali-Dict Vri */
  46. 'b089de57-f146-4095-b886-057863728c43', /* Buddhist Dictionary */
  47. '6afb8c05-5cbe-422e-b691-0d4507450cb7', /* PTS P-E dictionary */
  48. '0bfd87ec-f3ac-49a2-985e-28388779078d', /* Pali Proper Names Dict */
  49. '1cdc29e0-6783-4241-8784-5430b465b79c', /* Pāḷi Root In Saddanīti */
  50. '5718cbcf-684c-44d4-bbf2-4fa12f2588a4', /* Critical Pāli Dictionary */
  51. ],
  52. 'my' => [
  53. 'e740ef40-26d7-416e-96c2-925d6650ac6b', /* Tipiṭaka Pāḷi-Myanmar */
  54. 'beb45062-7c20-4047-bcd4-1f636ba443d1', /* U Hau Sein’s Pāḷi-Myanmar Dictionary */
  55. '1e299ccb-4fc4-487d-8d72-08f63d84c809', /* Pali Roots Dictionary */
  56. '6f9caea1-17fa-41f1-92e5-bd8e6e70e1d7', /* U Hau Sein’s Pāḷi-Myanmar */
  57. ],
  58. 'vi' => [
  59. '23f67523-fa03-48d9-9dda-ede80d578dd2', /* Pali Viet Dictionary */
  60. '4ac8a0d5-9c6f-4b9f-983d-84288d47f993', /* Pali Viet Abhi-Terms */
  61. '7c7ee287-35ba-4cf3-b87b-30f1fa6e57c9', /* Pali Viet Vinaya Terms */
  62. ],
  63. 'cm' => [],
  64. ];
  65. /**
  66. * The console command description.
  67. *
  68. * @var string
  69. */
  70. protected $description = '找出单词的首选意思。用于搜索列表';
  71. /**
  72. * Create a new command instance.
  73. *
  74. * @return void
  75. */
  76. public function __construct()
  77. {
  78. parent::__construct();
  79. }
  80. /**
  81. * Execute the console command.
  82. *
  83. * @return int
  84. */
  85. public function handle()
  86. {
  87. if (Tools::isStop()) {
  88. return 0;
  89. }
  90. $_word = $this->argument('word');
  91. // 获取字典中所有的语言
  92. $langInDict = UserDict::select('language')->groupBy('language')->get();
  93. $languages = [];
  94. foreach ($langInDict as $lang) {
  95. if (! empty($lang['language'])) {
  96. $languages[] = $lang['language'];
  97. }
  98. }
  99. // print_r($languages);
  100. foreach ($this->dict as $thisLang => $dictId) {
  101. $this->info("running $thisLang");
  102. $bar = $this->output->createProgressBar(UserDict::where('source', '_PAPER_')
  103. ->where('language', $thisLang)->count());
  104. foreach (
  105. UserDict::where('source', '_PAPER_')
  106. ->where('language', $thisLang)
  107. ->select('word', 'note')
  108. ->cursor() as $word
  109. ) {
  110. if (! empty($word['note'])) {
  111. Cache::put("dict_first_mean/{$thisLang}/{$word['word']}", mb_substr($word['note'], 0, 50, 'UTF-8'));
  112. }
  113. $bar->advance();
  114. }
  115. $bar->finish();
  116. for ($i = count($dictId) - 1; $i >= 0; $i--) {
  117. // code...
  118. $this->info("running $thisLang - {$dictId[$i]}");
  119. $count = 0;
  120. foreach (
  121. UserDict::where('dict_id', $dictId[$i])
  122. ->select('word', 'note')
  123. ->cursor() as $word
  124. ) {
  125. $cacheKey = "dict_first_mean/{$thisLang}/{$word['word']}";
  126. if (! empty($word['note'])) {
  127. $cacheValue = mb_substr($word['note'], 0, 50, 'UTF-8');
  128. if (! empty($_word) && $word['word'] === $_word) {
  129. Log::info($cacheKey.':'.$cacheValue);
  130. }
  131. Cache::put($cacheKey, $cacheValue);
  132. }
  133. if ($count % 1000 === 0) {
  134. $this->info("{$count}");
  135. }
  136. $count++;
  137. }
  138. }
  139. }
  140. return 0;
  141. }
  142. }