CorpusController.php 27 KB

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