forked from jjbreen/ViralCaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
titleAnalyser.py
49 lines (38 loc) · 1.37 KB
/
titleAnalyser.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
import csv
metrics = {}
def readMetric():
with open('metricData.csv', 'r') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
metrics[row["word"]] = row["value"]
def calculateValue(title):
words = title.split()
values = []
for word in words:
if metrics.has_key(word):
values.append(float(metrics[word]))
else:
values.append(0.5)
#print "title values: " + str(values)
#print "title value: " + str(sum(values) / len(values))
return sum(values) / len(values)
def main():
readMetric()
values = []
with open('YouTubeData.csv', 'r') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
title = row["b'v_title'"][2:-1]
value = calculateValue(title)
values.append(value)
with open('YouTubeData.csv', 'r') as csvfile:
with open('YouTubeData_WithTitleMetric.csv', 'w') as csvOut:
#write the header out
header = csvfile.readline().replace('\n', '').replace('\r', '')
#print header
csvOut.write(header + ", titleMetric\n")
for line, value in zip(csvfile, values):
line = line.replace('\n', '').replace('\r', '')
csvOut.write(line + ", " + str(value) + '\n')
if __name__ == "__main__":
main()