UpgradeWbwWeight.php 4.8 KB

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