CorpusController.php 33 KB

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