'이상한 나라의 앨리스'에서 앨리스는 몇번나올까?
1) 이상한 나라의 앨리스 txt파일을 구한다.
2) 이 파일에서 앨리스 단어의 개수를 구한다.
3) 이 파일에서 입력받은 단어들의 개수를 구한다.
-5개의 단어를 사용자한테 입력받는다.
ex)피오키오 할아버지, 베짱이, ... 이 단어들이 몇번씩 나오는지 출력!
4) 3번에서 얻은 데이터를 기반으로 그래프로 시각화
--------------------피오키오만 찾기------------------
book = open("피노키오의모험.txt",'r',encoding = 'utf-8')
file=book.read()
cnt = file.count("피노키오")
print(cnt)
book.close()
-----------------단어 5개 입력받고 결과출력------------
book = open("피노키오의모험.txt", 'r', encoding='utf-8')
file_content = book.read()
search_words = input("찾을 단어들을 공백으로 구분하여 입력하세요: ").split()
for word in search_words:
count = file_content.count(word)
print(f'단어 "{word}"는 파일 내에서 {count}번 나타납니다.')
book.close()
-----------------꺽은선 그래프로 출력-------------
import matplotlib.pyplot as plt
plt.rc('font', family='Malgun Gothic')
book = open("피노키오의모험.txt", 'r', encoding='utf-8')
file_content = book.read()
count_list = [] # 리스트 이름 수정
search_words = input("찾을 단어들을 공백으로 구분하여 입력하세요: ").split()
for word in search_words:
count = file_content.count(word)
count_list.append(count)
print(f'단어 "{word}"는 파일 내에서 {count}번 나타납니다.')
book.close()
# 시각화
plt.title("각 단어의 수")
plt.plot(search_words, count_list, marker='o') # 각 단어에 대한 빈도를 선 그래프로 표시
plt.xlabel("단어")
plt.ylabel("빈도")
plt.show()
-----------------막대그래프로 출력--------------
import matplotlib.pyplot as plt
plt.rc('font',family = 'Malgun Gothic')
book = open("피노키오의모험.txt", 'r', encoding='utf-8')
file_content = book.read()
count_list = []
search_words = input("찾을 단어들을 공백으로 구분하여 입력하세요: ").split()
for word in search_words:
count = file_content.count(word)
count_list.append(count) # 각 단어의 빈도를 리스트에 추가
print(f'단어 "{word}"는 파일 내에서 {count}번 나타납니다.')
book.close()
# 시각화
plt.title("각 단어의 수")
plt.bar(search_words, count_list) # 각 단어에 대한 빈도를 막대 그래프로 표시
plt.xlabel("단어")
plt.ylabel("빈도")
plt.show()
-----------------셀프테스트--------------
1.주제찾기
2.단어입력받기
3.단어개수출력
4.데이터시각화
-----------------꺽은선 그래프로 출력-------------
book = open("동인사담집_1_토끼의_간.txt", 'r',encoding='utf-8')
file_content = book.read()
count_list = [] # 빈도수를 저장할 리스트
words = input("찾을 단어들을 입력하세요 (공백으로 구분): ").split()
for word in words:
count = file_content.count(word)
count_list.append(count)
print(f'단어 "{word}"는 파일에서 {count}번 나타납니다.')
book.close()
# 시각화
import matplotlib.pyplot as plt
plt.rc('font', family='Malgun Gothic')
plt.title("각 단어의 수")
plt.plot(words, count_list, marker='o', linestyle='-', color='b') # 꺽은선 그래프로 표시
plt.xlabel("단어")
plt.ylabel("빈도")
plt.show()
-----------------막대그래프로 출력----------------
book = open("동인사담집_1_토끼의_간.txt", 'r',encoding='utf-8')
file_content = book.read()
count_list = [] # 빈도수를 저장할 리스트
words = input("찾을 단어들을 입력하세요 (공백으로 구분): ").split()
for word in words:
count = file_content.count(word)
count_list.append(count)
print(f'단어 "{word}"는 파일에서 {count}번 나타납니다.')
book.close()
# 시각화
import matplotlib.pyplot as plt
plt.rc('font', family='Malgun Gothic')
plt.title("각 단어의 수")
plt.bar(words, count_list) # 각 단어에 대한 빈도를 막대 그래프로 표시
plt.xlabel("단어")
plt.ylabel("빈도")
plt.show()
'(학) (공) (파)' 카테고리의 다른 글
01.31(16일차) (2) | 2024.01.31 |
---|---|
01.29(14일차) -2 (0) | 2024.01.30 |
01.29(14일차) (0) | 2024.01.30 |
01.26(13일차) (1) | 2024.01.26 |
01.25(12일차) (0) | 2024.01.25 |