CorpusController.php 31 KB

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