CorpusController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. foreach ($record as $key => $value) {
  251. # 遍历结果生成html文件
  252. $currSentId = $value->book_id.'-'.$value->paragraph.'-'.$value->word_start.'-'.$value->word_end;
  253. if($currSentId !== $lastSent){
  254. if($sentCount > 0){
  255. //保存上一个句子
  256. //增加标题的html标记
  257. $level = 0;
  258. if(isset($indexedHeading["{$value->book_id}-{$value->paragraph}"])){
  259. $level = $indexedHeading["{$value->book_id}-{$value->paragraph}"];
  260. }
  261. $content = $this->pushSent($content,$sent,$level,$mode);
  262. }
  263. //新建句子
  264. $sent = $this->newSent($value->book_id,$value->paragraph,$value->word_start,$value->word_end);
  265. $lastSent = $currSentId;
  266. }
  267. $sentContent=$value->content;
  268. $channelType = $indexChannel[$value->channel_uid]->type;
  269. if($indexChannel[$value->channel_uid]->type==="original" && $mode !== 'read'){
  270. //非阅读模式下。原文使用逐词解析数据。优先加载第一个translation channel 如果没有。加载默认逐词解析。
  271. $channelType = 'wbw';
  272. $html = "";
  273. if(count($this->wbwChannels)>0){
  274. //获取逐词解析数据
  275. $wbwBlock = WbwBlock::where('channel_uid',$this->wbwChannels[0])
  276. ->where('book_id',$value->book_id)
  277. ->where('paragraph',$value->paragraph)
  278. ->select('uid')
  279. ->first();
  280. if($wbwBlock){
  281. //找到逐词解析数据
  282. $wbwData = Wbw::where('block_uid',$wbwBlock->uid)
  283. ->whereBetween('wid',[$value->word_start,$value->word_end])
  284. ->select(['data','uid'])
  285. ->orderBy('wid')
  286. ->get();
  287. $wbwContent = [];
  288. foreach ($wbwData as $wbwrow) {
  289. $wbw = str_replace("&nbsp;",' ',$wbwrow->data);
  290. $wbw = str_replace("<br>",' ',$wbw);
  291. $xmlString = "<root>" . $wbw . "</root>";
  292. try{
  293. $xmlWord = simplexml_load_string($xmlString);
  294. }catch(Exception $e){
  295. continue;
  296. }
  297. $wordsList = $xmlWord->xpath('//word');
  298. foreach ($wordsList as $word) {
  299. $case = \str_replace(['#','.'],['$',''],$word->case->__toString());
  300. $case = \str_replace('$$','$',$case);
  301. $case = trim($case);
  302. $case = trim($case,"$");
  303. $wbwContent[] = [
  304. 'uid'=>$wbwrow->uid,
  305. 'word'=>['value'=>$word->pali->__toString(),'status'=>0],
  306. 'real'=> ['value'=>$word->real->__toString(),'status'=>0],
  307. 'meaning'=> ['value'=>\explode('$',$word->mean->__toString()) ,'status'=>0],
  308. 'type'=> ['value'=>$word->type->__toString(),'status'=>0],
  309. 'grammar'=> ['value'=>$word->gramma->__toString(),'status'=>0],
  310. 'case'=> ['value'=>\explode('$',$case),'status'=>0],
  311. 'parent'=> ['value'=>$word->parent->__toString(),'status'=>0],
  312. 'style'=> ['value'=>$word->style->__toString(),'status'=>0],
  313. 'factors'=> ['value'=>$word->org->__toString(),'status'=>0],
  314. 'factorMeaning'=> ['value'=>$word->om->__toString(),'status'=>0],
  315. 'confidence'=> 0.5,
  316. 'hasComment'=>Discussion::where('res_id',$wbwrow->uid)->exists(),
  317. ];
  318. }
  319. }
  320. $sentContent = \json_encode($wbwContent);
  321. }
  322. }
  323. }else{
  324. $html = Cache::remember("/sent/{$value->channel_uid}/{$currSentId}",10,
  325. function() use($value){
  326. return MdRender::render($value->content,$value->channel_uid);
  327. });
  328. }
  329. $newSent = [
  330. "content"=>$sentContent,
  331. "html"=> $html,
  332. "book"=> $value->book_id,
  333. "para"=> $value->paragraph,
  334. "wordStart"=> $value->word_start,
  335. "wordEnd"=> $value->word_end,
  336. "editor"=> [
  337. 'id'=>$value->editor_uid,
  338. 'nickName'=>'nickname',
  339. 'realName'=>'realName',
  340. 'avatar'=>'',
  341. ],
  342. "channel"=> [
  343. "name"=>$indexChannel[$value->channel_uid]->name,
  344. "type"=>$channelType,
  345. "id"=> $value->channel_uid,
  346. ],
  347. "updateAt"=> $value->updated_at,
  348. "suggestionCount" => SuggestionApi::getCountBySent($value->book_id,$value->paragraph,$value->word_start,$value->word_end,$value->channel_uid),
  349. ];
  350. switch ($indexChannel[$value->channel_uid]->type) {
  351. case 'original';
  352. case 'wbw';
  353. array_push($sent["origin"],$newSent);
  354. break;
  355. default:
  356. array_push($sent["translation"],$newSent);
  357. break;
  358. }
  359. $sentCount++;
  360. }
  361. $content = $this->pushSent($content,$sent,0,$mode);
  362. return \implode("",$content);
  363. }
  364. private function pushSent($result,$sent,$level=0,$mode='read'){
  365. $sentProps = base64_encode(\json_encode($sent)) ;
  366. if($mode === 'read'){
  367. $sentWidget = "<MdTpl tpl='sentread' props='{$sentProps}' />";
  368. }else{
  369. $sentWidget = "<MdTpl tpl='sentedit' props='{$sentProps}' />";
  370. }
  371. //增加标题的html标记
  372. if($level>0){
  373. $sentWidget = "<h{$level}>".$sentWidget."</h{$level}>";
  374. }
  375. array_push($result,$sentWidget);
  376. return $result;
  377. }
  378. private function newSent($book,$para,$word_start,$word_end){
  379. $sent = [
  380. "id"=>"{$book}-{$para}-{$word_start}-{$word_end}",
  381. "origin"=>[],
  382. "translation"=>[],
  383. ];
  384. #生成channel 数量列表
  385. $sentId = "{$book}-{$para}-{$word_start}-{$word_end}";
  386. $channelCount = Cache::remember("/sent1/{$sentId}/channels/count",
  387. 60,
  388. function() use($book,$para,$word_start,$word_end){
  389. $channels = Sentence::where('book_id',$book)
  390. ->where('paragraph',$para)
  391. ->where('word_start',$word_start)
  392. ->where('word_end',$word_end)
  393. ->select('channel_uid')
  394. ->groupBy('channel_uid')
  395. ->get();
  396. $channelList = [];
  397. foreach ($channels as $key => $value) {
  398. # code...
  399. $channelList[] = $value->channel_uid;
  400. }
  401. $channelInfo = Channel::whereIn("uid",$channelList)->select('type')->get();
  402. $output["tranNum"]=0;
  403. $output["nissayaNum"]=0;
  404. $output["commNum"]=0;
  405. $output["originNum"]=0;
  406. foreach ($channelInfo as $key => $value) {
  407. # code...
  408. switch($value->type){
  409. case "translation":
  410. $output["tranNum"]++;
  411. break;
  412. case "nissaya":
  413. $output["nissayaNum"]++;
  414. break;
  415. case "commentary":
  416. $output["commNum"]++;
  417. break;
  418. case "original":
  419. $output["originNum"]++;
  420. break;
  421. }
  422. }
  423. return $output;
  424. });
  425. $sent["tranNum"] = $channelCount['tranNum'];
  426. $sent["nissayaNum"] = $channelCount['nissayaNum'];
  427. $sent["commNum"] = $channelCount['commNum'];
  428. $sent["originNum"] = $channelCount['originNum'];
  429. return $sent;
  430. }
  431. private function markdownRender($input){
  432. }
  433. /**
  434. * Update the specified resource in storage.
  435. *
  436. * @param \Illuminate\Http\Request $request
  437. * @param \App\Models\Sentence $sentence
  438. * @return \Illuminate\Http\Response
  439. */
  440. public function update(Request $request, Sentence $sentence)
  441. {
  442. //
  443. }
  444. /**
  445. * Remove the specified resource from storage.
  446. *
  447. * @param \App\Models\Sentence $sentence
  448. * @return \Illuminate\Http\Response
  449. */
  450. public function destroy(Sentence $sentence)
  451. {
  452. //
  453. }
  454. }