-
Notifications
You must be signed in to change notification settings - Fork 0
Feat(#6): 대화 텍스트 내 URL 정규식 추출 기능 구현 #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| from fastapi import APIRouter | ||
| from app.dto.response import ApiResponse | ||
|
|
||
| # 음성 분석 전용 라우터 생성 | ||
| router = APIRouter(prefix="/analyze-voice", tags=["Analyze Voice"]) | ||
|
|
||
| @router.post("", response_model=ApiResponse[dict]) | ||
| async def analyze_voice_call(payload: dict): | ||
| """ | ||
| 스프링으로부터 음성 데이터 또는 STT 텍스트 정보를 받아 | ||
| 보이스피싱 및 악성 문맥 분석을 수행하는 엔드포인트 초안 | ||
| """ | ||
|
|
||
| # 더미 결과 구조 | ||
| dummy_result = { | ||
| "voice_phishing_detected": False, | ||
| "risk_score": 15, # 보이스피싱 위험도 점수 | ||
| "detected_keywords": [], # 탐지된 금융 사기 관련 키워드 목록 | ||
| "analysis_summary": "현재 통화 문맥상 금융 사기 및 피싱 징후가 발견되지 않은 안전한 상태입니다." | ||
| } | ||
|
|
||
| return ApiResponse.success(data=dummy_result, message="음성 분석이 완료되었습니다.") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import re | ||
|
|
||
| # URL을 탐지하기 위한 정규표현식 패턴 | ||
| URL_PATTERN = re.compile(r'https?://[^\s\'"<>]+') | ||
|
|
||
| def extract_urls_from_text(text: str) -> list[str]: | ||
| """ | ||
| 텍스트 본문에서 모든 웹 URL 주소를 추출하고 중복을 제거하여 반환합니다. | ||
| """ | ||
|
|
||
| if not text: | ||
| return [] | ||
|
|
||
| # 정규식 매칭 | ||
| raw_urls = URL_PATTERN.findall(text) | ||
|
|
||
| # 문장 끝에 붙은 불필요한 문장부호 우측 정제 | ||
| cleaned_urls = [] | ||
|
|
||
| for url in raw_urls: | ||
| cleaned_url = url.rstrip('.,?!:;)[]') | ||
| cleaned_urls.append(cleaned_url) | ||
|
|
||
| # 추출 순서를 보존하며 중복 제거 | ||
| unique_urls = list(dict.fromkeys(cleaned_urls)) | ||
|
|
||
| return unique_urls | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import unittest | ||
| from app.service.url.url_extractor import extract_urls_from_text | ||
|
|
||
| class TestUrlExtractor(unittest.TestCase): | ||
|
|
||
| def test_no_url_text(self): | ||
| """시나리오 1: URL이 아예 없는 순수 대화 텍스트 -> 빈 리스트 반환""" | ||
| text = "안녕하세요! 오늘 점심 뭐 드실래요? 맛있는 거 추천해주세요." | ||
| self.assertEqual(extract_urls_from_text(text), []) | ||
|
|
||
| def test_single_normal_url(self): | ||
| """시나리오 2: 일반적인 URL이 포함된 문장 -> 정상 추출""" | ||
| text = "국민은행 보안 업데이트 링크입니다. https://www.kookminbank.com 확인해보세요." | ||
| expected = ["https://www.kookminbank.com"] | ||
| self.assertEqual(extract_urls_from_text(text), expected) | ||
|
|
||
| def test_multiple_urls(self): | ||
| """시나리오 3: 한 문장에 서로 다른 URL이 2개 이상 포함된 문장 -> 모두 추출""" | ||
| text = "여기 구글 주소 https://google.com 이랑 네이버 주소 http://naver.com 보냅니다." | ||
| expected = ["https://google.com", "http://naver.com"] | ||
| self.assertEqual(extract_urls_from_text(text), expected) | ||
|
|
||
| def test_url_with_trailing_punctuation(self): | ||
| """시나리오 4: 문장 맨 끝에 온점이나 기호와 함께 URL이 위치한 경우 -> 기호 제외하고 깔끔하게 추출 (억까 방지)""" | ||
| text = "아래 단축 링크를 꼭 클릭해주세요: https://bit.ly/3xyz." | ||
| expected = ["https://bit.ly/3xyz"] | ||
| self.assertEqual(extract_urls_from_text(text), expected) | ||
|
|
||
| def test_duplicate_urls(self): | ||
| """시나리오 5: 동일한 URL이 반복되는 문장 -> 중복 제거되어 1개만 반환""" | ||
| text = "급합니다!! https://bit.ly/3xyz 빨리 확인하세요! 다시 보냅니다 https://bit.ly/3xyz" | ||
| expected = ["https://bit.ly/3xyz"] | ||
| self.assertEqual(extract_urls_from_text(text), expected) | ||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Preserve valid URL punctuation when trimming sentence punctuation.
rstrip('.,?!:;)[]')blindly removes valid trailing URL characters. For example,https://en.wikipedia.org/wiki/Foo_(bar)becomeshttps://en.wikipedia.org/wiki/Foo_(bar, which can make downstream URL analysis miss the original URL. Trim sentence punctuation only when it is clearly attached, and remove closing brackets only when they are unmatched.🐛 Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents