WbwLookupController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. /**
  120. *
  121. */
  122. private function langCheck($query,$lang){
  123. if($query===[]){
  124. return true;
  125. }else{
  126. if(in_array(strtolower($lang),$query)){
  127. return true;
  128. }else{
  129. $langFamily = explode('-',$lang)[0];
  130. foreach ($query as $value) {
  131. if(strpos($value,$langFamily) !== false){
  132. return true;
  133. }
  134. }
  135. }
  136. }
  137. return false;
  138. }
  139. /**
  140. * 自动查词
  141. *
  142. * @param \Illuminate\Http\Request $request
  143. * @return \Illuminate\Http\Response
  144. */
  145. public function store(Request $request)
  146. {
  147. //
  148. $startAt = microtime(true)*1000;
  149. // system regular
  150. $this->initSysDict();
  151. $channel = Channel::find($request->get('channel_id'));
  152. $orgData = $request->get('data');
  153. $lang = [];
  154. foreach ($request->get('lang',[]) as $value) {
  155. $lang[] = strtolower($value);
  156. }
  157. //句子中的单词
  158. $words = [];
  159. foreach ($orgData as $word) {
  160. # code...
  161. if( isset($word['type']) && $word['type']['value'] === '.ctl.'){
  162. continue;
  163. }
  164. if(!empty($word['real']['value'])){
  165. $words[] = $word['real']['value'];
  166. }
  167. }
  168. $result = $this->lookup($words,[],2);
  169. $indexed = $this->toIndexed($result);
  170. foreach ($orgData as $key => $word) {
  171. if( isset($word['type']) && $word['type']['value'] === '.ctl.'){
  172. continue;
  173. }
  174. if(empty($word['real']['value'])){
  175. continue;
  176. }
  177. $data = $word;
  178. if(isset($indexed[$word['real']['value']])){
  179. //parent
  180. $case = [];
  181. $parent = [];
  182. $factors = [];
  183. $factorMeaning = [];
  184. $meaning = [];
  185. $parent2 = [];
  186. $case2 = [];
  187. foreach ($indexed[$word['real']['value']] as $value) {
  188. //非base优先
  189. if(strstr($value->type,'base') === FALSE){
  190. $increment = 10;
  191. }else{
  192. $increment = 1;
  193. }
  194. //将全部结果加上得分放入数组
  195. $parent = $this->insertValue([$value->parent],$parent,$increment);
  196. if(!empty($value->type) && $value->type !== ".cp."){
  197. $case = $this->insertValue([$value->type."#".$value->grammar],$case,$increment);
  198. }
  199. $factors = $this->insertValue([$value->factors],$factors,$increment);
  200. $factorMeaning = $this->insertValue([$value->factormean],$factorMeaning,$increment);
  201. if($this->langCheck($lang,$value->language)){
  202. $meaning = $this->insertValue(explode('$',$value->mean),$meaning,$increment,false);
  203. }
  204. }
  205. if(count($case)>0){
  206. arsort($case);
  207. $first = array_keys($case)[0];
  208. $data['case'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
  209. }
  210. if(count($parent)>0){
  211. arsort($parent);
  212. $first = array_keys($parent)[0];
  213. $data['parent'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
  214. }
  215. if(count($factors)>0){
  216. arsort($factors);
  217. $first = array_keys($factors)[0];
  218. $data['factors'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
  219. }
  220. //拆分意思
  221. if(count($factorMeaning)>0){
  222. arsort($factorMeaning);
  223. $first = array_keys($factorMeaning)[0];
  224. $data['factorMeaning'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
  225. }
  226. $wbwFactorMeaning = [];
  227. if(!empty($data['factors']['value'])){
  228. foreach (explode("+",$data['factors']['value']) as $factor) {
  229. # code...
  230. $wbwAnalyses = WbwAnalysis::where('wbw_word',$factor)
  231. ->where('type',7)
  232. ->selectRaw('data,count(*)')
  233. ->groupBy("data")
  234. ->orderBy("count", "desc")
  235. ->first();
  236. if($wbwAnalyses){
  237. $wbwFactorMeaning[]=$wbwAnalyses->data;
  238. }else{
  239. $wbwFactorMeaning[]="";
  240. }
  241. }
  242. }
  243. $data['factorMeaning'] = ['value'=>implode('+',$wbwFactorMeaning),'status'=>3];
  244. if(!empty($data['parent'])){
  245. if(isset($indexed[$data['parent']['value']])){
  246. foreach ($indexed[$data['parent']['value']] as $value) {
  247. //根据base 查找词意
  248. //非base优先
  249. $increment = 10;
  250. if($this->langCheck($lang,$value->language)){
  251. $meaning = $this->insertValue(explode('$',$value->mean),$meaning,$increment,false);
  252. }
  253. //查找词源
  254. if(!empty($value->parent) && $value->parent !== $value->word && strstr($value->type,"base") !== FALSE ){
  255. $parent2 = $this->insertValue([$value->grammar."$".$value->parent],$parent2,1,false);
  256. }
  257. }
  258. }
  259. }
  260. if(count($meaning)>0){
  261. arsort($meaning);
  262. $first = array_keys($meaning)[0];
  263. $data['meaning'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
  264. }
  265. if(count($parent2)>0){
  266. arsort($parent2);
  267. $first = explode("$",array_keys($parent2)[0]);
  268. $data['parent2'] = ['value'=>$first[1],'status'=>3];
  269. $data['grammar2'] = ['value'=>$first[0],'status'=>3];
  270. }
  271. }
  272. $orgData[$key] = $data;
  273. }
  274. Log::info($orgData);
  275. return $this->ok($orgData);
  276. }
  277. /**
  278. * 自动查词
  279. *
  280. * @param string $sentId
  281. * @return \Illuminate\Http\Response
  282. */
  283. public function show(Request $request,string $sentId)
  284. {
  285. $startAt = microtime(true)*1000;
  286. $channel = Channel::find($request->get('channel_id'));
  287. //查询句子中的单词
  288. $sent = \explode('-',$sentId);
  289. $wbw = WbwTemplate::where('book',$sent[0])
  290. ->where('paragraph',$sent[1])
  291. ->whereBetween('wid',[$sent[2],$sent[3]])
  292. ->orderBy('wid')
  293. ->get();
  294. $words = [];
  295. foreach ($wbw as $row) {
  296. if($row->type !== '.ctl.' && !empty($row->real)){
  297. $words[] = $row->real;
  298. }
  299. }
  300. $result = $this->lookup($words,[],2);
  301. $indexed = $this->toIndexed($result);
  302. //生成自动填充结果
  303. $wbwContent = [];
  304. foreach ($wbw as $row) {
  305. $type = $row->type=='?'? '':$row->type;
  306. $grammar = $row->gramma=='?'? '':$row->gramma;
  307. $part = $row->part=='?'? '':$row->part;
  308. if(!empty($type) || !empty($grammar)){
  309. $case = "{$type}#$grammar";
  310. }else{
  311. $case = "";
  312. }
  313. $data = [
  314. 'sn'=>[$row->wid],
  315. 'word'=>['value'=>$row->word,'status'=>3],
  316. 'real'=> ['value'=>$row->real,'status'=>3],
  317. 'meaning'=> ['value'=>[],'status'=>3],
  318. 'type'=> ['value'=>$type,'status'=>3],
  319. 'grammar'=> ['value'=>$grammar,'status'=>3],
  320. 'case'=> ['value'=>$case,'status'=>3],
  321. 'style'=> ['value'=>$row->style,'status'=>3],
  322. 'factors'=> ['value'=>$part,'status'=>3],
  323. 'factorMeaning'=> ['value'=>'','status'=>3],
  324. 'confidence'=> 0.5
  325. ];
  326. if($row->type !== '.ctl.' && !empty($row->real)){
  327. if(isset($indexed[$row->real])){
  328. //parent
  329. $case = [];
  330. $parent = [];
  331. $factors = [];
  332. $factorMeaning = [];
  333. $meaning = [];
  334. $parent2 = [];
  335. $case2 = [];
  336. foreach ($indexed[$row->real] as $value) {
  337. //非base优先
  338. if(strstr($value->type,'base') === FALSE){
  339. $increment = 10;
  340. }else{
  341. $increment = 1;
  342. }
  343. //将全部结果加上得分放入数组
  344. $parent = $this->insertValue([$value->parent],$parent,$increment);
  345. $case = $this->insertValue([$value->type."#".$value->grammar],$case,$increment);
  346. $factors = $this->insertValue([$value->factors],$factors,$increment);
  347. $factorMeaning = $this->insertValue([$value->factormean],$factorMeaning,$increment);
  348. $meaning = $this->insertValue(explode('$',$value->mean),$meaning,$increment,false);
  349. }
  350. if(count($case)>0){
  351. arsort($case);
  352. $first = array_keys($case)[0];
  353. $data['case'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
  354. }
  355. if(count($parent)>0){
  356. arsort($parent);
  357. $first = array_keys($parent)[0];
  358. $data['parent'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
  359. }
  360. if(count($factors)>0){
  361. arsort($factors);
  362. $first = array_keys($factors)[0];
  363. $data['factors'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
  364. }
  365. if(count($factorMeaning)>0){
  366. arsort($factorMeaning);
  367. $first = array_keys($factorMeaning)[0];
  368. $data['factorMeaning'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
  369. }
  370. //根据base 查找词意
  371. if(!empty($data['parent'])){
  372. if(isset($indexed[$data['parent']['value']])){
  373. foreach ($indexed[$data['parent']['value']] as $value) {
  374. //非base优先
  375. $increment = 10;
  376. $meaning = $this->insertValue(explode('$',$value->mean),$meaning,$increment,false);
  377. }
  378. }else{
  379. //Log::error("no set parent".$data['parent']['value']);
  380. }
  381. }
  382. if(count($meaning)>0){
  383. arsort($meaning);
  384. $first = array_keys($meaning)[0];
  385. $data['meaning'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
  386. }
  387. }
  388. }
  389. $wbwContent[] = $data;
  390. }
  391. $endAt = microtime(true)*1000;
  392. return $this->ok(["rows"=>$wbwContent,
  393. "count"=>count($wbwContent),
  394. "time"=>(int)($endAt-$startAt)]);
  395. }
  396. private function toIndexed($words){
  397. //转成索引数组
  398. $indexed = [];
  399. foreach ($words as $key => $value) {
  400. # code...
  401. $indexed[$value->word][] = $value;
  402. }
  403. return $indexed;
  404. }
  405. private function insertValue($value,$container,$increment,$empty=true){
  406. foreach ($value as $one) {
  407. if($empty === false){
  408. if(empty($one)){
  409. break;
  410. }
  411. }
  412. $one=trim($one);
  413. $key = $one;
  414. if(empty($key)){
  415. $key = '_null';
  416. }
  417. if(isset($container[$key])){
  418. $container[$key] += $increment;
  419. }else{
  420. $container[$key] = $increment;
  421. }
  422. }
  423. return $container;
  424. }
  425. /**
  426. * Update the specified resource in storage.
  427. *
  428. * @param \Illuminate\Http\Request $request
  429. * @param \App\Models\UserDict $userDict
  430. * @return \Illuminate\Http\Response
  431. */
  432. public function update(Request $request, UserDict $userDict)
  433. {
  434. //
  435. }
  436. /**
  437. * Remove the specified resource from storage.
  438. *
  439. * @param \App\Models\UserDict $userDict
  440. * @return \Illuminate\Http\Response
  441. */
  442. public function destroy(UserDict $userDict)
  443. {
  444. //
  445. }
  446. }