123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?php
- namespace App\Model;
- use Illuminate\Database\Eloquent\Model;
- class TextLabel extends Model
- {
- protected $table = 'text_label';
- public static function boot()
- {
- $add = request()->post();
- parent::boot();
- static::saving(function ($model) use ($add) {
- $data = $model->attributes;
- $textLabel= [
- 'label' => [
- 'inspection' => new \ArrayObject(),
- 'test' => new \ArrayObject(),
- 'operation' => new \ArrayObject(),
- 'medicine' => new \ArrayObject(),
- 'symptom' => new \ArrayObject(),
- 'disease' => new \ArrayObject(),
- 'date' => new \ArrayObject()
- ],
- 'text' => $data['text']
- ];
- if (empty($add['symptom'])){
- $add['symptom'] = [];
- }
- if (empty($add['add_symptom'])){
- $add['add_symptom']['values'] = [];
- }
- if (empty($add['operation'])){
- $add['operation'] = [];
- }
- if (empty($add['add_operation'])){
- $add['add_operation']['values'] = [];
- }
- if (empty($add['medicine'])){
- $add['medicine'] = [];
- }
- if (empty($add['add_medicine'])){
- $add['add_medicine']['values'] = [];
- }
- if (empty($add['inspection'])){
- $add['inspection'] = [];
- }
- if (empty($add['test'])){
- $add['test'] = [];
- }
- if (empty($add['disease'])){
- $add['disease'] = [];
- }
- if (empty($add['add_disease'])){
- $add['add_disease']['values'] = [];
- }
- if (empty($add['date'])){
- $add['date'] = [];
- }
- if (empty($add['add_date'])){
- $add['add_date']['values'] = [];
- }
- $inspection = array_column($add['inspection'],'terms');
- $inspectionArray = array_column($add['inspection'],'tag','terms');
- $test = array_column($add['test'],'terms');
- $testArray = array_column($add['test'],'tag','terms');
- $symptom = array_unique(array_merge($add['symptom'],$add['add_symptom']['values']));
- $operation = array_unique(array_merge($add['operation'],$add['add_operation']['values']));
- $medicine = array_unique(array_merge($add['medicine'],$add['add_medicine']['values']));
- $disease = array_unique(array_merge($add['disease'],$add['add_disease']['values']));
- $date = array_unique(array_merge($add['date'],$add['add_date']['values']));
- $terms = array_filter(array_unique(array_merge($symptom,$operation,$medicine,$disease,$inspection,$test,$date)));
- foreach ($terms as $item){
- $count = mb_substr_count($data['text'],$item);
- $length = mb_strlen($data['text']);
- $offset = 0;
- for ($i = 0;$i < $count;$i++){
- if ($offset > $length){
- break;
- }
- $pos = mb_strpos($data['text'],$item,$offset);
- if ($pos === false){
- break;
- }
- $len = mb_strlen($item);
- $offset += $pos+$len;
- if (in_array($item,$inspection)){
- $textLabel['label']['inspection'][$item.'@#'.$inspectionArray[$item]][] = [$pos,$pos+$len];
- }
- if (in_array($item,$test)){
- $textLabel['label']['test'][$item.'@#'.$testArray[$item]][] = [$pos,$pos+$len];
- }
- if (in_array($item,$symptom)){
- $textLabel['label']['symptom'][$item][] = [$pos,$pos+$len];
- }
- if (in_array($item,$symptom)){
- $textLabel['label']['operation'][$item][] = [$pos,$pos+$len];
- }
- if (in_array($item,$symptom)){
- $textLabel['label']['medicine'][$item][] = [$pos,$pos+$len];
- }
- if (in_array($item,$disease)){
- $textLabel['label']['disease'][$item][] = [$pos,$pos+$len];
- }
- if (in_array($item,$date)){
- $textLabel['label']['date'][$item][] = [$pos,$pos+$len];
- }
- }
- }
- $json['data'][] = $textLabel;
- $json['filename'] = 'corpus.json';
- $url = 'http://121.36.94.218:10090/disease/ner/data';
- $post = json_encode($json);
- $result = self::sendPost($url,$post);
- if ($result['code'] == '200'){
- self::query()->where('id',$data['id'])->update(['status'=>1]);
- }
- });
- }
- public static function sendPost($url,$data){
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- curl_setopt($ch, CURLOPT_HEADER, false);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- $output = curl_exec($ch);
- curl_close($ch);
- $output = json_decode($output,true);
- return $output;
- }
- }
|