PIA_food_map / utils /parser.py
jsi6452's picture
feat]
ec2d80c
import json
from typing import Dict, List, Tuple
def load_config(config_path: str) -> Dict:
"""
JSON ์„ค์ • ํŒŒ์ผ์„ ์ฝ์–ด์„œ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
Args:
config_path (str): JSON ์„ค์ • ํŒŒ์ผ์˜ ๊ฒฝ๋กœ
Returns:
Dict: ์„ค์ • ์ •๋ณด๊ฐ€ ๋‹ด๊ธด ๋”•์…”๋„ˆ๋ฆฌ
"""
with open(config_path, 'r', encoding='utf-8') as f:
return json.load(f)
class PromptManager:
def __init__(self, config_path: str):
self.config = load_config(config_path)
self.sentences, self.index_mapping = self._extract_all_sentences_with_index()
self.reverse_mapping = self._create_reverse_mapping()
def _extract_all_sentences_with_index(self) -> Tuple[List[str], Dict]:
"""๋ชจ๋“  sentence์™€ ์ธ๋ฑ์Šค ๋งคํ•‘ ์ถ”์ถœ"""
sentences = []
index_mapping = {}
for event_idx, event_config in enumerate(self.config.get('PROMPT_CFG', [])):
prompts = event_config.get('prompts', {})
for status in ['normal', 'abnormal']:
for prompt_idx, prompt in enumerate(prompts.get(status, [])):
sentence = prompt.get('sentence', '')
sentences.append(sentence)
index_mapping[(event_idx, status, prompt_idx)] = sentence
return sentences, index_mapping
def _create_reverse_mapping(self) -> Dict:
"""sentence -> indices ์—ญ๋ฐฉํ–ฅ ๋งคํ•‘ ์ƒ์„ฑ"""
reverse_map = {}
for indices, sent in self.index_mapping.items():
if sent not in reverse_map:
reverse_map[sent] = []
reverse_map[sent].append(indices)
return reverse_map
def get_sentence_indices(self, sentence: str) -> List[Tuple[int, str, int]]:
"""ํŠน์ • sentence์˜ ๋ชจ๋“  ์ธ๋ฑ์Šค ์œ„์น˜ ๋ฐ˜ํ™˜"""
return self.reverse_mapping.get(sentence, [])
def get_details_by_sentence(self, sentence: str) -> List[Dict]:
"""sentence๋กœ ๋ชจ๋“  ๊ด€๋ จ ์ƒ์„ธ ์ •๋ณด ์ฐพ์•„ ๋ฐ˜ํ™˜"""
indices = self.get_sentence_indices(sentence)
return [self.get_details_by_index(*idx) for idx in indices]
def get_details_by_index(self, event_idx: int, status: str, prompt_idx: int) -> Dict:
"""์ธ๋ฑ์Šค๋กœ ์ƒ์„ธ ์ •๋ณด ์ฐพ์•„ ๋ฐ˜ํ™˜"""
event_config = self.config['PROMPT_CFG'][event_idx]
prompt = event_config['prompts'][status][prompt_idx]
return {
'event': event_config['event'],
'status': status,
'sentence': prompt['sentence'],
'top_candidates': event_config['top_candidates'],
'alert_threshold': event_config['alert_threshold'],
'event_idx': event_idx,
'prompt_idx': prompt_idx
}
def get_all_sentences(self) -> List[str]:
"""๋ชจ๋“  sentence ๋ฆฌ์ŠคํŠธ ๋ฐ˜ํ™˜"""
return self.sentences