UpgradeCompound.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Storage;
  5. use App\Models\WordIndex;
  6. use App\Models\WbwTemplate;
  7. use App\Tools\TurboSplit;
  8. use App\Http\Api\DictApi;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Http;
  11. class UpgradeCompound extends Command
  12. {
  13. /**
  14. * The name and signature of the console command.
  15. * php -d memory_limit=1024M artisan upgrade:compound --api=https://next.wikipali.org/api --from=182852 --to=30000
  16. * @var string
  17. */
  18. protected $signature = 'upgrade:compound {word?} {--book=} {--debug} {--test} {--continue} {--api=} {--from=0} {--to=0} {--min=7} {--max=50}';
  19. /**
  20. * The console command description.
  21. *
  22. * @var string
  23. */
  24. protected $description = 'auto split compound word';
  25. /**
  26. * Create a new command instance.
  27. *
  28. * @return void
  29. */
  30. public function __construct()
  31. {
  32. parent::__construct();
  33. }
  34. /**
  35. * Execute the console command.
  36. *
  37. * @return int
  38. */
  39. public function handle()
  40. {
  41. if (\App\Tools\Tools::isStop()) {
  42. $this->info('.stop exists');
  43. return 0;
  44. }
  45. $confirm = '';
  46. if ($this->option('api')) {
  47. $confirm .= 'api=' . $this->option('api') . "\n";
  48. }
  49. $confirm .= "min=" . $this->option('min') . "\n";
  50. $confirm .= "max=" . $this->option('max') . "\n";
  51. $confirm .= "from=" . $this->option('from') . "\n";
  52. $confirm .= "to=" . $this->option('to') . "\n";
  53. if (!$this->confirm($confirm)) {
  54. return 0;
  55. }
  56. $this->info('[' . date('Y-m-d H:i:s', time()) . '] upgrade:compound start');
  57. $dict_id = DictApi::getSysDict('robot_compound');
  58. if (!$dict_id) {
  59. $this->error('没有找到 robot_compound 字典');
  60. return 1;
  61. }
  62. $start = \microtime(true);
  63. //
  64. if ($this->option('test')) {
  65. //调试代码
  66. $ts = new TurboSplit();
  67. Storage::disk('local')->put("tmp/compound.md", "# Turbo Split");
  68. //获取需要拆的词
  69. $list = [
  70. [5, 20, 20],
  71. [21, 30, 20],
  72. [31, 40, 10],
  73. [41, 60, 10],
  74. ];
  75. foreach ($list as $take) {
  76. # code...
  77. $words = WordIndex::where('final', 0)
  78. ->whereBetween('len', [$take[0], $take[1]])
  79. ->select('word')
  80. ->take($take[2])->get();
  81. foreach ($words as $word) {
  82. $this->info($word->word);
  83. Storage::disk('local')->append("tmp/compound.md", "## {$word->word}");
  84. $parts = $ts->splitA($word->word);
  85. foreach ($parts as $part) {
  86. # code...
  87. $info = "`{$part['word']}`,{$part['factors']},{$part['confidence']}";
  88. $this->info($info);
  89. Storage::disk('local')->append("tmp/compound.md", "- {$info}");
  90. }
  91. }
  92. }
  93. $this->info("耗时:" . \microtime(true) - $start);
  94. return 0;
  95. }
  96. $_word = $this->argument('word');
  97. if (!empty($_word)) {
  98. $words = array((object)array('real' => $_word));
  99. $count = 1;
  100. } else if ($this->option('book')) {
  101. $words = WbwTemplate::select('real')
  102. ->where('book', $this->option('book'))
  103. ->where('type', '<>', '.ctl.')
  104. ->where('real', '<>', '')
  105. ->orderBy('real')
  106. ->groupBy('real')->cursor();
  107. $query = DB::select(
  108. 'SELECT count(*) from (
  109. SELECT "real" from wbw_templates where book = ? and type <> ? and real <> ? group by real) T',
  110. [$this->option('book'), '.ctl.', '']
  111. );
  112. $count = $query[0]->count;
  113. } else {
  114. $min = WordIndex::min('id');
  115. $max = WordIndex::max('id');
  116. if ($this->option('from') > 0) {
  117. $from = $min + $this->option('from');
  118. } else {
  119. $from = $min;
  120. }
  121. if ($this->option('to') > 0) {
  122. $to = $min + $this->option('to');
  123. } else {
  124. $to = $max;
  125. }
  126. $words = WordIndex::whereBetween('id', [$from, $to])
  127. ->where('len', '>', $this->option('min'))
  128. ->where('len', '<', $this->option('max'))
  129. ->orderBy('id')
  130. ->selectRaw('id,word as real')
  131. ->cursor();
  132. $count = $to - $from + 1;
  133. }
  134. $sn = 0;
  135. $wordIndex = array();
  136. $result = array();
  137. foreach ($words as $key => $word) {
  138. if (\App\Tools\Tools::isStop()) {
  139. return 0;
  140. }
  141. $sn++;
  142. $startAt = microtime(true);
  143. $now = date('Y-m-d H:i:s');
  144. $this->info("[{$now}]{$word->real} start id={$word->id}");
  145. $ts = new TurboSplit();
  146. if ($this->option('debug')) {
  147. $ts->debug(true);
  148. }
  149. $wordIndex[] = $word->real;
  150. $parts = $ts->splitA($word->real);
  151. $time = round(microtime(true) - $startAt, 2);
  152. $percent = (int)($sn * 100 / $count);
  153. $this->info("[{$percent}%][{$sn}] {$word->real} {$time}s");
  154. $resultCount = 0;
  155. foreach ($parts as $part) {
  156. if (isset($part['type']) && $part['type'] === ".v.") {
  157. continue;
  158. }
  159. $resultCount++;
  160. $new = array();
  161. $new['word'] = $part['word'];
  162. $new['factors'] = $part['factors'];
  163. if (isset($part['type'])) {
  164. $new['type'] = $part['type'];
  165. } else {
  166. $new['type'] = ".cp.";
  167. }
  168. if (isset($part['grammar'])) {
  169. $new['grammar'] = $part['grammar'];
  170. } else {
  171. $new['grammar'] = null;
  172. }
  173. if (isset($part['parent'])) {
  174. $new['parent'] = $part['parent'];
  175. } else {
  176. $new['parent'] = null;
  177. }
  178. $new['confidence'] = 50 * $part['confidence'];
  179. $result[] = $new;
  180. if (!empty($_word)) {
  181. $output = "[{$resultCount}],{$part['word']},{$part['type']},{$part['grammar']},{$part['parent']},{$part['factors']},{$part['confidence']}";
  182. $this->info($output);
  183. }
  184. }
  185. if (count($wordIndex) % 100 === 0) {
  186. //每100个单词上传一次
  187. $this->upload($wordIndex, $result, $this->option('api'));
  188. $wordIndex = array();
  189. $result = array();
  190. }
  191. }
  192. $this->upload($wordIndex, $result, $this->option('api'));
  193. $this->info('[' . date('Y-m-d H:i:s', time()) . '] upgrade:compound finished');
  194. return 0;
  195. }
  196. private function upload($index, $words, $url = null)
  197. {
  198. if (!$url) {
  199. $url = config('app.url') . '/api/v2/compound';
  200. } else {
  201. $url = $url . '/v2/compound';
  202. }
  203. $this->info('url = ' . $url);
  204. $this->info('uploading size=' . strlen(json_encode($words, JSON_UNESCAPED_UNICODE)));
  205. $response = Http::post(
  206. $url,
  207. [
  208. 'index' => $index,
  209. 'words' => $words,
  210. ]
  211. );
  212. if ($response->ok()) {
  213. $this->info('upload ok');
  214. } else {
  215. $this->error('upload fail.');
  216. }
  217. }
  218. }