파이썬의 문자열(String) 처리 메소드들과 디스크 상의 텍스트 파일 데이터를 읽어 들여 텍스트를 분석하는 실전 단어 분석 알고리즘을 학습합니다.
1. 텍스트 분석 핵심 용어 사전 (Glossary)
- String Slicing:
s[1:4],s[::-1]처럼 문자열의 지정된 인덱스 범위를 추출하거나 뒤집는 연산입니다. str.isalpha(): 읽어 들인 문자가 순수 알파벳 문자인지 검사하여 불리언(True/False)을 반환하는 메커니즘입니다.
2. 실전 코드: 파일 단어 분석 알고리즘 (maxWord & onlyLowerAlpha)
s = "Computer"
reversed_s = ""
for i in range(len(s)):
reversed_s += s[len(s) - 1 - i]
print(f"원문: {s} -> 뒤집은 문자열: {reversed_s}")
def onlyLowerAlpha(text):
clean_word = ""
for char in text:
if char.isalpha():
clean_word += char.lower()
return clean_word
def maxWordFromContent(content):
words = content.split()
maxword = ""
maxlen = 0
for word in words:
cleaned = onlyLowerAlpha(word)
if len(cleaned) > maxlen:
maxlen = len(cleaned)
maxword = cleaned
return maxword, maxlen
sample_text = "Love is real, real is love. Learning Python programming is fantastic!"
best_word, length = maxWordFromContent(sample_text)
print(f"가장 긴 단어: '{best_word}' (길이: {length})")