CorpusController.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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 App\Models\PaliSentence;
  11. use App\Models\SentSimIndex;
  12. use Illuminate\Http\Request;
  13. use Illuminate\Support\Facades\Cache;
  14. use App\Http\Api\MdRender;
  15. use App\Http\Api\SuggestionApi;
  16. use App\Http\Api\ChannelApi;
  17. use App\Http\Api\UserApi;
  18. use App\Http\Api\StudioApi;
  19. use Illuminate\Support\Facades\Log;
  20. use Illuminate\Support\Arr;
  21. use App\Http\Resources\TocResource;
  22. class CorpusController extends Controller
  23. {
  24. protected $result = [
  25. "uid"=> '',
  26. "title"=> '',
  27. "path"=>[],
  28. "sub_title"=> '',
  29. "summary"=> '',
  30. "content"=> '',
  31. "content_type"=> "html",
  32. "toc" => [],
  33. "status"=>30,
  34. "lang"=> "",
  35. "created_at"=> "",
  36. "updated_at"=> "",
  37. ];
  38. protected $wbwChannels = [];
  39. //句子需要查询的列
  40. protected $selectCol = [
  41. 'uid',
  42. 'book_id',
  43. 'paragraph',
  44. 'word_start',
  45. "word_end",
  46. 'channel_uid',
  47. 'content',
  48. 'editor_uid',
  49. 'acceptor_uid',
  50. 'pr_edit_at',
  51. 'updated_at'
  52. ];
  53. public function __construct()
  54. {
  55. }
  56. /**
  57. * Display a listing of the resource.
  58. *
  59. * @return \Illuminate\Http\Response
  60. */
  61. public function index(Request $request)
  62. {
  63. //
  64. switch ($request->get('view')) {
  65. case 'para':
  66. return $this->showPara($request);
  67. break;
  68. default:
  69. # code...
  70. break;
  71. }
  72. }
  73. /**
  74. * Store a newly created resource in storage.
  75. *
  76. * @param \Illuminate\Http\Request $request
  77. * @return \Illuminate\Http\Response
  78. */
  79. public function store(Request $request)
  80. {
  81. //
  82. }
  83. /**
  84. * Display the specified resource.
  85. *
  86. * @param \App\Models\Sentence $sentence
  87. * @return \Illuminate\Http\Response
  88. */
  89. public function show(Sentence $sentence)
  90. {
  91. //
  92. }
  93. public function getSentTpl($id,$channels,$mode='edit',$onlyProps=false){
  94. $sent = [];
  95. $sentId = \explode('-',$id);
  96. if($mode==='read'){
  97. $channelId = ChannelApi::getSysChannel('_System_Pali_VRI_');
  98. }else{
  99. $channelId = ChannelApi::getSysChannel('_System_Wbw_VRI_');
  100. }
  101. if($channelId !== false){
  102. array_push($channels,$channelId);
  103. }
  104. $record = Sentence::select($this->selectCol)
  105. ->where('book_id',$sentId[0])
  106. ->where('paragraph',$sentId[1])
  107. ->where('word_start',(int)$sentId[2])
  108. ->where('word_end',(int)$sentId[3])
  109. ->whereIn('channel_uid',$channels)
  110. ->get();
  111. $channelIndex = $this->getChannelIndex($channels);
  112. //获取wbw channel
  113. //目前默认的 wbw channel 是第一个translation channel
  114. foreach ($channels as $channel) {
  115. # code...
  116. if($channelIndex[$channel]->type==='translation'){
  117. $this->wbwChannels[] = $channel;
  118. break;
  119. }
  120. }
  121. return $this->makeContent($record,$mode,$channelIndex,[],$onlyProps);
  122. }
  123. /**
  124. * Display the specified resource.
  125. *
  126. * @param string $id
  127. * @return \Illuminate\Http\Response
  128. */
  129. public function showSent($id)
  130. {
  131. //
  132. $param = \explode('_',$id);
  133. if(count($param)>1){
  134. $channels = array_slice($param,1);
  135. }else{
  136. $channels = [];
  137. }
  138. $this->result['content'] = getSentTpl($param[0],$channels);
  139. return $this->ok($this->result);
  140. }
  141. public function showSentences($type,$id,$mode='read'){
  142. $param = \explode('_',$id);
  143. $sentId = \explode('-',$param[0]);
  144. $channels = [];
  145. #获取channel类型
  146. $sentChannel = Sentence::select('channel_uid')
  147. ->where('book_id',$sentId[0])
  148. ->where('paragraph',$sentId[1])
  149. ->where('word_start',$sentId[2])
  150. ->where('word_end',$sentId[3])
  151. ->get();
  152. foreach ($sentChannel as $key => $value) {
  153. # code...
  154. $channels[] = $value->channel_uid;
  155. }
  156. $channelInfo = Channel::whereIn("uid",$channels)->select(['uid','type','name'])->get();
  157. $indexChannel = [];
  158. $channels = [];
  159. foreach ($channelInfo as $key => $value) {
  160. # code...
  161. if($value->type === $type){
  162. $indexChannel[$value->uid] = $value;
  163. $channels[] = $value->uid;
  164. }
  165. }
  166. //获取句子数据
  167. $record = Sentence::select($this->selectCol)
  168. ->where('book_id',$sentId[0])
  169. ->where('paragraph',$sentId[1])
  170. ->where('word_start',$sentId[2])
  171. ->where('word_end',$sentId[3])
  172. ->whereIn('channel_uid',$channels)
  173. ->orderBy('paragraph')
  174. ->orderBy('word_start')
  175. ->get();
  176. if(count($record) ===0){
  177. return $this->error("no data");
  178. }
  179. $this->result['content'] = $this->makeContent($record,$mode,$indexChannel);
  180. return $this->ok($this->result);
  181. }
  182. /**
  183. * Store a newly created resource in storage.
  184. * @param \Illuminate\Http\Request $request
  185. * @param string $id
  186. * @param string $mode
  187. * @return \Illuminate\Http\Response
  188. */
  189. public function showPara(Request $request)
  190. {
  191. //
  192. $channels = [];
  193. if($request->get('mode') === 'edit'){
  194. //翻译模式加载json格式原文
  195. $channels[] = ChannelApi::getSysChannel('_System_Wbw_VRI_');
  196. }else{
  197. //阅读模式加载html格式原文
  198. $channels[] = ChannelApi::getSysChannel('_System_Pali_VRI_');
  199. }
  200. if($request->has('channel')){
  201. $channels = array_merge($channels,explode(",",$request->get('channel')) );
  202. }
  203. $para = explode(",",$request->get('par'));
  204. //段落所在章节
  205. $parent = PaliText::where('book',$request->get('book'))
  206. ->where('paragraph',$para[0])->first();
  207. $chapter = PaliText::where('book',$request->get('book'))
  208. ->where('paragraph',$parent->parent)->first();
  209. if($chapter){
  210. if(empty($chapter->toc)){
  211. $this->result['title'] = "unknown";
  212. }else{
  213. $this->result['title'] = $chapter->toc;
  214. $this->result['sub_title'] = $chapter->toc;
  215. $this->result['path'] = json_decode($chapter->path);
  216. }
  217. }
  218. $paraFrom = $para[0];
  219. $paraTo = end($para);
  220. $indexedHeading = [];
  221. #获取channel索引表
  222. $tranChannels = [];
  223. $channelInfo = Channel::whereIn("uid",$channels)->select(['uid','type','name'])->get();
  224. foreach ($channelInfo as $key => $value) {
  225. # code...
  226. if($value->type==="translation" ){
  227. $tranChannels[] = $value->uid;
  228. }
  229. }
  230. $indexChannel = [];
  231. $indexChannel = $this->getChannelIndex($channels);
  232. //获取wbw channel
  233. //目前默认的 wbw channel 是第一个translation channel
  234. foreach ($channels as $key => $value) {
  235. # code...
  236. if($indexChannel[$value]->type==='translation'){
  237. $this->wbwChannels[] = $value;
  238. break;
  239. }
  240. }
  241. //章节译文标题
  242. $title = Sentence::select($this->selectCol)
  243. ->where('book_id',$parent->book)
  244. ->where('paragraph',$parent->paragraph)
  245. ->whereIn('channel_uid',$tranChannels)
  246. ->first();
  247. if($title){
  248. $this->result['title'] = MdRender::render($title->content,$title->channel_uid);
  249. }
  250. /**
  251. * 获取句子数据
  252. */
  253. $record = Sentence::select($this->selectCol)
  254. ->where('book_id',$request->get('book'))
  255. ->whereIn('paragraph',$para)
  256. ->whereIn('channel_uid',$channels)
  257. ->orderBy('paragraph')
  258. ->orderBy('word_start')
  259. ->get();
  260. if(count($record) ===0){
  261. $this->result['content'] = "<span>No Data</span>";
  262. }else{
  263. $this->result['content'] = $this->makeContent($record,$request->get('mode','read'),$indexChannel,$indexedHeading);
  264. }
  265. return $this->ok($this->result);
  266. }
  267. /**
  268. * Store a newly created resource in storage.
  269. * @param \Illuminate\Http\Request $request
  270. * @param string $id
  271. * @return \Illuminate\Http\Response
  272. */
  273. public function showChapter(Request $request, string $id)
  274. {
  275. //
  276. $sentId = \explode('-',$id);
  277. $channels = [];
  278. if($request->has('channels')){
  279. $channels = explode('_',$request->get('channels'));
  280. }
  281. $mode = $request->get('mode','read');
  282. if($mode === 'read'){
  283. //阅读模式加载html格式原文
  284. $channelId = ChannelApi::getSysChannel('_System_Pali_VRI_');
  285. }else{
  286. //翻译模式加载json格式原文
  287. $channelId = ChannelApi::getSysChannel('_System_Wbw_VRI_');
  288. }
  289. if($channelId !== false){
  290. $channels[] = $channelId;
  291. }
  292. $chapter = PaliText::where('book',$sentId[0])->where('paragraph',$sentId[1])->first();
  293. if(!$chapter){
  294. return $this->error("no data");
  295. }
  296. if(empty($chapter->toc)){
  297. $this->result['title'] = "unknown";
  298. }else{
  299. $this->result['title'] = $chapter->toc;
  300. $this->result['sub_title'] = $chapter->toc;
  301. $this->result['path'] = json_decode($chapter->path);
  302. }
  303. $paraFrom = $sentId[1];
  304. $paraTo = $sentId[1]+$chapter->chapter_len-1;
  305. //获取标题
  306. $heading = PaliText::select(["book","paragraph","level"])
  307. ->where('book',$sentId[0])
  308. ->whereBetween('paragraph',[$paraFrom,$paraTo])
  309. ->where('level','<',8)
  310. ->get();
  311. //将标题段落转成索引数组 以便输出标题层级
  312. $indexedHeading = [];
  313. foreach ($heading as $key => $value) {
  314. # code...
  315. $indexedHeading["{$value->book}-{$value->paragraph}"] = $value->level;
  316. }
  317. #获取channel索引表
  318. $tranChannels = [];
  319. $channelInfo = Channel::whereIn("uid",$channels)->select(['uid','type','name'])->get();
  320. foreach ($channelInfo as $key => $value) {
  321. # code...
  322. if($value->type==="translation" ){
  323. $tranChannels[] = $value->uid;
  324. }
  325. }
  326. $indexChannel = [];
  327. $indexChannel = $this->getChannelIndex($channels);
  328. //获取wbw channel
  329. //目前默认的 wbw channel 是第一个translation channel
  330. foreach ($channels as $key => $value) {
  331. # code...
  332. if($indexChannel[$value]->type==='translation'){
  333. $this->wbwChannels[] = $value;
  334. break;
  335. }
  336. }
  337. $title = Sentence::select($this->selectCol)
  338. ->where('book_id',$sentId[0])
  339. ->where('paragraph',$sentId[1])
  340. ->whereIn('channel_uid',$tranChannels)
  341. ->first();
  342. if($title){
  343. $this->result['title'] = MdRender::render($title->content,$title->channel_uid);
  344. }
  345. /**
  346. * 获取句子数据
  347. * 算法:
  348. * 1. 如果标题和下一级第一个标题之间有段落。只输出这些段落和子目录
  349. * 2. 如果标题和下一级第一个标题之间没有间隔 且 chapter 长度大于10000个字符 且有子目录,只输出子目录
  350. * 3. 如果二者都不是,lazy load
  351. */
  352. //1. 计算 标题和下一级第一个标题之间 是否有间隔
  353. $nextChapter = PaliText::where('book',$sentId[0])
  354. ->where('paragraph',">",$sentId[1])
  355. ->where('level','<',8)
  356. ->orderBy('paragraph')
  357. ->value('paragraph');
  358. $between = $nextChapter - $sentId[1];
  359. //输出子目录
  360. $chapterLen = $chapter->chapter_len;
  361. $toc = PaliText::where('book',$sentId[0])
  362. ->whereBetween('paragraph',[$paraFrom+1,$paraFrom+$chapterLen-1])
  363. ->where('level','<',8)
  364. ->orderBy('paragraph')
  365. ->select(['book','paragraph','level','toc'])
  366. ->get();
  367. if($between > 1){
  368. //有间隔
  369. $paraTo = $nextChapter - 1;
  370. }else{
  371. if($chapter->chapter_strlen>15000){
  372. if(count($toc)>0){
  373. //有子目录只输出标题和目录
  374. $paraTo = $paraFrom;
  375. }else{
  376. //没有子目录 全部输出
  377. }
  378. }else{
  379. //章节小。全部输出 不输出章节
  380. $toc = [];
  381. }
  382. }
  383. $record = Sentence::select($this->selectCol)
  384. ->where('book_id',$sentId[0])
  385. ->whereBetween('paragraph',[$paraFrom,$paraTo])
  386. ->whereIn('channel_uid',$channels)
  387. ->orderBy('paragraph')
  388. ->orderBy('word_start')
  389. ->get();
  390. if(count($record) ===0){
  391. return $this->error("no data");
  392. }
  393. $this->result['content'] = $this->makeContent($record,$mode,$indexChannel,$indexedHeading);
  394. $this->result['toc'] = TocResource::collection($toc);
  395. return $this->ok($this->result);
  396. }
  397. private function getChannelIndex($channels,$type=null){
  398. #获取channel索引表
  399. $channelInfo = Channel::whereIn("uid",$channels)->select(['uid','type','name','owner_uid'])->get();
  400. $indexChannel = [];
  401. foreach ($channelInfo as $key => $value) {
  402. # code...
  403. if($type !== null && $value->type !== $type){
  404. continue;
  405. }
  406. $indexChannel[$value->uid] = $value;
  407. }
  408. foreach ($indexChannel as $uid => $value) {
  409. # 查询studio
  410. $indexChannel[$uid]['studio'] = StudioApi::getById($value->owner_uid);
  411. }
  412. return $indexChannel;
  413. }
  414. /**
  415. * 根据句子库数据生成文章内容
  416. * $record 句子数据
  417. * $mode read | edit | wbw
  418. * $indexChannel channel索引
  419. * $indexedHeading 标题索引 用于给段落加标题标签 <h1> ect.
  420. */
  421. private function makeContent($record,$mode,$indexChannel,$indexedHeading=[],$onlyProps=false){
  422. $content = [];
  423. $lastSent = "0-0";
  424. $sentCount = 0;
  425. $sent = [];
  426. $sent["origin"] = [];
  427. $sent["translation"] = [];
  428. //获取句子编号列表
  429. $sentList = [];
  430. foreach ($record as $key => $value) {
  431. $currSentId = "{$value->book_id}-{$value->paragraph}-{$value->word_start}-{$value->word_end}";
  432. $sentList[$currSentId]=[$value->book_id ,$value->paragraph,$value->word_start,$value->word_end];
  433. $value['sid'] = "{$currSentId}_{$value->channel_uid}";
  434. }
  435. //遍历列表查找每个句子的所有channel的数据,并填充
  436. foreach ($sentList as $currSentId => $arrSentId) {
  437. # code...
  438. $sent = $this->newSent($arrSentId[0],$arrSentId[1],$arrSentId[2],$arrSentId[3]);
  439. $sent["origin"] = [];
  440. $sent["translation"] = [];
  441. foreach ($indexChannel as $channelId => $info) {
  442. # code...
  443. $sid = "{$currSentId}_{$channelId}";
  444. $newSent = [
  445. "content"=>"",
  446. "html"=> "",
  447. "book"=> $arrSentId[0],
  448. "para"=> $arrSentId[1],
  449. "wordStart"=> $arrSentId[2],
  450. "wordEnd"=> $arrSentId[3],
  451. "channel"=> [
  452. "name"=>$info->name,
  453. "type"=>$info->type,
  454. "id"=> $info->uid,
  455. ],
  456. "studio" => $info['studio'],
  457. "updateAt"=> "",
  458. "suggestionCount" => SuggestionApi::getCountBySent($arrSentId[0],$arrSentId[1],$arrSentId[2],$arrSentId[3],$channelId),
  459. ];
  460. $row = Arr::first($record,function($value,$key) use($sid){
  461. return $value['sid']===$sid;
  462. });
  463. if($row){
  464. $newSent['id'] = $row->uid;
  465. $newSent['content'] = $row->content;
  466. $newSent['html'] = "";
  467. $newSent["editor"]=UserApi::getById($row->editor_uid);
  468. $newSent['updateAt'] = $row->updated_at;
  469. if($mode !== "read"){
  470. if(isset($row->acceptor_uid) && !empty($row->acceptor_uid)){
  471. $newSent["acceptor"]=UserApi::getById($row->acceptor_uid);
  472. $newSent["prEditAt"]=$row->pr_edit_at;
  473. }
  474. }
  475. switch ($info->type) {
  476. case 'wbw':
  477. case 'original':
  478. //
  479. // 在编辑模式下。
  480. // 如果是原文,查看是否有逐词解析数据,
  481. // 有的话优先显示。
  482. // 阅读模式直接显示html原文
  483. // 传过来的数据一定有一个原文channel
  484. //
  485. if($mode !== "read"){
  486. $newSent['channel']['type'] = "wbw";
  487. if(isset($this->wbwChannels[0])){
  488. //存在一个translation channel
  489. //尝试查找逐词解析数据。找到,替换现有数据
  490. $wbwData = $this->getWbw($arrSentId[0],$arrSentId[1],$arrSentId[2],$arrSentId[3],$channelId);
  491. if($wbwData){
  492. $newSent['content'] = $wbwData;
  493. $newSent['html'] = "";
  494. }
  495. }
  496. }else{
  497. $newSent['html'] = $row->content;
  498. $newSent['content'] = "";
  499. }
  500. break;
  501. case 'nissaya':
  502. $newSent['html'] = Cache::remember("/sent/{$channelId}/{$currSentId}",10,
  503. function() use($row,$mode){
  504. return MdRender::render($row->content,$row->channel_uid,null,$mode,"nissaya");
  505. });
  506. break;
  507. default:
  508. //译文需要markdown渲染
  509. $newSent['html'] = Cache::remember("/sent/{$channelId}/{$currSentId}",10,
  510. function() use($row){
  511. return MdRender::render($row->content,$row->channel_uid);
  512. });
  513. break;
  514. }
  515. }
  516. switch ($info->type) {
  517. case 'wbw':
  518. case 'original':
  519. array_push($sent["origin"],$newSent);
  520. break;
  521. default:
  522. array_push($sent["translation"],$newSent);
  523. break;
  524. }
  525. }
  526. if($onlyProps){
  527. return $sent;
  528. }
  529. $content = $this->pushSent($content,$sent,0,$mode);
  530. }
  531. /*
  532. foreach ($record as $key => $value) {
  533. # 遍历结果生成html文件
  534. $currSentId = $value->book_id.'-'.$value->paragraph.'-'.$value->word_start.'-'.$value->word_end;
  535. if($currSentId !== $lastSent){
  536. if($sentCount > 0){
  537. //保存上一个句子
  538. //增加标题的html标记
  539. $level = 0;
  540. if(isset($indexedHeading["{$value->book_id}-{$value->paragraph}"])){
  541. $level = $indexedHeading["{$value->book_id}-{$value->paragraph}"];
  542. }
  543. $content = $this->pushSent($content,$sent,$level,$mode);
  544. }
  545. //新建句子
  546. $sent = $this->newSent($value->book_id,$value->paragraph,$value->word_start,$value->word_end);
  547. $lastSent = $currSentId;
  548. }
  549. $sentContent=$value->content;
  550. $channelType = $indexChannel[$value->channel_uid]->type;
  551. if($indexChannel[$value->channel_uid]->type==="original" && $mode !== 'read'){
  552. //非阅读模式下。原文使用逐词解析数据。优先加载第一个translation channel 如果没有。加载默认逐词解析。
  553. $channelType = 'wbw';
  554. $html = "";
  555. if(count($this->wbwChannels)>0){
  556. //获取逐词解析数据
  557. $wbwBlock = WbwBlock::where('channel_uid',$this->wbwChannels[0])
  558. ->where('book_id',$value->book_id)
  559. ->where('paragraph',$value->paragraph)
  560. ->select('uid')
  561. ->first();
  562. if($wbwBlock){
  563. //找到逐词解析数据
  564. $wbwData = Wbw::where('block_uid',$wbwBlock->uid)
  565. ->whereBetween('wid',[$value->word_start,$value->word_end])
  566. ->select(['data','uid'])
  567. ->orderBy('wid')
  568. ->get();
  569. $wbwContent = [];
  570. foreach ($wbwData as $wbwrow) {
  571. $wbw = str_replace("&nbsp;",' ',$wbwrow->data);
  572. $wbw = str_replace("<br>",' ',$wbw);
  573. $xmlString = "<root>" . $wbw . "</root>";
  574. try{
  575. $xmlWord = simplexml_load_string($xmlString);
  576. }catch(Exception $e){
  577. continue;
  578. }
  579. $wordsList = $xmlWord->xpath('//word');
  580. foreach ($wordsList as $word) {
  581. $case = \str_replace(['#','.'],['$',''],$word->case->__toString());
  582. $case = \str_replace('$$','$',$case);
  583. $case = trim($case);
  584. $case = trim($case,"$");
  585. $wbwContent[] = [
  586. 'uid'=>$wbwrow->uid,
  587. 'word'=>['value'=>$word->pali->__toString(),'status'=>0],
  588. 'real'=> ['value'=>$word->real->__toString(),'status'=>0],
  589. 'meaning'=> ['value'=>\explode('$',$word->mean->__toString()) ,'status'=>0],
  590. 'type'=> ['value'=>$word->type->__toString(),'status'=>0],
  591. 'grammar'=> ['value'=>$word->gramma->__toString(),'status'=>0],
  592. 'case'=> ['value'=>\explode('$',$case),'status'=>0],
  593. 'parent'=> ['value'=>$word->parent->__toString(),'status'=>0],
  594. 'style'=> ['value'=>$word->style->__toString(),'status'=>0],
  595. 'factors'=> ['value'=>$word->org->__toString(),'status'=>0],
  596. 'factorMeaning'=> ['value'=>$word->om->__toString(),'status'=>0],
  597. 'confidence'=> 0.5,
  598. 'hasComment'=>Discussion::where('res_id',$wbwrow->uid)->exists(),
  599. ];
  600. }
  601. }
  602. $sentContent = \json_encode($wbwContent);
  603. }
  604. }
  605. }else{
  606. if($indexChannel[$value->channel_uid]->type==="original"){
  607. //原文直接使用
  608. $html = Cache::remember("/sent/{$value->channel_uid}/{$currSentId}",10,
  609. function() use($value){
  610. return $value->content;
  611. });
  612. }else{
  613. //译文需要markdown渲染
  614. $html = Cache::remember("/sent/{$value->channel_uid}/{$currSentId}",10,
  615. function() use($value){
  616. return MdRender::render($value->content,$value->channel_uid);
  617. });
  618. }
  619. }
  620. $newSent = [
  621. "content"=>$sentContent,
  622. "html"=> $html,
  623. "book"=> $value->book_id,
  624. "para"=> $value->paragraph,
  625. "wordStart"=> $value->word_start,
  626. "wordEnd"=> $value->word_end,
  627. "editor"=> [
  628. 'id'=>$value->editor_uid,
  629. 'nickName'=>'nickname',
  630. 'realName'=>'realName',
  631. 'avatar'=>'',
  632. ],
  633. "channel"=> [
  634. "name"=>$indexChannel[$value->channel_uid]->name,
  635. "type"=>$channelType,
  636. "id"=> $value->channel_uid,
  637. ],
  638. "updateAt"=> $value->updated_at,
  639. "suggestionCount" => SuggestionApi::getCountBySent($value->book_id,$value->paragraph,$value->word_start,$value->word_end,$value->channel_uid),
  640. ];
  641. switch ($indexChannel[$value->channel_uid]->type) {
  642. case 'original';
  643. case 'wbw';
  644. array_push($sent["origin"],$newSent);
  645. break;
  646. default:
  647. array_push($sent["translation"],$newSent);
  648. break;
  649. }
  650. $sentCount++;
  651. }
  652. if($onlyProps){
  653. return $sent;
  654. }
  655. $content = $this->pushSent($content,$sent,0,$mode);
  656. */
  657. $output = \implode("",$content);
  658. return "<div>{$output}</div>";
  659. }
  660. private function getWbw($book,$para,$start,$end,$channel){
  661. /**
  662. * 非阅读模式下。原文使用逐词解析数据。
  663. * 优先加载第一个translation channel 如果没有。加载默认逐词解析。
  664. */
  665. //获取逐词解析数据
  666. $wbwBlock = WbwBlock::where('channel_uid',$channel)
  667. ->where('book_id',$book)
  668. ->where('paragraph',$para)
  669. ->select('uid')
  670. ->first();
  671. if(!$wbwBlock){
  672. return false;
  673. }
  674. //找到逐词解析数据
  675. $wbwData = Wbw::where('block_uid',$wbwBlock->uid)
  676. ->whereBetween('wid',[$start,$end])
  677. ->select(['data','uid'])
  678. ->orderBy('wid')
  679. ->get();
  680. $wbwContent = [];
  681. foreach ($wbwData as $wbwrow) {
  682. $wbw = str_replace("&nbsp;",' ',$wbwrow->data);
  683. $wbw = str_replace("<br>",' ',$wbw);
  684. $xmlString = "<root>" . $wbw . "</root>";
  685. try{
  686. $xmlWord = simplexml_load_string($xmlString);
  687. }catch(Exception $e){
  688. continue;
  689. }
  690. $wordsList = $xmlWord->xpath('//word');
  691. foreach ($wordsList as $word) {
  692. $case = \str_replace(['#','.'],['$',''],$word->case->__toString());
  693. $case = \str_replace('$$','$',$case);
  694. $case = trim($case);
  695. $case = trim($case,"$");
  696. $wbwContent[] = [
  697. 'uid'=>$wbwrow->uid,
  698. 'word'=>['value'=>$word->pali->__toString(),'status'=>0],
  699. 'real'=> ['value'=>$word->real->__toString(),'status'=>0],
  700. 'meaning'=> ['value'=>\explode('$',$word->mean->__toString()) ,'status'=>0],
  701. 'type'=> ['value'=>$word->type->__toString(),'status'=>0],
  702. 'grammar'=> ['value'=>$word->gramma->__toString(),'status'=>0],
  703. 'case'=> ['value'=>\explode('$',$case),'status'=>0],
  704. 'parent'=> ['value'=>$word->parent->__toString(),'status'=>0],
  705. 'style'=> ['value'=>$word->style->__toString(),'status'=>0],
  706. 'factors'=> ['value'=>$word->org->__toString(),'status'=>0],
  707. 'factorMeaning'=> ['value'=>$word->om->__toString(),'status'=>0],
  708. 'confidence'=> 0.5,
  709. 'hasComment'=>Discussion::where('res_id',$wbwrow->uid)->exists(),
  710. ];
  711. }
  712. }
  713. return \json_encode($wbwContent,JSON_UNESCAPED_UNICODE);
  714. }
  715. /**
  716. * 将句子放进结果列表
  717. */
  718. private function pushSent($result,$sent,$level=0,$mode='read'){
  719. $sentProps = base64_encode(\json_encode($sent)) ;
  720. if($mode === 'read'){
  721. $sentWidget = "<MdTpl tpl='sentread' props='{$sentProps}' />";
  722. }else{
  723. $sentWidget = "<MdTpl tpl='sentedit' props='{$sentProps}' />";
  724. }
  725. //增加标题的html标记
  726. if($level>0){
  727. $sentWidget = "<h{$level}>".$sentWidget."</h{$level}>";
  728. }
  729. array_push($result,$sentWidget);
  730. return $result;
  731. }
  732. private function newSent($book,$para,$word_start,$word_end){
  733. $sent = [
  734. "id"=>"{$book}-{$para}-{$word_start}-{$word_end}",
  735. "origin"=>[],
  736. "translation"=>[],
  737. ];
  738. #生成channel 数量列表
  739. $sentId = "{$book}-{$para}-{$word_start}-{$word_end}";
  740. $channelCount = CorpusController::sentResCount($book,$para,$word_start,$word_end);
  741. $path = json_decode(PaliText::where('book',$book)->where('paragraph',$para)->value("path"),true);
  742. $sent["path"] = [];
  743. foreach ($path as $key => $value) {
  744. # code...
  745. $value['paliTitle'] = $value['title'];
  746. $sent["path"][] = $value;
  747. }
  748. $sent["tranNum"] = $channelCount['tranNum'];
  749. $sent["nissayaNum"] = $channelCount['nissayaNum'];
  750. $sent["commNum"] = $channelCount['commNum'];
  751. $sent["originNum"] = $channelCount['originNum'];
  752. $sent["simNum"] = $channelCount['simNum'];
  753. return $sent;
  754. }
  755. /**
  756. * 获取某个句子的相关资源的句子数量
  757. */
  758. public static function sentResCount($book,$para,$start,$end){
  759. $sentId = "{$book}-{$para}-{$start}-{$end}";
  760. $channelCount = Cache::remember("/sentence/{$sentId}/channels/count",
  761. 60,
  762. function() use($book,$para,$start,$end){
  763. $channels = Sentence::where('book_id',$book)
  764. ->where('paragraph',$para)
  765. ->where('word_start',$start)
  766. ->where('word_end',$end)
  767. ->select('channel_uid')
  768. ->groupBy('channel_uid')
  769. ->get();
  770. $channelList = [];
  771. foreach ($channels as $key => $value) {
  772. # code...
  773. $channelList[] = $value->channel_uid;
  774. }
  775. $simId = PaliSentence::where('book',$book)
  776. ->where('paragraph',$para)
  777. ->where('word_begin',$start)
  778. ->where('word_end',$end)
  779. ->value('id');
  780. if($simId){
  781. $output["simNum"]=SentSimIndex::where('sent_id',$simId)->value('count');
  782. }else{
  783. $output["simNum"]=0;
  784. }
  785. $channelInfo = Channel::whereIn("uid",$channelList)->select('type')->get();
  786. $output["tranNum"]=0;
  787. $output["nissayaNum"]=0;
  788. $output["commNum"]=0;
  789. $output["originNum"]=0;
  790. foreach ($channelInfo as $key => $value) {
  791. # code...
  792. switch($value->type){
  793. case "translation":
  794. $output["tranNum"]++;
  795. break;
  796. case "nissaya":
  797. $output["nissayaNum"]++;
  798. break;
  799. case "commentary":
  800. $output["commNum"]++;
  801. break;
  802. case "original":
  803. $output["originNum"]++;
  804. break;
  805. }
  806. }
  807. return $output;
  808. });
  809. return $channelCount;
  810. }
  811. private function markdownRender($input){
  812. }
  813. /**
  814. * Update the specified resource in storage.
  815. *
  816. * @param \Illuminate\Http\Request $request
  817. * @param \App\Models\Sentence $sentence
  818. * @return \Illuminate\Http\Response
  819. */
  820. public function update(Request $request, Sentence $sentence)
  821. {
  822. //
  823. }
  824. /**
  825. * Remove the specified resource from storage.
  826. *
  827. * @param \App\Models\Sentence $sentence
  828. * @return \Illuminate\Http\Response
  829. */
  830. public function destroy(Sentence $sentence)
  831. {
  832. //
  833. }
  834. }