텍스트마이닝1

Daum·2021년 3월 25일
0

Big Data

목록 보기
4/7
post-thumbnail

📝 텍스트마이닝(Text mining)

텍스트마이닝이란 자연어로 구성된 비정형 텍스트 데이터에서 패턴 또는 관계를 추출하여 의미있는 정보를 찾아내는 마이닝 기법이다.

#-\*- coding: utf-8 -\*- 
import re
import requests
from collections import Counter
from urllib.request import urlopen
from bs4 import BeautifulSoup
url='https://store.musinsa.com/app/reviews/views/1'
webpage = requests.get(url)
source = BeautifulSoup(webpage.text, from_encoding='utf-8')

#해당 페이지에서 원하는 데이터 추출
reviews = source.findAll('span',{'class': 'content-review'})
for review in reviews:
  print(review.get_text().strip())

#반복문을 통해 여러 페이지 조회
#해당 페이지에서 원하는 데이터 추출
review_list=[]
for n in range(10):
  url = 'https://store.musinsa.com/app/reviews/views/{}'.format(n+1)
  webpage = requests.get(url)
  source = BeautifulSoup(webpage.text, from_encoding='utf-8')
  reviews = source.findAll('span',{'class': 'content-review'})
  for review in reviews:
    review_list.append(review.get_text().strip().replace('\n','').replace('\t','').replace('\r',''))
#텍스트 파일에 저장
file = open('hello.txt','w',encoding='utf-8')
for review in review_list:
  file.write(review+'\n')
file.close()

#변수에 전체 댓글을 다시 저장
file = open('hello.txt','r',encoding='utf-8')
lines = file.readlines()

review_musinsa = []
for line in lines:
  review_musinsa.append(line)
file.close()
#Okt 패키지 안에 konlpy 모듈 호출
from konlpy.tag import Okt
okt = Okt()


#각 문장별로 형태소 구분하기
sentences_tag = []
for sentence in review_musinsa:
  morph = okt.pos(sentence)
  sentences_tag.append(morph)
  print(morph)
  print('-'*30)
print(sentences_tag)


#명사 혹은 형용사인 품사만 선별해 리스트에 담기
noun_adj_list = []
for sentences in sentences_tag:
  for word, tag in sentences:
    if tag in ['Noun','Adjective']:
      noun_adj_list.append(word)

#선별된 품사별 빈도수 계산 & 상위 빈도 50위 까지 출력
counts = Counter(noun_adj_list)
print(counts.most_common(50))

0개의 댓글