CorpusController.php 34 KB

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