UpgradeCompound.php 6.5 KB

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