DictController.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\UserDict;
  4. use App\Models\DictInfo;
  5. use Illuminate\Http\Request;
  6. use App\Tools\CaseMan;
  7. use Illuminate\Support\Facades\Log;
  8. use App\Http\Api\DictApi;
  9. require_once __DIR__."/../../../public/app/dict/grm_abbr.php";
  10. class DictController extends Controller
  11. {
  12. /**
  13. * Display a listing of the resource.
  14. *
  15. * @return \Illuminate\Http\Response
  16. */
  17. public function index(Request $request)
  18. {
  19. //
  20. $output = [];
  21. $wordDataOutput = [];
  22. $dictListOutput = [];
  23. $caseListOutput = [];
  24. $indexCol = ['word','note','dict_id'];
  25. $words = [];
  26. $word_base = [];
  27. $searched = [];
  28. $words[$request->get('word')] = [];
  29. $userLang = $request->get('lang',"zh");
  30. for ($i=0; $i < 2; $i++) {
  31. # code...
  32. $word_base = [];
  33. foreach ($words as $word => $case) {
  34. # code...
  35. $searched[] = $word;
  36. $result = UserDict::select($indexCol)->where('word',$word)->where('source','_PAPER_')->get();
  37. $anchor = $word;
  38. $wordData=[
  39. 'word'=> $word,
  40. 'factors'=> "",
  41. 'parents'=> "",
  42. 'case'=> [],
  43. 'grammar'=>$case,
  44. 'anchor'=> $anchor,
  45. 'dict' => [],
  46. ];
  47. /**
  48. * 按照语言调整词典顺序
  49. * 算法:准备理想的词典顺序容器。
  50. * 将查询的结果放置在对应的容器中。
  51. * 最后将结果扁平化
  52. * 准备字典容器
  53. * $wordDict = [
  54. * "zh"=>[
  55. * "0d79e8e8-1430-4c99-a0f1-b74f2b4b26d8"=>[];
  56. * ]
  57. * ]
  58. */
  59. foreach (DictApi::langOrder($userLang) as $langId) {
  60. # code...
  61. $dictContainer = [];
  62. foreach (DictApi::dictOrder($langId) as $dictId) {
  63. $dictContainer[$dictId] = [];
  64. }
  65. $wordDict[$langId] = $dictContainer;
  66. }
  67. $dictList=[
  68. 'href'=> '#'.$anchor,
  69. 'title'=> "{$word}",
  70. ];
  71. foreach ($result as $key => $value) {
  72. # code...
  73. $dictInfo= DictInfo::find($value->dict_id);
  74. $dict_lang = explode('-',$dictInfo->dest_lang);
  75. $anchor = "{$word}-{$dictInfo->shortname}";
  76. $currData = [
  77. 'dictname'=> $dictInfo->name,
  78. 'shortname'=> $dictInfo->shortname,
  79. 'dict_id' => $value->dict_id,
  80. 'lang' => $dict_lang[0],
  81. 'word'=> $word,
  82. 'note'=> $this->GrmAbbr($value->note,0),
  83. 'anchor'=> $anchor,
  84. ];
  85. if(isset($wordDict[$dict_lang[0]])){
  86. if(isset($wordDict[$dict_lang[0]][$value->dict_id])){
  87. array_push($wordDict[$dict_lang[0]][$value->dict_id],$currData);
  88. }else{
  89. array_push($wordDict[$dict_lang[0]]["others"],$currData);
  90. }
  91. }else{
  92. array_push($wordDict['others']['others'],$currData);
  93. }
  94. $dictList['children'][] = [
  95. 'href'=> '#'.$anchor,
  96. 'title'=> "{$dictInfo->shortname}",
  97. ];
  98. }
  99. /**
  100. * 把树状数据变为扁平数据
  101. */
  102. foreach ($wordDict as $oneLang) {
  103. # code...
  104. foreach ($oneLang as $langId => $dictId) {
  105. # code...
  106. foreach ($dictId as $oneData) {
  107. # code...
  108. $wordData['dict'][] = $oneData;
  109. }
  110. }
  111. }
  112. $wordDataOutput[]=$wordData;
  113. $dictListOutput[]=$dictList;
  114. //TODO 加变格查询
  115. $case = new CaseMan();
  116. $parent = $case->WordToBase($word);
  117. foreach ($parent as $base => $case) {
  118. # code...
  119. if(!in_array($base,$searched)){
  120. $word_base[$base] = $case;
  121. Log::info($case);
  122. }
  123. }
  124. }
  125. if(count($word_base)===0){
  126. break;
  127. }else{
  128. $words = $word_base;
  129. }
  130. }
  131. $output['words'] = $wordDataOutput;
  132. $output['dictlist'] = $dictListOutput;
  133. $output['caselist'] = $caseListOutput;
  134. //$result = UserDict::select('word')->where('word','like',"{$word}%")->groupBy('word')->get();
  135. //$output['like'] = $result;
  136. return $this->ok($output);
  137. }
  138. /**
  139. * Store a newly created resource in storage.
  140. *
  141. * @param \Illuminate\Http\Request $request
  142. * @return \Illuminate\Http\Response
  143. */
  144. public function store(Request $request)
  145. {
  146. //
  147. }
  148. /**
  149. * Display the specified resource.
  150. *
  151. * @param \App\Models\UserDict $userDict
  152. * @return \Illuminate\Http\Response
  153. */
  154. public function show(UserDict $userDict)
  155. {
  156. //
  157. }
  158. /**
  159. * Update the specified resource in storage.
  160. *
  161. * @param \Illuminate\Http\Request $request
  162. * @param \App\Models\UserDict $userDict
  163. * @return \Illuminate\Http\Response
  164. */
  165. public function update(Request $request, UserDict $userDict)
  166. {
  167. //
  168. }
  169. /**
  170. * Remove the specified resource from storage.
  171. *
  172. * @param \App\Models\UserDict $userDict
  173. * @return \Illuminate\Http\Response
  174. */
  175. public function destroy(UserDict $userDict)
  176. {
  177. //
  178. }
  179. private function GrmAbbr($input,$dictid){
  180. $mean = $input;
  181. foreach (GRM_ABBR as $key => $value) {
  182. # code...
  183. if($dictid !== 0){
  184. if($value["dictid"]=== $dictid && strpos($input,$value["abbr"]."|") == false){
  185. $mean = str_ireplace($value["abbr"],"|@{$value["abbr"]}-grammar_{$value["replace"]}",$mean);
  186. }
  187. }else{
  188. if( strpos($mean,"|@".$value["abbr"]) == false){
  189. $mean = str_ireplace($value["abbr"],"|@{$value["abbr"]}-grammar_{$value["replace"]}|",$mean);
  190. }
  191. }
  192. }
  193. return $mean;
  194. }
  195. }