CorpusController.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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. * @param string $mode
  272. * @return \Illuminate\Http\Response
  273. */
  274. public function showChapter(Request $request, string $id,string $mode='read')
  275. {
  276. //
  277. $param = \explode('_',$id);
  278. $sentId = \explode('-',$param[0]);
  279. $channels = [];
  280. if(count($param)>1){
  281. $channels = array_slice($param,1);
  282. }
  283. if($mode === 'read'){
  284. //阅读模式加载html格式原文
  285. $channelId = ChannelApi::getSysChannel('_System_Pali_VRI_');
  286. }else{
  287. //翻译模式加载json格式原文
  288. $channelId = ChannelApi::getSysChannel('_System_Wbw_VRI_');
  289. }
  290. if($channelId !== false){
  291. $channels[] = $channelId;
  292. }
  293. $chapter = PaliText::where('book',$sentId[0])->where('paragraph',$sentId[1])->first();
  294. if(!$chapter){
  295. return $this->error("no data");
  296. }
  297. if(empty($chapter->toc)){
  298. $this->result['title'] = "unknown";
  299. }else{
  300. $this->result['title'] = $chapter->toc;
  301. $this->result['sub_title'] = $chapter->toc;
  302. $this->result['path'] = json_decode($chapter->path);
  303. }
  304. $paraFrom = $sentId[1];
  305. $paraTo = $sentId[1]+$chapter->chapter_len-1;
  306. //获取标题
  307. $heading = PaliText::select(["book","paragraph","level"])
  308. ->where('book',$sentId[0])
  309. ->whereBetween('paragraph',[$paraFrom,$paraTo])
  310. ->where('level','<',8)
  311. ->get();
  312. //将标题段落转成索引数组 以便输出标题层级
  313. $indexedHeading = [];
  314. foreach ($heading as $key => $value) {
  315. # code...
  316. $indexedHeading["{$value->book}-{$value->paragraph}"] = $value->level;
  317. }
  318. #获取channel索引表
  319. $tranChannels = [];
  320. $channelInfo = Channel::whereIn("uid",$channels)->select(['uid','type','name'])->get();
  321. foreach ($channelInfo as $key => $value) {
  322. # code...
  323. if($value->type==="translation" ){
  324. $tranChannels[] = $value->uid;
  325. }
  326. }
  327. $indexChannel = [];
  328. $indexChannel = $this->getChannelIndex($channels);
  329. //获取wbw channel
  330. //目前默认的 wbw channel 是第一个translation channel
  331. foreach ($channels as $key => $value) {
  332. # code...
  333. if($indexChannel[$value]->type==='translation'){
  334. $this->wbwChannels[] = $value;
  335. break;
  336. }
  337. }
  338. $title = Sentence::select($this->selectCol)
  339. ->where('book_id',$sentId[0])
  340. ->where('paragraph',$sentId[1])
  341. ->whereIn('channel_uid',$tranChannels)
  342. ->first();
  343. if($title){
  344. $this->result['title'] = MdRender::render($title->content,$title->channel_uid);
  345. }
  346. /**
  347. * 获取句子数据
  348. * 算法:
  349. * 1. 如果标题和下一级第一个标题之间有段落。只输出这些段落和子目录
  350. * 2. 如果标题和下一级第一个标题之间没有间隔 且 chapter 长度大于10000个字符 且有子目录,只输出子目录
  351. * 3. 如果二者都不是,lazy load
  352. */
  353. //1. 计算 标题和下一级第一个标题之间 是否有间隔
  354. $nextChapter = PaliText::where('book',$sentId[0])
  355. ->where('paragraph',">",$sentId[1])
  356. ->where('level','<',8)
  357. ->orderBy('paragraph')
  358. ->value('paragraph');
  359. $between = $nextChapter - $sentId[1];
  360. //输出子目录
  361. $chapterLen = $chapter->chapter_len;
  362. $toc = PaliText::where('book',$sentId[0])
  363. ->whereBetween('paragraph',[$paraFrom+1,$paraFrom+$chapterLen-1])
  364. ->where('level','<',8)
  365. ->orderBy('paragraph')
  366. ->select(['book','paragraph','level','toc'])
  367. ->get();
  368. if($between > 1){
  369. //有间隔
  370. $paraTo = $nextChapter - 1;
  371. }else{
  372. if($chapter->chapter_strlen>15000){
  373. if(count($toc)>0){
  374. //有子目录只输出标题和目录
  375. $paraTo = $paraFrom;
  376. }else{
  377. //没有子目录 全部输出
  378. }
  379. }else{
  380. //章节小。全部输出 不输出章节
  381. $toc = [];
  382. }
  383. }
  384. $record = Sentence::select($this->selectCol)
  385. ->where('book_id',$sentId[0])
  386. ->whereBetween('paragraph',[$paraFrom,$paraTo])
  387. ->whereIn('channel_uid',$channels)
  388. ->orderBy('paragraph')
  389. ->orderBy('word_start')
  390. ->get();
  391. if(count($record) ===0){
  392. return $this->error("no data");
  393. }
  394. $this->result['content'] = $this->makeContent($record,$mode,$indexChannel,$indexedHeading);
  395. $this->result['toc'] = TocResource::collection($toc);
  396. return $this->ok($this->result);
  397. }
  398. private function getChannelIndex($channels,$type=null){
  399. #获取channel索引表
  400. $channelInfo = Channel::whereIn("uid",$channels)->select(['uid','type','name','owner_uid'])->get();
  401. $indexChannel = [];
  402. foreach ($channelInfo as $key => $value) {
  403. # code...
  404. if($type !== null && $value->type !== $type){
  405. continue;
  406. }
  407. $indexChannel[$value->uid] = $value;
  408. }
  409. foreach ($indexChannel as $uid => $value) {
  410. # 查询studio
  411. $indexChannel[$uid]['studio'] = StudioApi::getById($value->owner_uid);
  412. }
  413. return $indexChannel;
  414. }
  415. /**
  416. * 根据句子库数据生成文章内容
  417. * $record 句子数据
  418. * $mode read | edit | wbw
  419. * $indexChannel channel索引
  420. * $indexedHeading 标题索引 用于给段落加标题标签 <h1> ect.
  421. */
  422. private function makeContent($record,$mode,$indexChannel,$indexedHeading=[],$onlyProps=false){
  423. $content = [];
  424. $lastSent = "0-0";
  425. $sentCount = 0;
  426. $sent = [];
  427. $sent["origin"] = [];
  428. $sent["translation"] = [];
  429. //获取句子编号列表
  430. $sentList = [];
  431. foreach ($record as $key => $value) {
  432. $currSentId = "{$value->book_id}-{$value->paragraph}-{$value->word_start}-{$value->word_end}";
  433. $sentList[$currSentId]=[$value->book_id ,$value->paragraph,$value->word_start,$value->word_end];
  434. $value['sid'] = "{$currSentId}_{$value->channel_uid}";
  435. }
  436. //遍历列表查找每个句子的所有channel的数据,并填充
  437. foreach ($sentList as $currSentId => $arrSentId) {
  438. # code...
  439. $sent = $this->newSent($arrSentId[0],$arrSentId[1],$arrSentId[2],$arrSentId[3]);
  440. $sent["origin"] = [];
  441. $sent["translation"] = [];
  442. foreach ($indexChannel as $channelId => $info) {
  443. # code...
  444. $sid = "{$currSentId}_{$channelId}";
  445. $newSent = [
  446. "content"=>"",
  447. "html"=> "",
  448. "book"=> $arrSentId[0],
  449. "para"=> $arrSentId[1],
  450. "wordStart"=> $arrSentId[2],
  451. "wordEnd"=> $arrSentId[3],
  452. "channel"=> [
  453. "name"=>$info->name,
  454. "type"=>$info->type,
  455. "id"=> $info->uid,
  456. ],
  457. "studio" => $info['studio'],
  458. "updateAt"=> "",
  459. "suggestionCount" => SuggestionApi::getCountBySent($arrSentId[0],$arrSentId[1],$arrSentId[2],$arrSentId[3],$channelId),
  460. ];
  461. $row = Arr::first($record,function($value,$key) use($sid){
  462. return $value['sid']===$sid;
  463. });
  464. if($row){
  465. $newSent['id'] = $row->uid;
  466. $newSent['content'] = $row->content;
  467. $newSent['html'] = "";
  468. $newSent["editor"]=UserApi::getById($row->editor_uid);
  469. $newSent['updateAt'] = $row->updated_at;
  470. if($mode !== "read"){
  471. if(isset($row->acceptor_uid) && !empty($row->acceptor_uid)){
  472. $newSent["acceptor"]=UserApi::getById($row->acceptor_uid);
  473. $newSent["prEditAt"]=$row->pr_edit_at;
  474. }
  475. }
  476. switch ($info->type) {
  477. case 'wbw':
  478. case 'original':
  479. //
  480. // 在编辑模式下。
  481. // 如果是原文,查看是否有逐词解析数据,
  482. // 有的话优先显示。
  483. // 阅读模式直接显示html原文
  484. // 传过来的数据一定有一个原文channel
  485. //
  486. if($mode !== "read"){
  487. $newSent['channel']['type'] = "wbw";
  488. if(isset($this->wbwChannels[0])){
  489. //存在一个translation channel
  490. //尝试查找逐词解析数据。找到,替换现有数据
  491. $wbwData = $this->getWbw($arrSentId[0],$arrSentId[1],$arrSentId[2],$arrSentId[3],$channelId);
  492. if($wbwData){
  493. $newSent['content'] = $wbwData;
  494. $newSent['html'] = "";
  495. }
  496. }
  497. }else{
  498. $newSent['html'] = $row->content;
  499. $newSent['content'] = "";
  500. }
  501. break;
  502. case 'nissaya':
  503. $newSent['html'] = Cache::remember("/sent/{$channelId}/{$currSentId}",10,
  504. function() use($row,$mode){
  505. return MdRender::render($row->content,$row->channel_uid,null,$mode,"nissaya");
  506. });
  507. break;
  508. default:
  509. //译文需要markdown渲染
  510. $newSent['html'] = Cache::remember("/sent/{$channelId}/{$currSentId}",10,
  511. function() use($row){
  512. return MdRender::render($row->content,$row->channel_uid);
  513. });
  514. break;
  515. }
  516. }
  517. switch ($info->type) {
  518. case 'wbw':
  519. case 'original':
  520. array_push($sent["origin"],$newSent);
  521. break;
  522. default:
  523. array_push($sent["translation"],$newSent);
  524. break;
  525. }
  526. }
  527. if($onlyProps){
  528. return $sent;
  529. }
  530. $content = $this->pushSent($content,$sent,0,$mode);
  531. }
  532. /*
  533. foreach ($record as $key => $value) {
  534. # 遍历结果生成html文件
  535. $currSentId = $value->book_id.'-'.$value->paragraph.'-'.$value->word_start.'-'.$value->word_end;
  536. if($currSentId !== $lastSent){
  537. if($sentCount > 0){
  538. //保存上一个句子
  539. //增加标题的html标记
  540. $level = 0;
  541. if(isset($indexedHeading["{$value->book_id}-{$value->paragraph}"])){
  542. $level = $indexedHeading["{$value->book_id}-{$value->paragraph}"];
  543. }
  544. $content = $this->pushSent($content,$sent,$level,$mode);
  545. }
  546. //新建句子
  547. $sent = $this->newSent($value->book_id,$value->paragraph,$value->word_start,$value->word_end);
  548. $lastSent = $currSentId;
  549. }
  550. $sentContent=$value->content;
  551. $channelType = $indexChannel[$value->channel_uid]->type;
  552. if($indexChannel[$value->channel_uid]->type==="original" && $mode !== 'read'){
  553. //非阅读模式下。原文使用逐词解析数据。优先加载第一个translation channel 如果没有。加载默认逐词解析。
  554. $channelType = 'wbw';
  555. $html = "";
  556. if(count($this->wbwChannels)>0){
  557. //获取逐词解析数据
  558. $wbwBlock = WbwBlock::where('channel_uid',$this->wbwChannels[0])
  559. ->where('book_id',$value->book_id)
  560. ->where('paragraph',$value->paragraph)
  561. ->select('uid')
  562. ->first();
  563. if($wbwBlock){
  564. //找到逐词解析数据
  565. $wbwData = Wbw::where('block_uid',$wbwBlock->uid)
  566. ->whereBetween('wid',[$value->word_start,$value->word_end])
  567. ->select(['data','uid'])
  568. ->orderBy('wid')
  569. ->get();
  570. $wbwContent = [];
  571. foreach ($wbwData as $wbwrow) {
  572. $wbw = str_replace("&nbsp;",' ',$wbwrow->data);
  573. $wbw = str_replace("<br>",' ',$wbw);
  574. $xmlString = "<root>" . $wbw . "</root>";
  575. try{
  576. $xmlWord = simplexml_load_string($xmlString);
  577. }catch(Exception $e){
  578. continue;
  579. }
  580. $wordsList = $xmlWord->xpath('//word');
  581. foreach ($wordsList as $word) {
  582. $case = \str_replace(['#','.'],['$',''],$word->case->__toString());
  583. $case = \str_replace('$$','$',$case);
  584. $case = trim($case);
  585. $case = trim($case,"$");
  586. $wbwContent[] = [
  587. 'uid'=>$wbwrow->uid,
  588. 'word'=>['value'=>$word->pali->__toString(),'status'=>0],
  589. 'real'=> ['value'=>$word->real->__toString(),'status'=>0],
  590. 'meaning'=> ['value'=>\explode('$',$word->mean->__toString()) ,'status'=>0],
  591. 'type'=> ['value'=>$word->type->__toString(),'status'=>0],
  592. 'grammar'=> ['value'=>$word->gramma->__toString(),'status'=>0],
  593. 'case'=> ['value'=>\explode('$',$case),'status'=>0],
  594. 'parent'=> ['value'=>$word->parent->__toString(),'status'=>0],
  595. 'style'=> ['value'=>$word->style->__toString(),'status'=>0],
  596. 'factors'=> ['value'=>$word->org->__toString(),'status'=>0],
  597. 'factorMeaning'=> ['value'=>$word->om->__toString(),'status'=>0],
  598. 'confidence'=> 0.5,
  599. 'hasComment'=>Discussion::where('res_id',$wbwrow->uid)->exists(),
  600. ];
  601. }
  602. }
  603. $sentContent = \json_encode($wbwContent);
  604. }
  605. }
  606. }else{
  607. if($indexChannel[$value->channel_uid]->type==="original"){
  608. //原文直接使用
  609. $html = Cache::remember("/sent/{$value->channel_uid}/{$currSentId}",10,
  610. function() use($value){
  611. return $value->content;
  612. });
  613. }else{
  614. //译文需要markdown渲染
  615. $html = Cache::remember("/sent/{$value->channel_uid}/{$currSentId}",10,
  616. function() use($value){
  617. return MdRender::render($value->content,$value->channel_uid);
  618. });
  619. }
  620. }
  621. $newSent = [
  622. "content"=>$sentContent,
  623. "html"=> $html,
  624. "book"=> $value->book_id,
  625. "para"=> $value->paragraph,
  626. "wordStart"=> $value->word_start,
  627. "wordEnd"=> $value->word_end,
  628. "editor"=> [
  629. 'id'=>$value->editor_uid,
  630. 'nickName'=>'nickname',
  631. 'realName'=>'realName',
  632. 'avatar'=>'',
  633. ],
  634. "channel"=> [
  635. "name"=>$indexChannel[$value->channel_uid]->name,
  636. "type"=>$channelType,
  637. "id"=> $value->channel_uid,
  638. ],
  639. "updateAt"=> $value->updated_at,
  640. "suggestionCount" => SuggestionApi::getCountBySent($value->book_id,$value->paragraph,$value->word_start,$value->word_end,$value->channel_uid),
  641. ];
  642. switch ($indexChannel[$value->channel_uid]->type) {
  643. case 'original';
  644. case 'wbw';
  645. array_push($sent["origin"],$newSent);
  646. break;
  647. default:
  648. array_push($sent["translation"],$newSent);
  649. break;
  650. }
  651. $sentCount++;
  652. }
  653. if($onlyProps){
  654. return $sent;
  655. }
  656. $content = $this->pushSent($content,$sent,0,$mode);
  657. */
  658. $output = \implode("",$content);
  659. return "<div>{$output}</div>";
  660. }
  661. private function getWbw($book,$para,$start,$end,$channel){
  662. /**
  663. * 非阅读模式下。原文使用逐词解析数据。
  664. * 优先加载第一个translation channel 如果没有。加载默认逐词解析。
  665. */
  666. //获取逐词解析数据
  667. $wbwBlock = WbwBlock::where('channel_uid',$channel)
  668. ->where('book_id',$book)
  669. ->where('paragraph',$para)
  670. ->select('uid')
  671. ->first();
  672. if(!$wbwBlock){
  673. return false;
  674. }
  675. //找到逐词解析数据
  676. $wbwData = Wbw::where('block_uid',$wbwBlock->uid)
  677. ->whereBetween('wid',[$start,$end])
  678. ->select(['data','uid'])
  679. ->orderBy('wid')
  680. ->get();
  681. $wbwContent = [];
  682. foreach ($wbwData as $wbwrow) {
  683. $wbw = str_replace("&nbsp;",' ',$wbwrow->data);
  684. $wbw = str_replace("<br>",' ',$wbw);
  685. $xmlString = "<root>" . $wbw . "</root>";
  686. try{
  687. $xmlWord = simplexml_load_string($xmlString);
  688. }catch(Exception $e){
  689. continue;
  690. }
  691. $wordsList = $xmlWord->xpath('//word');
  692. foreach ($wordsList as $word) {
  693. $case = \str_replace(['#','.'],['$',''],$word->case->__toString());
  694. $case = \str_replace('$$','$',$case);
  695. $case = trim($case);
  696. $case = trim($case,"$");
  697. $wbwContent[] = [
  698. 'uid'=>$wbwrow->uid,
  699. 'word'=>['value'=>$word->pali->__toString(),'status'=>0],
  700. 'real'=> ['value'=>$word->real->__toString(),'status'=>0],
  701. 'meaning'=> ['value'=>\explode('$',$word->mean->__toString()) ,'status'=>0],
  702. 'type'=> ['value'=>$word->type->__toString(),'status'=>0],
  703. 'grammar'=> ['value'=>$word->gramma->__toString(),'status'=>0],
  704. 'case'=> ['value'=>\explode('$',$case),'status'=>0],
  705. 'parent'=> ['value'=>$word->parent->__toString(),'status'=>0],
  706. 'style'=> ['value'=>$word->style->__toString(),'status'=>0],
  707. 'factors'=> ['value'=>$word->org->__toString(),'status'=>0],
  708. 'factorMeaning'=> ['value'=>$word->om->__toString(),'status'=>0],
  709. 'confidence'=> 0.5,
  710. 'hasComment'=>Discussion::where('res_id',$wbwrow->uid)->exists(),
  711. ];
  712. }
  713. }
  714. return \json_encode($wbwContent,JSON_UNESCAPED_UNICODE);
  715. }
  716. /**
  717. * 将句子放进结果列表
  718. */
  719. private function pushSent($result,$sent,$level=0,$mode='read'){
  720. $sentProps = base64_encode(\json_encode($sent)) ;
  721. if($mode === 'read'){
  722. $sentWidget = "<MdTpl tpl='sentread' props='{$sentProps}' />";
  723. }else{
  724. $sentWidget = "<MdTpl tpl='sentedit' props='{$sentProps}' />";
  725. }
  726. //增加标题的html标记
  727. if($level>0){
  728. $sentWidget = "<h{$level}>".$sentWidget."</h{$level}>";
  729. }
  730. array_push($result,$sentWidget);
  731. return $result;
  732. }
  733. private function newSent($book,$para,$word_start,$word_end){
  734. $sent = [
  735. "id"=>"{$book}-{$para}-{$word_start}-{$word_end}",
  736. "origin"=>[],
  737. "translation"=>[],
  738. ];
  739. #生成channel 数量列表
  740. $sentId = "{$book}-{$para}-{$word_start}-{$word_end}";
  741. $channelCount = CorpusController::sentResCount($book,$para,$word_start,$word_end);
  742. $path = json_decode(PaliText::where('book',$book)->where('paragraph',$para)->value("path"),true);
  743. $sent["path"] = [];
  744. foreach ($path as $key => $value) {
  745. # code...
  746. $value['paliTitle'] = $value['title'];
  747. $sent["path"][] = $value;
  748. }
  749. $sent["tranNum"] = $channelCount['tranNum'];
  750. $sent["nissayaNum"] = $channelCount['nissayaNum'];
  751. $sent["commNum"] = $channelCount['commNum'];
  752. $sent["originNum"] = $channelCount['originNum'];
  753. $sent["simNum"] = $channelCount['simNum'];
  754. return $sent;
  755. }
  756. /**
  757. * 获取某个句子的相关资源的句子数量
  758. */
  759. public static function sentResCount($book,$para,$start,$end){
  760. $sentId = "{$book}-{$para}-{$start}-{$end}";
  761. $channelCount = Cache::remember("/sentence/{$sentId}/channels/count",
  762. 60,
  763. function() use($book,$para,$start,$end){
  764. $channels = Sentence::where('book_id',$book)
  765. ->where('paragraph',$para)
  766. ->where('word_start',$start)
  767. ->where('word_end',$end)
  768. ->select('channel_uid')
  769. ->groupBy('channel_uid')
  770. ->get();
  771. $channelList = [];
  772. foreach ($channels as $key => $value) {
  773. # code...
  774. $channelList[] = $value->channel_uid;
  775. }
  776. $simId = PaliSentence::where('book',$book)
  777. ->where('paragraph',$para)
  778. ->where('word_begin',$start)
  779. ->where('word_end',$end)
  780. ->value('id');
  781. if($simId){
  782. $output["simNum"]=SentSimIndex::where('sent_id',$simId)->value('count');
  783. }else{
  784. $output["simNum"]=0;
  785. }
  786. $channelInfo = Channel::whereIn("uid",$channelList)->select('type')->get();
  787. $output["tranNum"]=0;
  788. $output["nissayaNum"]=0;
  789. $output["commNum"]=0;
  790. $output["originNum"]=0;
  791. foreach ($channelInfo as $key => $value) {
  792. # code...
  793. switch($value->type){
  794. case "translation":
  795. $output["tranNum"]++;
  796. break;
  797. case "nissaya":
  798. $output["nissayaNum"]++;
  799. break;
  800. case "commentary":
  801. $output["commNum"]++;
  802. break;
  803. case "original":
  804. $output["originNum"]++;
  805. break;
  806. }
  807. }
  808. return $output;
  809. });
  810. return $channelCount;
  811. }
  812. private function markdownRender($input){
  813. }
  814. /**
  815. * Update the specified resource in storage.
  816. *
  817. * @param \Illuminate\Http\Request $request
  818. * @param \App\Models\Sentence $sentence
  819. * @return \Illuminate\Http\Response
  820. */
  821. public function update(Request $request, Sentence $sentence)
  822. {
  823. //
  824. }
  825. /**
  826. * Remove the specified resource from storage.
  827. *
  828. * @param \App\Models\Sentence $sentence
  829. * @return \Illuminate\Http\Response
  830. */
  831. public function destroy(Sentence $sentence)
  832. {
  833. //
  834. }
  835. }