UpgradeWbwAnalyses.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\Wbw;
  5. use App\Models\WbwAnalysis;
  6. class UpgradeWbwAnalyses extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. * php artisan upgrade:wbw.analyses 13607580802879488
  11. * @var string
  12. */
  13. protected $signature = 'upgrade:wbw.analyses {id?}';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = '用户逐词解析数据填充wbw analyses表';
  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. if(\App\Tools\Tools::isStop()){
  37. return 0;
  38. }
  39. $startAt = time();
  40. $this->info("upgrade:wbwanalyses start");
  41. $bar = $this->output->createProgressBar(Wbw::count());
  42. $counter =0;
  43. if(empty($this->argument('id'))){
  44. $it = Wbw::orderby('id')->cursor();
  45. }else{
  46. $arrId = explode(',',$this->argument('id'));
  47. $it = Wbw::whereIn('id',$arrId)->orderby('id')->cursor();
  48. }
  49. foreach ($it as $wbwrow) {
  50. $counter++;
  51. WbwAnalysis::where('wbw_id',$wbwrow->id)->delete();
  52. # code...
  53. $data = str_replace("&nbsp;",' ',$wbwrow->data);
  54. $data = str_replace("<br>",' ',$data);
  55. $xmlString = "<root>" . $data . "</root>";
  56. try{
  57. $xmlWord = simplexml_load_string($xmlString);
  58. }catch(Exception $e){
  59. continue;
  60. }
  61. $wordsList = $xmlWord->xpath('//word');
  62. foreach ($wordsList as $word) {
  63. $pali = $word->real->__toString();
  64. $factors = [];
  65. foreach ($word as $key => $value) {
  66. $strValue = trim($value->__toString());
  67. if ($strValue !== "?" &&
  68. $strValue !== "" &&
  69. $strValue !== ".ctl." &&
  70. $strValue !== ".a." &&
  71. mb_substr($strValue, 0, 3, "UTF-8") !== "[a]" &&
  72. $strValue !== "_un_auto_factormean_" &&
  73. $strValue !== "_un_auto_mean_") {
  74. $iType = 0;
  75. $lang = 'pali';
  76. $newData = [
  77. 'wbw_id'=>$wbwrow->id,
  78. 'wbw_word'=>$wbwrow->word,
  79. 'book_id'=>$wbwrow->book_id,
  80. 'paragraph'=>$wbwrow->paragraph,
  81. 'wid'=>$wbwrow->wid,
  82. 'type'=>0,
  83. 'data'=>$strValue,
  84. 'confidence'=>100,
  85. 'lang'=>'en',
  86. 'editor_id'=>$wbwrow->editor_id,
  87. 'created_at'=>$wbwrow->created_at,
  88. 'updated_at'=>$wbwrow->updated_at
  89. ];
  90. #TODO 加虚词
  91. switch ($key) {
  92. case 'type':
  93. $newData['type']=1;
  94. WbwAnalysis::insert($newData);
  95. break;
  96. case 'gramma':
  97. $newData['type']=2;
  98. WbwAnalysis::insert($newData);
  99. break;
  100. case 'mean':
  101. $newData['type']=3;
  102. WbwAnalysis::insert($newData);
  103. break;
  104. case 'org':
  105. $newData['type']=4;
  106. WbwAnalysis::insert($newData);
  107. $factors=explode("+",$strValue);
  108. break;
  109. case 'om':
  110. $newData['type']=5;
  111. WbwAnalysis::insert($newData);
  112. # 存储拆分意思
  113. $newData['type']=7;
  114. $factorMeaning = explode('+',$strValue);
  115. foreach ( $factors as $index => $factor) {
  116. if(isset($factorMeaning[$index]) &&
  117. !empty($factorMeaning[$index]) &&
  118. $factorMeaning[$index] !== "↓↓" ){
  119. $newData['wbw_word'] = $factor;
  120. $newData['data'] = $factorMeaning[$index];
  121. WbwAnalysis::insert($newData);
  122. }
  123. }
  124. break;
  125. case 'parent':
  126. $newData['type']=6;
  127. WbwAnalysis::insert($newData);
  128. break;
  129. case 'case':
  130. $newData['type']=8;
  131. WbwAnalysis::insert($newData);
  132. break;
  133. case 'rela':
  134. /*
  135. <rela>[{"sour_id":"p199-764-6","sour_spell":"dhammacakkappavattanatthaṃ","dest_id":"p199-764-8","dest_spell":"āmantanā","relation":"ADV","note":""}]</rela>
  136. */
  137. $newData['type']=9;
  138. $rlt = json_decode($strValue);
  139. foreach ($rlt as $rltValue) {
  140. # code...
  141. if(!empty($rltValue->relation)){
  142. $newData['data'] = $rltValue->relation;
  143. if(isset($word->gramma) && !empty($word->gramma)){
  144. $grm = explode('$',$word->gramma);
  145. if(count($grm)>0){
  146. $newData['d1'] = $grm[count($grm)-1];
  147. }else{
  148. $newData['d1'] = $word->type;
  149. }
  150. }
  151. $newData['d2'] = (int)(explode('-',$rltValue->dest_id)[2]) - (int)(explode('-',$rltValue->sour_id)[2]) ;
  152. WbwAnalysis::insert($newData);
  153. }
  154. }
  155. break;
  156. }
  157. }
  158. }
  159. }
  160. $bar->advance();
  161. }
  162. $bar->finish();
  163. $time = time() - $startAt;
  164. $this->info("wbw analyses done in {$time}");
  165. return 0;
  166. }
  167. }