File size: 1,415 Bytes
5ea2b9d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | import requests
from bs4 import BeautifulSoup
def get_synonyms_from_daum(word: str) -> list[str]:
try:
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'
}
params = {
'q': word
}
response = requests.get("https://dic.daum.net/search.do", params=params, headers=headers)
response.raise_for_status()
return extract_synonyms_from_html(response.text)
except Exception as e:
print(f"Error fetching from Daum: {e}")
def extract_synonyms_from_html(html: str) -> list[str]:
try:
soup = BeautifulSoup(html, 'html.parser')
synonyms = []
for tag in soup.select('.link_relate'):
text = tag.get_text(strip=True)
if text and text not in synonyms:
synonyms.append(text)
print(f"Extracted synonyms: {synonyms}")
return synonyms
except Exception as e:
print(f"Error parsing HTML: {e}")
return []
# ์์ ์ฌ์ฉ
# word = "๋ฅ๋ ฅ"
# synonyms = get_synonyms_from_daum(word)
def mask_by_position(sentence: str, start: int, end: int) -> str:
return sentence[:start-1] + "[MASK]" + sentence[end-1:]
# a = mask_by_position("๊ฐ์๊ฐ ์
๋ฌด๋ฅผ ํ๋ฉด์ ๊ฐ์ธ์ ๋ฅ๋ ฅ์ผ๋ก ๋ชจ๋ ์ผ์ ํด๊ฒฐํ ์ ์๋ค๋ฉด ์ ๋ง ๊ธฐ์ ๊ฒ์
๋๋ค",17,19)
# print(a)
|