2
0

CompoundController.php 3.7 KB

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