naivebayes - naive bayes classifier code not working -
my code returns same value inputs in variable text
from textblob.classifiers import naivebayesclassifier textblob import textblob nltk.corpus import stopwords operators=set(('not','never','no','but')) stop_words=set(stopwords.words("english"))-operators open('pws.csv', 'r') fp: cl = naivebayesclassifier(fp, format="csv") text="this perfect." text=text.lower() text=text.replace('.',' .') text=text.replace('!',' .') sent=0.0 x=0 text2="" if "whereas" in text: i=text.find("whereas") text=text[:i]+"."+text[i+7:] if "though" in text: i=text.find(",") text=text[:i]+"."+text[i+1:] w in text.split(): if w in stop_words: continue else: text2=text2+" "+w print (text2) test=open('test_data.csv') print(cl.accuracy(test)) blob=textblob(text2, classifier=cl) s in blob.sentences: print(s) x=x+1 if "but" in s: j=s.find("but") t1=str(s[:j]) t2=str(s[(j+3):]) b1=textblob(t1, classifier=cl) b2=textblob(t2, classifier=cl) s1=float(b1.classify()) s2=float(b2.classify()) if "not" in t1 or "n't" in t1: s1=s1*-1 if "not" in t2 or "n't" in t2: s2=s2*-1 sent=sent+(((1.5*s1)+s2)/2) else: if "not" in s or "n't" in s: print(float(s.classify())*-1) sent=sent+(float(s.classify())*-1) else: print(s.classify()) sent=sent+float(s.classify()) print("sentiment of overall document:",round((sent/x),2)) output:
runfile('e:/sem 4/btp/final codes/abc.py', wdir='e:/sem 4/btp/final codes') perfect . 0.0 perfect . -0.4 sentiment of overall document: -0.4 also, i'm unable understand why accuracy 0.
the file pws.csv follows:
good,0.6 amazing,0.95 great,0.8 awesome,0.95 love,0.7 like,0.5 better,0.4 beautiful,0.6 bad,-0.6 worst,-0.9 hate,-0.8 sad,-0.4 sadly,-0.4 disappointing,-0.6 angry,-0.7 happy,0.7 perfect,1.0 humour,0.7 best,1.0 inspirational,0.85 commendable,0.8 soothing,0.75 irrelevant,-0.4 fail,-0.7 lazy,-0.4 the code working fine earlier i'm unable find error now. please help.
Comments
Post a Comment