mahout-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From JAGANADH G <jagana...@gmail.com>
Subject Mahout 0.7 API Naive Bayes
Date Thu, 11 Oct 2012 10:03:50 GMT
Hi

I just created a sample use class of NaiveBayes . Can somebody say wheather
I am in the right track or not

Here is my code

public class NaiveBayesClassifierExample {

public static void loadClassifier(String strModelPath, Vector v)
throws IOException {
 Configuration conf = new Configuration();

NaiveBayesModel model = NaiveBayesModel.materialize(new Path(
 strModelPath), conf);
AbstractNaiveBayesClassifier classifier = new StandardNaiveBayesClassifier(
 model);

Vector st = classifier.classifyFull(v);
 System.out.println(st.toString());
 }

public static Vector createVect() throws IOException {
FeatureVectorEncoder encoder = new StaticWordValueEncoder("text");
 Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
StringReader in = new StringReader(
 "The movie sherk was very cool and attractive one. We like the movie"
+ "because of the theme and directon. All the actores were excellent");

TokenStream ts = analyzer.tokenStream("body", in);

CharTermAttribute termAtt = ts.addAttribute(CharTermAttribute.class);
 Vector v1 = new RandomAccessSparseVector(100000);

while (ts.incrementToken()) {
 char[] termBuffer = termAtt.buffer();
int termLen = termAtt.length();
 String w = new String(termBuffer, 0, termLen);
encoder.addToVector(w, 1.0, v1);
 }
 v1.normalize();
 return v1;
}

public static void main(String[] args) throws IOException {
 Vector v = createVect();
String mp =
"/home/u179995/Downloads/mahout-distribution-0.7/playg/movie_model";
 loadClassifier(mp, v);
}
}

-- 
**********************************
JAGANADH G
http://jaganadhg.in
*ILUGCBE*
http://ilugcbe.org.in

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message