-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMinePredict.py
64 lines (54 loc) · 1.48 KB
/
MinePredict.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
import time, re, json, numpy as np
from sklearn.svm import LinearSVC
from nltk.corpus import stopwords
from sklearn.pipeline import Pipeline
from nltk.stem.snowball import SnowballStemmer
from sklearn.multiclass import OneVsRestClassifier
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
s=set(stopwords.words('english'))
stemmer = SnowballStemmer("english", ignore_stopwords=True)
fh=open('Tags.txt','r')
fh2=open('cleaned.txt','r')
fh3=open('TTags.txt', 'r')
fh4=open('Tcleaned.txt','r')
tags={}
freq=[]
count=0
tagrows=fh.read().split('\n')[:500000]
X=fh2.read().split('\n')[:500000]
Y = [[] for i in range(len(X))]
for line in tagrows:
for tag in line.split():
if tag in tags:
tags[tag]+=1
else:
tags[tag]=1
#34945 unique tags in 10 lakh posts
for tag in sorted(tags,key=lambda tag:tags[tag], reverse=True):
if tags[tag] > 800:
count += 1
freq.append(tag)
else:
break
print "Training..."
for x,tag in enumerate(freq):
i=0
for row in tagrows:
if tag in row.split():
Y[i].append(tag)
i=i+1
classifier = Pipeline([
('vectorizer', CountVectorizer()),
('tfidf', TfidfTransformer()),
('clf', OneVsRestClassifier(LinearSVC(class_weight='auto'), n_jobs = -2))])
classifier.fit(X,Y)
print "Ready..."
while True:
T=[]
words = fh.readline().lower().replace(' \n','')
T.append(words)
print '\n',classifier.predict(T),fh3.readline(),'\n'
print "Exiting..."
fh.close()
fh2.close()