def classifyNB(vec2Classify: Array[Int], p0Vec: Array[Double], p1Vec: Array[Double], pClass1: Double): Int = {
var cnt = 0
val p1 = vec2Classify.map { x =>
val v = x * p1Vec(cnt)
cnt = 1
v
}.sum math.log(pClass1)
cnt = 0
val p0 = vec2Classify.map { x =>
val v = x * p0Vec(cnt)
cnt = 1
v
}.sum math.log(1.0 - pClass1)
//log(p(w/c0)p(c0))=log(p(w/c0)) log(p(c0))= sum(vec2Classify * p0Vec) log(1.0 - pClass1)
if (p1 > p0) 1 else 0
}
def main(args: Array[String]): Unit = {
val DataSet = dataSet()
val listOPosts = DataSet._1
val listClasses = DataSet._2
val myVocabList = listOPosts.reduce((a1, a2) => a1. :(a2)).distinct
/**
* myVocabList的数据
* ArrayBuffer(quit, buying, worthless, dog, food, stupid, mr, licks, ate, my, steak, how, to, stop, him, posting, garbage, dalmation, is, so, cute, I, love, maybe, not, take, park, has, flea, problems, help, please)
*/
var trainMat = new ArrayBuffer[Array[Int]](listOPosts.length)
listOPosts.foreach(postinDoc => trainMat.append(setWordsType(myVocabList, postinDoc)))
//训练集
val p = trainSet(trainMat.toArray, listClasses)
val p0V = p._2
val p1V = p._1
val pAb = p._3
val testEntry = Array("love", "my", "dalmation")
val thisDoc = setWordsType(myVocabList, testEntry)
println(testEntry.mkString(",") " classified as: " classifyNB(thisDoc, p0V, p1V, pAb))
val testEntry2 = Array("stupid", "garbage")
val thisDoc2 = setWordsType(myVocabList, testEntry2)
println(testEntry2.mkString(",") " classified as: " classifyNB(thisDoc2, p0V, p1V, pAb))
}
}