forked from jackfischer/Verideals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
help.py
198 lines (182 loc) · 5.53 KB
/
help.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import operator
import numpy as np
import en
from matplotlib import pyplot as plt
import string
import ebay
import json
def remove_paren():
brands_file = open("oldbrands.txt")
new_file = open("brands.txt", "a")
lines = brands_file.readlines()
for line in lines:
newline = line.lower()
if "(" in line:
newline = line[0:line.index("(")-1].lower()
new_file.write(newline)
def shortFreq():
old = open("enFreq.txt")
new = open("freq.txt", "a")
freqs = old.readlines()
for x in range(0, 50000):
new.write(freqs[x])
def getEngFreq():
freqF = open("freq.txt")
freqlines = freqF.readlines()
freqs = {}
for line in freqlines:
data = line.split()
freqs[data[0]] = float(data[1])
return freqs
def fun():
brandsfile = open("brands.txt")
brands = brandsfile.readlines()
prac = "this is my text and I like Ford cars they're pretty good i also like to eat Skittles and talk on my Verizon phone ABC"
for brand in brands:
brand = brand[0:len(brand)-1]
if brand in prac:
print brand
def tfios():
tfiosfile = open("tfios.txt")
lines = tfiosfile.readlines()
return lines
def count_words(texts):
words = {}
for text in texts:
text = text.translate(string.maketrans("",""), string.punctuation)
textWords = text.split()
for word in textWords:
if word in words:
words[word] += 1
else:
words[word] = 1
return words
def do_freqs(words):
total = 0
for word in words:
total += words[word]
freqs = {}
for word in words:
freqs[word.lower()] = (words[word]/1.0)/(total)
return freqs
def calc_freqs(words, eng_words):
freqs = {}
for word in words:
word = word.lower()
if word in eng_words:
freqs[word] = float(words[word]) - float(eng_words[word])
return freqs
def brand_calcs(texts):
brandsfile = open("brands.txt")
brands = brandsfile.readlines()
used = {}
for brand in brands:
brand = " " + brand[0:len(brand)-1]
for text in texts:
text = text.lower()
if brand in (" " + text):
if brand[1:] in used:
used[brand[1:]] += 1
else:
used[brand[1:]] = 1
return used
def remove_extras(freqs):
verblines = open("verbs.txt").readlines()
for verbb in verblines:
verbb = verbb[:len(verbb)-1]
if verbb in freqs:
del freqs[verbb]
try:
pastt = en.verb.past(verbb)
#print pastt
if pastt in freqs:
del freqs[pastt]
except:
pass
adjlines = open("adjs.txt").readlines()
for adj in adjlines:
adj = adj[:len(adj)-1]
if adj in freqs:
del freqs[adj]
otherlines = open("thingList.txt").readlines()
for other in otherlines:
other = other[:len(other)-1]
if other in freqs:
del freqs[other]
namelines = open("names.txt").readlines()
for nameish in namelines:
name = nameish.split(',')[0].lower()
if name in freqs:
del freqs[name]
for word in freqs.keys():
try:
plur = en.noun.plural(word)
if plur != word and plur in freqs:
freqs[word] = freqs[word] + freqs[plur]
del freqs[plur]
except:
print "oh boy"
return freqs
def getFreqy(lines=tfios()):
words = count_words(lines)
freqs = do_freqs(words)
litfreqs = calc_freqs(freqs, getEngFreq())
litfreqs = remove_extras(litfreqs)
final = sorted(litfreqs.items(), key=operator.itemgetter(1))
wordsOnly = []
numsOnly = []
r = 20 #num columns
for i in range(len(final)-r,len(final)):
w = final[i]
wordsOnly.append(w[0])
numsOnly.append(w[1])
fig = plt.plot()
w= .75
ind = np.arange(r)
plt.bar(ind, numsOnly, width=w)
plt.xticks(ind + w / 2, wordsOnly, rotation='vertical')
plt.show()
def getBrands(words=count_words(tfios())):
brands = brand_calcs(words)
r = -1
if len(brands) > 20:
r = 20
else:
r =len(brands)
brands = sorted(brands.items(), key=operator.itemgetter(1))
brandNames = []
brandFreqs = []
for i in range(len(brands) - r,len(brands)):
try:
brandNames.append(str(brands[i][0]))
brandFreqs.append(brands[i][1])
except:
print brands[i][0]
fig = plt.plot()
w = .75
ind = np.arange(r)
plt.bar(ind, brandFreqs, width=w)
plt.xticks(ind + w / 2, brandNames, rotation='vertical')
plt.show()
def ebaydeals():
textfile = open("texts.txt")
texts = textfile.readlines()
#getFreqy(texts)
#getBrands(words=count_words(texts))
brands = brand_calcs(texts)
brands = sorted(brands.items(), key=operator.itemgetter(1))
ebaydeals = []
for x in range(len(brands)-5 ,len(brands)):
jsonebay = ebay.deals(brands[x][0])
'''
for item in jsonebay['searchResult']['item']:
print item
print ""
'''
dict = {}
dict['title'] = jsonebay['searchResult']['item'][0]['title']
dict['url'] = jsonebay['searchResult']['item'][0]['viewItemURL']
dict['price'] = jsonebay['searchResult']['item'][0]['sellingStatus']['convertedCurrentPrice']['value']
dict['image_url'] = jsonebay['searchResult']['item'][0]['galleryURL']
ebaydeals.append(dict)
return ebaydeals