public function get_inference_result(Language_variable_collection $langvar_coll)
 {
     $output_coll = new Output_language_variable_collection();
     $weight_array = $langvar_coll->get_weight_array();
     $membership_matrix = $langvar_coll->get_membership_matrix();
     for ($x = 0; $x < $output_coll->get_number(); $x++) {
         $min_array = array();
         for ($y = 0; $y < count($weight_array); $y++) {
             $min_array[] = min(array($weight_array[$y], $membership_matrix[$y][$x]));
         }
         $max = max($min_array);
         $output_coll->set_membership($x, $max);
     }
     return $output_coll;
 }
 /**
  * 預設計算關係度的方法
  * @param Annotation $annotation
  * @return Output_language_variable_collection
  */
 protected function _get_membership_default(Annotation $annotation)
 {
     //請由子類別來覆寫此方法
     $speech_array = $this->get_feature($annotation);
     $this->_CI_load('library', 'fuzzy/Output_language_variable_collection', 'output_language_variable_collection');
     $membership = new Output_language_variable_collection();
     $this->CI->config->load('kals');
     $membership_function_variables = $this->CI->config->item($this->name . '.membership_function_variables');
     $membership->set_memberships(array(1, 0, 0));
     foreach ($membership_function_variables as $speech => $ms_var) {
         if (in_array($speech, $speech_array)) {
             $var = $membership_function_variables[$speech];
             $membership->set_memberships($var);
             break;
         }
     }
     return $membership;
 }
 /**
  * 預設計算關係度的方法
  * @param Annotation $annotation
  * @return Output_language_variable_collection
  */
 protected function _get_membership_default(Annotation $annotation)
 {
     $location_array = $this->get_feature($annotation);
     $this->_CI_load('library', 'fuzzy/Output_language_variable_collection', 'output_language_variable_collection');
     $membership = new Output_language_variable_collection();
     $this->CI->config->load('kals');
     $membership_function_variables = $this->CI->config->item($this->name . '.membership_function_variables');
     $membership->set_memberships(array(1, 0, 0));
     if (is_null($location_array)) {
         return $menbership;
     }
     //test_msg('lang location ', array(count($location_array), $location_array[0], is_array($location_array)));
     foreach ($membership_function_variables as $location_id => $ms_var) {
         if (in_array($location_id, $location_array) === TRUE) {
             $membership->set_memberships($ms_var);
             break;
         }
     }
     return $membership;
 }
Example #4
0
 /**
  * 產生提示文字
  * @return String
  */
 public function _produce_tip_text()
 {
     //langvar.speech.tip.recommend_by_threshold
     $threshold = $this->CI->config->item('langvar.' . $this->langvar_name . '.tip.recommend_by_threshold');
     $membership_function = $this->_get_membership_function();
     $anchor_speech = $this->annotation->get_anchor_speech();
     $this->_CI_load('library', 'fuzzy/Output_language_variable_collection');
     $tip_text_header = $this->name . '.';
     $recommend_langs = array();
     foreach ($membership_function as $speech => $ms) {
         if (FALSE === in_array($speech, $anchor_speech)) {
             //先計算是否高過recommend_by_threshold
             $ms_langcoll = new Output_language_variable_collection();
             $ms_langcoll->set_memberships($ms);
             $score = $ms_langcoll->get_defuzzy_code();
             $lang = $this->lang->line($tip_text_header . $speech);
             //test_msg($score, array($this->langvar_name.'.tip.recommend_by_threshold', $lang, $threshold, ($score < $threshold)));
             if ($score < $threshold) {
                 break;
             }
             if (FALSE === $lang) {
                 continue;
             }
             $recommend_langs[] = $lang;
             if (count($recommend_langs) > 4) {
                 break;
             }
         }
     }
     $recommend = '';
     foreach ($recommend_langs as $lang) {
         if ($recommend != '') {
             $recommend .= $this->lang->line('tip.comma');
         }
         $recommend .= $lang;
     }
     $text = $this->lang->line($this->name . '.text', $recommend);
     return $text;
 }
 public function get_inference_result(Language_variable_collection $langvar_coll)
 {
     $output_coll = new Output_language_variable_collection();
     $weight_array = $langvar_coll->get_weight_array();
     $membership_matrix = $langvar_coll->get_membership_matrix();
     for ($x = 0; $x < $output_coll->get_number(); $x++) {
         $multiplication_array = array();
         for ($y = 0; $y < count($weight_array); $y++) {
             $r = $weight_array[$y] * $membership_matrix[$y][$x];
             $multiplication_array[] = $r;
         }
         $result = 0;
         for ($z = 0; $z < count($multiplication_array); $z++) {
             $result = $result + $multiplication_array[$z];
         }
         if ($result > 1) {
             $result = 1;
         }
         $output_coll->set_membership($x, $result);
     }
     return $output_coll;
 }
Example #6
0
 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;
 }