今回は課題テーブルsubjectsのviewで類似度を算出し、similaritiesテーブルに格納しました。
public function view($id = null) {
if (!$this->Subject->exists($id)) {
throw new NotFoundException(__('Invalid subject'));
}
$options = array('conditions' => array('Subject.' . $this->Subject->primaryKey => $id));
$this->set('subject', $this->Subject->find('first', $options));
$subject = $this->Subject->find('first', $options);
App::import('Model', 'Similarity');
$this->Similarity = new Similarity;
for($i = 0; $i < count($subject['Report']) - 1 ; $i++){
for($j = $i + 1; $j < count($subject['Report']);$j++){
similar_text($subject['Report'][$i]['answer'], $subject['Report'][$j]['answer'], $diff);
$data = array('Similarity' =>
array('fromid' => $subject['Report'][$i]['id'],
'toid' => $subject['Report'][$j]['id'],
'value' => $diff
)
);
$this->Similarity->create();
$fields = array('fromid', 'toid', 'value');
$this->Similarity->save($data, false, $fields);
}
}
}
参考
http://php.net/manual/ja/function.similar-text.php
http://nitou.dip.jp/blog/archives/4238
0 件のコメント:
コメントを投稿