CorpusController.php 33 KB

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