Developments

파이썬으둜 μ˜λ‹¨μ–΄ λžœλ€ν•˜κ²Œ 500개 μƒμ„±ν•˜κΈ°

ν‘Έλ₯ΈκΉƒλ°œπŸ³οΈ 2024. 7. 16. 01:30

 

νŒŒμΈνŠœλ‹ 데이터λ₯Ό λ§Œλ“€λ˜ 쀑 μ•„λž˜μ²˜λŸΌ μ˜λ‹¨μ–΄μ™€ 뜻으둜 κ΅¬μ„±λœ json파일이 ν•„μš”ν–ˆλ‹€. λŒ€λž΅ 500κ°œμ •λ„ 있으면 μΆ©λΆ„ν•  κ±° κ°™μ•˜λ‹€.

{
    "plain": "μ†”μ§ν•œ",
    "parallel": "ν‰ν–‰ν•œ",
    "crack": "금이 κ°€λ‹€",
    "Red": "빨간색",
    "deal": "거래",
    "size": "크기",
}

 

ν˜Ήμ‹œλ‚˜ν•΄μ„œ chatgptμ—κ²Œ λ¬Όμ–΄λ³΄λ‹ˆ λ°”λ‘œ λ§Œλ“€μ–΄μ€¬λ‹€. 그런데 단지 결과만 μ•Œλ €μ€€κ²Œ μ•„λ‹ˆλΌ 파이썬 μ½”λ“œλ„ ν•¨κ»˜ μ•Œλ €μ€¬λ‹€.

λ‚˜λŠ” μ½”λ“œλ₯Ό μš”κ΅¬ν•œμ λ„ μ—†μ—ˆλŠ”λ° 말이닀. (μ•„λ§ˆλ„ 이전 λŒ€ν™”κΈ°λ‘μ„ λ°”νƒ•μœΌλ‘œ μΆ”λ‘ ν–ˆμ„κ²ƒμ΄λ‹€)

 

λ”κ΅¬λ‚˜ λ‚˜λŠ” μ½”λ“œλ‘œ 이런걸 λ§Œλ“€μ–΄λ‚΄λŠ”μˆ˜κ°€ μžˆμ„κ±°λΌ μƒκ°ν•œμ λ„ μ—†μ—ˆλ‹€.

μ½”λ“œλ₯Ό λ³΄λ‹ˆ ntlkλΌλŠ” 라이브러리 μ•ˆμ— μ˜μ–΄μ‚¬μ „μ΄ 있고 이것을 가져와 ꡬ글 λ²ˆμ—­ api둜 λ²ˆμ—­μ„ ν•˜λŠ” 것 κ°™λ‹€.

 

막상 μ½”λ“œλ₯Ό λŒλ €λ³΄λ‹ˆ μ‚¬λžŒλ“€μ΄ 거의 μ‚¬μš©ν•˜μ§€ μ•ŠλŠ” 단어듀을 λ¬΄μž‘μœ„λ‘œ 500개 κ°€μ Έμ˜€κΈ°μ—

ν•œλ²ˆ 더 chatgpt의 νž˜μ„ 빌렀 μ½”λ“œλ₯Ό λ³΄κ°•ν–ˆλ‹€.

 

κΈ°λŠ₯은 μ•„λž˜μ™€ κ°™λ‹€.

1. μƒμ„±ν•˜κ³ μž ν•˜λŠ” μ˜λ‹¨μ–΄ 수λ₯Ό μž…λ ₯ν•˜λ©΄(μ•„λž˜ 주석뢀뢄을 μˆ˜μ •)
2. μƒμœ„λΉˆλ„ 5000개의 단어λ₯Ό λ¨Όμ € 가져와(λ¬Όλ‘  이것도 μ•„λž˜ μ½”λ“œμ—μ„œ 5000을 μ›ν•˜λŠ” κ°’μœΌλ‘œ μˆ˜μ •ν•˜λ©΄λœλ‹€)
3. κ·Έ 쀑 λͺ…사와 λ™μ‚¬λ§Œ μΆ”λ €λ‚Έ ν›„
4. λžœλ€ν•˜κ²Œ 500개만 jsonν˜•μ‹μœΌλ‘œ 파일둜 μ €μž₯ν•˜λŠ” 것이닀.

 

* 이제 돌렀보자

1. λ¨Όμ € μ„€μΉ˜ν• κ²Œ ν•˜λ‚˜μžˆλ‹€

pip install nltk googletrans==4.0.0-rc1

 

