Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 package org.apache.mahout.classifier.bayes.mapreduce.common;
 import  org.apache.hadoop.mapred.JobConf;
 import  org.apache.hadoop.mapred.MapReduceBase;
 import  org.apache.hadoop.mapred.Mapper;
 import  org.apache.hadoop.mapred.OutputCollector;
 import  org.apache.hadoop.mapred.Reporter;
 import java.util.List;
 import java.util.Map;

Reads the input train set(preprocessed using the BayesFileFormatter).
 public class BayesFeatureMapper extends MapReduceBase implements
     Mapper<Text, Text, StringTuple, DoubleWritable> {
   private static final Logger log = LoggerFactory.getLogger(BayesFeatureMapper.class);
   private static final DoubleWritable one = new DoubleWritable(1.0);
   private int gramSize = 1;

We need to count the number of times we've seen a term with a given label and we need to output that. But this Mapper does more than just outputing the count. It first does weight normalisation. Secondly, it outputs for each unique word in a document value 1 for summing up as the Term Document Frequency. Which later is used to calculate the Idf Thirdly, it outputs for each label the number of times a document was seen(Also used in Idf Calculation)

key The label
value the features (all unique) associated w/ this label
output The OutputCollector to write the results to
reporter Not used
   public void map(Text key, Text value,
                   OutputCollector<StringTuple, DoubleWritable> output, Reporter reporter)
       throws IOException {
     //String line = value.toString();
     String label = key.toString();
     Map<Stringint[]> wordList = new HashMap<Stringint[]>(1000);
     List<Stringngrams  = new NGrams(value.toString(), ).generateNGramsWithoutLabel(); 
     for (String ngram : ngrams) {
       int[] count = wordList.get(ngram);
       if (count == null) {
         count = new int[1];
         count[0] = 0;
     double lengthNormalisation = 0.0;
     for (int[] D_kj : wordList.values()) {
       // key is label,word
       double dkjValue = (doubleD_kj[0];
       lengthNormalisation += dkjValue * dkjValue;
     lengthNormalisation = Math.sqrt(lengthNormalisation);
     // Output Length Normalized + TF Transformed Frequency per Word per Class
     // Log(1 + D_ij)/SQRT( SIGMA(k, D_kj) )
     for (Map.Entry<Stringint[]> entry : wordList.entrySet()) {
       // key is label,word
       String token = entry.getKey();
       StringTuple tuple = new StringTuple();
       DoubleWritable f = new DoubleWritable(Math.log(1.0 + entry.getValue()[0]) / lengthNormalisation);
    reporter.setStatus("Bayes Feature Mapper: Document Label: " + label);  
    // Output Document Frequency per Word per Class
    for (String token : wordList.keySet()) {
      // key is label,word
      StringTuple dfTuple = new StringTuple();
      StringTuple tokenCountTuple = new StringTuple();
    // output that we have seen the label to calculate the Count of Document per
    // class
    StringTuple labelCountTuple = new StringTuple();
  public void configure(JobConf job) {
    try {
      ..println("Bayes Parameter" + job.get("bayes.parameters"));
      Parameters params = Parameters.fromString(job.get("bayes.parameters",""));
       = Integer.valueOf(params.get("gramSize"));
    } catch (IOException ex) {
      .warn(ex.toString(), ex);
New to GrepCode? Check out our FAQ X