λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
βœ’οΈ Kibwa Voice Phishing Prev Project/Data Processing

[Data Processing] 단어 μ‚­μ œλ₯Ό ν†΅ν•œ 데이터 증강(Data Augmentation)

by A Lim Han 2023. 7. 28.

πŸ’ƒ 단어 μ‚­μ œλ₯Ό ν†΅ν•œ 데이터 증강(Data Augmentation)

1. OS 와 Random λͺ¨λ“ˆ Import

import os
import random

 

OS Random
운영 μ²΄μ œμ™€ μƒν˜Έμž‘μš©μ„ μœ„ν•œ λͺ¨λ“ˆλ‘œ,
λ””λ ‰ν† λ¦¬λ‚˜ 파일과 κ΄€λ ¨λœ λ‹€μ–‘ν•œ μž‘μ—…μ— 이용됨
λ‚œμˆ˜ 생성 및 μ‹œν€€μŠ€μ—μ„œμ˜
λ¬΄μž‘μœ„ μš”μ†Œ 선택 λ“±μ˜ κΈ°λŠ₯을 μœ„ν•œ λͺ¨λ“ˆ

2. 주어진 파일 κ²½λ‘œμ—μ„œ λžœλ€ν•˜κ²Œ ν•˜λ‚˜μ˜ 단어λ₯Ό μ‚­μ œν•˜λŠ” remove_random_word() ν•¨μˆ˜ κ΅¬ν˜„

def remove_random_word(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        content = file.read()
        words = content.split()

        if len(words) > 1:  # Ensure there's at least one word to remove
            index_to_remove = random.randint(0, len(words) - 1)
            words.pop(index_to_remove)

    with open(file_path, 'w', encoding='utf-8') as file:
        file.write(' '.join(words))

 

# ν•¨μˆ˜ 호좜 μ‹œ μ‹€ν–‰ 경둜

β‘  νŒŒμΌμ„ 읽은 ν›„ κ·Έ λ‚΄μš©μ„ 'content' 에 μ €μž₯

β‘‘ 곡백을 κΈ°μ€€μœΌλ‘œ 'content' λ₯Ό λΆ„ν• 

β‘’ λΆ„ν• ν•œ 결과물을 리슀트 'words' 에 μ €μž₯

β‘£ 'words' 의 길이가 1보닀 큰 경우(= μ‚­μ œν•  단어가 μ‘΄μž¬ν•˜λŠ” 경우) λžœλ€ν•œ 인덱슀 선택

β‘€ μ„ νƒλœ μΈλ±μŠ€μ— ν•΄λ‹Ήν•˜λŠ” 단어λ₯Ό 'words' μ—μ„œ μ‚­μ œ

β‘₯ λ³€κ²½λœ 'words' λ₯Ό νŒŒμΌμ— λ‹€μ‹œ μ“°λ©° 단어가 μ‚­μ œλœ μƒνƒœλ‘œ 파일 μ €μž₯

3. remove_random_word() ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•˜κΈ° μœ„ν•œ process_files() ν•¨μˆ˜ κ΅¬ν˜„

def process_files(folder_path):
    for i in range(1, 증강할 파일 개수 + 1):
        print("데이터 증강에 μ„±κ³΅ν–ˆμŠ΅λ‹ˆλ‹€.")
        file_name = f"file ({i}).txt"
        file_path = os.path.join(folder_path, file_name)
        if os.path.isfile(file_path):
            remove_random_word(file_path)

 

# ν•¨μˆ˜ 호좜 μ‹œ μ‹€ν–‰ 경둜

β‘  1λΆ€ν„° "증강할 파일 개수 + 1" κΉŒμ§€μ˜ 수λ₯Ό μˆœνšŒν•˜λ©° 파일 탐색

β‘‘ 파일이 μ‘΄μž¬ν•˜λŠ” 경우, remove_random_word() ν•¨μˆ˜λ₯Ό 호좜

β‘’ ν•΄λ‹Ή νŒŒμΌμ—μ„œ 단어λ₯Ό λžœλ€ν•˜κ²Œ μ‚­μ œ

4. λͺ¨λ“ˆκ³Ό 슀크립트의 λ™μž‘ ꡬ뢄을 μœ„ν•΄ if __name__ == "__main__" ꡬ문 μž‘μ„±

if __name__ == "__main__":
    folder_path = r"파일이 μœ„μΉ˜ν•œ 경둜"
    process_files(folder_path)

 

++ if __name__ == "__main__" ꡬ문은 μ™œ ν•„μš”ν• κΉŒ?

if __name__ == "__main__"  은 μŠ€ν¬λ¦½νŠΈκ°€ 직접 μ‹€ν–‰λ˜λŠ” 경우(μŠ€ν¬λ¦½νŠΈκ°€ λͺ¨λ“ˆλ‘œ μž„ν¬νŠΈλ˜μ§€ μ•Šκ³  λ°”λ‘œ μ‹€ν–‰λ˜λŠ” 경우)에 ν•œν•΄μ„œλ§Œ process_files ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•˜λŠ” 역할을 μˆ˜ν–‰ν•˜λŠ” ꡬ문이닀.

즉, μœ„ ꡬ문은 μŠ€ν¬λ¦½νŠΈκ°€ λͺ¨λ“ˆλ‘œ μž„ν¬νŠΈλ˜μ—ˆμ„ λ•Œ νŠΉμ • μ½”λ“œ 블둝이 μžλ™μœΌλ‘œ μ‹€ν–‰λ˜λŠ” 것을 λ°©μ§€ν•˜λ©°, 이λ₯Ό 톡해 λͺ¨λ“ˆκ³Ό 슀크립트의 λ™μž‘μ„ κ΅¬λΆ„ν•˜μ—¬ μ½”λ“œλ₯Ό μ‹€ν–‰ν•  수 μžˆλ‹€.

 

 


 

πŸ’ƒ 단어 μ‚­μ œλ₯Ό ν†΅ν•œ 데이터 증강(Data Augmentation) 슀크립트 μ½”λ“œ μ „λ¬Έ

import os
import random

def remove_random_word(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        content = file.read()
        words = content.split()

        if len(words) > 1:  # Ensure there's at least one word to remove
            index_to_remove = random.randint(0, len(words) - 1)
            words.pop(index_to_remove)

    with open(file_path, 'w', encoding='utf-8') as file:
        file.write(' '.join(words))

def process_files(folder_path):
    for i in range(1, 2233):
        print("μ™„λ£Œ")
        file_name = f"file ({i}).txt"
        file_path = os.path.join(folder_path, file_name)
        if os.path.isfile(file_path):
            remove_random_word(file_path)

if __name__ == "__main__":
    folder_path = r"C:\Users\USER\Desktop\Data Augmentation_단어 μ‚­μ œ(μˆ˜μ‚¬κΈ°κ΄€ μ‚¬μΉ­ν˜•)"
    process_files(folder_path)