WbwLookupController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\UserDict;
  4. use App\Models\DictInfo;
  5. use App\Models\WbwTemplate;
  6. use App\Models\Channel;
  7. use App\Models\WbwAnalysis;
  8. use Illuminate\Http\Request;
  9. use App\Tools\CaseMan;
  10. use Illuminate\Support\Facades\Log;
  11. use Illuminate\Support\Facades\Cache;
  12. use App\Http\Api\DictApi;
  13. class WbwLookupController extends Controller
  14. {
  15. private $dictList = [
  16. '85dcc61c-c9e1-4ae0-9b44-cd6d9d9f0d01',//社区汇总
  17. '4d3a0d92-0adc-4052-80f5-512a2603d0e8',// system irregular
  18. '8359757e-9575-455b-a772-cc6f036caea0',// system sandhi
  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. * Create a new command instance.
  28. *
  29. * @return void
  30. */
  31. private function initSysDict()
  32. {
  33. // system regular
  34. $this->dictList[] = DictApi::getSysDict('system_regular');
  35. $this->dictList[] = DictApi::getSysDict('robot_compound');
  36. $this->dictList[] = DictApi::getSysDict('community');
  37. $this->dictList[] = DictApi::getSysDict('community_extract');
  38. }
  39. /**
  40. * Display a listing of the resource.
  41. * @param \Illuminate\Http\Request $request
  42. *
  43. * @return \Illuminate\Http\Response
  44. */
  45. public function index(Request $request)
  46. {
  47. //
  48. $startAt = microtime(true)*1000;
  49. $this->initSysDict();
  50. $words = \explode(',',$request->get("word"));
  51. $bases = \explode(',',$request->get("base"));
  52. # 查询深度
  53. $deep = $request->get("deep",2);
  54. $result = $this->lookup($words,$bases,$deep);
  55. $endAt = microtime(true)*1000;
  56. return $this->ok(["rows"=>$result,
  57. "count"=>count($result),
  58. "time"=>(int)($endAt-$startAt)]);
  59. }
  60. public function lookup($words,$bases,$deep){
  61. $wordPool = array();
  62. $output = array();
  63. foreach ($words as $word) {
  64. $wordPool[$word] = ['base' => false,'done' => false,'apply' => false];
  65. }
  66. foreach ($bases as $base) {
  67. $wordPool[$base] = ['base' => true,'done' => false,'apply' => false];
  68. }
  69. /**
  70. * 先查询字典名称
  71. */
  72. $dict_info = DictInfo::whereIn('id',$this->dictList)->select('id','shortname')->get();
  73. $dict_name = [];
  74. foreach ($dict_info as $key => $value) {
  75. # code...
  76. $dict_name[$value->id] = $value->shortname;
  77. }
  78. $caseman = new CaseMan();
  79. for ($i=0; $i < $deep; $i++) {
  80. $newBase = array();
  81. $newWords = [];
  82. foreach ($wordPool as $word => $info) {
  83. # code...
  84. if($info['done'] === false){
  85. $newWords[] = $word;
  86. $wordPool[$word]['done'] = true;
  87. }
  88. }
  89. $data = UserDict::whereIn('word',$newWords)
  90. ->whereIn('dict_id',$this->dictList)
  91. ->leftJoin('dict_infos', 'user_dicts.dict_id', '=', 'dict_infos.id')
  92. ->orderBy('confidence','desc')
  93. ->get();
  94. foreach ($data as $row) {
  95. # code...
  96. array_push($output,$row);
  97. if(!empty($row->parent) && !isset($wordPool[$row->parent]) ){
  98. //将parent 插入待查询列表
  99. $wordPool[$row->parent] = ['base' => true,'done' => false,'apply' => false];
  100. }
  101. }
  102. //处理查询结果中的拆分信息
  103. $newWordPart = array();
  104. foreach ($wordPool as $word => $info) {
  105. if(!empty($info['factors'])){
  106. $factors = \explode('+',$info['factors']);
  107. foreach ($factors as $factor) {
  108. # 将没有的拆分放入单词查询列表
  109. if(!isset($wordPool[$factor])){
  110. $wordPool[$factor] = ['base' => true,'done' => false,'apply' => false];
  111. }
  112. }
  113. }
  114. }
  115. }
  116. return $output;
  117. }
  118. /**
  119. * 自动查词
  120. *
  121. * @param \Illuminate\Http\Request $request
  122. * @return \Illuminate\Http\Response
  123. */
  124. public function store(Request $request)
  125. {
  126. //
  127. $startAt = microtime(true)*1000;
  128. // system regular
  129. $this->initSysDict();
  130. $channel = Channel::find($request->get('channel_id'));
  131. $orgData = $request->get('data');
  132. //句子中的单词
  133. $words = [];
  134. foreach ($orgData as $word) {
  135. # code...
  136. if( isset($word['type']) && $word['type']['value'] === '.ctl.'){
  137. continue;
  138. }
  139. if(!empty($word['real']['value'])){
  140. $words[] = $word['real']['value'];
  141. }
  142. }
  143. $result = $this->lookup($words,[],2);
  144. $indexed = $this->toIndexed($result);
  145. foreach ($orgData as $key => $word) {
  146. if( isset($word['type']) && $word['type']['value'] === '.ctl.'){
  147. continue;
  148. }
  149. if(empty($word['real']['value'])){
  150. continue;
  151. }
  152. {
  153. $data = $word;
  154. if(isset($indexed[$word['real']['value']])){
  155. //parent
  156. $case = [];
  157. $parent = [];
  158. $factors = [];
  159. $factorMeaning = [];
  160. $meaning = [];
  161. $parent2 = [];
  162. $case2 = [];
  163. foreach ($indexed[$word['real']['value']] as $value) {
  164. //非base优先
  165. if(strstr($value->type,'base') === FALSE){
  166. $increment = 10;
  167. }else{
  168. $increment = 1;
  169. }
  170. //将全部结果加上得分放入数组
  171. $parent = $this->insertValue([$value->parent],$parent,$increment);
  172. if(!empty($value->type) && $value->type !== ".cp."){
  173. $case = $this->insertValue([$value->type."#".$value->grammar],$case,$increment);
  174. }
  175. $factors = $this->insertValue([$value->factors],$factors,$increment);
  176. $factorMeaning = $this->insertValue([$value->factormean],$factorMeaning,$increment);
  177. $meaning = $this->insertValue(explode('$',$value->mean),$meaning,$increment,false);
  178. }
  179. if(count($case)>0){
  180. arsort($case);
  181. $first = array_keys($case)[0];
  182. $data['case'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
  183. }
  184. if(count($parent)>0){
  185. arsort($parent);
  186. $first = array_keys($parent)[0];
  187. $data['parent'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
  188. }
  189. if(count($factors)>0){
  190. arsort($factors);
  191. $first = array_keys($factors)[0];
  192. $data['factors'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
  193. }
  194. //拆分意思
  195. if(count($factorMeaning)>0){
  196. arsort($factorMeaning);
  197. $first = array_keys($factorMeaning)[0];
  198. $data['factorMeaning'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
  199. }
  200. $wbwFactorMeaning = [];
  201. if(!empty($data['factors']['value'])){
  202. foreach (explode("+",$data['factors']['value']) as $factor) {
  203. # code...
  204. $wbwAnalyses = WbwAnalysis::where('wbw_word',$factor)
  205. ->where('type',7)
  206. ->selectRaw('data,count(*)')
  207. ->groupBy("data")
  208. ->orderBy("count", "desc")
  209. ->first();
  210. if($wbwAnalyses){
  211. $wbwFactorMeaning[]=$wbwAnalyses->data;
  212. }else{
  213. $wbwFactorMeaning[]="";
  214. }
  215. }
  216. }
  217. $data['factorMeaning'] = ['value'=>implode('+',$wbwFactorMeaning),'status'=>3];
  218. if(!empty($data['parent'])){
  219. if(isset($indexed[$data['parent']['value']])){
  220. foreach ($indexed[$data['parent']['value']] as $value) {
  221. //根据base 查找词意
  222. //非base优先
  223. $increment = 10;
  224. $meaning = $this->insertValue(explode('$',$value->mean),$meaning,$increment,false);
  225. //查找词源
  226. if(!empty($value->parent) && $value->parent !== $value->word && strstr($value->type,"base") !== FALSE ){
  227. $parent2 = $this->insertValue([$value->grammar."$".$value->parent],$parent2,1,false);
  228. }
  229. }
  230. }
  231. }
  232. if(count($meaning)>0){
  233. arsort($meaning);
  234. $first = array_keys($meaning)[0];
  235. $data['meaning'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
  236. }
  237. if(count($parent2)>0){
  238. arsort($parent2);
  239. $first = explode("$",array_keys($parent2)[0]);
  240. $data['parent2'] = ['value'=>$first[1],'status'=>3];
  241. $data['grammar2'] = ['value'=>$first[0],'status'=>3];
  242. }
  243. }
  244. $orgData[$key] = $data;
  245. }
  246. }
  247. return $this->ok($orgData);
  248. }
  249. /**
  250. * 自动查词
  251. *
  252. * @param string $sentId
  253. * @return \Illuminate\Http\Response
  254. */
  255. public function show(Request $request,string $sentId)
  256. {
  257. $startAt = microtime(true)*1000;
  258. $channel = Channel::find($request->get('channel_id'));
  259. //查询句子中的单词
  260. $sent = \explode('-',$sentId);
  261. $wbw = WbwTemplate::where('book',$sent[0])
  262. ->where('paragraph',$sent[1])
  263. ->whereBetween('wid',[$sent[2],$sent[3]])
  264. ->orderBy('wid')
  265. ->get();
  266. $words = [];
  267. foreach ($wbw as $row) {
  268. if($row->type !== '.ctl.' && !empty($row->real)){
  269. $words[] = $row->real;
  270. }
  271. }
  272. $result = $this->lookup($words,[],2);
  273. $indexed = $this->toIndexed($result);
  274. //生成自动填充结果
  275. $wbwContent = [];
  276. foreach ($wbw as $row) {
  277. $type = $row->type=='?'? '':$row->type;
  278. $grammar = $row->gramma=='?'? '':$row->gramma;
  279. $part = $row->part=='?'? '':$row->part;
  280. if(!empty($type) || !empty($grammar)){
  281. $case = "{$type}#$grammar";
  282. }else{
  283. $case = "";
  284. }
  285. $data = [
  286. 'sn'=>[$row->wid],
  287. 'word'=>['value'=>$row->word,'status'=>3],
  288. 'real'=> ['value'=>$row->real,'status'=>3],
  289. 'meaning'=> ['value'=>[],'status'=>3],
  290. 'type'=> ['value'=>$type,'status'=>3],
  291. 'grammar'=> ['value'=>$grammar,'status'=>3],
  292. 'case'=> ['value'=>$case,'status'=>3],
  293. 'style'=> ['value'=>$row->style,'status'=>3],
  294. 'factors'=> ['value'=>$part,'status'=>3],
  295. 'factorMeaning'=> ['value'=>'','status'=>3],
  296. 'confidence'=> 0.5
  297. ];
  298. if($row->type !== '.ctl.' && !empty($row->real)){
  299. if(isset($indexed[$row->real])){
  300. //parent
  301. $case = [];
  302. $parent = [];
  303. $factors = [];
  304. $factorMeaning = [];
  305. $meaning = [];
  306. $parent2 = [];
  307. $case2 = [];
  308. foreach ($indexed[$row->real] as $value) {
  309. //非base优先
  310. if(strstr($value->type,'base') === FALSE){
  311. $increment = 10;
  312. }else{
  313. $increment = 1;
  314. }
  315. //将全部结果加上得分放入数组
  316. $parent = $this->insertValue([$value->parent],$parent,$increment);
  317. $case = $this->insertValue([$value->type."#".$value->grammar],$case,$increment);
  318. $factors = $this->insertValue([$value->factors],$factors,$increment);
  319. $factorMeaning = $this->insertValue([$value->factormean],$factorMeaning,$increment);
  320. $meaning = $this->insertValue(explode('$',$value->mean),$meaning,$increment,false);
  321. }
  322. if(count($case)>0){
  323. arsort($case);
  324. $first = array_keys($case)[0];
  325. $data['case'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
  326. }
  327. if(count($parent)>0){
  328. arsort($parent);
  329. $first = array_keys($parent)[0];
  330. $data['parent'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
  331. }
  332. if(count($factors)>0){
  333. arsort($factors);
  334. $first = array_keys($factors)[0];
  335. $data['factors'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
  336. }
  337. if(count($factorMeaning)>0){
  338. arsort($factorMeaning);
  339. $first = array_keys($factorMeaning)[0];
  340. $data['factorMeaning'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
  341. }
  342. //根据base 查找词意
  343. if(!empty($data['parent'])){
  344. if(isset($indexed[$data['parent']['value']])){
  345. Log::info($data['parent']['value']."=".count($indexed[$data['parent']['value']]));
  346. foreach ($indexed[$data['parent']['value']] as $value) {
  347. //非base优先
  348. $increment = 10;
  349. $meaning = $this->insertValue(explode('$',$value->mean),$meaning,$increment,false);
  350. }
  351. }else{
  352. Log::error("no set parent".$data['parent']['value']);
  353. }
  354. }
  355. if(count($meaning)>0){
  356. arsort($meaning);
  357. Log::info('meanings=');
  358. Log::info(array_keys($meaning));
  359. $first = array_keys($meaning)[0];
  360. $data['meaning'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
  361. }
  362. }
  363. }
  364. $wbwContent[] = $data;
  365. }
  366. $endAt = microtime(true)*1000;
  367. return $this->ok(["rows"=>$wbwContent,
  368. "count"=>count($wbwContent),
  369. "time"=>(int)($endAt-$startAt)]);
  370. }
  371. private function toIndexed($words){
  372. //转成索引数组
  373. $indexed = [];
  374. foreach ($words as $key => $value) {
  375. # code...
  376. $indexed[$value->word][] = $value;
  377. }
  378. return $indexed;
  379. }
  380. private function insertValue($value,$container,$increment,$empty=true){
  381. foreach ($value as $one) {
  382. if($empty === false){
  383. if(empty($one)){
  384. break;
  385. }
  386. }
  387. $one=trim($one);
  388. $key = $one;
  389. if(empty($key)){
  390. $key = '_null';
  391. }
  392. if(isset($container[$key])){
  393. $container[$key] += $increment;
  394. }else{
  395. $container[$key] = $increment;
  396. }
  397. }
  398. return $container;
  399. }
  400. /**
  401. * Update the specified resource in storage.
  402. *
  403. * @param \Illuminate\Http\Request $request
  404. * @param \App\Models\UserDict $userDict
  405. * @return \Illuminate\Http\Response
  406. */
  407. public function update(Request $request, UserDict $userDict)
  408. {
  409. //
  410. }
  411. /**
  412. * Remove the specified resource from storage.
  413. *
  414. * @param \App\Models\UserDict $userDict
  415. * @return \Illuminate\Http\Response
  416. */
  417. public function destroy(UserDict $userDict)
  418. {
  419. //
  420. }
  421. }