UpgradeDictDefaultMeaning.php 5.5 KB

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