DictController.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. 'children' => [],
  71. ];
  72. foreach ($result as $key => $value) {
  73. # code...
  74. $dictInfo= DictInfo::find($value->dict_id);
  75. $dict_lang = explode('-',$dictInfo->dest_lang);
  76. $anchor = "{$word}-{$dictInfo->shortname}";
  77. $currData = [
  78. 'dictname'=> $dictInfo->name,
  79. 'shortname'=> $dictInfo->shortname,
  80. 'dict_id' => $value->dict_id,
  81. 'lang' => $dict_lang[0],
  82. 'word'=> $word,
  83. 'note'=> $this->GrmAbbr($value->note,0),
  84. 'anchor'=> $anchor,
  85. ];
  86. if(isset($wordDict[$dict_lang[0]])){
  87. if(isset($wordDict[$dict_lang[0]][$value->dict_id])){
  88. array_push($wordDict[$dict_lang[0]][$value->dict_id],$currData);
  89. }else{
  90. array_push($wordDict[$dict_lang[0]]["others"],$currData);
  91. }
  92. }else{
  93. array_push($wordDict['others']['others'],$currData);
  94. }
  95. }
  96. /**
  97. * 把树状数据变为扁平数据
  98. */
  99. foreach ($wordDict as $oneLang) {
  100. # code...
  101. foreach ($oneLang as $langId => $dictId) {
  102. # code...
  103. foreach ($dictId as $oneData) {
  104. # code...
  105. $wordData['dict'][] = $oneData;
  106. if(isset($dictList['children']) && count($dictList['children'])>0){
  107. $lastHref = end($dictList['children'])['href'];
  108. }else{
  109. $lastHref = '';
  110. }
  111. $currHref = '#'.$oneData['anchor'];
  112. if($lastHref !== $currHref){
  113. $dictList['children'][] = [
  114. 'href'=> $currHref,
  115. 'title'=> $oneData['shortname'],
  116. ];
  117. }
  118. }
  119. }
  120. }
  121. $wordDataOutput[]=$wordData;
  122. $dictListOutput[]=$dictList;
  123. //TODO 加变格查询
  124. $case = new CaseMan();
  125. $parent = $case->WordToBase($word);
  126. foreach ($parent as $base => $case) {
  127. # code...
  128. if(!in_array($base,$searched)){
  129. $word_base[$base] = $case;
  130. Log::info($case);
  131. }
  132. }
  133. }
  134. if(count($word_base)===0){
  135. break;
  136. }else{
  137. $words = $word_base;
  138. }
  139. }
  140. $output['words'] = $wordDataOutput;
  141. $output['dictlist'] = $dictListOutput;
  142. $output['caselist'] = $caseListOutput;
  143. //$result = UserDict::select('word')->where('word','like',"{$word}%")->groupBy('word')->get();
  144. //$output['like'] = $result;
  145. return $this->ok($output);
  146. }
  147. /**
  148. * Store a newly created resource in storage.
  149. *
  150. * @param \Illuminate\Http\Request $request
  151. * @return \Illuminate\Http\Response
  152. */
  153. public function store(Request $request)
  154. {
  155. //
  156. }
  157. /**
  158. * Display the specified resource.
  159. *
  160. * @param \App\Models\UserDict $userDict
  161. * @return \Illuminate\Http\Response
  162. */
  163. public function show(UserDict $userDict)
  164. {
  165. //
  166. }
  167. /**
  168. * Update the specified resource in storage.
  169. *
  170. * @param \Illuminate\Http\Request $request
  171. * @param \App\Models\UserDict $userDict
  172. * @return \Illuminate\Http\Response
  173. */
  174. public function update(Request $request, UserDict $userDict)
  175. {
  176. //
  177. }
  178. /**
  179. * Remove the specified resource from storage.
  180. *
  181. * @param \App\Models\UserDict $userDict
  182. * @return \Illuminate\Http\Response
  183. */
  184. public function destroy(UserDict $userDict)
  185. {
  186. //
  187. }
  188. private function GrmAbbr($input,$dictid){
  189. $mean = $input;
  190. foreach (GRM_ABBR as $key => $value) {
  191. # code...
  192. if($dictid !== 0){
  193. if($value["dictid"]=== $dictid && strpos($input,$value["abbr"]."|") == false){
  194. $mean = str_ireplace($value["abbr"],"|@{$value["abbr"]}-{$value["replace"]}",$mean);
  195. }
  196. }else{
  197. if( strpos($mean,"|@".$value["abbr"]) == false){
  198. $mean = str_ireplace($value["abbr"],"|@{$value["abbr"]}-{$value["replace"]}|",$mean);
  199. }
  200. }
  201. }
  202. return $mean;
  203. }
  204. }