public Category classify ( String text ) { StringTokenizer itr = new StringTokenizer ( text ) ; Map < Category , Double > scorePerCategory = new HashMap < Category , Double > ( ) ; double bestScore = Double . NEGATIVE_INFINITY ; Category bestCategory = null ; while ( itr . hasMoreTokens ( ) ) { String token = NaiveBayesGenerate . normalizeWord ( itr . nextToken ( ) ) ; for ( Category category : Category . values ( ) ) { int count = MapUtils . getInteger ( wordCountPerCategory . get ( category ) , token , 0 ) + 1 ; double wordScore = Math . log ( count / ( double ) ( tokensPerCategory . get ( category ) + V ) ) ; double totalScore = MapUtils . getDouble ( scorePerCategory , category , 0. ) + wordScore ; if ( totalScore > bestScore ) { bestScore = totalScore ; bestCategory = category ; } scorePerCategory . put ( category , totalScore ) ; } } return bestCategory ; }
Naive Bayes Text Classification with Add - 1 Smoothing