CorpusController.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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. default:
  411. //译文需要markdown渲染
  412. $newSent['html'] = Cache::remember("/sent/{$channelId}/{$currSentId}",10,
  413. function() use($row){
  414. return MdRender::render($row->content,$row->channel_uid);
  415. });
  416. break;
  417. }
  418. }
  419. switch ($info->type) {
  420. case 'wbw':
  421. case 'original':
  422. array_push($sent["origin"],$newSent);
  423. break;
  424. default:
  425. array_push($sent["translation"],$newSent);
  426. break;
  427. }
  428. }
  429. if($onlyProps){
  430. return $sent;
  431. }
  432. $content = $this->pushSent($content,$sent,0,$mode);
  433. }
  434. /*
  435. foreach ($record as $key => $value) {
  436. # 遍历结果生成html文件
  437. $currSentId = $value->book_id.'-'.$value->paragraph.'-'.$value->word_start.'-'.$value->word_end;
  438. if($currSentId !== $lastSent){
  439. if($sentCount > 0){
  440. //保存上一个句子
  441. //增加标题的html标记
  442. $level = 0;
  443. if(isset($indexedHeading["{$value->book_id}-{$value->paragraph}"])){
  444. $level = $indexedHeading["{$value->book_id}-{$value->paragraph}"];
  445. }
  446. $content = $this->pushSent($content,$sent,$level,$mode);
  447. }
  448. //新建句子
  449. $sent = $this->newSent($value->book_id,$value->paragraph,$value->word_start,$value->word_end);
  450. $lastSent = $currSentId;
  451. }
  452. $sentContent=$value->content;
  453. $channelType = $indexChannel[$value->channel_uid]->type;
  454. if($indexChannel[$value->channel_uid]->type==="original" && $mode !== 'read'){
  455. //非阅读模式下。原文使用逐词解析数据。优先加载第一个translation channel 如果没有。加载默认逐词解析。
  456. $channelType = 'wbw';
  457. $html = "";
  458. if(count($this->wbwChannels)>0){
  459. //获取逐词解析数据
  460. $wbwBlock = WbwBlock::where('channel_uid',$this->wbwChannels[0])
  461. ->where('book_id',$value->book_id)
  462. ->where('paragraph',$value->paragraph)
  463. ->select('uid')
  464. ->first();
  465. if($wbwBlock){
  466. //找到逐词解析数据
  467. $wbwData = Wbw::where('block_uid',$wbwBlock->uid)
  468. ->whereBetween('wid',[$value->word_start,$value->word_end])
  469. ->select(['data','uid'])
  470. ->orderBy('wid')
  471. ->get();
  472. $wbwContent = [];
  473. foreach ($wbwData as $wbwrow) {
  474. $wbw = str_replace("&nbsp;",' ',$wbwrow->data);
  475. $wbw = str_replace("<br>",' ',$wbw);
  476. $xmlString = "<root>" . $wbw . "</root>";
  477. try{
  478. $xmlWord = simplexml_load_string($xmlString);
  479. }catch(Exception $e){
  480. continue;
  481. }
  482. $wordsList = $xmlWord->xpath('//word');
  483. foreach ($wordsList as $word) {
  484. $case = \str_replace(['#','.'],['$',''],$word->case->__toString());
  485. $case = \str_replace('$$','$',$case);
  486. $case = trim($case);
  487. $case = trim($case,"$");
  488. $wbwContent[] = [
  489. 'uid'=>$wbwrow->uid,
  490. 'word'=>['value'=>$word->pali->__toString(),'status'=>0],
  491. 'real'=> ['value'=>$word->real->__toString(),'status'=>0],
  492. 'meaning'=> ['value'=>\explode('$',$word->mean->__toString()) ,'status'=>0],
  493. 'type'=> ['value'=>$word->type->__toString(),'status'=>0],
  494. 'grammar'=> ['value'=>$word->gramma->__toString(),'status'=>0],
  495. 'case'=> ['value'=>\explode('$',$case),'status'=>0],
  496. 'parent'=> ['value'=>$word->parent->__toString(),'status'=>0],
  497. 'style'=> ['value'=>$word->style->__toString(),'status'=>0],
  498. 'factors'=> ['value'=>$word->org->__toString(),'status'=>0],
  499. 'factorMeaning'=> ['value'=>$word->om->__toString(),'status'=>0],
  500. 'confidence'=> 0.5,
  501. 'hasComment'=>Discussion::where('res_id',$wbwrow->uid)->exists(),
  502. ];
  503. }
  504. }
  505. $sentContent = \json_encode($wbwContent);
  506. }
  507. }
  508. }else{
  509. if($indexChannel[$value->channel_uid]->type==="original"){
  510. //原文直接使用
  511. $html = Cache::remember("/sent/{$value->channel_uid}/{$currSentId}",10,
  512. function() use($value){
  513. return $value->content;
  514. });
  515. }else{
  516. //译文需要markdown渲染
  517. $html = Cache::remember("/sent/{$value->channel_uid}/{$currSentId}",10,
  518. function() use($value){
  519. return MdRender::render($value->content,$value->channel_uid);
  520. });
  521. }
  522. }
  523. $newSent = [
  524. "content"=>$sentContent,
  525. "html"=> $html,
  526. "book"=> $value->book_id,
  527. "para"=> $value->paragraph,
  528. "wordStart"=> $value->word_start,
  529. "wordEnd"=> $value->word_end,
  530. "editor"=> [
  531. 'id'=>$value->editor_uid,
  532. 'nickName'=>'nickname',
  533. 'realName'=>'realName',
  534. 'avatar'=>'',
  535. ],
  536. "channel"=> [
  537. "name"=>$indexChannel[$value->channel_uid]->name,
  538. "type"=>$channelType,
  539. "id"=> $value->channel_uid,
  540. ],
  541. "updateAt"=> $value->updated_at,
  542. "suggestionCount" => SuggestionApi::getCountBySent($value->book_id,$value->paragraph,$value->word_start,$value->word_end,$value->channel_uid),
  543. ];
  544. switch ($indexChannel[$value->channel_uid]->type) {
  545. case 'original';
  546. case 'wbw';
  547. array_push($sent["origin"],$newSent);
  548. break;
  549. default:
  550. array_push($sent["translation"],$newSent);
  551. break;
  552. }
  553. $sentCount++;
  554. }
  555. if($onlyProps){
  556. return $sent;
  557. }
  558. $content = $this->pushSent($content,$sent,0,$mode);
  559. */
  560. $output = \implode("",$content);
  561. return "<div>{$output}</div>";
  562. }
  563. private function getWbw($book,$para,$start,$end,$channel){
  564. /**
  565. * 非阅读模式下。原文使用逐词解析数据。
  566. * 优先加载第一个translation channel 如果没有。加载默认逐词解析。
  567. */
  568. //获取逐词解析数据
  569. $wbwBlock = WbwBlock::where('channel_uid',$channel)
  570. ->where('book_id',$book)
  571. ->where('paragraph',$para)
  572. ->select('uid')
  573. ->first();
  574. if(!$wbwBlock){
  575. return false;
  576. }
  577. //找到逐词解析数据
  578. $wbwData = Wbw::where('block_uid',$wbwBlock->uid)
  579. ->whereBetween('wid',[$start,$end])
  580. ->select(['data','uid'])
  581. ->orderBy('wid')
  582. ->get();
  583. $wbwContent = [];
  584. foreach ($wbwData as $wbwrow) {
  585. $wbw = str_replace("&nbsp;",' ',$wbwrow->data);
  586. $wbw = str_replace("<br>",' ',$wbw);
  587. $xmlString = "<root>" . $wbw . "</root>";
  588. try{
  589. $xmlWord = simplexml_load_string($xmlString);
  590. }catch(Exception $e){
  591. continue;
  592. }
  593. $wordsList = $xmlWord->xpath('//word');
  594. foreach ($wordsList as $word) {
  595. $case = \str_replace(['#','.'],['$',''],$word->case->__toString());
  596. $case = \str_replace('$$','$',$case);
  597. $case = trim($case);
  598. $case = trim($case,"$");
  599. $wbwContent[] = [
  600. 'uid'=>$wbwrow->uid,
  601. 'word'=>['value'=>$word->pali->__toString(),'status'=>0],
  602. 'real'=> ['value'=>$word->real->__toString(),'status'=>0],
  603. 'meaning'=> ['value'=>\explode('$',$word->mean->__toString()) ,'status'=>0],
  604. 'type'=> ['value'=>$word->type->__toString(),'status'=>0],
  605. 'grammar'=> ['value'=>$word->gramma->__toString(),'status'=>0],
  606. 'case'=> ['value'=>\explode('$',$case),'status'=>0],
  607. 'parent'=> ['value'=>$word->parent->__toString(),'status'=>0],
  608. 'style'=> ['value'=>$word->style->__toString(),'status'=>0],
  609. 'factors'=> ['value'=>$word->org->__toString(),'status'=>0],
  610. 'factorMeaning'=> ['value'=>$word->om->__toString(),'status'=>0],
  611. 'confidence'=> 0.5,
  612. 'hasComment'=>Discussion::where('res_id',$wbwrow->uid)->exists(),
  613. ];
  614. }
  615. }
  616. return \json_encode($wbwContent,JSON_UNESCAPED_UNICODE);
  617. }
  618. /**
  619. * 将句子放进结果列表
  620. */
  621. private function pushSent($result,$sent,$level=0,$mode='read'){
  622. $sentProps = base64_encode(\json_encode($sent)) ;
  623. if($mode === 'read'){
  624. $sentWidget = "<MdTpl tpl='sentread' props='{$sentProps}' />";
  625. }else{
  626. $sentWidget = "<MdTpl tpl='sentedit' props='{$sentProps}' />";
  627. }
  628. //增加标题的html标记
  629. if($level>0){
  630. $sentWidget = "<h{$level}>".$sentWidget."</h{$level}>";
  631. }
  632. array_push($result,$sentWidget);
  633. return $result;
  634. }
  635. private function newSent($book,$para,$word_start,$word_end){
  636. $sent = [
  637. "id"=>"{$book}-{$para}-{$word_start}-{$word_end}",
  638. "origin"=>[],
  639. "translation"=>[],
  640. ];
  641. #生成channel 数量列表
  642. $sentId = "{$book}-{$para}-{$word_start}-{$word_end}";
  643. $channelCount = CorpusController::sentResCount($book,$para,$word_start,$word_end);
  644. /*
  645. $channelCount = Cache::remember("/sent1/{$sentId}/channels/count",
  646. 60,
  647. function() use($book,$para,$word_start,$word_end){
  648. $channels = Sentence::where('book_id',$book)
  649. ->where('paragraph',$para)
  650. ->where('word_start',$word_start)
  651. ->where('word_end',$word_end)
  652. ->select('channel_uid')
  653. ->groupBy('channel_uid')
  654. ->get();
  655. $channelList = [];
  656. foreach ($channels as $key => $value) {
  657. # code...
  658. $channelList[] = $value->channel_uid;
  659. }
  660. $channelInfo = Channel::whereIn("uid",$channelList)->select('type')->get();
  661. $output["tranNum"]=0;
  662. $output["nissayaNum"]=0;
  663. $output["commNum"]=0;
  664. $output["originNum"]=0;
  665. foreach ($channelInfo as $key => $value) {
  666. # code...
  667. switch($value->type){
  668. case "translation":
  669. $output["tranNum"]++;
  670. break;
  671. case "nissaya":
  672. $output["nissayaNum"]++;
  673. break;
  674. case "commentary":
  675. $output["commNum"]++;
  676. break;
  677. case "original":
  678. $output["originNum"]++;
  679. break;
  680. }
  681. }
  682. return $output;
  683. });
  684. */
  685. $sent["tranNum"] = $channelCount['tranNum'];
  686. $sent["nissayaNum"] = $channelCount['nissayaNum'];
  687. $sent["commNum"] = $channelCount['commNum'];
  688. $sent["originNum"] = $channelCount['originNum'];
  689. $sent["simNum"] = $channelCount['simNum'];
  690. return $sent;
  691. }
  692. /**
  693. * 获取某个句子的相关资源的句子数量
  694. */
  695. public static function sentResCount($book,$para,$start,$end){
  696. $sentId = "{$book}-{$para}-{$start}-{$end}";
  697. $channelCount = Cache::remember("/sentence/{$sentId}/channels/count",
  698. 60,
  699. function() use($book,$para,$start,$end){
  700. $channels = Sentence::where('book_id',$book)
  701. ->where('paragraph',$para)
  702. ->where('word_start',$start)
  703. ->where('word_end',$end)
  704. ->select('channel_uid')
  705. ->groupBy('channel_uid')
  706. ->get();
  707. $channelList = [];
  708. foreach ($channels as $key => $value) {
  709. # code...
  710. $channelList[] = $value->channel_uid;
  711. }
  712. $simId = PaliSentence::where('book',$book)
  713. ->where('paragraph',$para)
  714. ->where('word_begin',$start)
  715. ->where('word_end',$end)
  716. ->value('id');
  717. if($simId){
  718. $output["simNum"]=SentSimIndex::where('sent_id',$simId)->value('count');
  719. }else{
  720. $output["simNum"]=0;
  721. }
  722. $channelInfo = Channel::whereIn("uid",$channelList)->select('type')->get();
  723. $output["tranNum"]=0;
  724. $output["nissayaNum"]=0;
  725. $output["commNum"]=0;
  726. $output["originNum"]=0;
  727. foreach ($channelInfo as $key => $value) {
  728. # code...
  729. switch($value->type){
  730. case "translation":
  731. $output["tranNum"]++;
  732. break;
  733. case "nissaya":
  734. $output["nissayaNum"]++;
  735. break;
  736. case "commentary":
  737. $output["commNum"]++;
  738. break;
  739. case "original":
  740. $output["originNum"]++;
  741. break;
  742. }
  743. }
  744. return $output;
  745. });
  746. return $channelCount;
  747. }
  748. private function markdownRender($input){
  749. }
  750. /**
  751. * Update the specified resource in storage.
  752. *
  753. * @param \Illuminate\Http\Request $request
  754. * @param \App\Models\Sentence $sentence
  755. * @return \Illuminate\Http\Response
  756. */
  757. public function update(Request $request, Sentence $sentence)
  758. {
  759. //
  760. }
  761. /**
  762. * Remove the specified resource from storage.
  763. *
  764. * @param \App\Models\Sentence $sentence
  765. * @return \Illuminate\Http\Response
  766. */
  767. public function destroy(Sentence $sentence)
  768. {
  769. //
  770. }
  771. }