본문 바로가기

부스트캠프 AI Tech/1주차 필수과제

[Week1 - 필수과제2] Text_Processing_1

*내용

 

  • Python String 다루는 방법
  • 여러가지 조건으로 string 변환

 

 

Problem1) 인풋으로 받는 스트링에서 정규화된 스트링 반환

 

def normalize(input_string):
    """
    * 모든 단어들은 소문자로 되어야함
    * 띄어쓰기는 한칸으로 되어야함
    * 앞뒤 필요없는 띄어쓰기는 제거해야함
         Parameters:
             input_string (string): 영어로 된 대문자, 소문자, 띄어쓰기, 문장부호, 숫자로 이루어진 string
             ex - "This is an example.", "   EXTRA   SPACE   "
         Returns:
             normalized_string (string): 위 요건을 충족시킨 정규회된 string
             ex - 'this is an example.'
    """
    normalized_string = None

    s = input_string.strip()
    s = s.lower()
    str_list = s.split()
    word = ""
    for i in str_list:
        word += i+" "

    normalized_string = word.rstrip()

    return normalized_string

 

 

Problem2) 인풋으로 받는 스트링에서 모든 모음 (a, e, i ,o ,u)를 제거 시킨 스트링을 반환

 

def no_vowels(input_string):
    """
        Parameters:
            input_string (string): 영어로 된 대문자, 소문자, 띄어쓰기, 문장부호로 이루어진 string
            ex - "This is an example."
        Returns:
            no_vowel_string (string): 모든 모음 (a, e, i, o, u)를 제거시킨 스트링
            ex - "Ths s n xmpl."
    """
    no_vowel_string = None
    no_vowel_string = input_string.replace("a","")
    no_vowel_string = no_vowel_string.replace("e","")
    no_vowel_string = no_vowel_string.replace("i","")
    no_vowel_string = no_vowel_string.replace("o","")
    no_vowel_string = no_vowel_string.replace("u","")
    no_vowel_string = no_vowel_string.replace("A","")
    no_vowel_string = no_vowel_string.replace("E","")
    no_vowel_string = no_vowel_string.replace("I","")
    no_vowel_string = no_vowel_string.replace("O","")
    no_vowel_string = no_vowel_string.replace("U","")

    return no_vowel_string

 

*학습회고

 

join, split으로 충분히 간결하게 할 수 있을것 같다.

Problem2)도 list를 만들어서 반복문으로 스크립트를 줄일 수 있을것 같다.