2
0

CorpusController.php 31 KB

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