-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sriram.py
131 lines (113 loc) · 2.56 KB
/
Sriram.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
file = open('yelp_academic_dataset_review.json')
# JJ: adjective or numeral, ordinal
# JJR: adjective, comparative
# JJS: adjective, superlative
# NN: noun, common, singular or mass
# NNP: noun, proper, singular
# NNPS: noun, proper, plural
# NNS: noun, common, plural
# PRP: pronoun, personal
# PRP$: pronoun, possessive
# RB: adverb
# RBR: adverb, comparative
# RBS: adverb, superlative
# VB: verb, base form
# VBD: verb, past tense
# VBG: verb, present participle or gerund
# VBN: verb, past participle
# VBP: verb, present tense, not 3rd person singular
# VBZ: verb, present tense, 3rd person singular
markers = {
"JJ" : 0,
"JJR" : 0,
"JJS" : 0,
"NN" : 1,
"NNP" : 1,
"NNPS" : 1,
"NNS" : 1,
"PRP" : 1,
"PRP$" : 1,
"RB" : 2,
"RBR" : 2,
"RBS" : 2,
"VB" : 3,
"VBD" : 3,
"VBG" : 3,
"VBN" : 3,
"VBP" : 3,
"VBZ" : 3,
}
import nltk
# Length, Verb, Adverb, Adjective, Noun
dataset = []
# count =
lenVsRating = {}
i = 0
for l in file:
line = eval(l)
# print line
sentence = line['text']
# print i
i+= 1
if i == 5000:
break
# print sentence
# sentence = "I have multiple texts and I would like to create profiles of them based on their usage of various parts of speech, like nouns and verbs. Basially, I need to count how many times each part of speech is used"
try:
tokens = nltk.word_tokenize(sentence.lower())
text = nltk.Text(tokens)
tags = nltk.pos_tag(text)
vec = [0]*6
# for i in sentence:
vec[0] = len(sentence)
for t in tags:
tag = t[1]
if tag in markers.keys():
# if tag[0] == 'V':
# vec[1] += 1
# if tag[0] == 'J':
# vec[3] += 1
if tag[0] == 'N':
vec[4] += 1
# if tag[0] == 'R':
# vec[2] += 1
vec[5] = float(line['stars'])
dataset.append(vec)
k = 3
if(vec[4]/k not in lenVsRating):
lenVsRating[vec[4]/k] = []
lenVsRating[vec[4]/k].append(vec[5])
except UnicodeDecodeError:
print "Found error"
continue
# print nltk.help.upenn_tagset()
import numpy as np
import pylab as plt
nvec = np.array(dataset)
from scipy.stats.stats import pearsonr
# ratings = nvec[:, 5]
# lens = nvec[:, 0]
# args = np.argsort(lens)
# lens = lens[args]
# ratings = ratings[args]
# plt.plot(lens, ratings)
kys = lenVsRating.keys()
kys = sorted(kys)
x = []
y = []
for ky in kys:
print ky,
print lenVsRating[ky]
x.append(ky)
y.append(np.mean(lenVsRating[ky]))
# plt.scatter(np.array(x)*k, y, s = 4)
# plt.show()
plt.scatter(np.array(x)*k, y, s = 150, color = 'navy', marker = '+')
plt.xlabel('Number of adjectives used')
plt.ylabel('Rating')
print pearsonr(x, y)
plt.show()
# Len - -0.6262
# Verb - 0.56
# Adjective - 0.25
# Noun - 0.047