WbwLookupController.php 17 KB

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