CompoundController.php 4.0 KB

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