-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmergeallof5.py
570 lines (395 loc) · 14.7 KB
/
mergeallof5.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
######################################task1.py
""" Features
The objective of this task is to explore the corpus, deals.txt.
The deals.txt file is a collection of deal descriptions, separated by a new line, from which
we want to glean the following insights:
1. What is the most popular term across all the deals?
2. What is the least popular term across all the deals?
3. How many types of guitars are mentioned across all the deals?
"""
###################################################
# Solution 1of Q1:
# A clumsy method for term extraction without analysis of NLPS
#
# Other methods:
# Use some existing NLP tools for term extraction
# http://termcoord.wordpress.com/about/testing-of-term-extraction-tools/free-term-extractors/
# from topia.termextract.src.topia.termextract import extract
#
# March 21, 2014
###################################################
openfile = open('..\data\deals.txt', 'r')
# create the dictionary d
d = {}
removewords = ['online', 'lessons', 'learn', 'shop', 'shipping', 'free', 'save','new','code', 'link',
'who', 'what', 'when', 'where', 'why', 'how','that', 'is', 'can', 'does', 'do', 'get', 'you', 'he', 'they','our','are',
'&', 'a','all','this', 'and','by','over', 'to', 'the', 'up','off', 'at', 'in', '-','or','from','for', 'on', 'of', 'with', 'your']
for line in openfile:
terms = line.split(' ')
for term in terms:
term = term.strip()
if not term:
continue
# remove non-terms
if term.lower() in removewords:
continue
# update and aggregate dictionary d with term
if not (term in d):
d[term] = 0
d[term] += 1
# the most/the least popular term
v = list(d.values())
#maxkey = k[v.index(max(v))]
maxvalue = max(v)
minvalue = min(v)
maxkeys = []
minkeys = []
for k, v in d.items():
if v == maxvalue:
maxkeys.append(k)
if v == minvalue:
minkeys.append(k)
# output results
print("The most popular terms\n", maxkeys)
print("the least popular terms\n", minkeys)
#
# 3. How many types of guitars are mentioned across all the deals?
#
openfile = open('..\data\deals.txt', 'r')
guitars = {}
for line in openfile:
terms = line.split(' ')
#print(terms)
if not 'Guitar' in terms:
continue
print(terms)
terms = (x for x in terms if not x in removewords)
if not terms in guitars:
guitars[terms] = 0
guitars[terms] +=1
print("Number of types of guitars =", len(guitars))
########################################### end of task1.py
###################################################### task1b.py
""" Features
The objective of this task is to explore the corpus, deals.txt.
The deals.txt file is a collection of deal descriptions, separated by a new line, from which
we want to glean the following insights:
1. What is the most popular term across all the deals?
2. What is the least popular term across all the deals?
3. How many types of guitars are mentioned across all the deals?
"""
####################################################
# Solution 2 of Q1:
# Use topia.termextract 1.1.0 for term extraction
#
####################################################
# load term extraction library
from topia.termextract import extract
extractor = extract.TermExtractor()
# define the trivial permissive filter
extractor.filter = extract.permissiveFilter
# load data
openfile = open('..\data\deals.txt', 'r')
d = {}
numberguitars = 0
for line in openfile:
terms = extractor(line)
# empty
if not terms:
continue
# take each term from terms
for term in terms:
# aggregate dictionary for each term
if not (term[0] in d):
d[term[0]] = 0
d[term[0]] += term[1]
# count guitar
if 'guitar' in term or 'guitars' in term:
numberguitars += 1
else:
if 'Guitar' in term or 'Guitars' in term:
numberguitars += 1
v = list(d.values())
maxvalue = max(v)
minvalue = min(v)
maxkeys = []
minkeys = []
for k, v in d.items():
if v == maxvalue:
maxkeys.append(k)
if v == minvalue:
minkeys.append(k)
# output results
print "1. the most popular terms\n", maxkeys
#print "2. the least popular terms\n", minkeys
print "3. the number of types of guitars", numberguitars
#############################end of task1b.py
#############################task2.py
""" Groups and Topics
The objective of this task is to explore the structure of the deals.txt file.
Building on task 1, we now want to start to understand the relationships to help us understand:
1. What groups exist within the deals?
2. What topics exist within the deals?
"""
###########################################
# Solution of Q2:
# Using termextract and tagging tools from topia for term extraction;
# Ranking for top k popular terms, which are restricted to NN or NNS
# Results for answering 1 and 2
# Other part of speech tagging methods can be used
###########################################
from topia.termextract import extract
from topia.termextract import tag
tagger = tag.Tagger()
tagger.initialize()
extractor = extract.TermExtractor()
extractor.filter = extract.permissiveFilter
# load data
openfile = open('..\data\deals.txt', 'r')
# define dictionary
d = {}
# iterative process
for line in openfile:
terms = extractor(line)
# remove empty
if not terms:
continue
# take each term from terms
for term in terms:
tag = tagger(term[0])
# remove unrelevant terms by tagging
if not tag[0][1] in ['NN', 'NNS']:
continue
#print tag[0][1]
# aggregate dictionary for each term
if not (term[0] in d):
d[term[0]] = 0
d[term[0]] += term[1]
# sorting for ranking topics
term_tuples = d.items()
term_tuples = sorted(term_tuples, key = lambda term: term[1], reverse = True)
# select 10 topics
i = 0
for k,v in term_tuples:
print k, v
if i >= 10:
break
i+=1
print "done!"
###############################end of task2.py
##############################task3.py
`""" Classification
The objective of this task is to build a classifier that can tell us whether a new, unseen deal
requires a coupon code or not.
We would like to see a couple of steps:
1. You should use bad_deals.txt and good_deals.txt as training data
2. You should use test_deals.txt to test your code
3. Each time you tune your code, please commit that change so we can see your tuning over time
Also, provide comments on:
- How general is your classifier?
- How did you test your classifier?
"""
###################################################
# Solution 1 of Q3
# 1. Using textblob for a simple text classification
# 2. Build two classifiers: naive bayes and decision tree
# 3. Evaluation by a simplge cross validation for average accuracy
# All running experiments were run successfully on my own local mahine
###################################################
# import useful tools or package for different learning problems
from __future__ import print_function
import logging
import numpy as np
from optparse import OptionParser
import sys
from time import time
import random
#from sklearn.datasets import fetch_20newsgroups
#from sklearn.datasets import load_files
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.feature_extraction.text import HashingVectorizer
from sklearn.feature_selection import SelectKBest, chi2
from sklearn.linear_model import RidgeClassifier
from sklearn.svm import LinearSVC
from sklearn.linear_model import SGDClassifier
from sklearn.linear_model import Perceptron
from sklearn.linear_model import PassiveAggressiveClassifier
from sklearn.naive_bayes import BernoulliNB, MultinomialNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.neighbors import NearestCentroid
from sklearn.utils.extmath import density
from sklearn import metrics
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.lda import LDA
from sklearn.qda import QDA
from textblob.classifiers import NaiveBayesClassifier
from textblob.classifiers import DecisionTreeClassifier
from numpy import genfromtxt, savetxt
# 1. load training data for building a simple naive bayes classifier for text classificaiton
bad_data = open('../data/bad_deals.txt','r')
dataset = []
for line in bad_data:
dataset.append((line, 'bad'))
good_data = open('../data/good_deals.txt','r')
for line in good_data:
dataset.append((line, 'good'))
test_data = open('../data/test_deals.txt','r')
test_dat = []
for line in test_data:
test_dat.append((line))
# split for train and test data
results_nbc= []
results_dtc= []
# a simple cross valiadation for evaluation
for i in range(1, 10):
# randomize
random.seed(i)
random.shuffle(dataset)
# split
train = dataset[:9*len(dataset)/10]
test = dataset[9*len(dataset)/10:]
# build classifiers
nbc = NaiveBayesClassifier(train)
dtc = DecisionTreeClassifier(train)
# save results
results_nbc.append(nbc.accuracy(test));
results_dtc.append(dtc.accuracy(test));
# output the mean of accuray
print('mean of accuracy:')
print('naive bayes', np.array(results_nbc).mean())
print('decision tree', np.array(results_dtc).mean())
# 2. use test_deals.txt for classification
nbc = NaiveBayesClassifier(dataset)
dtc = DecisionTreeClassifier(dataset)
print('naive bayes classification:')
for text in test_dat:
print(text, nbc.classify(text))
print('decision tree classification:')
for text in test_dat:
print(text, dtc.classify(text))
# other classifiers ...
#########################################enf of task3.py
###########################################task3b.py
""" Classification
The objective of this task is to build a classifier that can tell us whether a new, unseen deal
requires a coupon code or not.
We would like to see a couple of steps:
1. You should use bad_deals.txt and good_deals.txt as training data
2. You should use test_deals.txt to test your code
3. Each time you tune your code, please commit that change so we can see your tuning over time
Also, provide comments on:
- How general is your classifier?
- How did you test your classifier?
"""
############################################################################################
# Solution 2 for Q3
# 1. The solution is similar to the method for sentiment analysis, see below
# 2. show how to split sentences and generate word features
# 3. raw data is transformed into a new training data, different from the method used in textblob;
# ref: http://www.laurentluce.com/posts/twitter-sentiment-analysis-using-python-and-nltk/
#
# note: however, the performance is poorer than that by textblob, discussed in the first solution, from our empirical results
############################################################################################
# import useful tools or package for different learning problems
from __future__ import print_function
import logging
import numpy as np
from optparse import OptionParser
import sys
from time import time
import random
import nltk
#from pylab import *
#from sklearn.datasets import fetch_20newsgroups
#from sklearn.datasets import load_files
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.feature_extraction.text import HashingVectorizer
from sklearn.feature_selection import SelectKBest, chi2
from sklearn.linear_model import RidgeClassifier
from sklearn.svm import LinearSVC
from sklearn.linear_model import SGDClassifier
from sklearn.linear_model import Perceptron
from sklearn.linear_model import PassiveAggressiveClassifier
from sklearn.naive_bayes import BernoulliNB, MultinomialNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.neighbors import NearestCentroid
from sklearn.utils.extmath import density
from sklearn import metrics
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.lda import LDA
from sklearn.qda import QDA
from textblob.classifiers import NaiveBayesClassifier
from textblob.classifiers import DecisionTreeClassifier
from numpy import genfromtxt, savetxt
from sklearn.cross_validation import train_test_split
# 1. load training data for building a simple naive bayes classifier for text classificaiton
#
bad_data = open('../data/bad_deals.txt','r')
deals = []
good_data = open('../data/good_deals.txt','r')
for line in good_data:
deals.append((line, 'good'))
for line in bad_data:
deals.append((line, 'bad'))
test_data = open('../data/test_deals.txt','r')
test_dat = []
for line in test_data:
test_dat.append((line))
# end
##names = ["Nearest Neighbors", "Linear SVM", "RBF SVM", "Decision Tree",
## "Random Forest", "AdaBoost", "Naive Bayes"]
##classifiers = [
## KNeighborsClassifier(3),
## SVC(kernel="linear", C=0.025),
## SVC(gamma=2, C=1),
## DecisionTreeClassifier(max_depth = 5),
## RandomForestClassifier(max_depth=5, n_estimators=10, max_features=1),
## AdaBoostClassifier(),
## NaiveBayesClassifier()]
def get_word_features(wordlist):
wordlist = nltk.FreqDist(wordlist)
word_features = wordlist.keys()
return word_features
def get_words_in_deals(deals):
all_words = []
for (words, t) in deals:
all_words.extend(words)
return all_words
word_features = get_word_features(get_words_in_deals(deals))
def extract_features(deal):
deal_words = set(deal)
features = {}
for word in word_features:
features['contains(%s)' % word] = (word in deal_words)
return features
training_set = nltk.classify.apply_features(extract_features, deals)
# evaluation by split
X_train = training_set[:int(len(training_set) *0.9)]
X_test = training_set[int(len(training_set) *0.9):]
clf= NaiveBayesClassifier(X_train)
# accuracy
print(clf.accuracy(X_test))
##target = [x[len(x)-1] for x in training_set]
##train = [x[:len(x)-2] for x in training_set]
##
##X_train, X_test, y_train, y_test = train_test_split(train, target, test_size=.4)
##
### iterate over classifiers
##for name, clf in zip(names, classifiers):
## clf.fit(X_train, y_train)
##
## score = clf.score(X_test, y_test)
## print(name, score)
# classify
classifier = nltk.NaiveBayesClassifier.train(training_set)
for deal in test_dat:
print(classifier.classify(extract_features(deal.split())))
########################################end of task3b.py