ExportNissaya.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Storage;
  5. use App\Models\Channel;
  6. use App\Models\Sentence;
  7. use App\Models\NissayaEnding;
  8. use App\Models\UserDict;
  9. use App\Http\Api\DictApi;
  10. class SuttaType
  11. {
  12. public static function types(){
  13. return [
  14. 'mula'=>[
  15. 69,70,71,72,73,74,
  16. 75,76,77,78,79,80,
  17. 81,82,83,84,85,86,
  18. 87,88,89,90,91,92,
  19. 93,94,95,143,144,145,
  20. 146,147,148,149,150,151,
  21. 152,153,154,155,156,157,
  22. 158,159,160,161,162,163,
  23. 164,165,166,167,168,169,
  24. 170,171,213,214,215,216,217,
  25. ],
  26. 'atthakatha' => [
  27. 64,65,96,97,98,99,
  28. 100,101,102,103,104,105,
  29. 106,107,108,109,110,111,
  30. 112,113,114,115,116,117,
  31. 118,119,120,121,122,123,
  32. 124,125,126,127,128,129,
  33. 130,131,132,133,134,135,
  34. 136,137,138,139,140,141,142,
  35. ],
  36. 'tika' => [
  37. 66,67,68,172,173,174,
  38. 175,176,177,178,179,180,
  39. 181,182,183,184,185,186,
  40. 187,188,189,190,191,192,
  41. 193,194,195,196,197,198,
  42. 199,200,201,202,203,204,
  43. 205,206,207,208,209,210,211,212,
  44. ],
  45. 'vinaya' => [138,139,140,141,142,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,],
  46. 'sutta' => [
  47. 82,83,84,85,86,
  48. 87,88,89,90,91,92,93,
  49. 94,95,99,100,101,102,
  50. 103,104,105,106,107,108,
  51. 109,110,111,112,113,114,
  52. 115,116,117,118,119,120,
  53. 121,122,123,124,125,126,
  54. 127,128,129,130,131,132,
  55. 133,134,135,136,137,143,
  56. 144,145,146,147,148,149,
  57. 150,151,152,153,154,155,
  58. 156,157,158,159,160,161,
  59. 162,163,164,165,166,167,
  60. 168,169,170,171,181,182,
  61. 183,184,185,186,187,188,
  62. 189,190,191,192,193,194,
  63. 195,196,197,198,199,
  64. ],
  65. 'abhidhamma' => [69,70,71,72,73,74,75,76,77,78,79,80,81,96,97,98,172,173,174,175,176,177,178,179,180,],
  66. ];
  67. }
  68. public static function getTypeByBook($bookId){
  69. $types = [];
  70. foreach (SuttaType::types() as $type => $books) {
  71. if(in_array($bookId,$books)){
  72. $types[] = $type;
  73. }
  74. }
  75. return $types;
  76. }
  77. }
  78. class ExportNissaya extends Command
  79. {
  80. /**
  81. * The name and signature of the console command.
  82. * php artisan export:nissaya
  83. * @var string
  84. */
  85. protected $signature = 'export:nissaya';
  86. protected $my = ["ႁႏၵ","ခ္","ဃ္","ဆ္","ဈ္","ည္","ဌ္","ဎ္","ထ္","ဓ္","ဖ္","ဘ္","က္","ဂ္","စ္","ဇ္","ဉ္","ဠ္","ဋ္","ဍ္","ဏ္","တ္","ဒ္","န္","ဟ္","ပ္","ဗ္","မ္","ယ္","ရ္","လ္","ဝ္","သ္","င္","င်္","ဿ","ခ","ဃ","ဆ","ဈ","စျ","ည","ဌ","ဎ","ထ","ဓ","ဖ","ဘ","က","ဂ","စ","ဇ","ဉ","ဠ","ဋ","ဍ","ဏ","တ","ဒ","န","ဟ","ပ","ဗ","မ","ယ","ရ","႐","လ","ဝ","သ","aျ္","aွ္","aြ္","aြ","ၱ","ၳ","ၵ","ၶ","ၬ","ၭ","ၠ","ၡ","ၢ","ၣ","ၸ","ၹ","ၺ","႓","ၥ","ၧ","ၨ","ၩ","်","ျ","ႅ","ၼ","ွ","ႇ","ႆ","ၷ","ၲ","႒","႗","ၯ","ၮ","႑","kaၤ","gaၤ","khaၤ","ghaၤ","aှ","aိံ","aုံ","aော","aေါ","aအံ","aဣံ","aဥံ","aံ","aာ","aါ","aိ","aီ","aု","aဳ","aူ","aေ","အါ","အာ","အ","ဣ","ဤ","ဥ","ဦ","ဧ","ဩ","ႏ","ၪ","a္","္","aံ","ေss","ေkh","ေgh","ေch","ေjh","ေññ","ေṭh","ေḍh","ေth","ေdh","ေph","ေbh","ေk","ေg","ေc","ေj","ေñ","ေḷ","ေṭ","ေḍ","ေṇ","ေt","ေd","ေn","ေh","ေp","ေb","ေm","ေy","ေr","ေl","ေv","ေs","ေy","ေv","ေr","ea","eā","၁","၂","၃","၄","၅","၆","၇","၈","၉","၀","း","့","။","၊"];
  87. protected $en = ["ndra","kh","gh","ch","jh","ññ","ṭh","ḍh","th","dh","ph","bh","k","g","c","j","ñ","ḷ","ṭ","ḍ","ṇ","t","d","n","h","p","b","m","y","r","l","v","s","ṅ","ṅ","ssa","kha","gha","cha","jha","jha","ñña","ṭha","ḍha","tha","dha","pha","bha","ka","ga","ca","ja","ña","ḷa","ṭa","ḍa","ṇa","ta","da","na","ha","pa","ba","ma","ya","ra","ra","la","va","sa","ya","va","ra","ra","္ta","္tha","္da","္dha","္ṭa","္ṭha","္ka","္kha","္ga","္gha","္pa","္pha","္ba","္bha","္ca","္cha","္ja","္jha","္a","္ya","္la","္ma","္va","္ha","ssa","na","ta","ṭṭha","ṭṭa","ḍḍha","ḍḍa","ṇḍa","ṅka","ṅga","ṅkha","ṅgha","ha","iṃ","uṃ","o","o","aṃ","iṃ","uṃ","aṃ","ā","ā","i","ī","u","u","ū","e","ā","ā","a","i","ī","u","ū","e","o","n","ñ","","","aṃ","sse","khe","ghe","che","jhe","ññe","ṭhe","ḍhe","the","dhe","phe","bhe","ke","ge","ce","je","ñe","ḷe","ṭe","ḍe","ṇe","te","de","ne","he","pe","be","me","ye","re","le","ve","se","ye","ve","re","e","o","1","2","3","4","5","6","7","8","9","0","”","’",".",","];
  88. /**
  89. * The console command description.
  90. *
  91. * @var string
  92. */
  93. protected $description = '导出nissaya统计数据';
  94. /**
  95. * Create a new command instance.
  96. *
  97. * @return void
  98. */
  99. public function __construct()
  100. {
  101. parent::__construct();
  102. }
  103. /**
  104. * Execute the console command.
  105. *
  106. * @return int
  107. */
  108. public function handle()
  109. {
  110. if(\App\Tools\Tools::isStop()){
  111. return 0;
  112. }
  113. $nissaya_channels = Channel::where('type','nissaya')
  114. ->where('lang','my')
  115. ->select('uid')->get();
  116. $this->info('channel:'.count($nissaya_channels));
  117. //system regular
  118. $dict_id = DictApi::getSysDict('system_regular');
  119. if(!$dict_id){
  120. $this->error('没有找到 system_regular 字典');
  121. return 1;
  122. }else{
  123. $this->info("system_regular :{$dict_id}");
  124. }
  125. //获取缅文语尾表
  126. $nissayaEndings = NissayaEnding::select('ending')->groupBy('ending')->get();
  127. $endings = [];
  128. $maxLen = 0;
  129. foreach ($nissayaEndings as $key => $ending) {
  130. $endings[] = $ending->ending;
  131. if(mb_strlen($ending->ending,'UTF-8')>$maxLen){
  132. $maxLen = mb_strlen($ending->ending,'UTF-8');
  133. }
  134. }
  135. $this->info(count($endings).' ending');
  136. $filename = "public/export/nissaya.csv";
  137. Storage::disk('local')->put($filename, "");
  138. $file = fopen(storage_path("app/$filename"),"w");
  139. $bar = $this->output->createProgressBar(Sentence::whereIn('channel_uid',$nissaya_channels)->count());
  140. foreach (Sentence::whereIn('channel_uid',$nissaya_channels)->select(['content','book_id'])->cursor() as $sent) {
  141. $lines = explode("\n",$sent->content);
  142. foreach ($lines as $key => $line) {
  143. # code...
  144. if(substr_count(trim($line),'=') === 1){
  145. $nissaya_str = explode('=',$line);
  146. $pali = $this->my2en($nissaya_str[0]);
  147. $types = SuttaType::getTypeByBook($sent->book_id);
  148. $strTypes = implode(",",$types);
  149. //拆分
  150. $factors = UserDict::where('dict_id',$dict_id)->where('word',$pali)->value('factors');
  151. $factors = explode('+',$factors);
  152. if(count($factors)>1){
  153. $paliEnding = end($factors);
  154. }else{
  155. $paliEnding = '';
  156. }
  157. $nissaya_my = trim($nissaya_str[1]);
  158. $mEnding1 = $this->matchEnding($nissaya_my,$endings,$maxLen);
  159. if(!empty($paliEnding) && !empty($mEnding1[1])){
  160. $mixed = $paliEnding.$mEnding1[1];
  161. fputcsv($file,[$strTypes, $pali,$paliEnding,$nissaya_my,$mEnding1[1],$mixed]);
  162. }
  163. $mEnding2= ['',''];
  164. if(!empty($mEnding1[1])){
  165. $mEnding2 = $this->matchEnding($mEnding1[0],$endings,$maxLen);
  166. if(!empty($paliEnding) && !empty($mEnding2[1])){
  167. $mixed = $paliEnding.$mEnding2[1];
  168. fputcsv($file,[$strTypes, $pali,$paliEnding,$nissaya_my,$mEnding2[1],$mixed]);
  169. }
  170. }
  171. $mEnding3= ['',''];
  172. if(!empty($mEnding2[1])){
  173. $mEnding3 = $this->matchEnding($mEnding2[0],$endings,$maxLen);
  174. if(!empty($paliEnding) && !empty($mEnding3[1])){
  175. $mixed = $paliEnding.$mEnding3[1];
  176. fputcsv($file,[$strTypes, $pali,$paliEnding,$nissaya_my,$mEnding3[1],$mixed]);
  177. }
  178. }
  179. //fputcsv($file,[$strTypes, $pali,$paliEnding,$nissaya_my,$mEnding1[1],$mEnding2[1],$mEnding3[1]]);
  180. }
  181. }
  182. $bar->advance();
  183. }
  184. fclose($file);
  185. $bar->finish();
  186. $this->info('done');
  187. $this->info($filename);
  188. return 0;
  189. }
  190. public function my2en($my){
  191. return str_replace($this->my,$this->en,$my);
  192. }
  193. private function matchEnding($needle,$endings,$maxLen){
  194. $needle = trim($needle);
  195. if(mb_substr($needle,-1,1,'UTF-8') === '။'){
  196. $needle = mb_substr($needle,0,-1);
  197. }
  198. for ($i=1; $i <= $maxLen ; $i++) {
  199. $mEnding = mb_substr($needle,-$i);
  200. if(in_array($mEnding,$endings)){
  201. return [mb_substr($needle,0,mb_strlen($needle,'UTF-8')-$i),$mEnding];
  202. }
  203. }
  204. return [$needle,''];
  205. }
  206. }