-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcorrelation author rating and works count.py
174 lines (138 loc) · 3.97 KB
/
correlation author rating and works count.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
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python
# coding: utf-8
# In[35]:
import csv
from goodreads import client
gc = client.GoodreadsClient('pvr3ns4Le0DCqEpAG2jlQ', '4BM2D4d8ZvFcJNqRQ3OjQq1Qh3OrvMAvHiI0lTOUFE')
def takeSecond(elem):
return elem[1]
with open('datav3.csv','r',encoding="utf-8") as f:
reader = csv.reader(f)
fieldnames = next(reader)
# print(fieldnames)
csv_reader = csv.DictReader(f,fieldnames=fieldnames)
cntAuthor={}
authoridx={}
anthortotalrating={}
for row in csv_reader:
d={}
for k,v in row.items():
d[k]=v
if d['author'] in cntAuthor:
cntAuthor[d['author']]+=1
#print(anthortotalrating[d['author']])
anthortotalrating[d['author']]+=float(d['rating'])
else:
cntAuthor[d['author']]=1
authoridx[d['author']]=d['ID']
#print(float(d['rating']))
anthortotalrating[d['author']]=float(d['rating'])
#print(anthortotalrating)
authorratingtuple=[]
for i in cntAuthor:
if cntAuthor[i]>1:
authorratingtuple.append((i,round(anthortotalrating[i]/cntAuthor[i],3)))
authorratingtuple.sort(key=takeSecond)
#print(authorratingtuple)
authorwork={}
cnt=0
f=open('authorRatingWork.txt','w')
for i in authorratingtuple:
cnt+=1
author = gc.author(gc.book(authoridx[i[0]]).authors[0].gid)
wk=author.works_count
print("author:%s works_count:%s %s/%s"%(i[0],wk,cnt,len(authorratingtuple)))
authorwork[i]=wk
line="%s %s %s"%(i[0],i[1],wk)
f.write(line+"\n")
f.close()
# In[36]:
fl=open('authorRatingWork.txt','r')
for lines in fl.readlines():
print(lines)
fl.close()
# In[71]:
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
xaxis=[]
yaxis=[]
fl=open('authorRatingWork.txt','r')
for lines in fl.readlines():
templist=lines.split()
if int(templist[len(templist)-1])<20000:
xaxis.append(float(templist[len(templist)-2]))
yaxis.append(float(templist[len(templist)-1]))
'''
plt.bar(xaxis,yaxis,width=0.05)
#xticks(np.arange(0,5, step = 1))
plt.title("author rating versus works count",fontsize=16)
plt.xlabel("author rating",fontsize=10)
plt.ylabel("number of books written",fontsize=10)
plt.show()
'''
plt.figure(figsize=(40,30))
sns.set(style="whitegrid")
ls=sns.jointplot(x=xaxis,y=yaxis,kind="reg")
ls.set_axis_labels("Author ratings","Books Written")
# In[38]:
data={}
fl=open('authorRatingWork.txt','r')
for lines in fl.readlines():
templist=lines.split()
if (float(templist[len(templist)-2])<4.0 and float(templist[len(templist)-1])>5000 and float(templist[len(templist)-1])<20000):
print(templist)
# In[39]:
import csv
with open('datav3.csv','r',encoding="utf-8") as f:
reader = csv.reader(f)
fieldnames = next(reader)
# print(fieldnames)
csv_reader = csv.DictReader(f,fieldnames=fieldnames)
cntAuthor={}
authoridx={}
anthortotalrating={}
cnt=0
for row in csv_reader:
d={}
for k,v in row.items():
d[k]=v
if 'Disney' in d['author']:
print(d['title'])
cnt+=1
print(cnt)
# In[92]:
fl=open('authorRatingWork.txt','r')
lines=fl.readlines()
xx=[]
yy=[]
zz=[]
print(lines[0].split())
a=lines[0].split()
for i in range(1,11):
ll=lines[len(lines)-i].split()
aut=''
for j in ll[0:len(ll)-2]:
aut+=j
aut+=' '
xx.append(aut.rstrip())
yy.append(int(ll[len(ll)-1]))
zz.append(float(ll[len(ll)-2]))
print(xx)
print(yy)
print(zz)
plt.figure(figsize=(20,15))
sns.set(font_scale=1.5)
ls=sns.barplot(yy,xx,palette='rocket')
ls.set_xlabel("Books Written",fontsize=30)
ls.set_ylabel("Author Name",fontsize=30)
ls.set_title("Top ten rated author books count",fontsize=50)
fl.close()
# In[97]:
sns.set(font_scale=1.5)
plt.figure(figsize=(20,15))
xs=sns.barplot(zz,xx,palette='rocket')
xs.set_xlabel("average rating",fontsize=30)
xs.set_ylabel("Author Name",fontsize=30)
xs.set_title("Top ten rated author average ratings",fontsize=50)
# In[ ]: