Mahout文本聚类学习之DocumentProcessor类
做为文本聚类实现的第一步对语料分词是必须的,而DocumentProcessor类提供了一个基于MapReduce对大量数据集分词的高效灵活的实现。高效是其基于MapReduce分布式计算框架,灵活是其提供了可扩展的分词接口可以对多种语言分词的支持。
下面就要深入一下类内部的流程进行学习:DocumentProcessor类,它只提供了一个静态方法tokenizedDocuments();
tokenizedDocuments(Path, Class<? extends Analyzer>, Path, Configuration);
参数中设置了输入文件的路径,也就是前一步生成的文档集的序列文件;另一个是继承了lucene analyzer抽象类的一个子类,用于分词功能的扩展;第三个就是分词的输出路径;最后一个就是Job的一个Configruation对象。
1 public static void tokenizeDocuments(Path input, 2 Class<? extends Analyzer> analyzerClass, 3 Path output, 4 Configuration baseConf) 5 throws IOException, InterruptedException, ClassNotFoundException { 6 Configuration conf = new Configuration(baseConf); 7 // this conf parameter needs to be set enable serialisation of conf values 8 conf.set("io.serializations", "org.apache.hadoop.io.serializer.JavaSerialization," 9 + "org.apache.hadoop.io.serializer.WritableSerialization"); 10 //对分词的类进行设置,到时会直接实例化分词类的对象 11 conf.set(ANALYZER_CLASS, analyzerClass.getName()); 12 Job job = new Job(conf); 13 job.setJobName("DocumentProcessor::DocumentTokenizer: input-folder: " + input); 14 job.setJarByClass(DocumentProcessor.class); 15 //输出键值为Text做为文档的唯一标识 16 job.setOutputKeyClass(Text.class); 17 //StringTuple对象中有一个List<String>对象,可以理解为分词后将文档存储为词组的序列 18 job.setOutputValueClass(StringTuple.class); 19 FileInputFormat.setInputPaths(job, input); 20 FileOutputFormat.setOutputPath(job, output); 21 //SequenceFileTokenizerMapper是分词核心类 22 job.setMapperClass(SequenceFileTokenizerMapper.class); 23 job.setInputFormatClass(SequenceFileInputFormat.class); 24 job.setNumReduceTasks(0); 25 job.setOutputFormatClass(SequenceFileOutputFormat.class); 26 //运行job前删除已经存在的目录 27 HadoopUtil.delete(conf, output); 28 //将job提交到hadoop集群并等待其结束 29 boolean succeeded = job.waitForCompletion(true); 30 if (!succeeded) 31 throw new IllegalStateException("Job failed!"); 32 33 }
下面对SeqenceFileTokenizerMapper进行分析
1 public class SequenceFileTokenizerMapper extends Mapper<Text, Text, Text, StringTuple> { 2 3 private Analyzer analyzer; 4 5 @Override 6 protected void map(Text key, Text value, Context context) throws IOException, InterruptedException { 7 //调用分词提供的方法对value也就是文本正文的内容进行分词处理 8 TokenStream stream = analyzer.reusableTokenStream(key.toString(), new StringReader(value.toString())); 9 CharTermAttribute termAtt = stream.addAttribute(CharTermAttribute.class); 10 StringTuple document = new StringTuple(); 11 stream.reset(); 12 while (stream.incrementToken()) { 13 if (termAtt.length() > 0) { 14 //把词组加入StringTuple 15 document.add(new String(termAtt.buffer(), 0, termAtt.length())); 16 } 17 } 18 context.write(key, document); 19 } 20 21 @Override 22 protected void setup(Context context) throws IOException, InterruptedException { 23 super.setup(context); 24 //map任务开始时会从context中取出configuration对象,解析出分词组件的名称并生成对象的实例 25 analyzer = ClassUtils.instantiateAs(context.getConfiguration().get(DocumentProcessor.ANALYZER_CLASS, 26 DefaultAnalyzer.class.getName()), 27 Analyzer.class); 28 } 29 }
输入输出数据结构总结:输入数据结构SequenceFile形式存储的文件键值为<Text, Text>;输出数据结构为SequenceFile形式存储,文件的键值为<Text, StringTuple>。

浙公网安备 33010602011771号