CorpusController.php 18 KB

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