/** * Finds the Journal model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Record the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Record::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Updates an existing PoliceCase, Evidence and User model. * If update is successful, the browser will be redirected to the 'manage' page. * @param ineger $id * @return mixed * @throws NotFoundHttpException if the model cannot be found */ public function actionUpdate($id) { $record = Record::findOne($id); $owner = $record->owner; $userFullName = User::findOne($record->user_id)->getFullName(); if (!isset($record)) { throw new NotFoundHttpException("The record was not found."); } if ($record->load(Yii::$app->request->post())) { if ($record->save()) { if (!empty($owner) && $owner->load(Yii::$app->request->post()) && $owner->validate()) { $owner->save(); } } // VarDumper::dump($record->attributes, 10, true); // VarDumper::dump($record->getErrors(), 10, true); // die; return $this->redirect(['records/manage']); } return $this->render('update', ['record' => $record, 'owner' => $owner, 'userFullName' => $userFullName]); }
public function actionTrainData2() { $titleFile = fopen('titles.txt', 'w'); // Generate titles $titles = $this->getTitleRecord(); array_walk($titles, function ($title) use($titleFile) { fwrite($titleFile, $title['title'] . PHP_EOL); }); fclose($titleFile); echo 'Titles successfully generated.' . PHP_EOL; // Run sally command $sallyOptions = implode(' ', ['--input_format lines', '--output_format text', '--ngram_len 1', '--granularity tokens', '--vect_embed tfidf', '--vect_norm none', '--token_delim "%0a%0d%20%22.,:;!?"']); $sallyCommand = "sally {$sallyOptions} titles.txt titles.features.txt"; echo "Run: {$sallyCommand}\n"; exec($sallyCommand); // Read sally output and generate train set $trainFile = fopen('titles.train.svm', 'w'); $testFile = fopen('titles.test.svm', 'w'); $featuresFile = fopen('titles.features.txt', 'r'); $i = 0; $testIds = []; while (($line = fgets($featuresFile)) !== false) { $line = trim($line); if ($line[0] == '#') { continue; } // remove comment $posComment = strpos($line, '#'); $rest = trim(substr($line, 0, -(strlen($line) - $posComment))); if ($rest) { $featureTokens = explode(',', $rest); $features = []; foreach ($featureTokens as $token) { list($dimension, $weight) = explode('::', $token); $features[$dimension] = (double) $weight; } // find record /** @var Record $record */ $record = Record::findOne(['id' => $titles[$i]['id']]); $i++; $out = $testFile; $label = 0; $tasks = $record->getRecordTasks()->andWhere('is_prediction = 0')->all(); if ($tasks) { $out = $trainFile; $label = $tasks[0]->task_id; } else { $testIds[] = $record->id; } fwrite($out, $label . ' '); $parts = ['1:' . $record->window_id, '2:' . $record->window->process_id]; foreach ($features as $dim => $weight) { $parts[] = $dim . ':' . $weight; } fwrite($out, implode(' ', $parts)); fwrite($out, PHP_EOL); } } fclose($trainFile); fclose($testFile); // train libsvm model // best c=0.5, g=0.0078125 // best c=128.0, g=0.001953125, rate=67.3947 // best c=0.5, g=0.0078125, rate=99.5342 echo 'Run svm-train' . PHP_EOL; $trainCommnad = '/home/alx/soft/libsvm-3.20/svm-train -b 1 -c "0.5" -g "0.0078125" titles.train.svm svm.model'; exec($trainCommnad); // predict echo 'Run svm-predict' . PHP_EOL; $predictCommand = '/home/alx/soft/libsvm-3.20/svm-predict -b 1 -q titles.test.svm svm.model title.predict.txt'; exec($predictCommand); $predictionFile = fopen('title.predict.txt', 'r'); $i = 0; $transaction = \Yii::$app->db->beginTransaction(); // skip first line $dummyLine = fgets($predictionFile); $skiped = 0; while (($line = fgets($predictionFile)) !== false) { $line = trim($line); if (!isset($testIds[$i])) { throw new Exception('Что-то не сходится!'); } $recordId = $testIds[$i]; $i++; // Remove all predictions for this record RecordTask::deleteAll(['record_id' => $recordId, 'is_prediction' => 1]); $parts = explode(' ', $line); $predictTask = (int) $parts[0]; $probability = array_slice($parts, 1); $max = max(array_map('floatval', $probability)); if ($max < 0.75) { // echo "skip (with {$max})".PHP_EOL; $skiped++; continue; } // Save new prediction $model = new RecordTask(); $model->task_id = $predictTask; $model->record_id = $recordId; $model->is_prediction = 1; $model->save(false); } $transaction->commit(); echo "Skiped {$skiped} records\n"; }
public function actionAllocate() { $data = Yii::$app->request->post('Record'); $result = array(); if (is_numeric($data['id']) && $data['id'] > 0) { $model = Record::findOne($data['id']); if ($model) { if ($model->load(Yii::$app->request->post())) { $model->is_arrive = true; $model->is_valid = true; $model->is_reserve = true; $model->appointment == 0 ? $model->appointment = time() : null; $model->arrived_at = time(); if ($model->save()) { $result['status'] = 1; $result['message'] = '保存成功'; } else { $result['status'] = 0; $result['message'] = '保存失败'; } } $errors = $model->getFirstErrors(); if ($errors) { $result['status'] = 0; $result['message'] = current($errors); } } else { $result['status'] = 0; $result['message'] = '未找到该记录'; } } else { $result['status'] = 0; $result['message'] = '参数错误'; } return $this->renderJson($result); }
/** * @param int $id * @return null|\app\models\Record */ private static function getRecord($id) { return \app\models\Record::findOne($id); }
public function actionCostedit() { $data = Yii::$app->request->post('ChannelCost'); $result = array(); if (is_numeric($data['id']) && $data['id'] > 0) { $model = Record::findOne($data['id']); if (!$model) { $result['status'] = 0; $result['message'] = '未找到该记录'; } } else { $model = new ChannelCost(); } if ($model->load(Yii::$app->request->post())) { if ($model->save()) { $result['status'] = 1; $result['message'] = '保存成功'; } } $errors = $model->getFirstErrors(); if ($errors) { $result['status'] = 0; $result['message'] = current($errors); } return $this->renderJson($result); }