-
Notifications
You must be signed in to change notification settings - Fork 0
/
addToDictRepo.py
160 lines (136 loc) · 4.05 KB
/
addToDictRepo.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import mysql.connector
import json
from sklearn.feature_extraction.text import TfidfVectorizer
import pandas as pd
import openpyxl
import math
import pickle
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="password",
database="newsCrawler"
)
mycursor = mydb.cursor()
def dictToPickle(name,mydict):
with open('dictLists/{}.pkl'.format(name),'wb') as f:
pickle.dump(mydict, f, protocol=pickle.HIGHEST_PROTOCOL)
def dictToJson(dict):
with open("sample.json", "w") as outfile:
json.dump(dict, outfile)
def totalWordCount():
wordCount = 0
mycursor.execute("SELECT wordCount FROM tokenizedNews")
myresult = mycursor.fetchall()
for x in myresult:
wordCount = wordCount + x[0]
return wordCount
def uniqueLemmas():
globalLemmaList = []
mycursor.execute("SELECT * FROM tokenizedNews")
myresult = mycursor.fetchall()
#y = json.loads(myresult[100][4])
#print(type(y))
for x in range(len(myresult)):
try:
lemmaList = json.loads(myresult[x][4])
for lemma in lemmaList:
tempLemma = lemma[0]
if tempLemma not in globalLemmaList:
globalLemmaList.append(tempLemma)
except:
pass
#print('There are',len(globalLemmaList),'unique lemmas')
return globalLemmaList
def computeTF(articleID):
mycursor.execute("SELECT * FROM tokenizedNews WHERE ID = {}".format(articleID))
myresult = mycursor.fetchall()
tfDictionary = {}
wordsLength = myresult[0][-3]
bagOfWords = json.loads(myresult[0][4])
for x in bagOfWords:
tempNum = x[1] / float(wordsLength)
tfDictionary[x[0]] = format(tempNum, '.6f')
return tfDictionary
uniqueWords = uniqueLemmas()
wordsDict = dict.fromkeys(uniqueWords, 0)
def computeIDF():
idfDict = {}
mycursor.execute("SELECT article FROM news")
result = mycursor.fetchall()
documents = []
for x in result:
documents.append(x[0])
n = len(documents)
print('Computing IDF for',n,'Documents.')
for x in documents:
tempBagofWords=x.split(' ')
for word in tempBagofWords:
try:
wordsDict[word] +=1
except:
pass
erCount = 0
wordsToRemove = []
for word, val in wordsDict.items():
try:
numToDict = math.log(n/float(val))
wordsDict[word] = format(numToDict, '.5f')
except:
wordsToRemove.append(word)
for word in wordsToRemove:
del wordsDict[word]
print(len(wordsDict),'unique lemmas added')
return wordsDict
idf = computeIDF()
def computeTFIDF(articleID,idfs):
tfidf = {}
tfBagOfWords = computeTF(articleID)
#print(tfBagOfWords)
for word,val in tfBagOfWords.items():
try:
tempNum = float(val)*float(idfs[word])*10
tfidf[word] = format(tempNum, '.5f')
except:
pass
#print(word,val)
#print(idfs[word])
return tfidf
#print(computeTFIDF(185,idf))
#a = computeTFIDF(185,idf)
#dictToPickle('a',a)
#b = computeTFIDF(12,idf)
#df = pd.DataFrame([a,b])
#print(df)
#df.to_pickle('eris.pkl')
#df = pd.read_pickle('eris.pkl')
#print(df)
"""
print(a)
dictionaryDataframe = pd.DataFrame()
print(dictionaryDataframe)
dictionaryDataframe.append(a, ignore_index = True)
print(dictionaryDataframe)
"""
"""
print(len(documents))
print(type(documents[0]))
article = documents[0][0]
print(len(article))
print(type(article))
#for article in documents:
wordCollection = article.split(' ')
print(wordCollection)
#uniqueWords = set()
"""
mycursor.execute("SELECT id,articleInDict from tokenizedNews")
result = mycursor.fetchall()
articlesToDictCounter = 0
for x in result:
if x[1] == 0:
tempList = computeTFIDF(x[0],idf)
dictToPickle('{}'.format(x[0]),tempList)
mycursor.execute("UPDATE tokenizedNews SET articleInDict = 1 WHERE id = {};".format(x[0]))
mydb.commit()
articlesToDictCounter += 1
print(articlesToDictCounter,'articles added to Dictionary')