CorpusController.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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\Support\Str;
  13. use Illuminate\Http\Request;
  14. use Illuminate\Support\Facades\Cache;
  15. use App\Http\Api\MdRender;
  16. use App\Http\Api\SuggestionApi;
  17. use App\Http\Api\ChannelApi;
  18. use App\Http\Api\UserApi;
  19. use App\Http\Api\StudioApi;
  20. use Illuminate\Support\Facades\Log;
  21. use Illuminate\Support\Arr;
  22. use App\Http\Resources\TocResource;
  23. class CorpusController extends Controller
  24. {
  25. protected $result = [
  26. "uid"=> '',
  27. "title"=> '',
  28. "path"=>[],
  29. "sub_title"=> '',
  30. "summary"=> '',
  31. "content"=> '',
  32. "content_type"=> "html",
  33. "toc" => [],
  34. "status"=>30,
  35. "lang"=> "",
  36. "created_at"=> "",
  37. "updated_at"=> "",
  38. ];
  39. protected $wbwChannels = [];
  40. //句子需要查询的列
  41. protected $selectCol = [
  42. 'uid',
  43. 'book_id',
  44. 'paragraph',
  45. 'word_start',
  46. "word_end",
  47. 'channel_uid',
  48. 'content',
  49. 'editor_uid',
  50. 'acceptor_uid',
  51. 'pr_edit_at',
  52. 'updated_at'
  53. ];
  54. public function __construct()
  55. {
  56. }
  57. /**
  58. * Display a listing of the resource.
  59. *
  60. * @return \Illuminate\Http\Response
  61. */
  62. public function index(Request $request)
  63. {
  64. //
  65. switch ($request->get('view')) {
  66. case 'para':
  67. return $this->showPara($request);
  68. break;
  69. default:
  70. # code...
  71. break;
  72. }
  73. }
  74. /**
  75. * Store a newly created resource in storage.
  76. *
  77. * @param \Illuminate\Http\Request $request
  78. * @return \Illuminate\Http\Response
  79. */
  80. public function store(Request $request)
  81. {
  82. //
  83. }
  84. /**
  85. * Display the specified resource.
  86. *
  87. * @param \App\Models\Sentence $sentence
  88. * @return \Illuminate\Http\Response
  89. */
  90. public function show(Sentence $sentence)
  91. {
  92. //
  93. }
  94. public function getSentTpl($id,$channels,$mode='edit',$onlyProps=false){
  95. $sent = [];
  96. $sentId = \explode('-',$id);
  97. if($mode==='read'){
  98. $channelId = ChannelApi::getSysChannel('_System_Pali_VRI_');
  99. }else{
  100. $channelId = ChannelApi::getSysChannel('_System_Wbw_VRI_');
  101. }
  102. if($channelId !== false){
  103. array_push($channels,$channelId);
  104. }
  105. $record = Sentence::select($this->selectCol)
  106. ->where('book_id',$sentId[0])
  107. ->where('paragraph',$sentId[1])
  108. ->where('word_start',(int)$sentId[2])
  109. ->where('word_end',(int)$sentId[3])
  110. ->whereIn('channel_uid',$channels)
  111. ->get();
  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. * @return \Illuminate\Http\Response
  273. */
  274. public function showChapter(Request $request, string $id)
  275. {
  276. //
  277. $sentId = \explode('-',$id);
  278. $channels = [];
  279. if($request->has('channels')){
  280. $channels = explode('_',$request->get('channels'));
  281. }
  282. $mode = $request->get('mode','read');
  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>5000){
  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. Log::info("show chapter");
  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. foreach ($indexChannel as $channelId => $info) {
  442. # code...
  443. $sid = "{$currSentId}_{$channelId}";
  444. $newSent = [
  445. "content"=>"",
  446. "html"=> "",
  447. "book"=> $arrSentId[0],
  448. "para"=> $arrSentId[1],
  449. "wordStart"=> $arrSentId[2],
  450. "wordEnd"=> $arrSentId[3],
  451. "channel"=> [
  452. "name"=>$info->name,
  453. "type"=>$info->type,
  454. "id"=> $info->uid,
  455. ],
  456. "studio" => $info['studio'],
  457. "updateAt"=> "",
  458. "suggestionCount" => SuggestionApi::getCountBySent($arrSentId[0],$arrSentId[1],$arrSentId[2],$arrSentId[3],$channelId),
  459. ];
  460. $row = Arr::first($record,function($value,$key) use($sid){
  461. return $value['sid']===$sid;
  462. });
  463. if($row){
  464. $newSent['id'] = $row->uid;
  465. $newSent['content'] = $row->content;
  466. $newSent['html'] = "";
  467. $newSent["editor"]=UserApi::getById($row->editor_uid);
  468. $newSent['updateAt'] = $row->updated_at;
  469. if($mode !== "read"){
  470. if(isset($row->acceptor_uid) && !empty($row->acceptor_uid)){
  471. $newSent["acceptor"]=UserApi::getById($row->acceptor_uid);
  472. $newSent["prEditAt"]=$row->pr_edit_at;
  473. }
  474. }
  475. switch ($info->type) {
  476. case 'wbw':
  477. case 'original':
  478. //
  479. // 在编辑模式下。
  480. // 如果是原文,查看是否有逐词解析数据,
  481. // 有的话优先显示。
  482. // 阅读模式直接显示html原文
  483. // 传过来的数据一定有一个原文channel
  484. //
  485. if($mode !== "read"){
  486. $newSent['channel']['type'] = "wbw";
  487. if(isset($this->wbwChannels[0])){
  488. $newSent['channel']['name'] = $indexChannel[$this->wbwChannels[0]]->name;
  489. $newSent['channel']['id'] = $this->wbwChannels[0];
  490. //存在一个translation channel
  491. //尝试查找逐词解析数据。找到,替换现有数据
  492. $wbwData = $this->getWbw($arrSentId[0],$arrSentId[1],$arrSentId[2],$arrSentId[3],$this->wbwChannels[0]);
  493. if($wbwData){
  494. $newSent['content'] = $wbwData;
  495. $newSent['html'] = "";
  496. }
  497. }
  498. }else{
  499. $newSent['content'] = "";
  500. $newSent['html'] = $row->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. $output = \implode("",$content);
  534. return "<div>{$output}</div>";
  535. }
  536. public function getWbw($book,$para,$start,$end,$channel){
  537. /**
  538. * 非阅读模式下。原文使用逐词解析数据。
  539. * 优先加载第一个translation channel 如果没有。加载默认逐词解析。
  540. */
  541. //获取逐词解析数据
  542. $wbwBlock = WbwBlock::where('channel_uid',$channel)
  543. ->where('book_id',$book)
  544. ->where('paragraph',$para)
  545. ->select('uid')
  546. ->first();
  547. if(!$wbwBlock){
  548. return false;
  549. }
  550. //找到逐词解析数据
  551. $wbwData = Wbw::where('block_uid',$wbwBlock->uid)
  552. ->whereBetween('wid',[$start,$end])
  553. ->select(['book_id','paragraph','wid','data','uid'])
  554. ->orderBy('wid')
  555. ->get();
  556. $wbwContent = [];
  557. foreach ($wbwData as $wbwrow) {
  558. $wbw = str_replace("&nbsp;",' ',$wbwrow->data);
  559. $wbw = str_replace("<br>",' ',$wbw);
  560. $xmlString = "<root>" . $wbw . "</root>";
  561. try{
  562. $xmlWord = simplexml_load_string($xmlString);
  563. }catch(Exception $e){
  564. continue;
  565. }
  566. $wordsList = $xmlWord->xpath('//word');
  567. foreach ($wordsList as $word) {
  568. $case = \str_replace(['#','.'],['$',''],$word->case->__toString());
  569. $case = \str_replace('$$','$',$case);
  570. $case = trim($case);
  571. $case = trim($case,"$");
  572. $wbwId = explode('-',$word->id->__toString());
  573. $wbwData = [
  574. 'uid'=>$wbwrow->uid,
  575. 'book'=>$wbwrow->book_id,
  576. 'para'=>$wbwrow->paragraph,
  577. 'sn'=> array_slice($wbwId,2),
  578. 'word'=>['value'=>$word->pali->__toString(),'status'=>0],
  579. 'real'=> ['value'=>$word->real->__toString(),'status'=>0],
  580. 'meaning'=> ['value'=>$word->mean->__toString() ,'status'=>0],
  581. 'type'=> ['value'=>$word->type->__toString(),'status'=>0],
  582. 'grammar'=> ['value'=>$word->gramma->__toString(),'status'=>0],
  583. 'case'=> ['value'=>$word->case->__toString(),'status'=>0],
  584. 'parent'=> ['value'=>$word->parent->__toString(),'status'=>0],
  585. 'style'=> ['value'=>$word->style->__toString(),'status'=>0],
  586. 'factors'=> ['value'=>$word->org->__toString(),'status'=>0],
  587. 'factorMeaning'=> ['value'=>$word->om->__toString(),'status'=>0],
  588. 'confidence'=> $word->cf->__toString(),
  589. 'hasComment'=>Discussion::where('res_id',$wbwrow->uid)->exists(),
  590. ];
  591. if(isset($word->parent2)){
  592. $wbwData['parent2']['value'] = $word->parent2->__toString();
  593. if(isset($word->parent2['status'])){
  594. $wbwData['parent2']['status'] = (int)$word->parent2['status'];
  595. }else{
  596. $wbwData['parent2']['status'] = 0;
  597. }
  598. }
  599. if(isset($word->pg)){
  600. $wbwData['grammar2']['value'] = $word->pg->__toString();
  601. if(isset($word->pg['status'])){
  602. $wbwData['grammar2']['status'] = (int)$word->pg['status'];
  603. }else{
  604. $wbwData['grammar2']['status'] = 0;
  605. }
  606. }
  607. if(isset($word->rela)){
  608. $wbwData['relation']['value'] = $word->rela->__toString();
  609. if(isset($word->rela['status'])){
  610. $wbwData['relation']['status'] = (int)$word->rela['status'];
  611. }else{
  612. $wbwData['relation']['status'] = 7;
  613. }
  614. }
  615. if(isset($word->bmt)){
  616. $wbwData['bookMarkText']['value'] = $word->bmt->__toString();
  617. if(isset($word->bmt['status'])){
  618. $wbwData['bookMarkText']['status'] = (int)$word->bmt['status'];
  619. }else{
  620. $wbwData['bookMarkText']['status'] = 7;
  621. }
  622. }
  623. if(isset($word->bmc)){
  624. $wbwData['bookMarkColor']['value'] = $word->bmc->__toString();
  625. if(isset($word->bmc['status'])){
  626. $wbwData['bookMarkColor']['status'] = (int)$word->bmc['status'];
  627. }else{
  628. $wbwData['bookMarkColor']['status'] = 7;
  629. }
  630. }
  631. if(isset($word->note)){
  632. $wbwData['note']['value'] = $word->note->__toString();
  633. if(isset($word->note['status'])){
  634. $wbwData['note']['status'] = (int)$word->note['status'];
  635. }else{
  636. $wbwData['note']['status'] = 7;
  637. }
  638. }
  639. if(isset($word->pali['status'])){
  640. $wbwData['word']['status'] = (int)$word->pali['status'];
  641. }
  642. if(isset($word->real['status'])){
  643. $wbwData['real']['status'] = (int)$word->real['status'];
  644. }
  645. if(isset($word->mean['status'])){
  646. $wbwData['meaning']['status'] = (int)$word->mean['status'];
  647. }
  648. if(isset($word->type['status'])){
  649. $wbwData['type']['status'] = (int)$word->type['status'];
  650. }
  651. if(isset($word->gramma['status'])){
  652. $wbwData['grammar']['status'] = (int)$word->gramma['status'];
  653. }
  654. if(isset($word->case['status'])){
  655. $wbwData['case']['status'] = (int)$word->case['status'];
  656. }
  657. if(isset($word->parent['status'])){
  658. $wbwData['parent']['status'] = (int)$word->parent['status'];
  659. }
  660. if(isset($word->org['status'])){
  661. $wbwData['factors']['status'] = (int)$word->org['status'];
  662. }
  663. if(isset($word->om['status'])){
  664. $wbwData['factorMeaning']['status'] = (int)$word->om['status'];
  665. }
  666. if(isset($word->bmt['status'])){
  667. $wbwData['bmt']['status'] = (int)$word->bmt['status'];
  668. }
  669. $wbwContent[] = $wbwData;
  670. }
  671. }
  672. if(count($wbwContent)===0){
  673. return false;
  674. }
  675. return \json_encode($wbwContent,JSON_UNESCAPED_UNICODE);
  676. }
  677. /**
  678. * 将句子放进结果列表
  679. */
  680. private function pushSent($result,$sent,$level=0,$mode='read'){
  681. $sentProps = base64_encode(\json_encode($sent)) ;
  682. if($mode === 'read'){
  683. $sentWidget = "<MdTpl tpl='sentread' props='{$sentProps}' />";
  684. }else{
  685. $sentWidget = "<MdTpl tpl='sentedit' props='{$sentProps}' />";
  686. }
  687. //增加标题的html标记
  688. if($level>0){
  689. $sentWidget = "<h{$level}>".$sentWidget."</h{$level}>";
  690. }
  691. array_push($result,$sentWidget);
  692. return $result;
  693. }
  694. private function newSent($book,$para,$word_start,$word_end){
  695. $sent = [
  696. "id"=>"{$book}-{$para}-{$word_start}-{$word_end}",
  697. "book"=>$book,
  698. "para"=>$para,
  699. "wordStart"=>$word_start,
  700. "wordEnd"=>$word_end,
  701. "origin"=>[],
  702. "translation"=>[],
  703. ];
  704. #生成channel 数量列表
  705. $sentId = "{$book}-{$para}-{$word_start}-{$word_end}";
  706. $channelCount = CorpusController::sentResCount($book,$para,$word_start,$word_end);
  707. $path = json_decode(PaliText::where('book',$book)->where('paragraph',$para)->value("path"),true);
  708. $sent["path"] = [];
  709. foreach ($path as $key => $value) {
  710. # code...
  711. $value['paliTitle'] = $value['title'];
  712. $sent["path"][] = $value;
  713. }
  714. $sent["tranNum"] = $channelCount['tranNum'];
  715. $sent["nissayaNum"] = $channelCount['nissayaNum'];
  716. $sent["commNum"] = $channelCount['commNum'];
  717. $sent["originNum"] = $channelCount['originNum'];
  718. $sent["simNum"] = $channelCount['simNum'];
  719. return $sent;
  720. }
  721. /**
  722. * 获取某个句子的相关资源的句子数量
  723. */
  724. public static function sentResCount($book,$para,$start,$end){
  725. $sentId = "{$book}-{$para}-{$start}-{$end}";
  726. $channelCount = Cache::remember("/sentence/{$sentId}/channels/count",
  727. 60,
  728. function() use($book,$para,$start,$end){
  729. $channels = Sentence::where('book_id',$book)
  730. ->where('paragraph',$para)
  731. ->where('word_start',$start)
  732. ->where('word_end',$end)
  733. ->select('channel_uid')
  734. ->groupBy('channel_uid')
  735. ->get();
  736. $channelList = [];
  737. foreach ($channels as $key => $value) {
  738. # code...
  739. if(Str::isUuid($value->channel_uid)){
  740. $channelList[] = $value->channel_uid;
  741. }
  742. }
  743. $simId = PaliSentence::where('book',$book)
  744. ->where('paragraph',$para)
  745. ->where('word_begin',$start)
  746. ->where('word_end',$end)
  747. ->value('id');
  748. if($simId){
  749. $output["simNum"]=SentSimIndex::where('sent_id',$simId)->value('count');
  750. }else{
  751. $output["simNum"]=0;
  752. }
  753. $channelInfo = Channel::whereIn("uid",$channelList)->select('type')->get();
  754. $output["tranNum"]=0;
  755. $output["nissayaNum"]=0;
  756. $output["commNum"]=0;
  757. $output["originNum"]=0;
  758. foreach ($channelInfo as $key => $value) {
  759. # code...
  760. switch($value->type){
  761. case "translation":
  762. $output["tranNum"]++;
  763. break;
  764. case "nissaya":
  765. $output["nissayaNum"]++;
  766. break;
  767. case "commentary":
  768. $output["commNum"]++;
  769. break;
  770. case "original":
  771. $output["originNum"]++;
  772. break;
  773. }
  774. }
  775. return $output;
  776. });
  777. return $channelCount;
  778. }
  779. private function markdownRender($input){
  780. }
  781. /**
  782. * Update the specified resource in storage.
  783. *
  784. * @param \Illuminate\Http\Request $request
  785. * @param \App\Models\Sentence $sentence
  786. * @return \Illuminate\Http\Response
  787. */
  788. public function update(Request $request, Sentence $sentence)
  789. {
  790. //
  791. }
  792. /**
  793. * Remove the specified resource from storage.
  794. *
  795. * @param \App\Models\Sentence $sentence
  796. * @return \Illuminate\Http\Response
  797. */
  798. public function destroy(Sentence $sentence)
  799. {
  800. //
  801. }
  802. }