ExportNissaya.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Http\Api\DictApi;
  4. use App\Models\Channel;
  5. use App\Models\NissayaEnding;
  6. use App\Models\Sentence;
  7. use App\Models\UserDict;
  8. use App\Tools\Tools;
  9. use Illuminate\Console\Command;
  10. use Illuminate\Support\Facades\Storage;
  11. class SuttaType
  12. {
  13. public static function types()
  14. {
  15. return [
  16. 'mula' => [
  17. 69, 70, 71, 72, 73, 74,
  18. 75, 76, 77, 78, 79, 80,
  19. 81, 82, 83, 84, 85, 86,
  20. 87, 88, 89, 90, 91, 92,
  21. 93, 94, 95, 143, 144, 145,
  22. 146, 147, 148, 149, 150, 151,
  23. 152, 153, 154, 155, 156, 157,
  24. 158, 159, 160, 161, 162, 163,
  25. 164, 165, 166, 167, 168, 169,
  26. 170, 171, 213, 214, 215, 216, 217,
  27. ],
  28. 'atthakatha' => [
  29. 64, 65, 96, 97, 98, 99,
  30. 100, 101, 102, 103, 104, 105,
  31. 106, 107, 108, 109, 110, 111,
  32. 112, 113, 114, 115, 116, 117,
  33. 118, 119, 120, 121, 122, 123,
  34. 124, 125, 126, 127, 128, 129,
  35. 130, 131, 132, 133, 134, 135,
  36. 136, 137, 138, 139, 140, 141, 142,
  37. ],
  38. 'tika' => [
  39. 66, 67, 68, 172, 173, 174,
  40. 175, 176, 177, 178, 179, 180,
  41. 181, 182, 183, 184, 185, 186,
  42. 187, 188, 189, 190, 191, 192,
  43. 193, 194, 195, 196, 197, 198,
  44. 199, 200, 201, 202, 203, 204,
  45. 205, 206, 207, 208, 209, 210, 211, 212,
  46. ],
  47. 'vinaya' => [138, 139, 140, 141, 142, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217],
  48. 'sutta' => [
  49. 82, 83, 84, 85, 86,
  50. 87, 88, 89, 90, 91, 92, 93,
  51. 94, 95, 99, 100, 101, 102,
  52. 103, 104, 105, 106, 107, 108,
  53. 109, 110, 111, 112, 113, 114,
  54. 115, 116, 117, 118, 119, 120,
  55. 121, 122, 123, 124, 125, 126,
  56. 127, 128, 129, 130, 131, 132,
  57. 133, 134, 135, 136, 137, 143,
  58. 144, 145, 146, 147, 148, 149,
  59. 150, 151, 152, 153, 154, 155,
  60. 156, 157, 158, 159, 160, 161,
  61. 162, 163, 164, 165, 166, 167,
  62. 168, 169, 170, 171, 181, 182,
  63. 183, 184, 185, 186, 187, 188,
  64. 189, 190, 191, 192, 193, 194,
  65. 195, 196, 197, 198, 199,
  66. ],
  67. '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],
  68. ];
  69. }
  70. public static function getTypeByBook($bookId)
  71. {
  72. $types = [];
  73. foreach (SuttaType::types() as $type => $books) {
  74. if (in_array($bookId, $books)) {
  75. $types[] = $type;
  76. }
  77. }
  78. return $types;
  79. }
  80. }
  81. class ExportNissaya extends Command
  82. {
  83. /**
  84. * The name and signature of the console command.
  85. * php artisan export:nissaya
  86. *
  87. * @var string
  88. */
  89. protected $signature = 'export:nissaya';
  90. 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ā', '၁', '၂', '၃', '၄', '၅', '၆', '၇', '၈', '၉', '၀', 'း', '့', '။', '၊'];
  91. 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', '”', '’', '.', ','];
  92. /**
  93. * The console command description.
  94. *
  95. * @var string
  96. */
  97. protected $description = '导出nissaya统计数据';
  98. /**
  99. * Create a new command instance.
  100. *
  101. * @return void
  102. */
  103. public function __construct()
  104. {
  105. parent::__construct();
  106. }
  107. /**
  108. * Execute the console command.
  109. *
  110. * @return int
  111. */
  112. public function handle()
  113. {
  114. if (Tools::isStop()) {
  115. return 0;
  116. }
  117. $nissaya_channels = Channel::where('type', 'nissaya')
  118. ->where('lang', 'my')
  119. ->select('uid')->get();
  120. $this->info('channel:'.count($nissaya_channels));
  121. // system regular
  122. $dict_id = DictApi::getSysDict('system_regular');
  123. if (! $dict_id) {
  124. $this->error('没有找到 system_regular 字典');
  125. return 1;
  126. } else {
  127. $this->info("system_regular :{$dict_id}");
  128. }
  129. // 获取缅文语尾表
  130. $nissayaEndings = NissayaEnding::select('ending')->groupBy('ending')->get();
  131. $endings = [];
  132. $maxLen = 0;
  133. foreach ($nissayaEndings as $key => $ending) {
  134. $endings[] = $ending->ending;
  135. if (mb_strlen($ending->ending, 'UTF-8') > $maxLen) {
  136. $maxLen = mb_strlen($ending->ending, 'UTF-8');
  137. }
  138. }
  139. $this->info(count($endings).' ending');
  140. $filename = 'public/export/nissaya.csv';
  141. Storage::disk('local')->put($filename, '');
  142. $file = fopen(storage_path("app/$filename"), 'w');
  143. $bar = $this->output->createProgressBar(Sentence::whereIn('channel_uid', $nissaya_channels)->count());
  144. foreach (Sentence::whereIn('channel_uid', $nissaya_channels)->select(['content', 'book_id'])->cursor() as $sent) {
  145. $lines = explode("\n", $sent->content);
  146. foreach ($lines as $key => $line) {
  147. // code...
  148. if (substr_count(trim($line), '=') === 1) {
  149. $nissaya_str = explode('=', $line);
  150. $pali = $this->my2en($nissaya_str[0]);
  151. $types = SuttaType::getTypeByBook($sent->book_id);
  152. $strTypes = implode(',', $types);
  153. // 拆分
  154. $factors = UserDict::where('dict_id', $dict_id)->where('word', $pali)->value('factors');
  155. $factors = explode('+', $factors);
  156. if (count($factors) > 1) {
  157. $paliEnding = end($factors);
  158. } else {
  159. $paliEnding = '';
  160. }
  161. $nissaya_my = trim($nissaya_str[1]);
  162. $mEnding1 = $this->matchEnding($nissaya_my, $endings, $maxLen);
  163. if (! empty($paliEnding) && ! empty($mEnding1[1])) {
  164. $mixed = $paliEnding.$mEnding1[1];
  165. fputcsv($file, [$strTypes, $pali, $paliEnding, $nissaya_my, $mEnding1[1], $mixed]);
  166. }
  167. $mEnding2 = ['', ''];
  168. if (! empty($mEnding1[1])) {
  169. $mEnding2 = $this->matchEnding($mEnding1[0], $endings, $maxLen);
  170. if (! empty($paliEnding) && ! empty($mEnding2[1])) {
  171. $mixed = $paliEnding.$mEnding2[1];
  172. fputcsv($file, [$strTypes, $pali, $paliEnding, $nissaya_my, $mEnding2[1], $mixed]);
  173. }
  174. }
  175. $mEnding3 = ['', ''];
  176. if (! empty($mEnding2[1])) {
  177. $mEnding3 = $this->matchEnding($mEnding2[0], $endings, $maxLen);
  178. if (! empty($paliEnding) && ! empty($mEnding3[1])) {
  179. $mixed = $paliEnding.$mEnding3[1];
  180. fputcsv($file, [$strTypes, $pali, $paliEnding, $nissaya_my, $mEnding3[1], $mixed]);
  181. }
  182. }
  183. // fputcsv($file,[$strTypes, $pali,$paliEnding,$nissaya_my,$mEnding1[1],$mEnding2[1],$mEnding3[1]]);
  184. }
  185. }
  186. $bar->advance();
  187. }
  188. fclose($file);
  189. $bar->finish();
  190. $this->info('done');
  191. $this->info($filename);
  192. return 0;
  193. }
  194. public function my2en($my)
  195. {
  196. return str_replace($this->my, $this->en, $my);
  197. }
  198. private function matchEnding($needle, $endings, $maxLen)
  199. {
  200. $needle = trim($needle);
  201. if (mb_substr($needle, -1, 1, 'UTF-8') === '။') {
  202. $needle = mb_substr($needle, 0, -1);
  203. }
  204. for ($i = 1; $i <= $maxLen; $i++) {
  205. $mEnding = mb_substr($needle, -$i);
  206. if (in_array($mEnding, $endings)) {
  207. return [mb_substr($needle, 0, mb_strlen($needle,'UTF-8') - $i), $mEnding];
  208. }
  209. }
  210. return [$needle, ''];
  211. }
  212. }