CorpusController.php 18 KB

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