fix: /idea 긴 문장 검색 시 핵심 키워드 추출

'월드컵을 지상파에서 못보는 상황에 대한 반대의 글' → '월드컵 지상파에서 못보는'
불용어 제거 후 핵심 키워드 3~5개만 추출하여 Google 뉴스 검색.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
JOUNGWOOK KWON
2026-03-30 17:18:05 +09:00
parent 45a352c343
commit 3119823128

View File

@@ -263,8 +263,9 @@ def _fetch_sources_content(topic_data: dict) -> dict:
logger.info(f"[fetch_sources] need_search={need_search}, existing={len(existing_sources)}") logger.info(f"[fetch_sources] need_search={need_search}, existing={len(existing_sources)}")
if need_search: if need_search:
try: try:
search_url = f"https://news.google.com/rss/search?q={quote(topic)}&hl=ko&gl=KR&ceid=KR:ko" search_kw = _extract_search_keywords(topic) if len(topic) > 20 else topic
logger.info(f"[fetch_sources] RSS 검색: {topic[:40]}") search_url = f"https://news.google.com/rss/search?q={quote(search_kw)}&hl=ko&gl=KR&ceid=KR:ko"
logger.info(f"[fetch_sources] RSS 검색: {search_kw}")
rss_resp = requests.get(search_url, timeout=8, headers={'User-Agent': 'Mozilla/5.0'}) rss_resp = requests.get(search_url, timeout=8, headers={'User-Agent': 'Mozilla/5.0'})
feed = feedparser.parse(rss_resp.text) feed = feedparser.parse(rss_resp.text)
logger.info(f"[fetch_sources] RSS 결과: {len(feed.entries)}") logger.info(f"[fetch_sources] RSS 결과: {len(feed.entries)}")
@@ -911,14 +912,31 @@ async def cmd_idea(update: Update, context: ContextTypes.DEFAULT_TYPE):
) )
def _extract_search_keywords(text: str) -> str:
"""긴 문장에서 검색용 핵심 키워드 추출 (3~5개 명사/키워드)"""
# 불용어 제거
stopwords = {'', '', '', '', '', '', '', '', '', '으로', '에서', '', '',
'대한', '위한', '대해', '관한', '통한', '있는', '없는', '하는', '되는', '하고',
'', '이런', '저런', '어떤', '모든', '같은', '다른', '라는', '라고',
'', '글을', '내용', '정보', '상황', '경우', '부분', '반대'}
# 단어 분리 후 불용어 제거, 1글자 제외
words = [w for w in text.split() if w not in stopwords and len(w) > 1]
# 최대 5개 키워드
return ' '.join(words[:5])
def _search_and_build_topic(keyword: str, corner: str = '') -> dict: def _search_and_build_topic(keyword: str, corner: str = '') -> dict:
"""키워드로 Google 뉴스 검색 → 관련 기사 수집 → topic_data 생성""" """키워드로 Google 뉴스 검색 → 관련 기사 수집 → topic_data 생성"""
import requests import requests
import feedparser import feedparser
from urllib.parse import quote from urllib.parse import quote
# 긴 문장이면 핵심 키워드 추출
search_keyword = _extract_search_keywords(keyword) if len(keyword) > 20 else keyword
logger.info(f"[_search] 검색 키워드: {search_keyword} (원문: {keyword[:30]})")
# Google 뉴스 RSS로 검색 (requests로 빠르게 가져온 후 feedparser 파싱) # Google 뉴스 RSS로 검색 (requests로 빠르게 가져온 후 feedparser 파싱)
search_url = f"https://news.google.com/rss/search?q={quote(keyword)}&hl=ko&gl=KR&ceid=KR:ko" search_url = f"https://news.google.com/rss/search?q={quote(search_keyword)}&hl=ko&gl=KR&ceid=KR:ko"
sources = [] sources = []
best_description = '' best_description = ''
best_image = '' best_image = ''