2. λ‹€μŒ μ•„λž˜ μ½”λ“œλ₯Ό κ·Έλƒ₯ λΆ™μ—¬λ„£κΈ°ν•˜κ³  돌리면 λœλ‹€.

- μ²˜μŒμ—λŠ” 사전을 λ‹€μš΄λ‘œλ“œ ν•˜λŠ”λ° μ‹œκ°„μ΄ μ’€ κ±Έλ¦°λ‹€

import random, time
import nltk
from nltk.corpus import words, brown
from collections import Counter
from googletrans import Translator
import json

# NLTK 데이터 λ‹€μš΄λ‘œλ“œ (ν•œλ²ˆλ§Œ μ‹€ν–‰ν•˜λ©΄ λ©λ‹ˆλ‹€)
nltk.download('words')
nltk.download('brown')
nltk.download('averaged_perceptron_tagger')

# μƒμ„±ν•˜κ³ μž ν•˜λŠ” μ˜λ‹¨μ–΄ 개수λ₯Ό μž…λ ₯
word_count = 500

# λͺ¨λ“  μ˜μ–΄ 단어 λͺ©λ‘
word_list = words.words()

# Brown μ½”νΌμŠ€μ˜ 단어 λͺ©λ‘κ³Ό λΉˆλ„ 계산
brown_words = brown.words()
word_freq = Counter(brown_words)

# λΉˆλ„κ°€ 높은 μƒμœ„ 단어 5000개 선택
common_words = {word for word, freq in word_freq.most_common(5000)}

# word_listμ—μ„œ μƒμœ„ λΉˆλ„ λ‹¨μ–΄λ§Œ 선택
filtered_word_list = [word for word in word_list if word.lower() in common_words]

# ν’ˆμ‚¬ νƒœκΉ…ν•˜μ—¬ λͺ…사와 λ™μ‚¬λ§Œ 필터링
tagged_words = nltk.pos_tag(filtered_word_list)

# λͺ…사와 동사 ν’ˆμ‚¬ νƒœκ·Έ λͺ©λ‘
noun_tags = {'NN', 'NNS', 'NNP', 'NNPS'}
verb_tags = {'VB', 'VBD', 'VBG', 'VBN', 'VBP', 'VBZ'}

# λͺ…사와 λ™μ‚¬λ§Œ 필터링
filtered_nouns_and_verbs = [word for word, tag in tagged_words if tag in noun_tags or tag in verb_tags]

# κ·Έ 쀑 λžœλ€ν•˜κ²Œ 500개의 단어λ₯Ό μ„ νƒν•©λ‹ˆλ‹€.
random_words = random.sample(filtered_nouns_and_verbs, word_count)
# Google Translate APIλ₯Ό μ‚¬μš©ν•˜μ—¬ λ²ˆμ—­ν•©λ‹ˆλ‹€.
translator = Translator()
translated_dict = {}

def save():
    with open('translated_words.json', 'w', encoding='utf-8') as f:
        json.dump(translated_dict, f, ensure_ascii=False, indent=4)

for idx, word in enumerate(random_words):
    try:
        translated_word = translator.translate(word, src='en', dest='ko').text
        print(idx+1, word, translated_word)
        translated_dict[word] = translated_word
        time.sleep(1)
    except:
        print('쀑간에 였λ₯˜λ°œμƒ. μ§€κΈˆκΉŒμ§€ μž‘μ—…ν•œ 것듀 μ €μž₯함')
        save()
        
    
# JSON 파일둜 μ €μž₯ν•©λ‹ˆλ‹€.
save()

print(f"{len(random_words)}개의 단어가 λ²ˆμ—­λ˜κ³  JSON 파일둜 μ €μž₯λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")

 

μœ„ μ½”λ“œλ₯Ό 돌리면 μ•„λž˜ 파일과 같은 κ²°κ³Όλ₯Ό 얻을 수 μžˆλ‹€.

translated_words.json
0.01MB

 

거의 100% chatgptκ°€ μ½”λ”©ν•œ 결과이고 λ‚˜λŠ” 편의λ₯Ό μœ„ν•œ μ‚¬μ†Œν•œ μˆ˜μ •κ³Ό μ½”λ“œ μ‘°ν•©λ§Œ ν–ˆλ‹€.

이μͺ½ 뢄야에 μžˆμœΌλ©΄μ„œλ„ μƒˆμ‚Ό λ†€λΌμš΄ μ„Έμƒμž„μ„ λŠλ‚€ ν•˜λ£¨μ˜€λ‹€.