UpgradeWbwAnalyses.php 6.9 KB

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