Upload layer1_spatial_encoding.py with huggingface_hub
Browse files- layer1_spatial_encoding.py +83 -0
layer1_spatial_encoding.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
from typing import Dict, Any, List
|
| 3 |
+
|
| 4 |
+
class SpatialEncodingEngine:
|
| 5 |
+
"""
|
| 6 |
+
質問テキストを、動的に読み込まれたドメインスキーマに基づいて
|
| 7 |
+
ドメイン固有の空間座標に変換する汎用エンジン。
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
def __init__(self, domain_schema: Dict[str, Any], ontology_interface):
|
| 11 |
+
"""
|
| 12 |
+
特定のドメインスキーマを元にエンジンを初期化します。
|
| 13 |
+
|
| 14 |
+
Args:
|
| 15 |
+
domain_schema (Dict[str, Any]): 対象ドメインのスキーマ。
|
| 16 |
+
ontology_interface: ドメインのオントロジーを検索するためのインターフェース。
|
| 17 |
+
"""
|
| 18 |
+
if not domain_schema:
|
| 19 |
+
raise ValueError("ドメインスキーマが提供されていません。")
|
| 20 |
+
self.schema = domain_schema
|
| 21 |
+
self.ontology = ontology_interface
|
| 22 |
+
self.keyword_map = self.schema.get("keyword_map", {})
|
| 23 |
+
|
| 24 |
+
def _extract_keywords(self, text: str) -> List[str]:
|
| 25 |
+
"""スキーマのキーワードマップに存在するキーワードをテキストから抽出する。"""
|
| 26 |
+
found_keywords = [kw for kw in self.keyword_map.keys() if kw in text]
|
| 27 |
+
return found_keywords
|
| 28 |
+
|
| 29 |
+
def extract_coordinates_from_question(self, question: str, default_coord=(50, 50, 50)) -> List[Dict[str, Any]]:
|
| 30 |
+
"""
|
| 31 |
+
質問から関連座標を抽出します。このメソッドはドメインに依存しない汎用的なロジックです。
|
| 32 |
+
"""
|
| 33 |
+
keywords = self._extract_keywords(question)
|
| 34 |
+
|
| 35 |
+
if not keywords:
|
| 36 |
+
# キーワードが見つからない場合はデフォルト座標を返す
|
| 37 |
+
return [{"keyword": "default", "concept": "default", "coordinate": default_coord, "confidence": 0.5}]
|
| 38 |
+
|
| 39 |
+
# 質問全体のコンテキストから各軸の主要な値を決定する
|
| 40 |
+
# 各軸について、最も頻繁に出現したキーワードの座標を採用する
|
| 41 |
+
coord = list(default_coord)
|
| 42 |
+
axis_map = {'x': 0, 'y': 1, 'z': 2}
|
| 43 |
+
|
| 44 |
+
for axis_name, axis_index in axis_map.items():
|
| 45 |
+
axis_keywords = [kw for kw in keywords if self.keyword_map[kw]['axis'] == axis_name]
|
| 46 |
+
if axis_keywords:
|
| 47 |
+
# ここでは単純に最初に見つかったキーワードを採用
|
| 48 |
+
coord[axis_index] = self.keyword_map[axis_keywords[0]]['coord']
|
| 49 |
+
|
| 50 |
+
# この実装では、質問から単一の代表的な座標を生成する
|
| 51 |
+
final_coord = tuple(coord)
|
| 52 |
+
|
| 53 |
+
# TODO: オントロジー検索と信頼度計算のロジックを統合
|
| 54 |
+
# 現状は、抽出された主要な座標のみを返す
|
| 55 |
+
return [{
|
| 56 |
+
"keywords_found": keywords,
|
| 57 |
+
"concept": "inferred_concept", # オントロジー検索で決定
|
| 58 |
+
"coordinate": final_coord,
|
| 59 |
+
"confidence": 0.75 # 信頼度は別途計算
|
| 60 |
+
}]
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
# --- 使用例 ---
|
| 64 |
+
if __name__ == '__main__':
|
| 65 |
+
from domain_manager import DomainManager
|
| 66 |
+
from mock_objects import MockOntology
|
| 67 |
+
# --- 実行例 ---
|
| 68 |
+
coord_system = MedicalSpaceCoordinateSystem()
|
| 69 |
+
ontology = MockOntology()
|
| 70 |
+
engine = SpatialEncodingEngine(coord_system, ontology)
|
| 71 |
+
|
| 72 |
+
question = "心筋梗塞の急性期診断について"
|
| 73 |
+
coords_info = engine.extract_coordinates_from_question(question)
|
| 74 |
+
print(f"質問: '{question}'")
|
| 75 |
+
print("抽出された座標情報:")
|
| 76 |
+
import json
|
| 77 |
+
print(json.dumps(coords_info, indent=2, ensure_ascii=False))
|
| 78 |
+
|
| 79 |
+
if coords_info:
|
| 80 |
+
palace_rep = MemoryPalaceRepresentation()
|
| 81 |
+
location = palace_rep.coordinate_to_palace_location(coords_info[0]['coordinate'])
|
| 82 |
+
print("\n宮殿内の場所:")
|
| 83 |
+
print(json.dumps(location, indent=2, ensure_ascii=False))
|