뉴스 3줄 요약하기
원본기사 : 카운트다운 들어간 아르테미스 계획…'달의 여신'은 미소지을까 (naver.com)
카운트다운 들어간 아르테미스 계획…'달의 여신'은 미소지을까
29일(현지시간) 발사 예정인 ‘우주발사시스템’(SLS)로켓이 미국 플로리다주 케네디 우주센터의 발사대 39B에 설치돼 있다. (사진=나사) 2025년 인류를 달로 보내기 위한 첫 번째 여정이 하루 앞으
n.news.naver.com
바이너리 파일 읽기
# 기사 이미지
f = open("news/image", 'rb')
image = f.readlines()
f.close()
print(image)
# 기사 본문
f = open("news/article", 'rb')
article = f.readlines()
f.close()
print(article)
# 아직 코드화 되어있는 상태라 알아 볼 수 없다.
base64 디코딩
# 기사 이미지 디코딩
import base64
file_base64 = image[0]
path = "news/image.jpg"
with open(path, 'wb') as f:
decoded_data = base64.decodebytes(file_base64)
f.write(decoded_data)
#base64를 불러와서 이미지 디코딩 path는 파일경로
# 이미지 확인
from PIL import Image
img = Image.open(path)
img
#PIL패키지를 이용해 이미지 확인
#설치는 pip install pillow
# 기사 본문 디코딩
file_base64 = article[0]
decoded_data = base64.decodebytes(file_base64)
decoded_data
#바이트 형식의 문자열로 먼저 디코딩
#utf 또는 ascii형식으로 한번 더 디코딩 필요
article = decoded_data.decode('utf-8')
print(article)
문서 요약/키워드 추출
#gensim라이브러리를 이용하기
#gensim 최신 버전은 요약이 지원이 안되므로 3.7.3버전으로 설치해야함
#pip install gensim==3.7.3
from gensim.summarization.summarizer import summarize
from gensim.summarization.textcleaner import split_sentences
# 단어수 기반 요약 (word_count)
print(summarize(article, word_count=50))
#50글자 요약
print(summarize(article, ratio=0.1))
# 비율 기반 요약 (ratio) ratio=0.1 -> 전체의 10%로 요약하기
article_summarized = summarize(article, ratio=0.1)
#요약한 기사를 새로운 변수에 삽입
키워드 추출
import collections
import textwrap
import re
# 줄바꿈 정렬
article_align = textwrap.fill(article, width=50)
print(article_align)
#기사를 보기좋게 정렬
#50글자 마다 줄바꿈
# 단어 추출

# 빈도수 산출

# 키워드 추출

#가장 많이 나온 5개 단어
요약 리포트 작성