UpgradeWbwWeight.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\WbwTemplate;
  4. use Illuminate\Console\Command;
  5. class UpgradeWbwWeight extends Command
  6. {
  7. /**
  8. * The name and signature of the console command.
  9. * php artisan upgrade:wbw.weight 66
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'upgrade:wbw.weight {book?} {para?}';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'upgrade wbw template weight by word bold ';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return int
  33. */
  34. public function handle()
  35. {
  36. $book = $this->argument('book');
  37. $para = $this->argument('para');
  38. for ($iBook = 1; $iBook <= 217; $iBook++) {
  39. if ($book && $book != $iBook) {
  40. continue;
  41. }
  42. $this->info('running book='.$iBook);
  43. $maxPara = WbwTemplate::where('book', $iBook)->max('paragraph');
  44. $bar = $this->output->createProgressBar($maxPara);
  45. // 以段落为单元计算权重
  46. for ($iPara = 1; $iPara <= $maxPara; $iPara++) {
  47. if ($para && $para != $iPara) {
  48. continue;
  49. }
  50. $bar->advance();
  51. $words = WbwTemplate::where('book', $iBook)
  52. ->where('paragraph', $iPara)
  53. ->orderBy('wid', 'asc')
  54. ->get();
  55. $katama = false; // 单词前面是否有katama
  56. $katamaDis = 0; // 单词与katama的距离
  57. $katamaWeight = 1; // katama 权重
  58. $arrKatama = []; // katama 权重结果
  59. //
  60. /**
  61. * 先计算katama权重
  62. * 将每个单词的katama权重放在arrKatama数组里面
  63. */
  64. for ($iWord = 0; $iWord < count($words); $iWord++) {
  65. // 计算katama加分
  66. if (empty($words[$iWord]->real)) {
  67. $katama = false;
  68. $katamaWeight = 0;
  69. }
  70. if ($katama) {
  71. $katamaDis++;
  72. $katamaWeight = 463 * (pow(0.2, ($katamaDis - 1)) + 1);
  73. }
  74. $arrKatama[] = $katamaWeight;
  75. if (mb_substr($words[$iWord]->real, 0, 5, 'UTF-8') === 'katam') {
  76. $katama = true;
  77. $katamaDis = 0;
  78. }
  79. }
  80. $start = -1; // 黑体字开始
  81. $bold = 0; // 连续黑体字计数器
  82. for ($iWord = 0; $iWord < count($words); $iWord++) {
  83. $wid = $words[$iWord]->wid;
  84. $weight = 1.01;
  85. WbwTemplate::where('id', $words[$iWord]->id)->update(['weight' => $weight * 1000]);
  86. if ($words[$iWord]->style === 'bld' && ! empty($words[$iWord]->real)) {
  87. // 是黑体字
  88. if ($start === -1) {
  89. // 黑体计数尚未开始
  90. $start = $iWord;
  91. $bold = 1;
  92. } else {
  93. $bold++;
  94. }
  95. } else {
  96. if ($start >= 0) {
  97. /**
  98. * 某词自己不是黑体,但是前面的词是黑体字
  99. * 先保存这个词
  100. * 然后,修改前面的黑体字单词的权重
  101. */
  102. $result = WbwTemplate::where('id', $words[$iWord]->id)
  103. ->update(['weight' => floor(($weight + $arrKatama[$iWord]) * 1000)]);
  104. $weight = 1.01 + 23 * pow(10, (2 - $bold));
  105. for ($i = $start; $i < $iWord; $i++) {
  106. $result = WbwTemplate::where('id', $words[$i]->id)
  107. ->update(['weight' => floor(($weight + $arrKatama[$i]) * 1000)]);
  108. }
  109. $start = -1;
  110. $bold = 0;
  111. } else {
  112. // 前词不是黑体
  113. $result = WbwTemplate::where('id', $words[$iWord]->id)
  114. ->update(['weight' => floor(($weight + $arrKatama[$iWord]) * 1000)]);
  115. }
  116. }
  117. }
  118. }
  119. $bar->finish();
  120. }
  121. return 0;
  122. }
  123. }