시작하기무료로 시작하기

반복된 문자 찾기

감성 분석으로 돌아가 볼까요? 다음 과제는 트윗에 나타나는 늘어진(길게 늘인) 단어를 치환하는 일입니다. 늘어진 단어란 동일 문자가 두 번 이상 연속해서 반복되는 단어를 말해요. 예: "Awesoooome".

이런 단어를 치환하는 것은 아주 중요해요. 분류기가 원래 단어와 다른 용어로 취급해 빈도가 낮아질 수 있기 때문이에요.

이를 찾기 위해 캡처 그룹을 사용하고, 숫자로 다시 참조할 거예요. 예: \4.

Awesoooome을 찾고 싶다면, 먼저 Awes를 캡처하고, 그다음 o를 매치한 뒤 같은 문자를 다시 참조하고, 마지막으로 me를 매치하면 됩니다.

세 개의 트윗 텍스트가 담긴 리스트 sentiment_analysisre 모듈이 세션에 로드되어 있어요. IPython 셸에서 print()로 데이터를 확인할 수 있어요.

이 연습은 강의의 일부입니다

Python에서의 정규 표현식

강의 보기

연습 안내

  • 설명한 대로 늘어진 단어를 매치하는 정규식을 완성하세요.
  • sentiment_analysis 리스트의 요소들에서 늘어진 단어가 있는지 검색하고 결과를 match_elongated에 할당하세요.
  • 캡처한 그룹 번호 0을 변수 elongated_word에 할당하세요.
  • 변수 elongated_word에 담긴 결과를 출력하세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

# Complete the regex to match an elongated word
regex_elongated = r"____(____)____\w*"

for tweet in sentiment_analysis:
	# Find if there is a match in each tweet 
	match_elongated = re.____(____, ____)
    
	if match_elongated:
		# Assign the captured group zero 
		elongated_word = match_elongated.____(____)
        
		# Complete the format method to print the word
		print("Elongated word found: {____}".format(word=____))
	else:
		print("No elongated word found") 
코드 편집 및 실행