UpgradeDict.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. * php artisan upgrade:dict
  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. //读取 description
  63. $desFile = $dir."/description.md";
  64. if(file_exists($desFile)){
  65. $description = file_get_contents($desFile);
  66. }else{
  67. $description = $this->dictInfo['meta']['description'];
  68. }
  69. $tableDict = DictInfo::firstOrNew([
  70. "id" => $this->dictInfo['meta']['uuid']
  71. ]);
  72. $tableDict->id = $this->dictInfo['meta']['uuid'];
  73. $tableDict->name = $this->dictInfo['meta']['dictname'];
  74. $tableDict->shortname = $this->dictInfo['meta']['shortname'];
  75. $tableDict->description = $description;
  76. $tableDict->src_lang = $this->dictInfo['meta']['src_lang'];
  77. $tableDict->dest_lang = $this->dictInfo['meta']['dest_lang'];
  78. $tableDict->rows = $this->dictInfo['meta']['rows'];
  79. $tableDict->owner_id = config("mint.admin.root_uuid");
  80. $tableDict->meta = json_encode($this->dictInfo['meta']);
  81. $tableDict->save();
  82. if($this->option('part')){
  83. $this->info(" dict id = ".$this->dictInfo['meta']['uuid']);
  84. }else{
  85. $del = UserDict::where("dict_id",$this->dictInfo['meta']['uuid'])->delete();
  86. $this->info("delete {$del} rows dict id = ".$this->dictInfo['meta']['uuid']);
  87. }
  88. /**
  89. * 允许一个字典拆成若干个小文件
  90. * 文件名 为 ***.csv , ***-1.csv , ***-2.csv
  91. *
  92. */
  93. $filename = $dir.'/'.pathinfo($infoFile,PATHINFO_FILENAME);
  94. $csvFile = $filename . ".csv";
  95. $count = 0;
  96. $bar = $this->output->createProgressBar($this->dictInfo['meta']['rows']);
  97. while (file_exists($csvFile)) {
  98. # code...
  99. $this->info("runing:{$csvFile}");
  100. $inputRow = 0;
  101. if (($fp = fopen($csvFile, "r")) !== false) {
  102. $this->cols = array();
  103. while (($data = fgetcsv($fp, 0, ',')) !== false) {
  104. if ($inputRow == 0) {
  105. foreach ($data as $key => $colname) {
  106. # 列名列表
  107. $this->cols[$colname] = $key;
  108. }
  109. }else{
  110. if($this->option('part')){
  111. //仅仅提取拆分零件
  112. $word = $this->get($data,'word');
  113. $factor1 = $this->get($data,'factors');
  114. $factor1 = \str_replace([' ','(',')','=','-','$'],"+",$factor1);
  115. foreach (\explode('+',$factor1) as $part) {
  116. # code...
  117. if(empty($part)){
  118. continue;
  119. }
  120. if(isset($newPart[$part])){
  121. $newPart[$part][0]++;
  122. }else{
  123. $partExists = Cache::remember('dict/part/'.$part,config('cache.expire',1000),function() use($part){
  124. return UserDict::where('word',$part)->exists();
  125. });
  126. if(!$partExists){
  127. $count++;
  128. $newPart[$part] = [1,$word];
  129. $this->info("{$count}:{$part}-{$word}");
  130. }
  131. }
  132. }
  133. }else{
  134. $newDict = new UserDict();
  135. $newDict->id = app('snowflake')->id();
  136. $newDict->word = $data[$this->cols['word']];
  137. $newDict->type = $this->get($data,'type');
  138. $newDict->grammar = $this->get($data,'grammar');
  139. $newDict->parent = $this->get($data,'parent');
  140. $newDict->mean = $this->get($data,'mean');
  141. $newDict->note = $this->get($data,'note');
  142. $newDict->factors = $this->get($data,'factors');
  143. $newDict->factormean = $this->get($data,'factormean');
  144. $newDict->status = $this->get($data,'status');
  145. $newDict->language = $this->get($data,'language');
  146. $newDict->confidence = $this->get($data,'confidence');
  147. $newDict->source = $this->get($data,'source');
  148. $newDict->create_time =(int)(microtime(true)*1000);
  149. $newDict->creator_id = 1;
  150. $newDict->dict_id = $this->dictInfo['meta']['uuid'];
  151. $newDict->save();
  152. }
  153. $bar->advance();
  154. }
  155. $inputRow++;
  156. }
  157. }
  158. $count++;
  159. $csvFile = $filename . "-{$count}.csv";
  160. }
  161. $bar->finish();
  162. Storage::disk('local')->put("tmp/pm-part.csv", "part,count,word");
  163. if(isset($newPart)){
  164. foreach ($newPart as $part => $info) {
  165. # 写入磁盘文件
  166. Storage::disk('local')->append("tmp/pm-part.csv", "{$part},{$info[0]},{$info[1]}");
  167. }
  168. }
  169. $this->info("done");
  170. }
  171. }
  172. }
  173. }
  174. //子目录搜素完毕
  175. return;
  176. }else{
  177. //获取子目录失败
  178. $this->error("scandir fail");
  179. return;
  180. }
  181. }else{
  182. $this->error("this is not dir input={$dir}");
  183. return;
  184. }
  185. }
  186. /**
  187. * 获取列的值
  188. */
  189. protected function get($data,$colname,$defualt=""){
  190. if(isset($this->cols[$colname])){
  191. return $data[$this->cols[$colname]];
  192. }else if(isset($this->dictInfo['cols'][$colname])){
  193. return $this->dictInfo['cols'][$colname];
  194. }else{
  195. return $defualt;
  196. }
  197. }
  198. /**
  199. * Execute the console command.
  200. *
  201. * @return int
  202. */
  203. public function handle()
  204. {
  205. if(\App\Tools\Tools::isStop()){
  206. return 0;
  207. }
  208. $this->info("upgrade dict start");
  209. $this->scandict(config("mint.path.dict_text"));
  210. $this->info("upgrade dict done");
  211. return 0;
  212. }
  213. }