安西番町薬局
| 名称 | 安西番町薬局 |
| 郵便番号 | 760-0017 |
| 住所 | 香川県高松市番町1丁目2番23号1階 |
| オンライン資格確認の運用開始日 | 2022/9/29 |
| 病院・診療所・薬局区分 | 薬局 |
| 都道府県 | 香川県 |
class SAP_Vector_Indexer { public static function build_index() { $post_types = get_option('sap_target_post_types', ['post']); $posts = get_posts([ 'post_type' => $post_types, 'numberposts' => -1, 'post_status' => 'publish' ]); $index = []; foreach ($posts as $post) { $content = strtolower( $post->post_title . ' ' . wp_strip_all_tags($post->post_excerpt . ' ' . mb_substr($post->post_content, 0, 500)) ); $tokens = preg_split('/[\s,。.、\.\-]+/u', $content); $freq = []; foreach ($tokens as $t) { if (mb_strlen($t) < 2) continue; $freq[$t] = ($freq[$t] ?? 0) + 1; } $index[$post->ID] = $freq; } update_option('sap_vector_index', $index, false); } } class SAP_BM25 { public static function score($query_terms, $index) { $N = count($index); $k1 = 1.5; $b = 0.75; $doc_lengths = []; $avgdl = 0; foreach ($index as $id => $vector) { $len = array_sum($vector); $doc_lengths[$id] = $len; $avgdl += $len; } $avgdl = $avgdl / max($N,1); $scores = []; foreach ($index as $id => $vector) { $score = 0; $dl = $doc_lengths[$id]; foreach ($query_terms as $term) { $df = 0; foreach ($index as $doc) { if (isset($doc[$term])) $df++; } if ($df == 0) continue; $idf = log(($N - $df + 0.5)/($df + 0.5) + 1); $tf = $vector[$term] ?? 0; $score += $idf * ( ($tf * ($k1 + 1)) / ($tf + $k1 * (1 - $b + $b * ($dl/$avgdl))) ); } if ($score > 0) $scores[$id] = $score; } arsort($scores); return $scores; } } class SAP_Medical_Classifier { public static function classify($query) { $map = [ 'ed' => '泌尿器科', '勃起' => '泌尿器科', '薄毛' => '皮膚科', 'aga' => '皮膚科', '不眠' => '心療内科', '動悸' => '循環器内科' ]; $query = strtolower($query); foreach ($map as $keyword => $dept) { if (strpos($query, $keyword) !== false) { return $dept; } } return null; } } class SAP_AI_Search { public static function search($query) { $expanded = SAP_Synonym::expand($query); $index = get_option('sap_vector_index', []); $scores = SAP_BM25::score($expanded, $index); return $scores; } } class SAP_REST { public static function init(){ register_rest_route('sap/v1','/ai-search',[ 'methods'=>'GET', 'callback'=>[__CLASS__,'search'], 'permission_callback'=>'__return_true' ]); } public static function search($req){ $query = sanitize_text_field($req['q']); $cache_key = 'sap_ai_'.md5($query); if($cached = get_transient($cache_key)){ return $cached; } $results = SAP_AI_Search::search($query); $response = [ 'query'=>$query, 'department'=>SAP_Medical_Classifier::classify($query), 'results'=>$results ]; set_transient($cache_key,$response,600); return $response; } }