2
0

CompoundController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\UserDict;
  4. use Illuminate\Http\Request;
  5. use App\Http\Api\DictApi;
  6. use App\Tools\TurboSplit;
  7. class CompoundController extends Controller
  8. {
  9. /**
  10. * Display a listing of the resource.
  11. *
  12. * @return \Illuminate\Http\Response
  13. */
  14. public function index()
  15. {
  16. //
  17. }
  18. /**
  19. * Store a newly created resource in storage.
  20. *
  21. * @param \Illuminate\Http\Request $request
  22. * @return \Illuminate\Http\Response
  23. */
  24. public function store(Request $request)
  25. {
  26. /**
  27. *
  28. */
  29. $dict_id = DictApi::getSysDict('robot_compound');
  30. if(!$dict_id){
  31. return $this->error('没有找到 robot_compound 字典');
  32. }
  33. //删除旧数据
  34. $del = UserDict::where('dict_id',$dict_id)
  35. ->whereIn('word',$request->get('index'))
  36. ->delete();
  37. foreach ($request->get('words') as $key => $word) {
  38. $new = new UserDict;
  39. $new->id = app('snowflake')->id();
  40. $new->word = $word['word'];
  41. $new->factors = $word['factors'];
  42. $new->dict_id = $dict_id;
  43. $new->source = '_ROBOT_';
  44. $new->create_time = (int)(microtime(true)*1000);
  45. $new->type = $word['type'];
  46. $new->grammar = $word['grammar'];
  47. $new->parent = $word['parent'];
  48. $new->confidence = $word['confidence'];
  49. $new->note = $word['confidence'];
  50. $new->language = 'cm';
  51. $new->creator_id = 1;
  52. $new->flag = 0;//标记为维护状态
  53. $new->save();
  54. }
  55. return $this->ok(count($request->get('words')));
  56. }
  57. /**
  58. * Display the specified resource.
  59. * @param \Illuminate\Http\Request $request
  60. * @param \App\Models\DhammaTerm $dhammaTerm
  61. * @return \Illuminate\Http\Response
  62. */
  63. public function show(Request $request,string $word)
  64. {
  65. //
  66. $start = microtime(true);
  67. $dict_id = DictApi::getSysDict('robot_compound');
  68. if(!$dict_id){
  69. return $this->error('没有找到 robot_compound 字典');
  70. }
  71. $result = UserDict::where('dict_id',$dict_id)
  72. ->where('word',$word)
  73. ->orderBy('confidence','desc')
  74. ->get();
  75. if(count($result)>0){
  76. return $this->ok(['rows'=>$result,'count'=>count($result),'mode'=>'dict']);
  77. }else if(mb_strlen($word,'UTF-8')<60){
  78. $ts = new TurboSplit();
  79. $parts = $ts->splitA($word);
  80. $time = microtime(true) - $start;
  81. return $this->ok(['rows'=>$parts,'count'=>count($parts),'mode'=>'realtime','time'=>$time]);
  82. }else{
  83. return $this->ok(['rows'=>[],'count'=>0]);
  84. }
  85. }
  86. /**
  87. * Update the specified resource in storage.
  88. *
  89. * @param \Illuminate\Http\Request $request
  90. * @param \App\Models\DhammaTerm $dhammaTerm
  91. * @return \Illuminate\Http\Response
  92. */
  93. public function update(Request $request, DhammaTerm $dhammaTerm)
  94. {
  95. //
  96. }
  97. /**
  98. * Remove the specified resource from storage.
  99. *
  100. * @param \App\Models\DhammaTerm $dhammaTerm
  101. * @return \Illuminate\Http\Response
  102. */
  103. public function destroy(DhammaTerm $dhammaTerm)
  104. {
  105. //
  106. }
  107. }