TextLabel.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace App\Model;
  3. use Illuminate\Database\Eloquent\Model;
  4. class TextLabel extends Model
  5. {
  6. protected $table = 'text_label';
  7. public static function boot()
  8. {
  9. $add = request()->post();
  10. parent::boot();
  11. static::saving(function ($model) use ($add) {
  12. $data = $model->attributes;
  13. $textLabel= [
  14. 'label' => [
  15. 'inspection' => new \ArrayObject(),
  16. 'test' => new \ArrayObject(),
  17. 'operation' => new \ArrayObject(),
  18. 'medicine' => new \ArrayObject(),
  19. 'symptom' => new \ArrayObject(),
  20. 'disease' => new \ArrayObject(),
  21. 'date' => new \ArrayObject()
  22. ],
  23. 'text' => $data['text']
  24. ];
  25. if (empty($add['symptom'])){
  26. $add['symptom'] = [];
  27. }
  28. if (empty($add['add_symptom'])){
  29. $add['add_symptom']['values'] = [];
  30. }
  31. if (empty($add['operation'])){
  32. $add['operation'] = [];
  33. }
  34. if (empty($add['add_operation'])){
  35. $add['add_operation']['values'] = [];
  36. }
  37. if (empty($add['medicine'])){
  38. $add['medicine'] = [];
  39. }
  40. if (empty($add['add_medicine'])){
  41. $add['add_medicine']['values'] = [];
  42. }
  43. if (empty($add['inspection'])){
  44. $add['inspection'] = [];
  45. }
  46. if (empty($add['test'])){
  47. $add['test'] = [];
  48. }
  49. if (empty($add['disease'])){
  50. $add['disease'] = [];
  51. }
  52. if (empty($add['add_disease'])){
  53. $add['add_disease']['values'] = [];
  54. }
  55. if (empty($add['date'])){
  56. $add['date'] = [];
  57. }
  58. if (empty($add['add_date'])){
  59. $add['add_date']['values'] = [];
  60. }
  61. $inspection = array_column($add['inspection'],'terms');
  62. $inspectionArray = array_column($add['inspection'],'tag','terms');
  63. $test = array_column($add['test'],'terms');
  64. $testArray = array_column($add['test'],'tag','terms');
  65. $symptom = array_unique(array_merge($add['symptom'],$add['add_symptom']['values']));
  66. $operation = array_unique(array_merge($add['operation'],$add['add_operation']['values']));
  67. $medicine = array_unique(array_merge($add['medicine'],$add['add_medicine']['values']));
  68. $disease = array_unique(array_merge($add['disease'],$add['add_disease']['values']));
  69. $date = array_unique(array_merge($add['date'],$add['add_date']['values']));
  70. $terms = array_filter(array_unique(array_merge($symptom,$operation,$medicine,$disease,$inspection,$test,$date)));
  71. foreach ($terms as $item){
  72. $count = mb_substr_count($data['text'],$item);
  73. $length = mb_strlen($data['text']);
  74. $offset = 0;
  75. for ($i = 0;$i < $count;$i++){
  76. if ($offset > $length){
  77. break;
  78. }
  79. $pos = mb_strpos($data['text'],$item,$offset);
  80. if ($pos === false){
  81. break;
  82. }
  83. $len = mb_strlen($item);
  84. $offset += $pos+$len;
  85. if (in_array($item,$inspection)){
  86. $textLabel['label']['inspection'][$item.'@#'.$inspectionArray[$item]][] = [$pos,$pos+$len];
  87. }
  88. if (in_array($item,$test)){
  89. $textLabel['label']['test'][$item.'@#'.$testArray[$item]][] = [$pos,$pos+$len];
  90. }
  91. if (in_array($item,$symptom)){
  92. $textLabel['label']['symptom'][$item][] = [$pos,$pos+$len];
  93. }
  94. if (in_array($item,$symptom)){
  95. $textLabel['label']['operation'][$item][] = [$pos,$pos+$len];
  96. }
  97. if (in_array($item,$symptom)){
  98. $textLabel['label']['medicine'][$item][] = [$pos,$pos+$len];
  99. }
  100. if (in_array($item,$disease)){
  101. $textLabel['label']['disease'][$item][] = [$pos,$pos+$len];
  102. }
  103. if (in_array($item,$date)){
  104. $textLabel['label']['date'][$item][] = [$pos,$pos+$len];
  105. }
  106. }
  107. }
  108. $json['data'][] = $textLabel;
  109. $json['filename'] = 'corpus.json';
  110. $url = 'http://121.36.94.218:10090/disease/ner/data';
  111. $post = json_encode($json);
  112. $result = self::sendPost($url,$post);
  113. if ($result['code'] == '200'){
  114. self::query()->where('id',$data['id'])->update(['status'=>1]);
  115. }
  116. });
  117. }
  118. public static function sendPost($url,$data){
  119. $ch = curl_init();
  120. curl_setopt($ch, CURLOPT_URL, $url);
  121. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  122. curl_setopt($ch, CURLOPT_POST, true);
  123. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  124. curl_setopt($ch, CURLOPT_HEADER, false);
  125. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  126. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  127. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  128. $output = curl_exec($ch);
  129. curl_close($ch);
  130. $output = json_decode($output,true);
  131. return $output;
  132. }
  133. }