[feat] solution/backend: 발행 때 온톨로지 키워드 생성을 켠다 — 호텔 판정 · 업종별 유형어 게이트

generate:false 로 불러 새 숙소가 사전 밖 단어를 받지 못했다. 생성을 켜면 호텔이 펜션 사전의
`군산 독채펜션` 을 받는데, 메타 게이트가 '펜션' 을 업종어로 보고 자료 없이 통과시켰다.

- site_ontology: generate+sync — 처음 보는 숙소만 저쪽이 기다리게 하고 publish 상한 60초
- seo_keywords: 외부 분류(없으면 상호)에 '호텔' 이면 stay.hotel, 자기 유형어만 자료 없이 통과
- snapshot: place.external_category 를 실음 — 호텔 판정 근거
- .env.example · ARCHITECTURE · DEVLOG: ONTOLOGY_LLM_PROVIDER, 발행 흐름, 실측

test_seo_keywords 20 passed(호텔 4건 추가). 전체 1041 passed / 47 failed —
47건은 이 변경 전 main 과 같은 목록

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
HyeongBaeYangandClaude Opus 5.5 committed 2026-10-01 13:49:37 +09:00
1 parent 847943a8bc
commit 94c4116689
7 files changed
+86 -20

No files matched your search

+8 -5
View File
@@ -7,6 +7,8 @@ from config.server_configs import external_api_config
# 로컬 임베딩 모델이라 호출당 1~2초다.
TIMEOUT_SEC = 15.0
# 처음 보는 업체는 publish 가 OpenAI 키워드 생성을 기다린다(키워드 15개 + 질문 5개).
PUBLISH_TIMEOUT_SEC = 60.0
DEFAULT_LIMIT = 40
PUBLISH_PATH = "/v1/merchants/publish"
@@ -25,8 +27,8 @@ def is_configured() -> bool:
return bool(base_url())
async def _post(client: httpx.AsyncClient, path: str, body: dict) -> dict:
res = await client.post(f"{base_url()}{path}", json=body)
async def _post(client: httpx.AsyncClient, path: str, body: dict, timeout: float = TIMEOUT_SEC) -> dict:
res = await client.post(f"{base_url()}{path}", json=body, timeout=timeout)
if res.status_code >= 400:
raise SiteOntologyError(f"{path} HTTP {res.status_code}: {res.text[:200]}")
try:
@@ -40,17 +42,18 @@ async def _post(client: httpx.AsyncClient, path: str, body: dict) -> dict:
async def match_for_merchant(merchant: dict, limit: int = DEFAULT_LIMIT) -> dict:
"""업체를 저장하고, 그 업체로 해석된 추천 결과(/v1/match 응답)를 돌려준다."""
body = {**merchant, "generate": False}
# sync 는 처음 보는 업체에만 먹는다 — 첫 발행부터 새 키워드가 실리고, 갱신은 저쪽 큐가 한다.
body = {**merchant, "generate": True, "sync": True}
try:
async with httpx.AsyncClient(timeout=TIMEOUT_SEC) as client:
try:
await _post(client, PUBLISH_PATH, body)
await _post(client, PUBLISH_PATH, body, PUBLISH_TIMEOUT_SEC)
except SiteOntologyError as ex:
if not body.get("regionId"):
raise
LOG.w(f"[site-ontology] regionId={body['regionId']} 로 저장하지 못했다 — 지역 없이 다시 보낸다: {ex}")
body = {**body, "regionId": None}
await _post(client, PUBLISH_PATH, body)
await _post(client, PUBLISH_PATH, body, PUBLISH_TIMEOUT_SEC)
result = await _post(client, MATCH_PATH, {"query": merchant["externalId"], "limit": limit})
except httpx.HTTPError as ex:
raise SiteOntologyError(f"{type(ex).__name__}: {ex}") from ex
+23 -12
View File
@@ -9,6 +9,9 @@ from services.external import site_ontology
from services.site_payload import _parse_address_parts
LODGING_INDUSTRY = "stay.pension"
HOTEL_INDUSTRY = "stay.hotel"
# 업종별 숙박 유형어. 자기 유형어만 자료 없이 통과한다 — 호텔에 `군산 독채펜션` 이 붙지 않게.
_TYPE_WORDS = {LODGING_INDUSTRY: "펜션", HOTEL_INDUSTRY: "호텔"}
MAX_KEYWORDS = 10
MAX_NEARBY = 8
@@ -57,9 +60,9 @@ _REGION_KEYS = {
}
# 자료에 없어도 되는 낱말 — 업종어와 "근처" 류.
_GENERIC_WORDS = frozenset({"펜션", "숙소", "숙박", "스테이", "근처", "가까운", "주변", "인근", "예약", "추천"})
_GENERIC_WORDS = frozenset({"숙소", "숙박", "스테이", "근처", "가까운", "주변", "인근", "예약", "추천"})
# "독채펜션"·"감성숙소" 처럼 붙여 쓴 업종어는 떼고 앞부분만 자료와 대조한다.
_GENERIC_SUFFIXES = ("펜션", "숙소", "스테이")
_GENERIC_SUFFIXES = ("숙소", "스테이")
# 제목에는 싣지 않는 낱말.
_TITLE_BLOCKED_WORDS = frozenset({"예약", "추천"})
# 한 글자 낱말("봄"·"뷰")은 소개문 어딘가에 우연히 들어 있어 대조가 무의미하다 — 통과시키지 않는다.
@@ -102,6 +105,12 @@ def _locality_word(*addresses: str | None) -> str:
return word[:-1] if len(word) > 2 and word.endswith(("시", "군", "구")) else word
def industry_of(place: dict) -> str:
"""외부 분류(없으면 상호)에 '호텔' 이 있으면 호텔, 아니면 펜션."""
text = f"{_text(place.get('external_category'))} {_text(place.get('name'))}"
return HOTEL_INDUSTRY if "호텔" in text else LODGING_INDUSTRY
def build_merchant(place_id: str, snapshot: dict) -> dict | None:
"""스냅샷 → SiteOntology publish 요청 본문."""
place = (snapshot or {}).get("place") or {}
@@ -157,7 +166,7 @@ def build_merchant(place_id: str, snapshot: dict) -> dict | None:
return {
"externalId": place_id,
"name": name,
"industryId": LODGING_INDUSTRY,
"industryId": industry_of(place),
"regionId": region_key(road_address, address),
"description": description,
"profile": {key: value for key, value in profile.items() if value},
@@ -174,13 +183,14 @@ def _evidence(merchant: dict) -> str:
return _compact(" ".join(_text(p) for p in parts if p))
def _supported(keyword: str, evidence: str) -> bool:
def _supported(keyword: str, evidence: str, type_word: str = "펜션") -> bool:
"""모든 낱말이 자료에 있고, **자료로 확인한 낱말이 하나는 있어야** 한다."""
suffixes = (*_GENERIC_SUFFIXES, type_word)
specific = False
for word in keyword.split():
if word in _GENERIC_WORDS:
if word in _GENERIC_WORDS or word == type_word:
continue
core = next((word[: -len(s)] for s in _GENERIC_SUFFIXES if word.endswith(s) and len(word) > len(s)), word)
core = next((word[: -len(s)] for s in suffixes if word.endswith(s) and len(word) > len(s)), word)
core = _compact(core)
if len(core) < _MIN_CORE_LEN or core not in evidence:
return False
@@ -193,15 +203,15 @@ def _is_question(item: dict) -> bool:
return item.get("category") == "질문형" or "?" in canonical or canonical.endswith(("요", "까"))
def _usable(item, evidence: str) -> str | None:
def _usable(item, evidence: str, type_word: str) -> str | None:
"""메타 태그에 실어도 되는 추천이면 그 표기를, 아니면 None."""
if not isinstance(item, dict) or item.get("status") != "ok" or _is_question(item):
return None
canonical = _text(item.get("canonical"))
return canonical if canonical and _supported(canonical, evidence) else None
return canonical if canonical and _supported(canonical, evidence, type_word) else None
def _title_keyword(result: dict, evidence: str, locality: str) -> str | None:
def _title_keyword(result: dict, evidence: str, locality: str, type_word: str) -> str | None:
"""제목 업종어 자리에 넣을 대표 키워드 — 유형 레인에서 고른다."""
lane = next(
(l for l in result.get("byLane") or [] if isinstance(l, dict) and l.get("key") == "type"),
@@ -209,7 +219,7 @@ def _title_keyword(result: dict, evidence: str, locality: str) -> str | None:
)
items = [i for i in lane.get("items") or [] if isinstance(i, dict)]
for item in sorted(items, key=lambda i: i.get("category") != "코어"):
canonical = _usable(item, evidence)
canonical = _usable(item, evidence, type_word)
if not canonical or _TITLE_BLOCKED_WORDS.intersection(canonical.split()):
continue
if locality and locality not in canonical:
@@ -221,10 +231,11 @@ def _title_keyword(result: dict, evidence: str, locality: str) -> str | None:
def select(result: dict, merchant: dict) -> dict:
"""/v1/match 응답 → {keywords, titleKeyword?}."""
evidence = _evidence(merchant)
type_word = _TYPE_WORDS.get(merchant.get("industryId"), _TYPE_WORDS[LODGING_INDUSTRY])
keywords: list[str] = []
seen: set[str] = set()
for item in result.get("matches") or []:
canonical = _usable(item, evidence)
canonical = _usable(item, evidence, type_word)
if not canonical or _compact(canonical) in seen:
continue
seen.add(_compact(canonical))
@@ -233,7 +244,7 @@ def select(result: dict, merchant: dict) -> dict:
break
seo: dict = {"keywords": keywords}
title = _title_keyword(result, evidence, _locality_word((merchant.get("profile") or {}).get("address")))
title = _title_keyword(result, evidence, _locality_word((merchant.get("profile") or {}).get("address")), type_word)
if title:
seo["titleKeyword"] = title
return seo
+2
View File
@@ -109,6 +109,8 @@ async def build_snapshot(place) -> dict:
"name": place.name,
"category": category.value,
"category_name": schema.label,
# 숙박 안의 유형(호텔·펜션) 판정 근거 — services/seo_keywords.industry_of
"external_category": getattr(place, "external_category", None),
"road_address": place.road_address,
"address": place.address,
"phone": place.phone,
+31 -2
View File
@@ -177,8 +177,8 @@ async def test_업체를_저장한_뒤_그_업체로_추천을_받는다(ontolog
await site_ontology.match_for_merchant(merchant)
assert [url for url, _ in sent] == ["http://onto.test/v1/merchants/publish", "http://onto.test/v1/match"]
# generate:false 가 빠지면 SiteOntology 가 LLM 키워드 생성을 큐에 넣는다.
assert sent[0][1] == {**merchant, "generate": False}
# 처음 보는 업체는 생성을 기다려 첫 발행부터 새 키워드가 실린다(sync 는 저쪽이 처음 보는 업체에만 먹인다).
assert sent[0][1] == {**merchant, "generate": True, "sync": True}
assert sent[1][1] == {"query": "place-1", "limit": site_ontology.DEFAULT_LIMIT}
@@ -296,3 +296,32 @@ async def test_SiteOntology_가_죽어도_발행된다(auth_headers, client, db_
assert site["site"]["status"] == SiteStatus.PUBLISHED.value
payload = json.loads(open(r["payload_path"], encoding="utf-8").read())
assert "seo" not in payload
# ── 호텔 ───────────────────────────────────────────────────────────────────
def test_외부_분류에_호텔이_있으면_호텔_업종으로_보낸다():
merchant = seo_keywords.build_merchant("place-1", _snapshot(external_category="여행 > 숙박 > 호텔"))
assert merchant["industryId"] == "stay.hotel"
def test_외부_분류가_없으면_상호로_가른다():
assert seo_keywords.build_merchant("place-1", _snapshot(name="군산 베이 호텔"))["industryId"] == "stay.hotel"
assert seo_keywords.build_merchant("place-1", _snapshot())["industryId"] == "stay.pension"
def _item(canonical):
return {"canonical": canonical, "status": "ok", "category": "코어"}
def test_호텔에는_펜션_유형어를_싣지_않는다():
merchant = seo_keywords.build_merchant("place-1", _snapshot(name="군산 베이 호텔"))
result = {"matches": [_item("군산 독채펜션"), _item("군산 독채 호텔"), _item("말랭이마을 숙소")]}
assert seo_keywords.select(result, merchant)["keywords"] == ["군산 독채 호텔", "말랭이마을 숙소"]
def test_펜션에는_호텔_유형어를_싣지_않는다():
merchant = seo_keywords.build_merchant("place-1", _snapshot())
result = {"matches": [_item("군산 독채 호텔"), _item("군산 독채펜션")]}
assert seo_keywords.select(result, merchant)["keywords"] == ["군산 독채펜션"]