WbwLookupController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\UserDict;
  4. use App\Models\DictInfo;
  5. use App\Models\WbwTemplate;
  6. use Illuminate\Http\Request;
  7. use App\Tools\CaseMan;
  8. use Illuminate\Support\Facades\Log;
  9. use Illuminate\Support\Facades\Cache;
  10. class WbwLookupController extends Controller
  11. {
  12. private $dictList = [
  13. '85dcc61c-c9e1-4ae0-9b44-cd6d9d9f0d01',//社区汇总
  14. '4d3a0d92-0adc-4052-80f5-512a2603d0e8',// system irregular
  15. '57afac99-0887-455c-b18e-67c8682158b0',// system regular
  16. 'ef620a93-a55d-4756-89c5-e188ab009e45',//社区字典
  17. '8359757e-9575-455b-a772-cc6f036caea0',// system sandhi
  18. 'c42980f0-5967-4833-b695-84183344f68f',// robot compound
  19. '61f23efb-b526-4a8e-999e-076965034e60',// pali myanmar grammar
  20. 'eae9fd6f-7bac-4940-b80d-ad6cd6f433bf',// Concise P-E Dict
  21. '2f93d0fe-3d68-46ee-a80b-11fa445a29c6',// unity
  22. 'beb45062-7c20-4047-bcd4-1f636ba443d1',// U Hau Sein
  23. '8833de18-0978-434c-b281-a2e7387f69be',// 巴汉增订
  24. '3acf0c0f-59a7-4d25-a3d9-bf394a266ebd',// 汉译パーリ语辞典-黃秉榮
  25. ];
  26. /**
  27. * Display a listing of the resource.
  28. * @param \Illuminate\Http\Request $request
  29. *
  30. * @return \Illuminate\Http\Response
  31. */
  32. public function index(Request $request)
  33. {
  34. //
  35. $startAt = microtime(true);
  36. /**
  37. * 先查询字典名称
  38. */
  39. $dict_info = DictInfo::whereIn('id',$this->dictList)->select('id','shortname')->get();
  40. $dict_name = [];
  41. foreach ($dict_info as $key => $value) {
  42. # code...
  43. $dict_name[$value->id] = $value->shortname;
  44. }
  45. $caseman = new CaseMan();
  46. $output = array();
  47. $wordPool = array();
  48. $input = \explode(',',$request->get("word"));
  49. foreach ($input as $word) {
  50. $wordPool[$word] = ['base' => false,'done' => false,'apply' => false];
  51. }
  52. if(empty($request->get("deep"))){
  53. $deep = 2;
  54. }else{
  55. $deep = $request->get("deep");
  56. }
  57. for ($i=0; $i < $deep; $i++) {
  58. # 查询深度
  59. foreach ($wordPool as $word => $info) {
  60. # code...
  61. if($info['done'] === false){
  62. $wordPool[$word]['done'] = true;
  63. $count = 0;
  64. foreach ($this->dictList as $dictId) {
  65. # code...
  66. $result = Cache::remember("dict/{$dictId}/".$word,10,function() use($word,$dictId,$dict_name){
  67. $data = UserDict::where('word',$word)->where('dict_id',$dictId)->orderBy('confidence','desc')->get();
  68. foreach ($data as $key => $value) {
  69. # code...
  70. $value->dict_shortname = $dict_name[$dictId];
  71. }
  72. return $data;
  73. });
  74. $count += count($result);
  75. if(count($result)>0){
  76. foreach ($result as $dictword) {
  77. # code...
  78. array_push($output,$dictword);
  79. if(!isset($wordPool[$word]['factors']) && !empty($dictword->factors)){
  80. //将第一个拆分作为最佳拆分存储
  81. $wordPool[$word]['factors'] = $dictword->factors;
  82. }
  83. }
  84. }
  85. }
  86. if($count == 0){
  87. //没查到 去尾查
  88. $newBase = array();
  89. $parents = $caseman->WordToBase($word);
  90. foreach ($parents as $base => $rows) {
  91. array_push($output,$rows);
  92. }
  93. }
  94. }
  95. }
  96. //查询结果中的拆分信息
  97. $newWordPart = array();
  98. foreach ($wordPool as $word => $info) {
  99. if(!empty($info['factors'])){
  100. $factors = \explode('+',$info['factors']);
  101. foreach ($factors as $factor) {
  102. # 将没有的拆分放入单词查询列表
  103. if(!isset($wordPool[$factor])){
  104. $newWordPart[$factor] = 0;
  105. }
  106. }
  107. }
  108. }
  109. foreach ($newWordPart as $part => $value) {
  110. # 将拆分放入池中
  111. $wordPool[$part] = ['base' => false,'done' => false,'apply' => false];
  112. }
  113. }
  114. return $this->ok(["rows"=>$output,'count'=>count($output)]);
  115. }
  116. /**
  117. * Store a newly created resource in storage.
  118. *
  119. * @param \Illuminate\Http\Request $request
  120. * @return \Illuminate\Http\Response
  121. */
  122. public function store(Request $request)
  123. {
  124. //
  125. }
  126. /**
  127. * Display the words best match in specified sentence .
  128. *
  129. * @param string $sentId
  130. * @return \Illuminate\Http\Response
  131. */
  132. public function show(string $sentId)
  133. {
  134. //查询句子中的单词
  135. $sent = \explode('-',$sentId);
  136. WbwTemplate::where('book',$sent[0])
  137. ->where('paragraph',$sent[1])
  138. ->whereBetween('wid',[$sent[2],$sent[3]])
  139. ->orderBy('wid')
  140. ->get();
  141. }
  142. /**
  143. * Update the specified resource in storage.
  144. *
  145. * @param \Illuminate\Http\Request $request
  146. * @param \App\Models\UserDict $userDict
  147. * @return \Illuminate\Http\Response
  148. */
  149. public function update(Request $request, UserDict $userDict)
  150. {
  151. //
  152. }
  153. /**
  154. * Remove the specified resource from storage.
  155. *
  156. * @param \App\Models\UserDict $userDict
  157. * @return \Illuminate\Http\Response
  158. */
  159. public function destroy(UserDict $userDict)
  160. {
  161. //
  162. }
  163. }