DictController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. require_once __DIR__."/../../../public/app/dict/grm_abbr.php";
  9. class DictController extends Controller
  10. {
  11. /**
  12. * Display a listing of the resource.
  13. *
  14. * @return \Illuminate\Http\Response
  15. */
  16. public function index(Request $request)
  17. {
  18. //
  19. $output = [];
  20. $wordDataOutput = [];
  21. $dictListOutput = [];
  22. $caseListOutput = [];
  23. $indexCol = ['word','note','dict_id'];
  24. $words = [];
  25. $word_base = [];
  26. $searched = [];
  27. $words[$request->get('word')] = [];
  28. for ($i=0; $i < 2; $i++) {
  29. # code...
  30. $word_base = [];
  31. foreach ($words as $word => $case) {
  32. # code...
  33. $searched[] = $word;
  34. $result = UserDict::select($indexCol)->where('word',$word)->where('source','_PAPER_')->get();
  35. $anchor = $word;
  36. $wordData=[
  37. 'word'=> $word,
  38. 'factors'=> "",
  39. 'parents'=> "",
  40. 'case'=> [],
  41. 'grammar'=>$case,
  42. 'anchor'=> $anchor,
  43. 'dict' => [],
  44. ];
  45. $dictList=[
  46. 'href'=> '#'.$anchor,
  47. 'title'=> "{$word}",
  48. ];
  49. foreach ($result as $key => $value) {
  50. # code...
  51. $dictInfo= DictInfo::find($value->dict_id);
  52. $anchor = "{$word}-{$dictInfo->shortname}";
  53. $wordData['dict'][] = [
  54. 'dictname'=> $dictInfo->name,
  55. 'word'=> $word,
  56. 'note'=> $this->GrmAbbr($value->note,0),
  57. 'anchor'=> $anchor,
  58. ];
  59. $dictList['children'][] = [
  60. 'href'=> '#'.$anchor,
  61. 'title'=> "{$dictInfo->shortname}",
  62. ];
  63. }
  64. $wordDataOutput[]=$wordData;
  65. $dictListOutput[]=$dictList;
  66. //TODO 加变格查询
  67. $case = new CaseMan();
  68. $parent = $case->WordToBase($word);
  69. foreach ($parent as $base => $case) {
  70. # code...
  71. if(!in_array($base,$searched)){
  72. $word_base[$base] = $case;
  73. Log::info($case);
  74. }
  75. }
  76. }
  77. if(count($word_base)===0){
  78. break;
  79. }else{
  80. $words = $word_base;
  81. }
  82. }
  83. $output['words'] = $wordDataOutput;
  84. $output['dictlist'] = $dictListOutput;
  85. $output['caselist'] = $caseListOutput;
  86. //$result = UserDict::select('word')->where('word','like',"{$word}%")->groupBy('word')->get();
  87. //$output['like'] = $result;
  88. return $this->ok($output);
  89. }
  90. /**
  91. * Store a newly created resource in storage.
  92. *
  93. * @param \Illuminate\Http\Request $request
  94. * @return \Illuminate\Http\Response
  95. */
  96. public function store(Request $request)
  97. {
  98. //
  99. }
  100. /**
  101. * Display the specified resource.
  102. *
  103. * @param \App\Models\UserDict $userDict
  104. * @return \Illuminate\Http\Response
  105. */
  106. public function show(UserDict $userDict)
  107. {
  108. //
  109. }
  110. /**
  111. * Update the specified resource in storage.
  112. *
  113. * @param \Illuminate\Http\Request $request
  114. * @param \App\Models\UserDict $userDict
  115. * @return \Illuminate\Http\Response
  116. */
  117. public function update(Request $request, UserDict $userDict)
  118. {
  119. //
  120. }
  121. /**
  122. * Remove the specified resource from storage.
  123. *
  124. * @param \App\Models\UserDict $userDict
  125. * @return \Illuminate\Http\Response
  126. */
  127. public function destroy(UserDict $userDict)
  128. {
  129. //
  130. }
  131. private function GrmAbbr($input,$dictid){
  132. $mean = $input;
  133. foreach (GRM_ABBR as $key => $value) {
  134. # code...
  135. if($dictid !== 0){
  136. if($value["dictid"]=== $dictid && strpos($input,$value["abbr"]."|") == false){
  137. $mean = str_ireplace($value["abbr"],"|@{$value["abbr"]}-grammar_{$value["replace"]}",$mean);
  138. }
  139. }else{
  140. if( strpos($mean,"|@".$value["abbr"]) == false){
  141. $mean = str_ireplace($value["abbr"],"|@{$value["abbr"]}-grammar_{$value["replace"]}|",$mean);
  142. }
  143. }
  144. }
  145. return $mean;
  146. }
  147. }