UpgradeWbwAnalyses.php 7.2 KB

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