CorpusController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Sentence;
  4. use App\Models\Channel;
  5. use App\Models\PaliText;
  6. use App\Models\WbwTemplate;
  7. use App\Models\WbwBlock;
  8. use App\Models\Wbw;
  9. use App\Models\Discussion;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\Cache;
  12. use App\Http\Api\MdRender;
  13. use App\Http\Api\SuggestionApi;
  14. use App\Http\Api\ChannelApi;
  15. use Illuminate\Support\Facades\Log;
  16. class CorpusController extends Controller
  17. {
  18. protected $result = [
  19. "uid"=> '',
  20. "title"=> '',
  21. "path"=>[],
  22. "sub_title"=> '',
  23. "summary"=> '',
  24. "content"=> '',
  25. "content_type"=> "html",
  26. "status"=>30,
  27. "lang"=> "",
  28. "created_at"=> "",
  29. "updated_at"=> "",
  30. ];
  31. protected $wbwChannels = [];
  32. protected $selectCol = ['book_id','paragraph','word_start',"word_end",'channel_uid','content','updated_at'];
  33. public function __construct()
  34. {
  35. }
  36. /**
  37. * Display a listing of the resource.
  38. *
  39. * @return \Illuminate\Http\Response
  40. */
  41. public function index()
  42. {
  43. //
  44. }
  45. /**
  46. * Store a newly created resource in storage.
  47. *
  48. * @param \Illuminate\Http\Request $request
  49. * @return \Illuminate\Http\Response
  50. */
  51. public function store(Request $request)
  52. {
  53. //
  54. }
  55. /**
  56. * Display the specified resource.
  57. *
  58. * @param \App\Models\Sentence $sentence
  59. * @return \Illuminate\Http\Response
  60. */
  61. public function show(Sentence $sentence)
  62. {
  63. //
  64. }
  65. public function getSentTpl($id,$channels){
  66. $sent = [];
  67. $sentId = \explode('-',$id);
  68. $channelId = ChannelApi::getSysChannel('_System_Wbw_VRI_');
  69. if($channelId !== false){
  70. array_push($channels,$channelId);
  71. }
  72. $record = Sentence::select($this->selectCol)
  73. ->where('book_id',$sentId[0])
  74. ->where('paragraph',$sentId[1])
  75. ->where('word_start',$sentId[2])
  76. ->where('word_end',$sentId[3])
  77. ->whereIn('channel_uid',$channels)
  78. ->get();
  79. Log::info("sent count:".count($record));
  80. $channelIndex = $this->getChannelIndex($channels);
  81. $content = $this->makeContent($record,"edit",$channelIndex);
  82. return $content;
  83. }
  84. /**
  85. * Display the specified resource.
  86. *
  87. * @param string $id
  88. * @return \Illuminate\Http\Response
  89. */
  90. public function showSent($id)
  91. {
  92. //
  93. $param = \explode('_',$id);
  94. if(count($param)>1){
  95. $channels = array_slice($param,1);
  96. }else{
  97. $channels = [];
  98. }
  99. $this->result['content'] = getSentTpl($param[0],$channels);
  100. return $this->ok($this->result);
  101. }
  102. public function showSentences($type,$id,$mode='read'){
  103. $param = \explode('_',$id);
  104. $sentId = \explode('-',$param[0]);
  105. $channels = [];
  106. #获取channel类型
  107. $sentChannel = Sentence::select('channel_uid')
  108. ->where('book_id',$sentId[0])
  109. ->where('paragraph',$sentId[1])
  110. ->where('word_start',$sentId[2])
  111. ->where('word_end',$sentId[3])
  112. ->get();
  113. foreach ($sentChannel as $key => $value) {
  114. # code...
  115. $channels[] = $value->channel_uid;
  116. }
  117. $channelInfo = Channel::whereIn("uid",$channels)->select(['uid','type','name'])->get();
  118. $indexChannel = [];
  119. $channels = [];
  120. foreach ($channelInfo as $key => $value) {
  121. # code...
  122. if($value->type === $type){
  123. $indexChannel[$value->uid] = $value;
  124. $channels[] = $value->uid;
  125. }
  126. }
  127. //获取句子数据
  128. $record = Sentence::select($this->selectCol)
  129. ->where('book_id',$sentId[0])
  130. ->where('paragraph',$sentId[1])
  131. ->where('word_start',$sentId[2])
  132. ->where('word_end',$sentId[3])
  133. ->whereIn('channel_uid',$channels)
  134. ->orderBy('paragraph')
  135. ->orderBy('word_start')
  136. ->get();
  137. if(count($record) ===0){
  138. return $this->error("no data");
  139. }
  140. $this->result['content'] = $this->makeContent($record,$mode,$indexChannel);
  141. return $this->ok($this->result);
  142. }
  143. public function showChapter($id,$mode='read')
  144. {
  145. //
  146. $param = \explode('_',$id);
  147. $sentId = \explode('-',$param[0]);
  148. $channels = [];
  149. if(count($param)>1){
  150. $channels = array_slice($param,1);
  151. }
  152. if($mode === 'read'){
  153. //阅读模式加载md格式原文
  154. $channelId = ChannelApi::getSysChannel('_System_Pali_VRI_');
  155. }else{
  156. //翻译模式加载json格式原文
  157. $channelId = ChannelApi::getSysChannel('_System_Wbw_VRI_');
  158. }
  159. if($channelId !== false){
  160. $channels[] = $channelId;
  161. }
  162. $chapter = PaliText::where('book',$sentId[0])->where('paragraph',$sentId[1])->first();
  163. if(!$chapter){
  164. return $this->error("no data");
  165. }
  166. if(empty($chapter->toc)){
  167. $this->result['title'] = "unknown";
  168. }else{
  169. $this->result['title'] = $chapter->toc;
  170. $this->result['sub_title'] = $chapter->toc;
  171. $this->result['path'] = json_decode($chapter->path);
  172. }
  173. $paraFrom = $sentId[1];
  174. $paraTo = $sentId[1]+$chapter->chapter_len-1;
  175. //获取标题
  176. $heading = PaliText::select(["book","paragraph","level"])
  177. ->where('book',$sentId[0])
  178. ->whereBetween('paragraph',[$paraFrom,$paraTo])
  179. ->where('level','<',8)
  180. ->get();
  181. //将标题段落转成索引数组 以便输出标题层级
  182. $indexedHeading = [];
  183. foreach ($heading as $key => $value) {
  184. # code...
  185. $indexedHeading["{$value->book}-{$value->paragraph}"] = $value->level;
  186. }
  187. #获取channel索引表
  188. $tranChannels = [];
  189. $channelInfo = Channel::whereIn("uid",$channels)->select(['uid','type','name'])->get();
  190. $indexChannel = [];
  191. foreach ($channelInfo as $key => $value) {
  192. # code...
  193. $indexChannel[$value->uid] = $value;
  194. if($value->type==="translation" ){
  195. $tranChannels[] = $value->uid;
  196. }
  197. }
  198. //获取wbw channel
  199. //目前默认的 wbw channel 是第一个translation channel
  200. foreach ($channels as $key => $value) {
  201. # code...
  202. if($indexChannel[$value]->type==='translation'){
  203. $this->wbwChannels[] = $value;
  204. break;
  205. }
  206. }
  207. $title = Sentence::select($this->selectCol)
  208. ->where('book_id',$sentId[0])
  209. ->where('paragraph',$sentId[1])
  210. ->whereIn('channel_uid',$tranChannels)
  211. ->first();
  212. if($title){
  213. $this->result['title'] = MdRender::render($title->content,$title->channel_uid);
  214. }
  215. //获取句子数据
  216. $record = Sentence::select($this->selectCol)
  217. ->where('book_id',$sentId[0])
  218. ->whereBetween('paragraph',[$paraFrom,$paraTo])
  219. ->whereIn('channel_uid',$channels)
  220. ->orderBy('paragraph')
  221. ->orderBy('word_start')
  222. ->get();
  223. if(count($record) ===0){
  224. return $this->error("no data");
  225. }
  226. $this->result['content'] = $this->makeContent($record,$mode,$indexChannel,$indexedHeading);
  227. return $this->ok($this->result);
  228. }
  229. private function getChannelIndex($channels){
  230. #获取channel索引表
  231. $channelInfo = Channel::whereIn("uid",$channels)->select(['uid','type','name'])->get();
  232. $indexChannel = [];
  233. foreach ($channelInfo as $key => $value) {
  234. # code...
  235. $indexChannel[$value->uid] = $value;
  236. }
  237. return $indexChannel;
  238. }
  239. /**
  240. * 根据句子库数据生成文章内容
  241. * $record 句子数据
  242. * $mode read | edit | wbw
  243. * $indexChannel channel索引
  244. * $indexedHeading 标题索引 用于给段落加标题标签 <h1> ect.
  245. */
  246. private function makeContent($record,$mode,$indexChannel,$indexedHeading=[]){
  247. $content = [];
  248. $lastSent = "0-0";
  249. $sentCount = 0;
  250. $sent = [];
  251. $sent["origin"] = [];
  252. $sent["translation"] = [];
  253. foreach ($record as $key => $value) {
  254. # 遍历结果生成html文件
  255. $currSentId = $value->book_id.'-'.$value->paragraph.'-'.$value->word_start.'-'.$value->word_end;
  256. if($currSentId !== $lastSent){
  257. if($sentCount > 0){
  258. //保存上一个句子
  259. //增加标题的html标记
  260. $level = 0;
  261. if(isset($indexedHeading["{$value->book_id}-{$value->paragraph}"])){
  262. $level = $indexedHeading["{$value->book_id}-{$value->paragraph}"];
  263. }
  264. $content = $this->pushSent($content,$sent,$level,$mode);
  265. }
  266. //新建句子
  267. $sent = $this->newSent($value->book_id,$value->paragraph,$value->word_start,$value->word_end);
  268. $lastSent = $currSentId;
  269. }
  270. $sentContent=$value->content;
  271. $channelType = $indexChannel[$value->channel_uid]->type;
  272. if($indexChannel[$value->channel_uid]->type==="original" && $mode !== 'read'){
  273. //非阅读模式下。原文使用逐词解析数据。优先加载第一个translation channel 如果没有。加载默认逐词解析。
  274. $channelType = 'wbw';
  275. $html = "";
  276. if(count($this->wbwChannels)>0){
  277. //获取逐词解析数据
  278. $wbwBlock = WbwBlock::where('channel_uid',$this->wbwChannels[0])
  279. ->where('book_id',$value->book_id)
  280. ->where('paragraph',$value->paragraph)
  281. ->select('uid')
  282. ->first();
  283. if($wbwBlock){
  284. //找到逐词解析数据
  285. $wbwData = Wbw::where('block_uid',$wbwBlock->uid)
  286. ->whereBetween('wid',[$value->word_start,$value->word_end])
  287. ->select(['data','uid'])
  288. ->orderBy('wid')
  289. ->get();
  290. $wbwContent = [];
  291. foreach ($wbwData as $wbwrow) {
  292. $wbw = str_replace("&nbsp;",' ',$wbwrow->data);
  293. $wbw = str_replace("<br>",' ',$wbw);
  294. $xmlString = "<root>" . $wbw . "</root>";
  295. try{
  296. $xmlWord = simplexml_load_string($xmlString);
  297. }catch(Exception $e){
  298. continue;
  299. }
  300. $wordsList = $xmlWord->xpath('//word');
  301. foreach ($wordsList as $word) {
  302. $case = \str_replace(['#','.'],['$',''],$word->case->__toString());
  303. $case = \str_replace('$$','$',$case);
  304. $case = trim($case);
  305. $case = trim($case,"$");
  306. $wbwContent[] = [
  307. 'uid'=>$wbwrow->uid,
  308. 'word'=>['value'=>$word->pali->__toString(),'status'=>0],
  309. 'real'=> ['value'=>$word->real->__toString(),'status'=>0],
  310. 'meaning'=> ['value'=>\explode('$',$word->mean->__toString()) ,'status'=>0],
  311. 'type'=> ['value'=>$word->type->__toString(),'status'=>0],
  312. 'grammar'=> ['value'=>$word->gramma->__toString(),'status'=>0],
  313. 'case'=> ['value'=>\explode('$',$case),'status'=>0],
  314. 'parent'=> ['value'=>$word->parent->__toString(),'status'=>0],
  315. 'style'=> ['value'=>$word->style->__toString(),'status'=>0],
  316. 'factors'=> ['value'=>$word->org->__toString(),'status'=>0],
  317. 'factorMeaning'=> ['value'=>$word->om->__toString(),'status'=>0],
  318. 'confidence'=> 0.5,
  319. 'hasComment'=>Discussion::where('res_id',$wbwrow->uid)->exists(),
  320. ];
  321. }
  322. }
  323. $sentContent = \json_encode($wbwContent);
  324. }
  325. }
  326. }else{
  327. $html = Cache::remember("/sent/{$value->channel_uid}/{$currSentId}",10,
  328. function() use($value){
  329. return MdRender::render($value->content,$value->channel_uid);
  330. });
  331. }
  332. $newSent = [
  333. "content"=>$sentContent,
  334. "html"=> $html,
  335. "book"=> $value->book_id,
  336. "para"=> $value->paragraph,
  337. "wordStart"=> $value->word_start,
  338. "wordEnd"=> $value->word_end,
  339. "editor"=> [
  340. 'id'=>$value->editor_uid,
  341. 'nickName'=>'nickname',
  342. 'realName'=>'realName',
  343. 'avatar'=>'',
  344. ],
  345. "channel"=> [
  346. "name"=>$indexChannel[$value->channel_uid]->name,
  347. "type"=>$channelType,
  348. "id"=> $value->channel_uid,
  349. ],
  350. "updateAt"=> $value->updated_at,
  351. "suggestionCount" => SuggestionApi::getCountBySent($value->book_id,$value->paragraph,$value->word_start,$value->word_end,$value->channel_uid),
  352. ];
  353. switch ($indexChannel[$value->channel_uid]->type) {
  354. case 'original';
  355. case 'wbw';
  356. array_push($sent["origin"],$newSent);
  357. break;
  358. default:
  359. array_push($sent["translation"],$newSent);
  360. break;
  361. }
  362. $sentCount++;
  363. }
  364. $content = $this->pushSent($content,$sent,0,$mode);
  365. return \implode("",$content);
  366. }
  367. private function pushSent($result,$sent,$level=0,$mode='read'){
  368. $sentProps = base64_encode(\json_encode($sent)) ;
  369. if($mode === 'read'){
  370. $sentWidget = "<MdTpl tpl='sentread' props='{$sentProps}' />";
  371. }else{
  372. $sentWidget = "<MdTpl tpl='sentedit' props='{$sentProps}' />";
  373. }
  374. //增加标题的html标记
  375. if($level>0){
  376. $sentWidget = "<h{$level}>".$sentWidget."</h{$level}>";
  377. }
  378. array_push($result,$sentWidget);
  379. return $result;
  380. }
  381. private function newSent($book,$para,$word_start,$word_end){
  382. $sent = [
  383. "id"=>"{$book}-{$para}-{$word_start}-{$word_end}",
  384. "origin"=>[],
  385. "translation"=>[],
  386. ];
  387. #生成channel 数量列表
  388. $sentId = "{$book}-{$para}-{$word_start}-{$word_end}";
  389. $channelCount = Cache::remember("/sent1/{$sentId}/channels/count",
  390. 60,
  391. function() use($book,$para,$word_start,$word_end){
  392. $channels = Sentence::where('book_id',$book)
  393. ->where('paragraph',$para)
  394. ->where('word_start',$word_start)
  395. ->where('word_end',$word_end)
  396. ->select('channel_uid')
  397. ->groupBy('channel_uid')
  398. ->get();
  399. $channelList = [];
  400. foreach ($channels as $key => $value) {
  401. # code...
  402. $channelList[] = $value->channel_uid;
  403. }
  404. $channelInfo = Channel::whereIn("uid",$channelList)->select('type')->get();
  405. $output["tranNum"]=0;
  406. $output["nissayaNum"]=0;
  407. $output["commNum"]=0;
  408. $output["originNum"]=0;
  409. foreach ($channelInfo as $key => $value) {
  410. # code...
  411. switch($value->type){
  412. case "translation":
  413. $output["tranNum"]++;
  414. break;
  415. case "nissaya":
  416. $output["nissayaNum"]++;
  417. break;
  418. case "commentary":
  419. $output["commNum"]++;
  420. break;
  421. case "original":
  422. $output["originNum"]++;
  423. break;
  424. }
  425. }
  426. return $output;
  427. });
  428. $sent["tranNum"] = $channelCount['tranNum'];
  429. $sent["nissayaNum"] = $channelCount['nissayaNum'];
  430. $sent["commNum"] = $channelCount['commNum'];
  431. $sent["originNum"] = $channelCount['originNum'];
  432. return $sent;
  433. }
  434. private function markdownRender($input){
  435. }
  436. /**
  437. * Update the specified resource in storage.
  438. *
  439. * @param \Illuminate\Http\Request $request
  440. * @param \App\Models\Sentence $sentence
  441. * @return \Illuminate\Http\Response
  442. */
  443. public function update(Request $request, Sentence $sentence)
  444. {
  445. //
  446. }
  447. /**
  448. * Remove the specified resource from storage.
  449. *
  450. * @param \App\Models\Sentence $sentence
  451. * @return \Illuminate\Http\Response
  452. */
  453. public function destroy(Sentence $sentence)
  454. {
  455. //
  456. }
  457. }