-
Notifications
You must be signed in to change notification settings - Fork 2
/
read_database.py
119 lines (104 loc) · 3.96 KB
/
read_database.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
#module for the application
from __future__ import division
import pickle
from datetime import datetime,timedelta
from content_management import Content, Database, Write_Cont, Write_DB
CAT_DICT = {'Serv':'Service','Looks':'Looks','Cam':'Camera','Vfm':'Value for Money','Disp':'Display','perf':'Performance', 'Serv':'Service','Perf':'Performance', 'Cam':'Camera','OverAll':'OverAll', 'Bat':'Battery', 'Look':'Look', 'Camera':'Camera' }
def _openFile(filename):
fileObj = open(filename,'rb')
bigList = pickle.load(fileObj)
return bigList
def _prepareDateList(bigList):
dateList = [0]
for element in range(len(bigList)):
date = bigList[element][0].encode()
date = date.split('-')
epoch = datetime.datetime.utcfromtimestamp(0)
for i in range(len(date)):
date[i] = int(date[i])
dt = datetime.datetime(date[0],date[1],date[2])
var = (dt - epoch).total_seconds() * 1000.0
if (dateList[-1]!=var):
dateList.append(var)
return dateList
def _prepareCategoryList(bigList):
'''This function would return the overall sentiment of the whole situation (categorywise) but would not return it return datewise'''
element1 = bigList[0][1]
categoryList = list(element1.keys())
noOfCategories = len(categoryList)
categoryListFinale = {}
for category in categoryList:
categoryListFinale[category] = []
datelist = [0]
Overall_sentiment=0
length = 0
for element in range(len(bigList)):
myDict = bigList[element][1]
date = bigList[element][0]
date = date.split('-')
epoch =datetime.utcfromtimestamp(0)
for i in range(len(date)):
date[i] = int(date[i])
dt = datetime(date[0],date[1],date[2])
td = (dt - epoch)
td = (td.microseconds + (td.seconds + td.days * 86400) * 10**6) / 10**3
for category in categoryList:
sentimentValue = myDict[category]
if ((sentimentValue!=0)and(datelist[-1]!=td)):
Overall_sentiment = Overall_sentiment + sentimentValue
categoryListFinale[category].append([td,sentimentValue])
length = length + 1
datelist.append(td)
Overall_sentiment = Overall_sentiment/length
return (categoryList, categoryListFinale,Overall_sentiment)
def _url_update(categoryList, productname, filename):
dict1 = Content()
dict2 = Database()
categoryList = list(categoryList)
a = []
for i in range(len(categoryList)):
a.append(CAT_DICT[categoryList[i]])
dict1[productname]=[]
def _check(filename):
bigList = _openFile(filename)
categoryList,categoryListFinale,Overall_sentiment = _prepareCategoryList(bigList)
a = categoryListFinale
print (a)
return a
def _prepareCategoryList2(bigList):
'''This function would return the overall sentiment of the whole situation (categorywise) but would not return it return datewise'''
element1 = bigList[0][1]
categoryList = list(element1.keys())
noOfCategories = len(categoryList)
categoryListFinale = {}
for category in categoryList:
categoryListFinale[category] = [0,0]
datelist = [0]
Overall_sentiment=0
length = 0
for element in range(len(bigList)):
myDict = bigList[element][1]
date = bigList[element][0]
date = date.split('-')
epoch =datetime.utcfromtimestamp(0)
for i in range(len(date)):
date[i] = int(date[i])
dt = datetime(date[0],date[1],date[2])
td = (dt - epoch)
td = (td.microseconds + (td.seconds + td.days * 86400) * 10**6) / 10**3
for category in categoryList:
sentimentValue = myDict[category]
if ((sentimentValue!=0)):
if (sentimentValue > 0 ):
categoryListFinale[category][0]=categoryListFinale[category][0]+sentimentValue
if (sentimentValue < 0):
categoryListFinale[category][1]=categoryListFinale[category][1]+sentimentValue
for category in categoryList:
total = categoryListFinale[category][0] - categoryListFinale[category][1]
categoryListFinale[category][0] = categoryListFinale[category][0]/total *100.0
categoryListFinale[category][1] = 0 - categoryListFinale[category][1]/total*100.0
return categoryListFinale
def _pie(filename):
bigList = _openFile(filename)
categoryListFinale = _prepareCategoryList2(bigList)
return categoryListFinale