UpgradeWbwAnalyses.php 6.5 KB

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