function index() { $clustering = Clustering::get_clustering(); $data = array(1, 3, 2, 5, 6, 2, 3, 1, 30, 36, 45, 3, 15, 17); $number = 3; $clustering->setup($data, $number); $result = $clustering->get_result(); $positions = $clustering->get_result_positions(); $this->unit->run(count($result), 3, '資料應該分成了3群'); $this->unit->run(count($positions), 3, '中心點應該也有3個'); $this->unit->run_false($result[0], array(), '第一群不是空陣列'); $this->unit->run_false($result[1], array(), '第二群不是空陣列'); $this->unit->run_false($result[2], array(), '第三群不是空陣列'); $this->unit->run_false($positions, array(), '中心點不是空陣列'); //------------------------- $clustering = Clustering::get_clustering(); $data = array(1, 1, 1, 1, 1, 2, 20, 20, 20, 20); $number = 3; $clustering->setup($data, $number); $result = $clustering->get_result(); $positions = $clustering->get_result_positions(); $this->unit->run(count($result), 3, '資料應該分成了3群'); $this->unit->run(count($positions), 3, '中心點應該也有3個'); $this->unit->run_false($result[0], array(), '第一群不是空陣列'); // $this->unit->run($result[1], array(), '第二群不是空陣列(這一個測試會故意錯誤,但這是因為k-means對偏離值影響很大。但KALS並不是要做很正確的分群,只是要個大概的中心點位置,所以重點在於下面的positions能有就好。)'); $this->unit->run_false($result[2], array(), '第三群不是空陣列'); $this->unit->run_false($positions, array(), '中心點不是空陣列'); // $this->unit->run($test_result // , $expected_result // , $test_name); //context_complete(); unit_test_report($this, __METHOD__); }
protected function renew_function_variables() { //這邊請用k-means來做 $this->_CI_load('library', 'kals_resource/Webpage', 'webpage'); $function_variables = NULL; $webpage_id = intval($this->get_field('webpage_id')); $webpage = new Webpage($webpage_id); $annotation_coll = $webpage->get_appended_annotation(); //標註數量太少的話就沒有分群的價值,取消分群 if ($annotation_coll->length() < 20) { return $this; } $data = array(); foreach ($annotation_coll as $annotation) { $data[] = $this->get_feature($annotation); } $this->_CI_load('library', 'fuzzy/Output_language_variable_collection', 'output_language_variable_collection'); $membership = new Output_language_variable_collection(); $number = $membership->get_number(); $this->_CI_load('library', 'fuzzy/Clustering', 'clustering'); $clustering = Clustering::get_clustering(); $clustering->set_data($data); $clustering->set_clusters_number($number); $positions = $clustering->get_result_positions(); $function_variables = ''; foreach ($positions as $p) { if ($function_variables != '') { $function_variables .= ','; } $function_variables .= $p; } $this->set_field('function_variables', $function_variables); return $this; }