-
Notifications
You must be signed in to change notification settings - Fork 0
/
tfidf.py
64 lines (58 loc) · 2.14 KB
/
tfidf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from pathlib import Path
import os
import jieba
import jieba.posseg as pseg
import sys
import string
from openpyxl import load_workbook, Workbook
import xlwt
import json
import time
import operator
from sklearn import feature_extraction
from operator import itemgetter, attrgetter
from sklearn.feature_extraction.text import TfidfTransformer, TfidfVectorizer, CountVectorizer
side = "C:/Users/Big data/PycharmProjects/project/data/after/"
path = Path(side)
all_file = list(path.glob('**/*.json'))
parse = []
lib = {}
sFilePath = './tfidf'
if not os.path.exists(sFilePath):
os.mkdir(sFilePath)
sFilePath_1 = './tfidffile'
if not os.path.exists(sFilePath_1):
os.mkdir(sFilePath_1)
data = {}
for files in all_file:
title = str(files)[59:-4]
# print(files)
files_1 = open(r'%s'%files,'r',encoding='utf-8',errors='ignore').read()
# print(files_1)
corpus = [files_1]
# print(corpus)
vectorizer = CountVectorizer()
transformer = TfidfTransformer()
tfidf = transformer.fit_transform(vectorizer.fit_transform(corpus))
word = vectorizer.get_feature_names() # 所有文件的關鍵字
weight = tfidf.toarray() # 對應的tfidf矩陣
# print(tfidf)
# print(len(word))
# print(tfidf)
# 將每份文件的TF-IDF寫入tfidffile資料夾中保存
for i in range(len(weight)):
i = str(i)
print(u"Writing all the tf-idf in the", i, u" file into ", sFilePath+'/'+str.zfill(i, 5)+'.json')
with open(sFilePath + '/' + str.zfill(i, 5) + '.json', 'a+', encoding='utf-8', errors='ignore') as json_file:
for j in range(len(word)):
i = int(i)
j = int(j)
j_1 = str(weight[i][j])
data[str(word[j])] = float(j_1)
data_order = {k: v for k, v in sorted(data.items(), key=lambda x: x[1], reverse=True)}
print(data_order)
# data = {str(word[j]), float(j_1)}
json_file.write(json.dumps(data_order, ensure_ascii=False))
# json_file.close()
# print(data_order)
# # [[word,score],[],[],[]]