Upload app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980import randomfrom io import BytesIOfrom datetime import date, datetime
import streamlit as stfrom openpyxl import Workbookfrom openpyxl.styles import Font, Alignment, Border, Side, PatternFillfrom openpyxl.utils import get_column_letter
st.set_page_config(page_title="์๋ด์ผ์ง", page_icon="๐", layout="wide")
st.markdown("""<style>.block-container { padding-top: 1.5rem; max-width: 1100px; }.hero {background: linear-gradient(135deg,#2E7D32,#66BB6A); padding:28px; border-radius:22px; text-align:center; margin-bottom:20px;}.hero h1 {color:white; margin:0; font-size:38px;}.hero p {color:white; margin-top:10px; font-size:18px;}.notice {background:#fff8e1; border-left:7px solid #f9a825; padding:14px; border-radius:12px; margin-bottom:18px; line-height:1.7;}.okbox {background:#e8f5e9; border-left:7px solid #43a047; padding:14px; border-radius:12px; margin:10px 0;}.warnbox {background:#fff3e0; border-left:7px solid #fb8c00; padding:14px; border-radius:12px; margin:10px 0;}</style>""", unsafe_allow_html=True)
st.markdown("""<div class="hero"> <h1>๐ ์๋ด์ผ์ง ์์ฑ๊ธฐ</h1> <p>๋น ๋ฅธ ์ง์ ์
๋ ฅ ๋ฒ์ ์
๋๋ค. ์๋ด ๊ณผ์ ๊ณผ ํ์ธ ์ ์ฐจ๊ฐ ํฌํจ๋๋๋ก ๋ฌธ์๋ฅผ ์์ฑํฉ๋๋ค.</p></div>""", unsafe_allow_html=True)
st.markdown("""<div class="notice"><strong>์ถ์ ํ
์คํธ์ฉ:</strong> ์๋์ ๋ฌธ์ ํ์ง์ ๋จผ์ ํ์ธํ๊ธฐ ์ํด ์๊ธ์ DB ์๋์กฐํ ๊ธฐ๋ฅ์ ์ ์ธํ์ต๋๋ค. ์ง์ ์
๋ ฅ ํ ์๋ด ๊ณผ์ ยทํ์ธ ๋ด์ฉยท์กฐ์น๊ณํ์ด ํฌํจ๋ ์๋ด์ผ์ง๋ฅผ ์์ฑํฉ๋๋ค.</div>""", unsafe_allow_html=True)
INSTITUTION_GUIDES = { "๋ฐฉ๋ฌธ์์": ["์๋น์ค ์๊ฐ", "์์๋ณดํธ์ฌ ๋ณ๊ฒฝ", "๊ฐ์ฌ์ง์ ๋ฒ์", "์ธ์งํ๋", "๋ณต์ฝํ์ธ", "๋์์๋ฐฉ"], "๋ฐฉ๋ฌธ๋ชฉ์": ["๋ชฉ์ ์ ํ ๊ฑด๊ฐ์ํ", "ํผ๋ถ์ํ", "๋์์๋ฐฉ", "์ด๋ ๋ณด์กฐ", "๋ชฉ์ ๊ฑฐ๋ถ๊ฐ", "๋ณดํธ์ ํ์กฐ"], "๋ฐฉ๋ฌธ๊ฐํธ": ["ํ์/ํ๋น ๊ด์ฐฐ", "์์ฒ๊ด๋ฆฌ", "ํฌ์ฝ๊ด๋ฆฌ", "๊ฑด๊ฐ์๋ด", "์๋ฃ๊ธฐ๊ด ์ฐ๊ณ", "๊ฐํธ์ฒ์น"], "์ฃผ๊ฐ๋ณดํธ": ["์ก์", "ํ๋ก๊ทธ๋จ ์ฐธ์ฌ", "์์ฌ", "๋ฐฐ๋ณ", "๋์์๋ฐฉ", "์ ์์ง์", "๊ฐ์กฑ์ํต"], "์์์": ["์
์์ํ ์ ์", "์์ฌ/์๋ฉด/๋ฐฐ๋ณ", "์์ฐฝ์๋ฐฉ", "๋์์๋ฐฉ", "ํ๋ก๊ทธ๋จ ์ฐธ์ฌ", "๊ฐ์กฑ๋ฉดํ"], "๊ณต๋์ํ๊ฐ์ ": ["๊ณต๋์ํ ์ ์", "์ ์์ง์", "๊ฐ๋ณ ์๊ตฌ", "๊ฐ์กฑ์ํต", "ํ๋ก๊ทธ๋จ ์ฐธ์ฌ", "์์ ๊ด๋ฆฌ"],}
TYPE_GUIDES = { "์๊ธ์์๋ด": { "focus": "์๊ธ์์ ๊ฑด๊ฐ์ํ, ์ ์์ํ, ์ผ์์ํ ๋ถํธ๊ฐ, ์๋น์ค ๋ง์กฑ๋, ์ํ๋ณํ๋ฅผ ์ค์ฌ์ผ๋ก ์๋ด", "action": "์๊ธ์ ์๊ตฌ๋ฅผ ๊ธ์ฌ์ ๊ณต๊ณํ๊ณผ ์ผ์ผ ์๋น์ค ์ ๊ณต ๋ด์ฉ์ ๋ฐ์", }, "๋ณดํธ์์๋ด": { "focus": "๋ณดํธ์์ ์๊ฒฌ, ๊ฐ์กฑ ์์ฒญ์ฌํญ, ์ํ๋ณํ ์ ๋ฌ, ์๋น์ค ์กฐ์ ํ์์ฑ์ ์ค์ฌ์ผ๋ก ์๋ด", "action": "๋ณดํธ์ ์๊ฒฌ์ ๊ธฐ๋กํ๊ณ ํ์ํ ๊ฒฝ์ฐ ๊ธ์ฌ์ ๊ณต๊ณํ ๋ณ๊ฒฝ ๋๋ ์ง์ ๊ณต์ ", }, "์ข
์ฌ์์๋ด": { "focus": "์๋น์ค ์ ๊ณต ์ค ๊ด์ฐฐ์ฌํญ, ์
๋ฌด ๊ณ ์ถฉ, ์๊ธ์ ์ํ ๋ณํ, ์์ ๊ด๋ฆฌ ํ์์ฑ์ ์ค์ฌ์ผ๋ก ์๋ด", "action": "์ข
์ฌ์ ๊ด์ฐฐ ๋ด์ฉ์ ๋ฐ์ํ์ฌ ์๋น์ค ๋ฐฉ๋ฒ์ ์กฐ์ ํ๊ณ ํ์ ์ ๊ต์กยทํ์ ์๊ฑด์ผ๋ก ์ฐ๊ฒฐ", }, "๊ธฐํ์๋ด": { "focus": "๊ธฐ๊ด ์ด์์ ํ์ํ ์๋ด, ์ง์ญ์์ ์ฐ๊ณ, ๋ฏผ์ ์๋ฐฉ, ํน์ด์ฌํญ ํ์ธ์ ์ค์ฌ์ผ๋ก ์๋ด", "action": "์๋ด ๋ด์ฉ์ ๊ธฐ๊ด ๊ธฐ๋ก์ผ๋ก ๋จ๊ธฐ๊ณ ํ์์กฐ์น ๋ด๋น์์ ๊ธฐํ์ ์ค์ ", },}
QA_RULES = { "์๊ธ์์๋ด": [ "ํ์ฌ ๊ฐ์ฅ ๋ถํธํ๊ฑฐ๋ ํ๋ ์ ์ ๋ฌด์์
๋๊น?", "์ต๊ทผ ๊ฑด๊ฐ์ํ, ํต์ฆ, ์ด์ง๋ผ, ์์ฌ๋, ์๋ฉด, ๋ฐฐ๋ณยท๋ฐฐ๋จ์ ๋ณํ๊ฐ ์์์ต๋๊น?", "์๋น์ค ์ ๊ณต ๊ณผ์ ์์ ๋ง์กฑํ๋ ์ ๊ณผ ๋ถํธํ ์ ์ ๋ฌด์์
๋๊น?", "์์๋ณดํธ์ฌ ๋๋ ๊ธฐ๊ด์ ์ถ๊ฐ๋ก ์์ฒญํ๊ณ ์ถ์ ๋์์ด ์์ต๋๊น?", "๋์, ์์ฐฝ, ์ธ์งํผ๋, ์ ์๋ถ์ ๋ฑ ์์ ์ ๊ฑฑ์ ๋๋ ์ผ์ด ์์์ต๋๊น?", ], "๋ณดํธ์์๋ด": [ "๋ณดํธ์๊ฐ ๋ณด๊ธฐ์ ์๊ธ์์ ์ต๊ทผ ๊ฑด๊ฐ์ํ๋ ์ํ์ํ ๋ณํ๋ ๋ฌด์์
๋๊น?", "๊ฐ์กฑ์ด ๊ฑฑ์ ํ๋ ์ํ์์ธ์ด๋ ๋ณ์์ง๋ฃ ํ์์ฑ์ด ์์ต๋๊น?", "ํ์ฌ ์๋น์ค ์๊ฐ, ์ ๊ณต๋ด์ฉ, ์ข
์ฌ์ ๋ฐฐ์น์ ์กฐ์ ์ด ํ์ํฉ๋๊น?", "๋ณดํธ์๊ฐ ๊ธฐ๊ด์ ์์ฒญํ๊ฑฐ๋ ๊ณต์ ํ๊ณ ์ถ์ ์ฌํญ์ ๋ฌด์์
๋๊น?", "๊ธ์ฌ์ ๊ณต๊ณํ์ ๋ฐ์ํด์ผ ํ ๊ฐ์กฑ ์๊ฒฌ์ด ์์ต๋๊น?", ], "์ข
์ฌ์์๋ด": [ "์๋น์ค ์ ๊ณต ์ค ๊ด์ฐฐํ ์๊ธ์์ ์ฃผ์ ๋ณํ๋ ๋ฌด์์
๋๊น?", "๋์, ์์ฐฝ, ์์ฌ๋ ๊ฐ์, ์ธ์งํผ๋, ํญ์ธยท๊ฑฐ๋ถ ๋ฑ ์ํ ์งํ๊ฐ ์์์ต๋๊น?", "ํ์ฌ ์ ๊ณต ๋ฐฉ๋ฒ ์ค ์กฐ์ ์ด ํ์ํ ๋ถ๋ถ์ ๋ฌด์์
๋๊น?", "์
๋ฌด ์ํ ์ค ์ด๋ ค์์ด๋ ์ถ๊ฐ ๊ต์กยท๏ฟฝ๏ฟฝ๏ฟฝ์๊ฐ ํ์ํ ๋ด์ฉ์ ๋ฌด์์
๋๊น?", "๋ณดํธ์ ๋๋ ๋ด๋น์์๊ฒ ์ฆ์ ๊ณต์ ํด์ผ ํ ์ฌํญ์ด ์์ต๋๊น?", ], "๊ธฐํ์๋ด": [ "์๋ด์ด ํ์ํ ์ฃผ์ ์ํฉ์ ๋ฌด์์
๋๊น?", "์๊ธ์, ๋ณดํธ์, ์ข
์ฌ์ ์ค ๋๊ตฌ์ ๊ด๋ จ๋ ๋ด์ฉ์
๋๊น?", "๊ธฐ๊ด ์ฐจ์์ ์กฐ์น๋ ์ธ๋ถ ์ฐ๊ณ๊ฐ ํ์ํ ๋ถ๋ถ์ ๋ฌด์์
๋๊น?", "ํ์ ํ์ธ์ด ํ์ํ ์์ ๊ณผ ๋ด๋น์๋ ๋๊ตฌ์
๋๊น?", ],}
STATUS_CHANGE_KEYWORDS = ["๋์", "์์ฐฝ", "๋ฐ์ ", "์์ฒ", "์ด์ง", "ํต์ฆ", "์์ฌ๋", "์ฌ๋ ", "์ฐํ", "์๋ฉด", "๋ฐฐ๋ณ", "๋ฐฐ๋จ", "์ค๊ธ", "๊ธฐ์ ๊ท", "๋ณดํ", "ํ์ฒญ", "์ธ์ง", "ํผ๋", "๋ถ์", "์ฐ์ธ", "์
์", "ํด์"]CASE_RISK_KEYWORDS = ["๋ฐ๋ณต", "์ง์", "์
ํ", "๊ณต๊ฒฉ", "ํญ์ธ", "๊ฑฐ๋ถ", "๋ง์", "๋ฐฐํ", "๊ฐ๋ฑ", "๋คํผ", "ํ๋", "๋ฐฉ์", "์ํด", "์ํ", "์
์", "์๊ธ", "๋ณดํธ์ ๋ถ์ฌ"]CARE_PLAN_KEYWORDS = ["์๋น์ค", "์๊ฐ", "๋ณ๊ฒฝ", "์กฐ์ ", "์ถ๊ฐ", "์์ฒญ", "๋ณ์", "๋ํ", "์์ฌ", "์ด๋", "๋ชฉ์", "๋ณต์ฝ", "ํ๋ก๊ทธ๋จ", "๊ธ์ฌ", "๊ณํ"]
FALL_KEYWORDS = ["๋์", "๋์ด", "๋ฏธ๋", "์ด์ง", "ํ๊ธฐ", "ํ์ฒญ", "๋ณดํ", "๋ถ์ถ", "์งํก", "์์ปค", "ํ ์ฒด์ด", "๋ฌธํฑ", "์กฐ๋ช
", "์ผ๊ฐ", "ํ์ฅ์ค", "์ก์"]PRESSURE_INJURY_KEYWORDS = ["์์ฐฝ", "๋ฐ์ ", "์์ฒ", "ํผ๋ถ", "์ง๋ฌด", "์ผ์ถ", "๋๋ ์ฑ", "์ฒด์", "์์", "์นจ์", "ํ ์ฒด์ด", "ํต์ฆ", "์ด๊ฐ", "์์", "ํ์"]DEMENTIA_KEYWORDS = ["์น๋งค", "์ธ์ง", "๊ธฐ์ต", "๋ฐ๋ณต", "๋ฐฐํ", "๋ง์", "ํ๊ฐ", "๋ถ์", "์ด์กฐ", "๊ณต๊ฒฉ", "๊ฑฐ๋ถ", "๊ท๊ฐ", "์ง์", "๊ณ ์ง", "ํผ๋", "์ฌ๋ง"]GUARDIAN_BURDEN_KEYWORDS = ["ํ๋ค", "์์ง", "๋ถ๋ด", "์ง์นจ", "ํผ์", "๊ฑฑ์ ", "๋ถ์", "์์ฒญ", "ํ๋", "๋ณ๊ฒฝ", "๋ํ", "์๋ด"]
RESTRICTED_REPLACEMENTS = { "์ด์ ๊ณผ ๋์ผ": "๊ธ๊ฒฉํ ์
ํ ์๊ฒฌ์ ์ ํ์ ์ด๋ ๊ธฐ์กด ๊ด์ฐฐ ํญ๋ชฉ์ ์ ์ง ์ฌ๋ถ๋ฅผ ๊ตฌ์ฒด์ ์ผ๋ก ์ฌํ์ธํจ", "์ํ ์ํธ": "์์ฌ, ์ด๋, ์ ์ ํํ ๋ฑ ์ผ์ ๊ธฐ๋ฅ์ด ๋น๊ต์ ์์ ์ ์ผ๋ก ์ ์ง๋๋ ๊ฒ์ผ๋ก ํ์ธํจ", "ํน์ด์ฌํญ ์์": "์๋น์ค ์ด์ฉ ์ค ๊ธ๊ฒฉํ ์ฌ์ ๋ณํ ์์ธ์ ํฌ์ฐฉ๋์ง ์์์ผ๋ ์ ๊ธฐ ๊ด์ฐฐ์ ์ง์ํ๊ธฐ๋ก ํจ", "๋ณ์ผ ์์": "์๋ด ์์ ์์ ์ฆ์ ๊ฐ์
์ด ํ์ํ ๋๋ฐ ์ํฉ์ ํ์ธ๋์ง ์์์ผ๋ ๋ค์ ๊ธ์ฌ ์ ๊ณต ์ ์ฌํ์ธํ๊ธฐ๋ก ํจ", "๋ฌธ์ ์์": "ํ์ฌ ํ์ธ๋ ์ํ์์ธ์ ์ ํ์ ์ด๋ ์๋ด๋์๋ณ ๊ด์ฐฐ ํญ๋ชฉ์ ๊ธฐ์ค์ผ๋ก ์ง์ ์ ๊ฒํ๊ธฐ๋ก ํจ",}
INSTITUTION_SOAP_GUIDES = { "๋ฐฉ๋ฌธ์์": { "focus": "๊ฐ์ ๋ด ์์กด ๊ธฐ๋ฅ ์ ์ง, ์ฃผ๊ฑฐํ๊ฒฝ ๋์ ์ํ, ๋ณดํธ์ ๋ถ์ฌ์๊ฐ, ๋น๊ธ์ฌ์ฑ ์๊ตฌ์ ๊ธ์ฌ์ ๊ณต ๋ฒ์ ์กฐ์จ", "plan": "๊ฐ์ ๋ด ์ด๋ ๋์ ๊ณผ ํ์ฅ์ค ์ด์ฉ ํ๊ฒฝ์ ์ ๊ฒํ๊ณ ๊ธ์ฌ์ ๊ณต๊ณํ ๋ฒ์ ์์์ ์ ์ฒดํ๋ ๋ฐ ์ ์์ง์ ๋ฐฉ๋ฒ์ ์กฐ์ ํจ", }, "๋ฐฉ๋ฌธ๋ชฉ์": { "focus": "๋ชฉ์ ์ ํ ๊ฑด๊ฐ์ํ, ํผ๋ถ ๋ฐ์ , ๊ฐ์ผ์ฑ ํผ๋ถ์งํ, ์ด๋ยท๋ฏธ๋๋ผยทํ์ ์๋ฐฉ, ๋ชฉ์ ๊ฑฐ๋ถ๊ฐ", "plan": "๋ชฉ์ ์ ํ ์ปจ๋์
๊ณผ ํผ๋ถ์ํ๋ฅผ ํ์ธํ๊ณ ๋ฏธ๋๋ผ ๋ฐฉ์ง, ์ฒด์จ ๋ณํ, ํผ๋ถ ๋ง์ฐฐ ์ต์ํ ์กฐ์น๋ฅผ ์ ์ฉํจ", }, "๋ฐฉ๋ฌธ๊ฐํธ": { "focus": "๋ง์ฑ์งํ ๊ด๋ฆฌ, ๋ณต์ฝ ์ดํ, ๊ฐํธ์ฒ์น ์ํ, ๋ฐฉ๋ฌธ๊ฐํธ์ง์์ ์ ํจ์ฑ, ๋ณดํธ์์ ์ฒ์น ์ดํด๋", "plan": "ํ๋ ฅ์งํ์ ์ฒ์น ์ํ๋ฅผ ํ์ธํ๊ณ ๋ณต์ฝยท์์ฒยท๊ด ์ฝ์
๊ด๋ฆฌ ํ์ ์ ์๋ฃ๊ธฐ๊ด ๋๋ ๋ณดํธ์์ ์ฐ๊ณํจ", }, "์ฃผ๊ฐ๋ณดํธ": { "focus": "์ก์ ์์ , ๋ฑ์ ๊ฑฐ๋ถ, ํ๋ก๊ทธ๋จ ์ฐธ์ฌ๋, ์์ฌยท์์ ์ฐธ์ฌ, ๋ณดํธ์ ๋ถ์ ๋ถ๋ด ๊ฒฝ๊ฐ", "plan": "์ก์ ๋ฐ ์ผํฐ ๋ด ์ด๋ ๋์ ์ ์ ๊ฒํ๊ณ ํ๋ก๊ทธ๋จ ์ฐธ์ฌ ์์ค๊ณผ ๋ณดํธ์ ๋ถ๋ด ๋ณํ๋ฅผ ํจ๊ป ํ์ธํจ", }, "์์์": { "focus": "์
์์ํ ์ ์, ๊ณต๋์ํ ๊ฐ๋ฑ, ์์ฌยท์๋ฉดยท๋ฐฐ๋ณ, ์์ฐฝยท๋์ ์๋ฐฉ, ๋ณดํธ์ ์๋ด ๋ฐ์", "plan": "๊ณต๋์ํ ์ ์๊ณผ ์ ์ฒดยท์ ์ ์ํ๋ฅผ ๊ด์ฐฐํ๊ณ ๋ณดํธ์ ๋ฉดํยท์๋ด ๋ด์ฉ์ ์์ค ์ผ์ด๊ณํ์ ๋ฐ์ํจ", }, "๊ณต๋์ํ๊ฐ์ ": { "focus": "์๊ท๋ชจ ๊ณต๋์ฒด ๊ด๊ณ, ์๊ธฐ๊ฒฐ์ ๊ถ, ๊ฐ๋ณ ์ํ์ต๊ด, ๊ฐ์ ํ ํ๊ฒฝ ์ ์ง, ์ค๋ด ์ด๋ ๋์ ์์ ", "plan": "๊ฐ๋ณ ์ํ์ต๊ด๊ณผ ๊ณต๋์ํ ๊ฐ๋ฑ์์ธ์ ์กฐ์จํ๊ณ ์๊ท๋ชจ ํ๊ฒฝ์์์ ์ ์ ์์ ๋ฐ ์์ ๊ด๋ฆฌ ๊ณํ์ ์๋ฆฝํจ", },}
SUPPORT_METHOD_RULES = { "์ด๋์ง์": { "keywords": ["์ด๋", "๋ณดํ", "๋ถ์ถ", "ํ ์ฒด์ด", "์งํก", "์์ปค", "ํ์ฅ์ค", "๋์", "ํ์ฒญ", "์ด์ง"], "items": [ "์นจ์๊ณผ ํ ์ฒด์ด ์ด๋ ์ ๋ถ์ถ์ ์ ๊ณตํจ", "๋ณดํ๊ธฐ ๋ฐ ์งํก์ด ์ฌ์ฉ์ ์ง์ํจ", "์ค๋ด ์ด๋ ์ ์์ ์ ํ์ธํจ", "์ด๋ ๋์ ์ ์ ๋ฆฌํ์ฌ ๋์์ ์๋ฐฉํจ", "ํ์ฅ์ค ์ด๋ ์ ๋ํ ๋ฐ ์์ ๏ฟฝ๏ฟฝ์ฐฐ์ ์ค์ํจ", ], }, "์ผ์์ํ์ง์": { "keywords": ["์์", "์ธ๋ฉด", "๊ตฌ๊ฐ", "์๋ณต", "์ฐฉํ", "์๋ฐํฑ", "๋ชฉ์", "ํผ๋ถ"], "items": [ "์ธ๋ฉด ๋ฐ ๊ตฌ๊ฐ๊ด๋ฆฌ๋ฅผ ์ง์ํจ", "์๋ณต ์ฐฉํ์๋ฅผ ์ง์ํจ", "๊ฐ์ธ์์ ์ํ๋ฅผ ์ ๊ฒํจ", "์๋ฐํฑ ์ํ๋ฅผ ํ์ธํ๊ณ ์์๊ด๋ฆฌ๋ฅผ ์ง์ํจ", "๋ชฉ์ ์ ํ ๊ฑด๊ฐ์ํ์ ํผ๋ถ์ํ๋ฅผ ํ์ธํจ", ], }, "๋์์๋ฐฉ ์ง์": { "keywords": FALL_KEYWORDS, "items": [ "๋ณดํ ์ ๋ถ์ถ์ ์ ๊ณตํจ", "๋ฏธ๋๋ผ ๋ฐฉ์ง ํ๊ฒฝ์ ์ ๊ฒํจ", "์นจ์ ๋๊ฐ ์ํ๋ฅผ ํ์ธํจ", "์ผ๊ฐ ์ด๋ ์ ์์ ๊ด์ฐฐ์ ์ค์ํจ", "์ด๋ ์ ์ด์ง๋ผ ์ฌ๋ถ๋ฅผ ํ์ธํจ", "๋ณดํ๋ณด์กฐ๊ธฐ ์ฌ์ฉ์ ์๋ดํจ", ], }, "์์ฐฝ์๋ฐฉ ์ง์": { "keywords": PRESSURE_INJURY_KEYWORDS, "items": [ "์ ๊ธฐ์ ์ฒด์๋ณ๊ฒฝ์ ์ค์ํจ", "ํผ๋ถ์ํ๋ฅผ ํ์ธํจ", "์์ฐฝ ๋ฐ์ ์ฌ๋ถ๋ฅผ ๊ด์ฐฐํจ", "์์์ํ๋ฅผ ํ์ธํจ", "์ฅ์๊ฐ ๋์ผ ์์ธ๋ฅผ ์๋ฐฉํจ", "ํผ๋ถ ์ฒญ๊ฒฐ์ ์ ์งํจ", ], }, "์ธ์ง์ง์": { "keywords": DEMENTIA_KEYWORDS + ["์ธ์ง์ ํ", "๊ธฐ์ต์ ํ", "์ผ์ ํ", "์์ ์์น"], "items": [ "๋ ์ง์ ์ฅ์๋ฅผ ๋ฐ๋ณต ์๋ดํจ", "ํ์๋ํ๋ฅผ ์ค์ํจ", "์ต์ํ ํ๊ฒฝ์ ์ ์งํ๋๋ก ์ง์ํจ", "์ธ์ง์๊ทน ํ๋์ ์ ๊ณตํจ", "์ผ์ ํ๋ฅผ ํ์ฉํ์ฌ ์๋ดํจ", "์์ ์์น์ ๋ฐ๋ณต ๊ต์กํจ", ], }, "BPSD ์ง์": { "keywords": ["๋ฐ๋ณต์ง๋ฌธ", "๋ง์", "๋ฐฐํ", "๊ณต๊ฒฉ", "ํญ์ธ", "๊ท๊ฐ", "ํ๊ฐ", "๊ฑฐ๋ถ", "๋ถ์", "์ด์กฐ"], "items": [ "๋์ผ ์ง๋ฌธ์ ์์ ์ ์ผ๋ก ์๋ํจ", "์๊ฐ์๋ฃ๋ฅผ ํ์ฉํ์ฌ ์ค๋ช
ํจ", "์ ๋ฉด ๋ฐ๋ฐ์ ์ง์ํจ", "๊ฐ์ ์ ๊ณต๊ฐํจ", "ํ์ ๋ฅผ ์ ํํจ", "์์ ํ ์ด๋์ ์ง์ํจ", "์๊ทน์์ธ์ ์ ๊ฑฐํ๊ณ ์ฐจ๋ถํ ํ๊ฒฝ์ ์ ๊ณตํจ", ], }, "์ ์์ง์": { "keywords": ["์ธ๋ก์", "์ฐ์ธ", "๋ถ์", "๋ฌด๊ธฐ๋ ฅ", "์์ธ", "๋๋ฌผ", "์ง์ฆ", "์ ์", "๊ฐ์กฑ", "๊ฑฑ์ "], "items": [ "๋ง๋ฒ ์๋น์ค๋ฅผ ์ ๊ณตํจ", "๊ณต๊ฐ์ ๊ฒฝ์ฒญ์ ์ค์ํจ", "๊ธ์ ์ ํผ๋๋ฐฑ์ ์ ๊ณตํจ", "์ทจ๋ฏธํ๋ ์ฐธ์ฌ๋ฅผ ์ง์ํจ", "์ฑ์ทจ๊ฐ์ ๊ฒฝํํ๋๋ก ์ง์ํจ", "์์ ์ ์ธ ํ๊ฒฝ์ ์ ๊ณตํจ", "๊ฐ์กฑ ์ฐ๋ฝ์ ์ง์ํจ", ], }, "์ฌํ์ฐธ์ฌ์ง์": { "keywords": ["๊ณ ๋ฆฝ", "ํ๋ก๊ทธ๋จ", "๊ด๊ณ", "๋์ธ", "์ฐธ์ฌ", "ํ๋", "๋ง๋คํผ", "๊ฐ๋ฑ"], "items": [ "์ง๋จ ํ๋ก๊ทธ๋จ ์ฐธ์ฌ๋ฅผ ์ง์ํจ", "๋๋ ๊ด๊ณ ํ์ฑ์ ์ง์ํจ", "์ง์ญ์ฌํ ํ๋ ์ฐธ์ฌ๋ฅผ ์ฐ๊ณํจ", "๋ํ ๊ธฐํ๋ฅผ ์ ๊ณตํจ", "๊ณต๋ํ๋ ์ฐธ์ฌ๋ฅผ ์ ๋ํจ", ], }, "์์์ง์": { "keywords": ["์์ฌ", "์์", "์ฒด์ค", "์์", "์๋ถ", "ํ์", "๊ธฐํธ์", "์ ํธ ์์"], "items": [ "์ ํธ ์์์ ํ์ธํจ", "๊ธฐํธ์์ ๋ฐ์ํจ", "์๋ถ์ญ์ทจ๋ฅผ ๋
๋ คํจ", "์์์ํ๋ฅผ ๊ด์ฐฐํจ", "์ฒด์ค๋ณํ๋ฅผ ํ์ธํจ", "์์๋ณด์ถฉ์ ์ง์ํจ", ], }, "๋ฐฐ๋ณ๋ฐฐ๋จ์ง์": { "keywords": ["๋ฐฐ๋ณ", "๋ฐฐ๋จ", "๋ณ๋น", "์์ค๊ธ", "์ค๊ธ", "๊ธฐ์ ๊ท", "ํ์ฅ์ค"], "items": [ "์๋ถ์ญ์ทจ๋ฅผ ๋
๋ คํจ", "๊ท์น์ ๋ฐฐ๋ณ์ต๊ด์ ์ง์ํจ", "ํ๋๋ ์ฆ๊ฐ๋ฅผ ์ ๋ํจ", "๋ฐฐ๋ณ๋ฐฐ๋จ ํจํด์ ํ์ธํจ", "์ ๊ธฐ ๋ณ๊ธฐ์ ๋๋ฅผ ์ค์ํจ", "์์๊ด๋ฆฌ๋ฅผ ์ง์ํจ", ], }, "๋ณต์ฝ์ง์": { "keywords": ["๋ณต์ฝ", "์ฝ", "ํฌ์ฝ", "์ฝ๋ฌผ", "๋ณต์ฉ", "ํ์", "ํ๋น"], "items": [ "๋ณต์ฝ ์ฌ๋ถ๋ฅผ ํ์ธํจ", "์ฝ๋ฌผ ๋ณด๊ด์ํ๋ฅผ ์ ๊ฒํจ", "๋ณดํธ์์ ๋ณต์ฝ ์ ๋ณด๋ฅผ ๊ณต์ ํจ", "๋ณต์ฝ์ผ์ง๋ฅผ ํ์ธํจ", "ํฌ์ฝ์๊ฐ์ ์๋ดํจ", ], }, "๋ณดํธ์์ง์": { "keywords": GUARDIAN_BURDEN_KEYWORDS + ["๋ณดํธ์", "๊ฐ์กฑ", "๊ฐ์กฑ๊ฐ๋ฑ"], "items": [ "๋ณดํธ์ ์๋ด์ ์ค์ํจ", "๋๋ด ๋ถ๋ด์ ๊ฒฝ์ฒญํจ", "์ง์ญ์์ ์ฐ๊ณ๋ฅผ ์๋ดํจ", "๊ฐ์กฑ ์๊ฒฌ์ ์กฐ์ ํจ", "์์ฌ์ํต์ ์ง์ํจ", ], }, "์ง์ญ์์ ์ฐ๊ณ": { "keywords": ["์น๋งค", "๊ฑด๊ฐ๊ด๋ฆฌ", "๊ฒฝ์ ", "๊ณ ๋ฆฝ", "๋ณ์", "๋ณด๊ฑด์", "๋ณต์ง๊ด", "ํ์ ๋ณต์ง์ผํฐ", "์ง์ญ์๏ฟฝ๏ฟฝ๏ฟฝ", "๋ฐฉ๋ฌธ๊ฐํธ"], "items": [ "์น๋งค์์ฌ์ผํฐ ์ฐ๊ณ๋ฅผ ๊ฒํ ํจ", "๋ฐฉ๋ฌธ๊ฐํธ ์๋น์ค๋ฅผ ์ฐ๊ณํจ", "๋ณด๊ฑด์ ํ๋ก๊ทธ๋จ์ ์๋ดํจ", "ํ์ ๋ณต์ง์ผํฐ ์ฐ๊ณ๋ฅผ ๊ฒํ ํจ", "๋ณต์ง๊ด ํ๋ก๊ทธ๋จ ์ฐธ์ฌ๋ฅผ ์ฐ๊ณํจ", ], },}
def _join_qna_answers(qna_answers): if not qna_answers: return "" lines = [] for question, answer in qna_answers.items(): answer = _clean_text(answer) if answer: lines.append(f"Q. {question}\nA. {answer}") return "\n".join(lines)
def _contains_keywords(text, keywords): source = str(text or "") return any(keyword in source for keyword in keywords)
def _replace_restricted_phrases(text): result = str(text or "") for bad, good in RESTRICTED_REPLACEMENTS.items(): result = result.replace(bad, good) return result
def _formalize_sentence(text): cleaned = _replace_restricted_phrases(_clean_text(text)) if not cleaned: return "" if cleaned.endswith(("ํจ", "์", "๋จ", "ํ์ธ๋จ", "ํ๋จ๋จ", "์ฌ์ ๋จ", "์์", "์์", "ํ์ฌ", "๋ง์ํ์ฌ", ".")): return cleaned.rstrip(".") return f"{cleaned}๋ก ํ์ธ๋จ"
def _risk_flag(text, keywords): return _contains_keywords(text, keywords)
def build_individual_support_methods(data, specialized=None): qna_text = _join_qna_answers(data.get("qna_answers", {})) combined = " ".join([ data.get("institution_type", ""), data.get("care_grade", ""), data.get("main_condition", ""), data.get("cognitive_status", ""), data.get("mobility_status", ""), data.get("diet_status", ""), data.get("toileting_status", ""), data.get("client_status_change", ""), data.get("guardian_opinion", ""), data.get("staff_observation", ""), data.get("custom_need", ""), qna_text, ])
selected = {} for category, rule in SUPPORT_METHOD_RULES.items(): if _contains_keywords(combined, rule["keywords"]): selected[category] = rule["items"][:3]
if specialized: if specialized.get("fall"): selected.setdefault("๋์์๋ฐฉ ์ง์", SUPPORT_METHOD_RULES["๋์์๋ฐฉ ์ง์"]["items"][:3]) if specialized.get("pressure"): selected.setdefault("์์ฐฝ์๋ฐฉ ์ง์", SUPPORT_METHOD_RULES["์์ฐฝ์๋ฐฉ ์ง์"]["items"][:3]) if specialized.get("dementia"): selected.setdefault("์ธ์ง์ง์", SUPPORT_METHOD_RULES["์ธ์ง์ง์"]["items"][:3]) selected.setdefault("BPSD ์ง์", SUPPORT_METHOD_RULES["BPSD ์ง์"]["items"][2:5]) if specialized.get("guardian_burden"): selected.setdefault("๋ณดํธ์์ง์", SUPPORT_METHOD_RULES["๋ณดํธ์์ง์"]["items"][:3])
if not selected: selected = { "์ด๋์ง์": SUPPORT_METHOD_RULES["์ด๋์ง์"]["items"][:2], "์ ์์ง์": SUPPORT_METHOD_RULES["์ ์์ง์"]["items"][:2], "์ผ์์ํ์ง์": SUPPORT_METHOD_RULES["์ผ์์ํ์ง์"]["items"][:2], }
limited = list(selected.items())[:5] lines = ["[๊ฐ์ธ๋ณ ์ด์ฉ๊ณํ ์ง์๋ฐฉ๋ฒ]"] for category, items in limited: lines.append(f"{category}") for item in items[:5]: lines.append(f"- {item}") return "\n".join(lines)
def analyze_specialized_risks(data): qna_text = _join_qna_answers(data.get("qna_answers", {})) combined = " ".join([ data.get("main_condition", ""), data.get("cognitive_status", ""), data.get("mobility_status", ""), data.get("diet_status", ""), data.get("toileting_status", ""), data.get("client_status_change", ""), data.get("guardian_opinion", ""), data.get("staff_observation", ""), data.get("custom_need", ""), qna_text, ]) fall = _risk_flag(combined, FALL_KEYWORDS) pressure = _risk_flag(combined, PRESSURE_INJURY_KEYWORDS) dementia = _risk_flag(combined, DEMENTIA_KEYWORDS) guardian_burden = _risk_flag(data.get("guardian_opinion", "") + " " + qna_text, GUARDIAN_BURDEN_KEYWORDS)
count = sum([fall, pressure, dementia]) if count >= 2 or _contains_keywords(combined, ["์๊ธ", "์
์", "์
ํ", "๊ณต๊ฒฉ", "์์ฒ", "์ผ์ถ", "๊ฐ์ผ", "๋ฐ๋ณต ๋์"]): level = "์ํ" elif count == 1 or guardian_burden: level = "์ฃผ์" else: level = "์์ "
details = [] plans = [] if fall: details.append("๋์ ์ํ ๋จ์๊ฐ ํ์ธ๋์ด ๋ฐ์ ์์ , ์ฅ์, ๋์, ํต์ฆยท๋ถ์ข
ยท๋ฉยท์์ ๋ณํ, ๋ณดํ ๊ฐ๋ฅ ์ฌ๋ถ ํ์ธ์ด ํ์ํจ") plans.append("๋์์๋ฐฉ์ ์ํด ์ด๋ ๋์ , ํ์ฅ์ค ์ด์ฉ, ๋ฏธ๋๋ผ ๋ฐฉ์ง, ๋ณดํ๋ณด์กฐ๊ธฐ ์ฌ์ฉ ์ฌ๋ถ๋ฅผ ์ ๊ฒํ๊ณ ์ด๋ ์ ๋ถ์ถ ๋ฐ ์ง์ ๋ํ์ ๊ฐํํจ") if pressure: details.append("์์ฐฝ ๋๋ ํผ๋ถ์์ ์ํ ๋จ์๊ฐ ํ์ธ๋์ด ๋ฐ์ ๋ถ์, ๋ฐ์ ยท์์ฒ ํฌ๊ธฐ, ํต์ฆ, ์ด๊ฐ, ์ผ์ถ๋ฌผ, ์ฒด์๋ณ๊ฒฝ ์ฌ๋ถ ํ์ธ์ด ํ์ํจ") plans.append("ํผ๋ถ๊ด์ฐฐ์ ๊ฐํํ๊ณ ์ฒด์๋ณ๊ฒฝ, ์๋ฐ๋ถ์ ๊ด๋ฆฌ, ์์ยท์๋ถ ์ญ์ทจ ํ์ธ, ํ์ ์ ๋ฐฉ๋ฌธ๊ฐํธ ๋๋ ์๋ฃ๊ธฐ๊ด ์ฐ๊ณ๋ฅผ ๊ฒํ ํจ") if dementia: details.append("์น๋งค ๋๋ ์ธ์งยทํ๋ ๋ณํ ๋จ์๊ฐ ํ์ธ๋์ด ๋ฐ๋ณต์ง๋ฌธ, ์ง๋จ๋ ฅ, ๋ฐฐํ, ๋ถ์, ๊ท๊ฐ ์๊ตฌ, ๊ณต๊ฒฉ์ ๋ฐ์ ์ฌ๋ถ ํ์ธ์ด ํ์ํจ") plans.append("์ ๋ฉด ๋ฐ๋ฐ์ ํผํ๊ณ ๊ฐ์ ๊ณต๊ฐ, ์งง์ ์ค๋ช
, ์ต์ํ ํ์ ์ ํ, ๊ฐ๋ณ ํ๋ก๊ทธ๋จ ๋๋ ์์ ํ๊ฒฝ ์ ๊ณต์ ์ ์ฉํจ") if guardian_burden: details.append("๋ณดํธ์์ ๋๋ด ๋ถ๋ด ๋๋ ์ถ๊ฐ ์์ฒญ์ด ํ์ธ๋์ด ์ ์ฒด์ ยท์ ์์ ๋ถ๋ด๊ณผ ์๋น์ค ์กฐ์ ํ์์ฑ์ ํจ๊ป ๊ฒํ ํจ") plans.append("๋ณดํธ์ ์๊ฒฌ์ ์๋ด๊ธฐ๋ก์ ๋ฐ์ํ๊ณ ์๋น์ค ์กฐ์ ๊ฐ๋ฅ ๋ฒ์, ๋ณ์์ง๋ฃ, ์๋ด, ์ง์ญ์์ ์ฐ๊ณ ํ์์ฑ์ ์๋ดํจ")
if not details: details.append("์ฆ์ ๊ณ ์ํ์ผ๋ก ๋ณผ ๋จ์๋ ์ ํ์ ์ด๋ ์๋ด๋์๋ณ ๊ฑด๊ฐ์ํ, ์๋น์ค ๋ง์กฑ๋, ํ์ ์์ฒญ์ฌํญ์ ๊ธฐ์ค์ผ๋ก ์ ๊ธฐ ๊ด์ฐฐ์ด ํ์ํจ") if not plans: plans.append("ํ์ฌ ๊ธ์ฌ์ ๊ณต๊ณํ์ ์ ์งํ๋ ๋ค์ ์๋ด ๋ฐ ์๋น์ค ์ ๊ณต ์ ์ํ๋ณํ, ๋ณดํธ์ ์์ฒญ, ์ข
์ฌ์ ๊ด์ฐฐ์ฌํญ์ ์ฌํ์ธํจ")
return { "combined": combined, "fall": fall, "pressure": pressure, "dementia": dementia, "guardian_burden": guardian_burden, "level": level, "details": details, "plans": plans, }
def build_soap_sections(data, analysis, specialized): inst_guide = INSTITUTION_SOAP_GUIDES.get(data["institution_type"], INSTITUTION_SOAP_GUIDES["๋ฐฉ๋ฌธ์์"]) subject_sources = [ data.get("guardian_opinion", ""), data.get("custom_need", ""), _join_qna_answers(data.get("qna_answers", {})), data.get("client_status_change", ""), ] subjective = _clean_text(" / ".join([_formalize_sentence(x) for x in subject_sources if _clean_text(x)])) if not subjective: subjective = f"{data['counseling_type']} ๊ณผ์ ์์ {data['counseling_reason']} ๊ด๋ จ ๋ถํธ๊ฐ๊ณผ ์ถ๊ฐ ์์ฒญ ์ฌ๋ถ๋ฅผ ํ์ธํจ"
objective_items = [ f"์ฃผ์ ์งํ ๋ฐ ์ํ๋ {_formalize_sentence(data.get('main_condition')) or '๊ธฐ์กด ์งํ ๋ฐ ๊ธฐ๋ฅ์ํ์ ๋ํ ์ง์ ๊ด์ฐฐ ํ์๋ก ํ์ธ๋จ'}", f"์ธ์ง์ํ๋ {_formalize_sentence(data.get('cognitive_status')) or '์ ๊ธฐ ๊ด์ฐฐ ํ์๋ก ํ์ธ๋จ'}", f"์ด๋์ํ๋ {_formalize_sentence(data.get('mobility_status')) or '์์ ํ ์ด๋ ์ง์ ํ์๋ก ํ์ธ๋จ'}", f"์์ฌ์ํ๋ {_formalize_sentence(data.get('diet_status')) or '๊ฐ๋ณ ์์ฌ์ํ ํ์ธ ํ์๋ก ํ์ธ๋จ'}", f"๋ฐฐ๋ณยท๋ฐฐ๋จ์ํ๋ {_formalize_sentence(data.get('toileting_status')) or '๋ฐฐ๋ณยท๋ฐฐ๋จ ์์ ๊ด์ฐฐ ํ์๋ก ํ์ธ๋จ'}", f"{data['institution_type']} ์ค์ ํ๊ฐ์งํ๋ {inst_guide['focus']}์", ] objective = "\n".join([f"- {item}" for item in objective_items])
risk_summary = " / ".join(specialized["details"]) assessment = ( f"์๋ด๋์์ {data['counseling_type']}์ด๋ฉฐ, ์๋ด๋ด์ฉ์ ์ข
ํฉํ ์ํ์์ค์ {specialized['level']} ๋จ๊ณ๋ก ์ฌ์ ๋จ. " f"{risk_summary} ๋ํ ์ํ๋ณํ๊ธฐ๋ก์ง ์์ฑ ํ์ ์ฌ๋ถ๋ {'ํ์ํจ' if analysis['status_needed'] else '์ ๊ธฐ ๊ด์ฐฐ๋ก ๊ด๋ฆฌ ๊ฐ๋ฅํจ'}์ด๋ฉฐ, " f"์ฌ๋ก๊ด๋ฆฌ ์ฐ๊ณ ํ์ ์ฌ๋ถ๋ {'๊ฒํ ํ์ํจ' if analysis['case_needed'] or specialized['level'] == '์ํ' else '์ฆ์ ์ฐ๊ณ ํ์์ฑ์ ๋ฎ์'}." )
plan_items = [inst_guide["plan"]] + specialized["plans"] + [ analysis["care_plan_reason"], analysis["status_reason"], analysis["case_reason"], ] unique_plans = [] for item in plan_items: item = _formalize_sentence(item) if item and item not in unique_plans: unique_plans.append(item) plan = "\n".join([f"- {item}" for item in unique_plans[:8]])
return f"""[SOAP ์๋ด๊ธฐ๋ก]S(์ฃผ๊ด์ ์ง์ ): {subjective}
O(๊ฐ๊ด์ ๊ด์ฐฐ):{objective}
A(์ ๋ฌธ์ ์ฌ์ ): {assessment}
P(์กฐ์น๊ณํ):{plan}""".strip()
def analyze_counseling_answers(data): qna_text = _join_qna_answers(data.get("qna_answers", {})) combined = " ".join([ data.get("main_condition", ""), data.get("cognitive_status", ""), data.get("mobility_status", ""), data.get("diet_status", ""), data.get("toileting_status", ""), data.get("client_status_change", ""), data.get("guardian_opinion", ""), data.get("staff_observation", ""), data.get("custom_need", ""), qna_text, ]) status_needed = _contains_keywords(combined, STATUS_CHANGE_KEYWORDS) case_needed = _contains_keywords(combined, CASE_RISK_KEYWORDS) care_plan_needed = _contains_keywords(combined, CARE_PLAN_KEYWORDS) or status_needed or case_needed if case_needed: risk_level = "์ํ" elif status_needed: risk_level = "์ฃผ์" else: risk_level = "์์ " return { "qna_text": qna_text, "combined": combined, "risk_level": risk_level, "status_needed": status_needed, "case_needed": case_needed, "care_plan_needed": care_plan_needed, "status_reason": "์๋ด ๋ต๋ณ์์ ์ํ๋ณํ ์์ฌ ๋จ์๊ฐ ํ์ธ๋์ด ์ํ๋ณํ๊ธฐ๋ก์ง ์์ฑ ํ์์ฑ์ด ์์" if status_needed else "์ํ๋ณํ๊ธฐ๋ก์ง ์ฆ์ ์์ฑ ํ์์ฑ์ ๋ฎ์ผ๋ ์ ๊ธฐ ๊ด์ฐฐ์ ํ์ํจ", "case_reason": "์๋ด ๋ต๋ณ์์ ๋ฐ๋ณตยท์
ํยท์ํ ์์ค์ ๋ฌธ์ ๊ฐ ํ์ธ๋์ด ์ฌ๋ก๊ด๋ฆฌ ์ฐ๊ณ ๊ฒํ ๊ฐ ํ์ํจ" if case_needed else "์ฌ๋ก๊ด๋ฆฌ ์ฆ์ ์ฐ๊ณ ํ์์ฑ์ ๋ฎ์ผ๋ ๋์ผ ๋ฌธ์ ๊ฐ ๋ฐ๋ณต๋๋ฉด ํ์ ์๊ฑด์ผ๋ก ๊ฒํ ํจ", "care_plan_reason": "์๋ด ๊ฒฐ๊ณผ๋ฅผ ๊ธ์ฌ์ ๊ณต๊ณํ ๋๋ ์๋น์ค ์ ๊ณต ๋ฐฉ๋ฒ์ ๋ฐ์ํ ํ์๊ฐ ์์" if care_plan_needed else "ํ ๊ธ์ฌ์ ๊ณต๊ณํ ์ ์ง๊ฐ ๊ฐ๋ฅํ๋ ๋ค์ ์๋ด ์ ์ฌํ์ธํจ", }
def build_qna_generated_sections(data): analysis = analyze_counseling_answers(data) qna_text = analysis["qna_text"] or "Q&A ์
๋ ฅ์ ์ฌ์ฉํ์ง ์์์ผ๋ฉฐ ๊ธฐ๋ณธ ์
๋ ฅ ๋ด์ฉ์ ์ค์ฌ์ผ๋ก ์๋ด์ผ์ง๋ฅผ ์์ฑํจ" status_text = _clean_text(data.get("client_status_change")) or "์ง์ ์
๋ ฅ๋ ๊ธ๊ฒฉํ ์ํ๋ณํ๋ ์ ํ์ ์ด๋ ์๋ด ๋ต๋ณ์ ํตํด ์ง์ ๊ด์ฐฐ ํ์์ฑ์ด ํ์ธ๋จ" guardian_text = _clean_text(data.get("guardian_opinion")) or "๋ณดํธ์ ์๊ฒฌ์ ํ์ ์ ์ถ๊ฐ ์๋ด์ผ๋ก ํ์ธ ์์ ์" staff_text = _clean_text(data.get("staff_observation")) or "์ข
์ฌ์ ๊ด์ฐฐ์ฌํญ์ ์๋น์ค ์ ๊ณต ์ค ์ถ๊ฐ ํ์ธ ์์ ์" need_text = _clean_text(data.get("custom_need")) or "์ถ๊ฐ ์๊ตฌ๋ ์๋ด ๋ด์ฉ์ ๋ฐ๋ผ ๊ธ์ฌ์ ๊ณต ๊ณผ์ ์์ ์ฌํ์ธ ์์ ์" return f"""[Q&A ํ์ธ ๋ด์ฉ]{qna_text}
[์๋ด๋ด์ฉ]์๋ด๋ถ๋ฅ๋ {data['counseling_type']}์ด๋ฉฐ, ์๋ด์ฌ์ ๋ {data['counseling_reason']}์. Q&A ๋ฐ ๊ธฐ๋ณธ ์
๋ ฅ์ ์ข
ํฉํ ๊ฒฐ๊ณผ, ์๊ธ์ ์ํ๋ณํ๋ "{status_text}"๋ก ํ์ธ๋จ. ๋ณดํธ์ ์๊ฒฌ์ "{guardian_text}"์ด๋ฉฐ, ์ข
์ฌ์ ๊ด์ฐฐ์ฌํญ์ "{staff_text}"๋ก ์ ๋ฆฌ๋จ.
[์ํ๋ถ์]์ํ์์ค์ {analysis['risk_level']} ๋จ๊ณ๋ก ํ๋จ๋จ. ์ฃผ์ ์งํ ๋ฐ ์ํ, ์ธ์งยท์ด๋ยท์์ฌยท๋ฐฐ๋ณ ์ํ์ ์๋ด ๋ต๋ณ์ ํจ๊ป ๊ฒํ ํ ๊ฒฐ๊ณผ, ๋จ์ ๋ฏผ์์ฑ ์๋ด์ด ์๋๋ผ ๊ธ์ฌ์ ๊ณต ๊ณผ์ ์์ ์ฌํ์ธ์ด ํ์ํ ์๋ด์ผ๋ก ํ๋จ๋จ.
[์กฐ์น๊ณํ]ํ์ธ๋ ์๊ตฌ ๋ฐ ์์ฒญ์ฌํญ์ "{need_text}"์. ๋ด๋น์๋ ์๋ด ๋ด์ฉ์ ์ง์๊ณผ ๊ณต์ ํ๊ณ , ์๋น์ค ์ ๊ณต ์ค ๊ด์ฐฐ ํญ๋ชฉ์ ๊ตฌ์ฒดํํ๋ฉฐ, ํ์ ์ ๋ณดํธ์์๊ฒ ์งํ ์ํฉ์ ์๋ดํจ.
[ํ์๊ด๋ฆฌ]๋ค์ ์๋น์ค ์ ๊ณต ์ ๋์ผ ์ฆ์ ๋ฐ๋ณต ์ฌ๋ถ, ์ํ ์งํ ์ง์ ์ฌ๋ถ, ๋ณดํธ์ ์ถ๊ฐ ์์ฒญ ์ฌ๋ถ๋ฅผ ์ฌํ์ธํจ. ํ์ ์ ์๋ด์ผ์ง ํ์ ๊ธฐ๋ก, ์ํ๋ณํ๊ธฐ๋ก์ง, ์๊ตฌ์ฌ์ , ์ฌ๋ก๊ด๋ฆฌ ํ์ ์๊ฑด์ผ๋ก ์ฐ๊ฒฐํจ.
[๊ธ์ฌ์ ๊ณต๊ณํ ๋ฐ์ ์ฌ๋ถ]{analysis['care_plan_reason']}
[์ํ๋ณํ๊ธฐ๋ก์ง ์์ฑ ํ์ ์ฌ๋ถ]{analysis['status_reason']}
[์ฌ๋ก๊ด๋ฆฌ ์ฐ๊ณ ํ์ ์ฌ๋ถ]{analysis['case_reason']}""".strip(), analysis
OPENERS = [ "์๋ด ๊ณผ์ ์์ ํ์ธํ ๊ฒฐ๊ณผ", "๊ธ์ผ ์๋ด์ ํตํด ํ์
ํ ๋ด์ฉ์", "์ต๊ทผ ์๋น์ค ์ ๊ณต ์ํฉ์ ์ข
ํฉํด ๋ณด๋ฉด", "์๊ธ์ ๋ฐ ๊ด๋ จ์์ ์๊ฒฌ์ ํ์ธํ ๊ฒฐ๊ณผ",]TONES = [ "ํ์ฌ ์ํ๋ฅผ ์ ์งํ๋ฉด์๋ ์ธ์ฌํ ๊ด์ฐฐ์ด ์๊ตฌ๋ฉ๋๋ค.", "์ผ์์ํ์ ์์ ์ฑ์ ๋์ด๊ธฐ ์ํ ์ง์์ ์ธ ์ง์์ด ํ์ํฉ๋๋ค.", "๊ฐ๋ณ ์๊ตฌ๊ฐ ๊ธ์ฌ์ ๊ณต ๊ณผ์ ์ ์ถฉ๋ถํ ๋ฐ์๋๋๋ก ์กฐ์ ์ด ํ์ํฉ๋๋ค.", "์ํ ๋ณํ ๊ฐ๋ฅ์ฑ์ ๊ณ ๋ คํ์ฌ ์ ๊ธฐ์ ์ธ ํ์ธ๊ณผ ๊ธฐ๋ก์ด ํ์ํฉ๋๋ค.",]
def mask_name(name: str) -> str: name = str(name or "").strip() if len(name) >= 3: return name[0] + "โ" + name[2:] if len(name) == 2: return name[0] + "โ" return name
def _clean_text(value: str) -> str: text = str(value or "").strip() while ",," in text: text = text.replace(",,", ",") return text.strip(" ,")
def _symptom_process(data): """์
๋ ฅ๋ ์ํ๋ณํ์ ๋ฐ๋ผ ์๋ด ๊ณผ์ ์ด ๋จ์ ๋ณต๋ถ์ด ๋์ง ์๋๋ก ํ์ธ ์ ์ฐจ๋ฅผ ์๋ ๊ตฌ์ฑํฉ๋๋ค.""" combined = " ".join([ data.get("main_condition", ""), data.get("client_status_change", ""), data.get("guardian_opinion", ""), data.get("staff_observation", ""), data.get("custom_need", ""), ])
steps = [] if any(k in combined for k in ["์ด์ง", "ํ๊ธฐ", "ํ์ฒญ", "๊ธฐ๋ฆฝ", "๋ฉ๏ฟฝ๏ฟฝ"]): steps.extend([ "์๊ธ์์๊ฒ ์ด์ง๋ผ ์ฆ์์ด ์ธ์ ๋ถํฐ ์์๋์ง, ํ๋ฃจ ์ค ์ด๋ ์๊ฐ๋์ ์ฌํ์ง, ์์๋ค ์ผ์ด๋ ๋ ์ฌํด์ง๋์ง, ์ง์์๊ฐ๊ณผ ๋ฐ๋ณต ์ฌ๋ถ๋ฅผ ํ์ธํ์๋ค.", "์๋ด ์ค ์์, ์์์ํ, ์์๋, ํธํก์ํ, ๋ณดํ ์ ํ์ฒญ๊ฑฐ๋ฆผ ์ฌ๋ถ๋ฅผ ๊ด์ฐฐํ๊ณ , ํ์ ์ ํ์ยท๋งฅ๋ฐ์ ์ฌํ์ธํ๋๋ก ํ์๋ค.", "๊ณ ํ์ยท๋น๋จ ๋ณ๋ ฅ์ด ์์ด ๋ณต์ฝ ์ฌ๋ถ, ์์ฌ๋, ์๋ถ์ญ์ทจ, ์ ํ๋น ์์ฌ ์ฆ์ ์ฌ๋ถ๋ฅผ ํจ๊ป ํ์ธํ์๋ค.", "์ด๋ ์ ๋์์ํ์ด ๋์ ํผ์ ๋ณดํํ์ง ์๋๋ก ์๋ดํ๊ณ , ์ก์ยทํ์ฅ์ค ์ด๋ยทํ๋ก๊ทธ๋จ ์ด๋ ์ ์ง์ ๋ํ ํ์์ฑ์ ํ์ธํ์๋ค.", ]) if any(k in combined for k in ["์์ฌ", "์์", "์์", "์ฒด์ค", "์ฌ๋ ", "์ฐํ"]): steps.extend([ "์ต๊ทผ ์์ฌ๋ ๋ณํ, ์ ํธ์, ์ฌ๋ ์ฌ๋ถ, ์์ฌ ์ค ํผ๋ก๊ฐ, ์๋ถ์ญ์ทจ ์ ๋๋ฅผ ํ์ธํ์๋ค.", "์์ฌ๋ ๊ฐ์๊ฐ ์ง์๋ ๊ฒฝ์ฐ ๋ณดํธ์์๊ฒ ์๋ดํ๊ณ ํ์ ์ ์์์ํ ๊ด์ฐฐ ๋ฐ ์๋ฃ์๋ด ์ฐ๊ณ๋ฅผ ๊ฒํ ํ๊ธฐ๋ก ํ์๋ค.", ]) if any(k in combined for k in ["๋์", "๋ณดํ", "์งํก", "ํ ์ฒด์ด", "๋ถ์ถ", "์ด๋"]): steps.extend([ "๋ณดํ ๋ณด์กฐ๋๊ตฌ ์ฌ์ฉ์ํ, ์ด๋ ๋์ , ํ์ฅ์ค ์ด๋ ์ ์ํ์์ธ, ๋ฐ๋ฅ ๋ฏธ๋๋ผ ์ฌ๋ถ๋ฅผ ํ์ธํ์๋ค.", "๋์์๋ฐฉ์ ์ํด ์ด๋ ์ ํ ์ง์ ํ์ธ, ์๋ฆฌ ์ด๋ ์ ๋ถ์ถ, ํ๋ก๊ทธ๋จ ์ฐธ์ฌ ์ ์์ ํ ์ข์ ๋ฐฐ์น๋ฅผ ์ ๊ฒํ๊ธฐ๋ก ํ์๋ค.", ]) if any(k in combined for k in ["์น๋งค", "์ธ์ง", "๊ธฐ์ต", "๋ถ์", "์ด์กฐ", "๋ฐฐํ"]): steps.extend([ "์ธ์ง์ํ์ ์ ์์ํ๋ฅผ ํ์ธํ๊ธฐ ์ํด ์ต๊ทผ ํผ๋, ๋ฐ๋ณต์ง๋ฌธ, ๋ถ์, ํ๋ก๊ทธ๋จ ์ฐธ์ฌ ๋ณํ ์ฌ๋ถ๋ฅผ ๊ด์ฐฐํ์๋ค.", "์ค๋ช
์ ์งง๊ณ ๋ฐ๋ณต์ ์ผ๋ก ์ ๊ณตํ๊ณ , ์ต์ํ ํ๊ฒฝ์์ ์์ ๊ฐ์ ๋๋ผ๋๋ก ์ ์์ง์ง๋ฅผ ๋ณํํ๊ธฐ๋ก ํ์๋ค.", ]) if any(k in combined for k in ["๋ฐฐ๋ณ", "๋ฐฐ๋จ", "๊ธฐ์ ๊ท", "์ค๊ธ", "๋ณ๋น"]): steps.extend([ "๋ฐฐ๋ณยท๋ฐฐ๋จ ์์, ํ์ฅ์ค ์ด๋ ๊ฐ๋ฅ ์ฌ๋ถ, ์ค๊ธ ๋๋ ๋ณ๋น ์ฌ๋ถ๋ฅผ ํ์ธํ๊ณ ์์๊ด๋ฆฌ ํ์์ฑ์ ์ ๊ฒํ์๋ค.", ])
if not steps: steps = [ "์๊ธ์์๊ฒ ํ์ฌ ๋ถํธ๊ฐ์ ๋ด์ฉ, ๋ฐ์ ์์ , ๋ฐ๋ณต ์ฌ๋ถ, ์ผ์์ํ์ ๋ฏธ์น๋ ์ํฅ์ ํ์ธํ์๋ค.", "์๋ด ์ค ํ์ , ์์ฌํํ, ์ด๋์ํ, ์์ฌยท๋ฐฐ๋ณ ์ํ ๋ฑ ๊ธฐ๋ณธ์ ์ธ ์ํ ๋ณํ๋ฅผ ๊ด์ฐฐํ์๋ค.", "๋ณดํธ์ ์๊ฒฌ๊ณผ ์ข
์ฌ์ ๊ด์ฐฐ๋ด์ฉ์ ๋น๊ตํ์ฌ ๊ธ์ฌ์ ๊ณต ๊ณผ์ ์์ ์ถ๊ฐ ํ์ธ์ด ํ์ํ ๋ถ๋ถ์ ์ ๋ฆฌํ์๋ค.", "ํ์ธ๋ ๋ด์ฉ์ ๋ด๋น์์ ๊ณต์ ํ๊ณ , ํ์ ์ ์๊ตฌ์ฌ์ ยท์ํ๋ณํ๊ธฐ๋ก์งยท๊ธ์ฌ์ ๊ณต๊ณํ์์ ๋ฐ์ํ๊ธฐ๋ก ํ์๋ค.", ]
# ์ค๋ณต ์ ๊ฑฐ unique = [] for step in steps: if step not in unique: unique.append(step) return "\n".join([f"{i}. {step}" for i, step in enumerate(unique[:7], 1)])
def build_counseling_text(data): inst_keywords = ", ".join(INSTITUTION_GUIDES.get(data["institution_type"], [])) guide = TYPE_GUIDES[data["counseling_type"]]
main_condition = _clean_text(data.get("main_condition")) or "๊ธฐ์กด ์งํ ๋ฐ ๊ธฐ๋ฅ์ํ์ ๋ํ ์ง์ ๊ด์ฐฐ ํ์" cognitive = _clean_text(data.get("cognitive_status")) or "์ ๊ธฐ ๊ด์ฐฐ ํ์" mobility = _clean_text(data.get("mobility_status")) or "์์ ํ ์ด๋ ์ง์ ํ์" diet = _clean_text(data.get("diet_status")) or "๊ฐ๋ณ ์์ฌ์ํ ํ์ธ ํ์" toileting = _clean_text(data.get("toileting_status")) or "๋ฐฐ๋ณยท๋ฐฐ๋จ ์์ ๊ด์ฐฐ ํ์" status_part = _clean_text(data.get("client_status_change")) or "๊ธ๊ฒฉํ ๋ณํ๋ ํ์ธ๋์ง ์์์ผ๋ ์ง์์ ์ธ ๊ด์ฐฐ์ด ํ์ํจ" guardian_part = _clean_text(data.get("guardian_opinion")) or "๋ณดํธ์ ์๊ฒฌ์ ์ถ๊ฐ ํ์ธ์ด ํ์ํ๋ฉฐ, ํ์ ์ ์ ์ ์๋ด์ ํตํด ๋ฐ์ ์์ " staff_part = _clean_text(data.get("staff_observation")) or "์๋น์ค ์ ๊ณต ์ค ๊ด์ฐฐ๋ ๋ด์ฉ์ ์ข
ํฉํ์ฌ ๊ธ์ฌ ๋ฐฉํฅ์ ์ ๊ฒํจ" need_part = _clean_text(data.get("custom_need")) or "์ํ๋ณํ ์ฌ๋ถ๋ฅผ ๊ด์ฐฐํ๋ฉฐ ํ์ฌ ์ ๊ณต ์ค์ธ ์๋น์ค๊ฐ ์๊ธ์์ ์๊ตฌ์ ์ํ์ ์ ์ ํ์ง ์ ๊ฒํจ" process_text = _symptom_process(data)
qna_sections, analysis = build_qna_generated_sections(data) specialized = analyze_specialized_risks(data) soap_sections = build_soap_sections(data, analysis, specialized) support_methods = build_individual_support_methods(data, specialized)
risk_labels = [] if specialized["fall"]: risk_labels.append("๋์ ์ํ") if specialized["pressure"]: risk_labels.append("์์ฐฝยทํผ๋ถ์์ ์ํ") if specialized["dementia"]: risk_labels.append("์น๋งคยท์ธ์งํ๋ ์ํ") if specialized["guardian_burden"]: risk_labels.append("๋ณดํธ์ ๋ถ๋ดยท์์ฒญ") risk_text = ", ".join(risk_labels) if risk_labels else "์ฆ์ ๊ณ ์ํ ๋จ์ ์ ํ์ "
content = f"""[์๋ด ์ฌ์ ]{data['masked_name']} ์๊ธ์๋ {data['care_grade']} ๋์์๋ก์ {data['institution_type']} ์๋น์ค ์ด์ฉ ์ค {data['counseling_reason']} ์๋ด์ด ํ์ํ์ฌ ์๋ด์ ์ค์ํ์๋ค. ์ฃผ์ ์งํ ๋ฐ ์ํ๋ {main_condition}์ด๋ฉฐ, ์ต๊ทผ ํ์ธ๋ ์ํ๋ณํ๋ "{status_part}"์ด๋ค.
[ํ์ฌ ์ํ ํ์ธ]1. ์ธ์ง์ํ: {cognitive}2. ์ด๋์ํ: {mobility}3. ์์ฌ์ํ: {diet}4. ๋ฐฐ๋ณยท๋ฐฐ๋จ์ํ: {toileting}5. ๊ธฐ๊ด์ ํ์ ์ค์ ํ์ธ ํญ๋ชฉ: {inst_keywords}
[์๋ด ๋ฐ ํ์ธ ๊ณผ์ ]{process_text}
[๋ณดํธ์ ๋ฐ ์ข
์ฌ์ ์๊ฒฌ]1. ๋ณดํธ์ ์๊ฒฌ: {guardian_part}2. ์ข
์ฌ์ ๊ด์ฐฐ์ฌํญ: {staff_part}3. ์ถ๊ฐ ์๊ตฌ ๋ฐ ์์ฒญ์ฌํญ: {need_part}
{qna_sections}
{soap_sections}
[๊ฐ์ธ๋ณ ์ด์ฉ๊ณํ ์ง์๋ฐฉ๋ฒ]{support_methods.replace('[๊ฐ์ธ๋ณ ์ด์ฉ๊ณํ ์ง์๋ฐฉ๋ฒ]', '').strip()}
[์ํ์์ธ ์๋๋ถ์]๊ธฐ๊ด์ ํ, ์๋ด๋์, ๋ณดํธ์ ์๊ฒฌ, ์๊ธ์ ์ํ๋ณํ ๋ฐ Q&A ๋ต๋ณ์ ์ข
ํฉํ ๊ฒฐ๊ณผ {risk_text}์ด ํ์ธ๋จ. ์ข
ํฉ ์ํ์์ค์ {specialized['level']} ๋จ๊ณ๋ก ํ๋จ๋จ.
[์๋ด ๊ฒฐ๊ณผ]์๋ด ๊ฒฐ๊ณผ, ์ด๋ฒ ๋ด์ฉ์ ๋จ์ ๋ถํธ์ฌํญ ํ์ธ์ ๊ทธ์น์ง ์๊ณ {guide['focus']}ํ ์๋ด์ผ๋ก ํ๋จ๋๋ค. ์ํ์์ค์ {analysis['risk_level']} ๋จ๊ณ๋ก ํ๋จ๋๋ฉฐ, ์ํ๋ณํ๊ธฐ๋ก์ง ์์ฑ ํ์ ์ฌ๋ถ์ ์ฌ๋ก๊ด๋ฆฌ ์ฐ๊ณ ํ์ ์ฌ๋ถ๋ฅผ ํจ๊ป ํ์ธํ์๋ค.
[๊ธ์ฌ์ ๊ณต ๋ฐ์]ํ์ธ๋ ๋ด์ฉ์ {guide['action']}ํ๋ ๋ฐฉํฅ์ผ๋ก ๊ด๋ฆฌํ๊ณ , {data['institution_type']} ์๋น์ค ์ ๊ณต ๊ณผ์ ์์ ์๊ธ์์ ์์ ๊ณผ ์ํ๋ณํ๋ฅผ ์ฐ์ ์ ์ผ๋ก ํ์ธํ๋ค. {analysis['care_plan_reason']}""".strip() content = _replace_restricted_phrases(content)
action = f"""1. ์๋ด๋ด์ฉ์ ๋ด๋น์์ ๊ณต์ ํ๊ณ , ๋ค์ ์๋น์ค ์ ๊ณต ์ ์ํ๋ณํ ์ฌ๋ถ๋ฅผ ์ฐ์ ํ์ธํ๋ค.2. ์ด์ง๋ผ, ๋ณดํ๋ถ์, ์์ฌ๋ ๊ฐ์ ๋ฑ ์ํ ์งํ๊ฐ ๋ฐ๋ณต๋๋ฉด ํ์ยท๋งฅ๋ฐ ๋ฑ ๊ธฐ๋ณธ์ํ ํ์ธ ํ ๋ณดํธ์์๊ฒ ์ฆ์ ์๋ดํ๋ค.3. ์ด๋ ์ ๋์์๋ฐฉ์ ์ํด ํผ์ ์ด๋ํ์ง ์๋๋ก ์๋ดํ๊ณ , ์ก์ยทํ์ฅ์ค ์ด๋ยทํ๋ก๊ทธ๋จ ์ด๋ ์ ์ง์ ๋ํ์ ๊ฐํํ๋ค.4. ๋ณดํธ์๊ฐ ์์ฒญํ ๋ณ์์ง๋ฃ ๋๋ ๋ณ์๋ํ ํ์์ฑ์ ์ฆ์ ์ง์ ์ฌ๋ถ๋ฅผ ๊ด์ฐฐํ ๋ค ๊ธฐ๊ด ๋ด๋ถ ์ ์ฐจ์ ๋ฐ๋ผ ์ฐ๊ณํ๋ค.5. ์ํ๋ณํ๊ธฐ๋ก์ง ์์ฑ ํ์ ์ฌ๋ถ: {analysis['status_reason']}6. ์ฌ๋ก๊ด๋ฆฌ ์ฐ๊ณ ํ์ ์ฌ๋ถ: {analysis['case_reason']}7. ๊ธ์ฌ์ ๊ณต๊ณํ ๋ฐ์ ์ฌ๋ถ: {analysis['care_plan_reason']}8. ๋์ยท์์ฐฝยท์น๋งค ์ํ ์๋๋ถ์ ๊ฒฐ๊ณผ: {risk_text} / {specialized['level']} ๋จ๊ณ๋ก ํ๋จ๋จ.9. ๊ฐ์ธ๋ณ ์ด์ฉ๊ณํ ์ง์๋ฐฉ๋ฒ์ ์๋ด๋ด์ฉ์ ์ ์๋ ์ค์ ์ ๊ณต ๊ฐ๋ฅ ํญ๋ชฉ์ ๊ธฐ์ค์ผ๋ก ์ง์์๊ฒ ๊ณต์ ํ๊ณ ์๋น์ค ์ ๊ณต ์ ์ ์ฉํจ.""".strip() action = _replace_restricted_phrases(action) return content, action, support_methods
def evaluate_record(data): required = { "์๋ด์ผ์": bool(data["counseling_date"]), "์๋ด๋ถ๋ฅ": bool(data["counseling_type"]), "์๋ด์ฌ์ ": bool(data["counseling_reason"]), "์ํ๋ณํ ๋ฐ์": bool(data["client_status_change"].strip()) or "์ํ๋ณํ" in data.get("counseling_content", ""), "๋ณดํธ์ ์๊ฒฌ ๋ฐ์": bool(data["guardian_opinion"].strip()) or data["counseling_type"] == "๋ณดํธ์์๋ด", "์ข
์ฌ์ ๊ด์ฐฐ ๋ฐ์": bool(data["staff_observation"].strip()) or data["counseling_type"] == "์ข
์ฌ์์๋ด", "ํ์์กฐ์น": bool(data.get("action_plan", "").strip()), "๊ธ์ฌ์ ๊ณต ๋ฐ์": "๊ธ์ฌ" in data.get("counseling_content", "") or "๊ณํ" in data.get("action_plan", ""), "์ง์๋ฐฉ๋ฒ ๊ตฌ์ฒดํ": bool(data.get("support_methods", "").strip()) or "๊ฐ์ธ๋ณ ์ด์ฉ๊ณํ ์ง์๋ฐฉ๋ฒ" in data.get("counseling_content", ""), } score = int(sum(required.values()) / len(required) * 100) missing = [k for k, v in required.items() if not v] if score >= 90: result = "ํ๊ฐ ์ ํฉ๋๊ฐ ๋์ต๋๋ค. ์๋ด์ฌ์ , ์ํ๋ณํ, ๋ณดํธ์ ์๊ฒฌ, ํ์์กฐ์น๊ฐ ์ ๋ฐ์๋์์ต๋๋ค." elif score >= 75: result = "๋์ฒด๋ก ์ ํฉํ๋ ์ผ๋ถ ๋ณด์์ด ํ์ํฉ๋๋ค. ๋ถ์กฑ ํญ๋ชฉ์ ์ถ๊ฐํ๋ฉด ํ๊ฐ ๋์๋ ฅ์ด ๋์์ง๋๋ค." else: result = "๋ณด์์ด ํ์ํฉ๋๋ค. ์๋ด์ฌ์ , ์ํ๋ณํ, ๋ณดํธ์ ์๊ฒฌ, ํ์์กฐ์น๋ฅผ ๋ ๊ตฌ์ฒด์ ์ผ๋ก ์
๋ ฅํด ์ฃผ์ธ์." return score, missing, result
def style_range(ws, cell_range, fill=None, font=None, alignment=None): thin = Side(style="thin", color="000000") border = Border(left=thin, right=thin, top=thin, bottom=thin) cells = ws[cell_range] if not isinstance(cells, tuple): cells = ((cells,),) for row in cells: for cell in row: cell.border = border if fill: cell.fill = fill if font: cell.font = font if alignment: cell.alignment = alignment
def generate_excel(data): wb = Workbook() ws = wb.active ws.title = "์๋ด์ผ์ง"
ws.page_setup.paperSize = ws.PAPERSIZE_A4 ws.page_setup.orientation = "portrait" ws.page_setup.fitToWidth = 1 ws.page_setup.fitToHeight = 0 ws.sheet_properties.pageSetUpPr.fitToPage = True ws.page_margins.left = 0.3 ws.page_margins.right = 0.3 ws.page_margins.top = 0.4 ws.page_margins.bottom = 0.4 ws.sheet_view.showGridLines = False
title_fill = PatternFill("solid", fgColor="E2F0D9") header_fill = PatternFill("solid", fgColor="F2F2F2")
for col in range(1, 10): ws.column_dimensions[get_column_letter(col)].width = 11.5
ws.merge_cells("A1:G2") ws.cell(1, 1).value = "์ ๋ด ์ผ ์ง" ws.cell(1, 1).font = Font(name="Malgun Gothic", size=24, bold=True) ws.cell(1, 1).alignment = Alignment(horizontal="center", vertical="center", wrap_text=True) style_range(ws, "A1:G2", fill=title_fill)
ws.cell(1, 8).value = "๋ด๋น์" ws.cell(1, 9).value = "์์ค์ฅ" ws.cell(2, 8).value = "" ws.cell(2, 9).value = "" for r in [1, 2]: for c in [8, 9]: cell = ws.cell(r, c) cell.alignment = Alignment(horizontal="center", vertical="center") cell.font = Font(name="Malgun Gothic", size=11, bold=(r == 1)) if r == 1: cell.fill = header_fill style_range(ws, f"{get_column_letter(c)}{r}:{get_column_letter(c)}{r}") ws.row_dimensions[1].height = 28 ws.row_dimensions[2].height = 45
def write_info_row(row, a, b, c_val, d, e, f): ws.cell(row, 1).value = a ws.merge_cells(start_row=row, start_column=2, end_row=row, end_column=3) ws.cell(row, 2).value = b ws.cell(row, 4).value = c_val ws.merge_cells(start_row=row, start_column=5, end_row=row, end_column=6) ws.cell(row, 5).value = d ws.cell(row, 7).value = e ws.merge_cells(start_row=row, start_column=8, end_row=row, end_column=9) ws.cell(row, 8).value = f for col in [1, 4, 7]: ws.cell(row, col).fill = header_fill ws.cell(row, col).font = Font(name="Malgun Gothic", size=11, bold=True) ws.cell(row, col).alignment = Alignment(horizontal="center", vertical="center", wrap_text=True) for col in [2, 5, 8]: ws.cell(row, col).font = Font(name="Malgun Gothic", size=11) ws.cell(row, col).alignment = Alignment(horizontal="center", vertical="center", wrap_text=True) style_range(ws, f"A{row}:I{row}") ws.row_dimensions[row].height = 30
r = 3 write_info_row(r, "๊ธฐ๊ด๋ช
", data["institution_name"], "๊ธฐ๊ด์ ํ", data["institution_type"], "์๋ด๋ถ๋ฅ", data["counseling_type"]); r += 1 write_info_row(r, "์๊ธ์", data["masked_name"], "์ฅ๊ธฐ์์๋ฑ๊ธ", data["care_grade"], "์๋ด์ผ์", data["counseling_date"]); r += 1 write_info_row(r, "์๋ด์", data["counselor"], "์๋ด์ฌ์ ", data["counseling_reason"], "ํ๊ฐ์ ํฉ๋", f"{data['evaluation_score']}์ "); r += 1
detail_rows = [ ("์๋ด๋ด์ฉ", data["counseling_content"], 150), ("๊ฐ์ธ๋ณ ์ด์ฉ๊ณํ ์ง์๋ฐฉ๋ฒ", data.get("support_methods", ""), 110), ("์กฐ์น๊ณํ", data["action_plan"], 90), ("ํ๊ฐ ์ ํฉ๋ ์ ๊ฒ", data["compliance_result"], 60), ("๋ถ์กฑํ ํญ๋ชฉ", data["missing_items"], 45), ] for label, content_val, min_h in detail_rows: ws.cell(r, 1).value = label ws.cell(r, 1).font = Font(name="Malgun Gothic", size=11, bold=True) ws.cell(r, 1).alignment = Alignment(horizontal="center", vertical="center", wrap_text=True) ws.cell(r, 1).fill = header_fill ws.merge_cells(start_row=r, start_column=2, end_row=r, end_column=9) ws.cell(r, 2).value = content_val ws.cell(r, 2).font = Font(name="Malgun Gothic", size=11) ws.cell(r, 2).alignment = Alignment(horizontal="left", vertical="top", wrap_text=True) lines = sum((len(line) // 38) + 1 for line in str(content_val).split("\n")) ws.row_dimensions[r].height = max(min_h, lines * 22 + 25) style_range(ws, f"A{r}:I{r}") r += 1
output = BytesIO() wb.save(output) output.seek(0) return output
st.subheader("1๋จ๊ณ. ๊ธฐ๋ณธ์ ๋ณด ์
๋ ฅ")col1, col2, col3 = st.columns(3)institution_name = col1.text_input("๊ธฐ๊ด๋ช
", "์ฅ๊ธฐ์์๊ธฐ๊ด")institution_type = col2.selectbox("๊ธฐ๊ด์ ํ", list(INSTITUTION_GUIDES.keys()))counseling_type = col3.radio("์๋ด๋ถ๋ฅ", ["์๊ธ์์๋ด", "๋ณดํธ์์๋ด", "์ข
์ฌ์์๋ด", "๊ธฐํ์๋ด"], horizontal=True)
col4, col5, col6, col7 = st.columns(4)name = col4.text_input("์๊ธ์ ์ด๋ฆ", "ํ๊ธธ๋")masked_name = col5.text_input("๊ฐ๋ช
ํ๊ธฐ", mask_name(name))care_grade = col6.selectbox("์ฅ๊ธฐ์์๋ฑ๊ธ", ["1๋ฑ๊ธ", "2๋ฑ๊ธ", "3๋ฑ๊ธ", "4๋ฑ๊ธ", "5๋ฑ๊ธ", "์ธ์ง์ง์๋ฑ๊ธ", "๋ฏธ์
๋ ฅ"], index=2)counseling_date = col7.date_input("์๋ด์ผ์", value=date.today())
col8, col9, col10 = st.columns(3)counselor = col8.text_input("์๋ด์", "์ฌํ๋ณต์ง์ฌ")counseling_reason = col9.selectbox("์๋ด์ฌ์ ", ["์ ๊ธฐ์๋ด", "์ ๊ท ์ด์ฉ ํ ์ด๊ธฐ์๋ด", "์ํ๋ณํ ํ์ธ", "๋ณดํธ์ ์์ฒญ", "์๋น์ค ๋ณ๊ฒฝ ์์ฒญ", "์ง์ ๊ด์ฐฐ์ฌํญ", "๋์/์์ฐฝ/๊ฑด๊ฐ์ํ", "ํ๋ก๊ทธ๋จ ์ฐธ์ฌ ๋ณํ", "ํด์/์ ์/์
์ ๊ด๋ จ", "๊ธฐํ"])main_condition = col10.text_input("์ฃผ์ ์งํ/์ํ", "")
st.subheader("2๋จ๊ณ. ์
๋ ฅ ๋ฐฉ์ ์ ํ")input_mode = st.radio("์
๋ ฅ ๋ฐฉ์", ["๊ธฐ๋ณธ ์
๋ ฅ", "Q&A ์
๋ ฅ", "๊ธฐ๋ณธ+Q&A ์
๋ ฅ"], horizontal=True)use_basic_input = input_mode in ["๊ธฐ๋ณธ ์
๋ ฅ", "๊ธฐ๋ณธ+Q&A ์
๋ ฅ"]use_qna_input = input_mode in ["Q&A ์
๋ ฅ", "๊ธฐ๋ณธ+Q&A ์
๋ ฅ"]
cognitive_status = ""mobility_status = ""diet_status = ""toileting_status = ""client_status_change = ""guardian_opinion = ""staff_observation = ""custom_need = ""qna_answers = {}
if use_basic_input: st.markdown("### ๊ธฐ๋ณธ ์
๋ ฅ") c1, c2 = st.columns(2) cognitive_status = c1.text_area("์ธ์ง์ํ", placeholder="์: ๋ฐ๋ณต์ง๋ฌธ ์ฆ๊ฐ, ์๊ฐ/์ฅ์ ํผ๋ ๋ฑ", height=80) mobility_status = c2.text_area("์ด๋์ํ", placeholder="์: ๋ณดํ ๋ถ์์ , ๋ถ์ถ ํ์ ๋ฑ", height=80)
c3, c4 = st.columns(2) diet_status = c3.text_area("์์ฌ์ํ", placeholder="์: ์์ฌ๋ ๊ฐ์, ์ฌ๋ , ์ฐํ๊ณค๋ ๋ฑ", height=80) toileting_status = c4.text_area("๋ฐฐ๋ณ/๋ฐฐ๋จ์ํ", placeholder="์: ๋ณ๋น, ์ค๊ธ, ๊ธฐ์ ๊ท ์ฌ์ฉ ์ฆ๊ฐ ๋ฑ", height=80)
c5, c6 = st.columns(2) client_status_change = c5.text_area("์๊ธ์ ์ํ๋ณํ", placeholder="์: ์ต๊ทผ ์ด์ง๋ผ์ฆ์ด ์์ด ๋ณดํ์ ๊บผ๋ฆผ, ์์ฌ๋ ๊ฐ์, ์๋ฉด ๋ณํ ๋ฑ", height=100) guardian_opinion = c6.text_area("๋ณดํธ์ ์๊ฒฌ", placeholder="์: ์ด์ง๋ผ์ด ์ฌํด์ง๋ฉด ๋ณ์์ง๋ฃ๋ฅผ ์ํด ๋ณ์๋ํ ์์ฒญ", height=100)
c7, c8 = st.columns(2) staff_observation = c7.text_area("์ข
์ฌ์ ๊ด์ฐฐ์ฌํญ", placeholder="์: ์ด๋ ์ ๋ถ์ถ ์ ๊ณต, ์์/๋ณดํ์ํ ๊ด์ฐฐ, ์์ฌ๋ ํ์ธ ๋ฑ", height=100) custom_need = c8.text_area("์ถ๊ฐ ์๊ตฌ / ์์ฒญ์ฌํญ", placeholder="์: ์ด๋ ๋์ ๊ฐํ, ํ์ ์ฌํ์ธ, ๋ณดํธ์์๊ฒ ์ฆ์๋ณํ ์๋ด ๋ฑ", height=100)
if use_qna_input: st.markdown(f"### Q&A ์
๋ ฅ - {counseling_type}") st.info(f"{counseling_type}์ ๋ง๋ ์ง๋ฌธ์ด ์๋ ํ์๋ฉ๋๋ค. ๋ต๋ณ ๋ด์ฉ์ ์๋ด๋ด์ฉ, ์ํ๋ถ์, ์กฐ์น๊ณํ, ํ์๊ด๋ฆฌ, ๊ธ์ฌ์ ๊ณต๊ณํ ๋ฐ์ ์ฌ๋ถ ํ๋จ์ ์ฌ์ฉ๋ฉ๋๋ค.") questions = QA_RULES.get(counseling_type, QA_RULES["๊ธฐํ์๋ด"]) for idx, question in enumerate(questions, 1): qna_answers[question] = st.text_area(f"Q{idx}. {question}", key=f"qna_{counseling_type}_{idx}", height=80)
if st.button("๐ ์๋ด์ผ์ง ๋น ๋ฅด๊ฒ ์์ฑ", use_container_width=True): data = { "institution_name": institution_name, "institution_type": institution_type, "counseling_type": counseling_type, "name": name, "masked_name": masked_name, "care_grade": care_grade, "counseling_date": str(counseling_date), "counselor": counselor, "counseling_reason": counseling_reason, "main_condition": main_condition, "cognitive_status": cognitive_status, "mobility_status": mobility_status, "diet_status": diet_status, "toileting_status": toileting_status, "client_status_change": client_status_change, "guardian_opinion": guardian_opinion, "staff_observation": staff_observation, "custom_need": custom_need, "input_mode": input_mode, "qna_answers": qna_answers, } content, action, support_methods = build_counseling_text(data) data["counseling_content"] = content data["action_plan"] = action data["support_methods"] = support_methods score, missing, result = evaluate_record(data) data["evaluation_score"] = score data["missing_items"] = ", ".join(missing) if missing else "๋ถ์กฑ ํญ๋ชฉ ์์" data["compliance_result"] = result st.session_state["counseling_data"] = data st.session_state.pop("counseling_excel", None)
if "counseling_data" in st.session_state: data = st.session_state["counseling_data"] st.markdown("---") st.subheader("3๋จ๊ณ. ์์ฑ ๊ฒฐ๊ณผ ํ์ธ/์์ ") data["counseling_content"] = st.text_area("์๋ด๋ด์ฉ", value=data["counseling_content"], height=260) data["support_methods"] = st.text_area("๊ฐ์ธ๋ณ ์ด์ฉ๊ณํ ์ง์๋ฐฉ๋ฒ", value=data.get("support_methods", ""), height=150) data["action_plan"] = st.text_area("์กฐ์น๊ณํ", value=data["action_plan"], height=130) score, missing, result = evaluate_record(data) data["evaluation_score"] = score data["missing_items"] = ", ".join(missing) if missing else "๋ถ์กฑ ํญ๋ชฉ ์์" data["compliance_result"] = result
analysis = analyze_counseling_answers(data) m1, m2, m3 = st.columns(3) m1.metric("ํ๊ฐ ์ ํฉ๋", f"{score}์ ") m2.metric("๋ถ์กฑ ํญ๋ชฉ", data["missing_items"]) m3.metric("์ถ๋ ฅ", "์์
์ ํ ์์ฑ")
link1, link2, link3 = st.columns(3) link1.metric("์ํ๋ณํ๊ธฐ๋ก์ง", "ํ์" if analysis["status_needed"] else "๊ด์ฐฐ") link2.metric("์ฌ๋ก๊ด๋ฆฌ ์ฐ๊ณ", "๊ฒํ " if analysis["case_needed"] else "๋ฎ์") link3.metric("๊ธ์ฌ๊ณํ ๋ฐ์", "ํ์" if analysis["care_plan_needed"] else "์ ์ง") st.caption(analysis["status_reason"]) st.caption(analysis["case_reason"]) st.caption(analysis["care_plan_reason"]) if score >= 90: st.markdown(f'<div class="okbox">{result}</div>', unsafe_allow_html=True) else: st.markdown(f'<div class="warnbox">{result}</div>', unsafe_allow_html=True)
if st.button("๐ฆ ์์
ํ์ผ ์ค๋นํ๊ธฐ", use_container_width=True): st.session_state["counseling_excel"] = generate_excel(data) st.success("์์
ํ์ผ์ด ์ค๋น๋์์ต๋๋ค.")
if "counseling_excel" in st.session_state: filename = f"์๋ด์ผ์ง_{data['masked_name']}_{data['counseling_type']}_{datetime.now().strftime('%Y%m%d')}.xlsx" st.download_button("๐ฅ ์๋ด์ผ์ง ์์
๋ค์ด๋ก๋", data=st.session_state["counseling_excel"], file_name=filename, mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", use_container_width=True)
st.caption("๋น ๋ฅธ ์ถ์ ํ
์คํธ ๋ฒ์ : DB ์๋์กฐํ ์์ด ์ง์ ์
๋ ฅ์ผ๋ก ์๋ด์ผ์ง ํ์ง๊ณผ ์๋๋ฅผ ๋จผ์ ํ์ธํฉ๋๋ค.")app.pyAdded+980