class Text_mining():
def __init__(self):
pass
def find_word(self):
from time import sleep
text = input('스크립트명을 입력하세요 ~ ')
word = input('찾을 단어 입력하세요 ~ ')
file = open('d:\data\\' + text, 'r')
cnt = 0
for i in file: # 스크립트를 리스트로 변환
x = i.split(' ') # 공백으로 단어를 구분
for j in x: # 단어를 하나씩 b 에 담는 것
cnt += j.lower().count(word)
result = '\r%d개' %cnt
print(result, end = '')
sleep(0.00001)
def list_word(self):
import re
import simplejson
file = input('전체단어를 output.txt로 생성할 스크립트를 입력하세요 ~ ')
word = []
for j in open('d:\data\\' + file, 'r'): # 파일 로딩
for k in re.sub('[^A-z ]', '', re.sub('\n', '', j)).strip().upper().split(' '): # 셜록홈즈 텍스트 정제
word.append(k)
word = list(filter(lambda x: x != '', word))
f = open('d:\data\output.txt', 'w')
simplejson.dump(word, f)
f.close()
def positive(self):
import re
import simplejson
#import csv
file = input('긍정단어의 갯수 및 output_pos.txt를 생성 할 스크립트를 입력하세요 ~ ')
positive = []
for i in open('d:\data\positive-words.txt', 'r'): # 긍정단어 리스트 만들기
positive.append((re.sub("\n", '', i)).upper())
word = []
sum = 0
for i in open('d:\data\\' + file, 'r'): # 파일 로딩
for j in re.sub('[^A-z ]', '', re.sub('\n', '', i)).strip().upper().split(' '): # 텍스트 정제
if j in positive: # 긍정단어 카운트
word.append(j)
sum = sum + 1
word = list(filter(lambda x: x!= '', word))
f = open('d:\data\output_pos.txt', 'w')
simplejson.dump(word, f)
f.close()
print(sum)
#with open('d:\\data\\script.csv', 'w') as f:
# w = csv.writer(f)
# w.writerows(file2)
def negative(self):
import re
import simplejson
file = input('부정단어의 갯수 및 output_neg.txt를 생성 할 스크립트를 입력하세요 ~ ')
negative = []
for i in open('d:\data\\negative-words.txt', 'r'): # 부정단어 리스트 만들기
negative.append((re.sub("\n", '', i)).upper())
word = []
sum = 0
for i in open('d:\data\\' + file, 'r'): # 파일 로딩
for j in re.sub('[^A-z ]', '', re.sub('\n', '', i)).strip().upper().split(' '): # 텍스트 정제
if j in negative: # 부정단어 카운트
word.append(j)
sum = sum + 1
word = list(filter(lambda x: x != '', word))
f = open('d:\data\output_neg.txt', 'w')
simplejson.dump(word, f)
f.close()
print(sum)
def show_cloud(self):
from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
from os import path
file = input('워드 클라우드를 그릴 텍스트 파일을 입력하세요 ~ ')
d = path.dirname('d:\data\\')
text = open(path.join(d, file), mode='r',
encoding='UTF-8').read()
wordcloud = WordCloud(stopwords=STOPWORDS, background_color='white',
width=1000, height=800, colormap='PuRd').generate(text)
plt.figure(figsize=(13, 13))
plt.imshow(wordcloud)
plt.axis('off')
plt.show()
def special_cnt(self):
file = input('스크립트를 입력하세요 ~ ')
text_file = open('d:\data\\' + file, 'r')
lines = text_file.readlines()
total1 = 0
total2 = 0
for i in lines:
cnt = sum(j.isalpha() for j in i)
cnt += sum(j.isdigit() for j in i)
cnt += sum(j.isspace() for j in i)
total1 += len(i)
total2 += cnt
print(total1 - total2)