UpgradeWbwAnalyses.php 5.5 KB

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