CorpusController.php 28 KB

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