WbwLookupController.php 17 KB

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