def __init__(self): self.meaningDict={} trainList,trainClassifyVec,self.testList,self.testClassifyVec=self.getData() vocabList=createVocabList(trainList) trainMat=[] for postingData in trainList: trainMat.append(bagOfWords2VecMN(vocabList,postingData)) self.p0V,self.p1V,self.pAb=trainNB(trainMat,trainClassifyVec) self.vocabList=vocabList
def testTrainNB(): listOPosts,trainCategory=loadDataSet() vocabList=createVocabList(listOPosts) trainMat=[] for postinDoc in listOPosts: trainMat.append(setOfWords2Vec(vocabList,postinDoc)) p0V,p1V,pAb=trainNB(trainMat,trainCategory) testEntry=['love','wang','xi','my','dalmation'] vec2classify=array(setOfWords2Vec(vocabList,testEntry)) print testEntry,'classify as:',classifyNB(vec2classify,p0V,p1V,pAb)