UpgradeDict.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Support\Str;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Facades\Cache;
  6. use Illuminate\Support\Facades\Storage;
  7. use App\Models\UserDict;
  8. use App\Models\DictInfo;
  9. class UpgradeDict extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'upgrade:dict {uuid?} {--part}';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = '导入csv字典';
  23. protected $dictInfo;
  24. protected $cols;
  25. /**
  26. * Create a new command instance.
  27. *
  28. * @return void
  29. */
  30. public function __construct()
  31. {
  32. parent::__construct();
  33. }
  34. private function scandict($dir){
  35. if(is_dir($dir)){
  36. $this->info("scan:".$dir);
  37. if($files = scandir($dir)){
  38. //进入目录搜索字典或子目录
  39. foreach ($files as $file) {
  40. //进入语言目录循环搜索
  41. $fullPath = $dir."/".$file;
  42. if(is_dir($fullPath) && $file !== '.' && $file !== '..'){
  43. //是目录继续搜索
  44. $this->scandict($fullPath);
  45. }else{
  46. //是文件,查看是否是字典信息文件
  47. $infoFile = $fullPath;
  48. if(pathinfo($infoFile,PATHINFO_EXTENSION) === 'ini'){
  49. $this->dictInfo = parse_ini_file($infoFile,true);
  50. if(isset($this->dictInfo['meta']['dictname'])){
  51. //是字典信息文件
  52. $this->info($this->dictInfo['meta']['dictname']);
  53. if(Str::isUuid($this->argument('uuid'))){
  54. if($this->argument('uuid') !== $this->dictInfo['meta']['uuid']){
  55. continue;
  56. }
  57. }
  58. if(!Str::isUuid($this->dictInfo['meta']['uuid'])){
  59. $this->error("not uuid");
  60. continue;
  61. }
  62. $tableDict = DictInfo::firstOrNew([
  63. "id" => $this->dictInfo['meta']['uuid']
  64. ]);
  65. $tableDict->id = $this->dictInfo['meta']['uuid'];
  66. $tableDict->name = $this->dictInfo['meta']['dictname'];
  67. $tableDict->shortname = $this->dictInfo['meta']['shortname'];
  68. $tableDict->description = $this->dictInfo['meta']['description'];
  69. $tableDict->src_lang = $this->dictInfo['meta']['src_lang'];
  70. $tableDict->dest_lang = $this->dictInfo['meta']['dest_lang'];
  71. $tableDict->rows = $this->dictInfo['meta']['rows'];
  72. $tableDict->owner_id = config("app.admin.root_uuid");
  73. $tableDict->meta = json_encode($this->dictInfo['meta']);
  74. $tableDict->save();
  75. if($this->option('part')){
  76. $this->info(" dict id = ".$this->dictInfo['meta']['uuid']);
  77. }else{
  78. $del = UserDict::where("dict_id",$this->dictInfo['meta']['uuid'])->delete();
  79. $this->info("delete {$del} rows dict id = ".$this->dictInfo['meta']['uuid']);
  80. }
  81. $filename = $dir.'/'.pathinfo($infoFile,PATHINFO_FILENAME);
  82. $csvFile = $filename . ".csv";
  83. $count = 0;
  84. $bar = $this->output->createProgressBar($this->dictInfo['meta']['rows']);
  85. while (file_exists($csvFile)) {
  86. # code...
  87. $this->info("runing:{$csvFile}");
  88. $inputRow = 0;
  89. if (($fp = fopen($csvFile, "r")) !== false) {
  90. $this->cols = array();
  91. while (($data = fgetcsv($fp, 0, ',')) !== false) {
  92. if ($inputRow == 0) {
  93. foreach ($data as $key => $colname) {
  94. # 列名列表
  95. $this->cols[$colname] = $key;
  96. }
  97. }else{
  98. if($this->option('part')){
  99. //仅仅提取拆分零件
  100. $word = $this->get($data,'word');
  101. $factor1 = $this->get($data,'factors');
  102. $factor1 = \str_replace([' ','(',')','=','-','$'],"+",$factor1);
  103. foreach (\explode('+',$factor1) as $part) {
  104. # code...
  105. if(empty($part)){
  106. continue;
  107. }
  108. if(isset($newPart[$part])){
  109. $newPart[$part][0]++;
  110. }else{
  111. $partExists = Cache::remember('dict/part/'.$part,1000,function() use($part){
  112. return UserDict::where('word',$part)->exists();
  113. });
  114. if(!$partExists){
  115. $count++;
  116. $newPart[$part] = [1,$word];
  117. $this->info("{$count}:{$part}-{$word}");
  118. }
  119. }
  120. }
  121. }else{
  122. $newDict = new UserDict();
  123. $newDict->id = app('snowflake')->id();
  124. $newDict->word = $data[$this->cols['word']];
  125. $newDict->type = $this->get($data,'type');
  126. $newDict->grammar = $this->get($data,'grammar');
  127. $newDict->parent = $this->get($data,'parent');
  128. $newDict->mean = $this->get($data,'mean');
  129. $newDict->note = $this->get($data,'note');
  130. $newDict->factors = $this->get($data,'factors');
  131. $newDict->factormean = $this->get($data,'factormean');
  132. $newDict->status = $this->get($data,'status');
  133. $newDict->language = $this->get($data,'language');
  134. $newDict->confidence = $this->get($data,'confidence');
  135. $newDict->source = $this->get($data,'source');
  136. $newDict->create_time =(int)(microtime(true)*1000);
  137. $newDict->creator_id = 1;
  138. $newDict->dict_id = $this->dictInfo['meta']['uuid'];
  139. $newDict->save();
  140. }
  141. $bar->advance();
  142. }
  143. $inputRow++;
  144. }
  145. }
  146. $count++;
  147. $csvFile = $filename . "{$count}.csv";
  148. }
  149. $bar->finish();
  150. Storage::disk('local')->put("tmp/pm-part.csv", "part,count,word");
  151. if(isset($newPart)){
  152. foreach ($newPart as $part => $info) {
  153. # 写入磁盘文件
  154. Storage::disk('local')->append("tmp/pm-part.csv", "{$part},{$info[0]},{$info[1]}");
  155. }
  156. }
  157. $this->info("done");
  158. }
  159. }
  160. }
  161. }
  162. //子目录搜素完毕
  163. return;
  164. }else{
  165. //获取子目录失败
  166. $this->error("scandir fail");
  167. return;
  168. }
  169. }else{
  170. $this->error("this is not dir input={$dir}");
  171. return;
  172. }
  173. }
  174. /**
  175. * 获取列的值
  176. */
  177. protected function get($data,$colname,$defualt=""){
  178. if(isset($this->cols[$colname])){
  179. return $data[$this->cols[$colname]];
  180. }else if(isset($this->dictInfo['cols'][$colname])){
  181. return $this->dictInfo['cols'][$colname];
  182. }else{
  183. return $defualt;
  184. }
  185. }
  186. /**
  187. * Execute the console command.
  188. *
  189. * @return int
  190. */
  191. public function handle()
  192. {
  193. $this->info("upgrade dict start");
  194. $this->scandict(config("app.path.dict_text"));
  195. $this->info("upgrade dict done");
  196. return 0;
  197. }
  198. }