用java中的hadoop wordcount删除标点符号和html实体

bf1o4zei  于 2021-05-27  发布在  Hadoop
关注(0)|答案(1)|浏览(589)

我尝试删除所有标点(“,;:!?()[]”)以及所有使用hadoopapache中java的wordcount代码的html实体(&…)(https://hadoop.apache.org/docs/current/hadoop-mapreduce-client/hadoop-mapreduce-client-core/mapreducetutorial.html). 如果我只删除带有分隔符的标点符号,它的效果就和从stringescapeutils包中删除带有unescapehtml(word)的html实体一样好。
但是当我同时运行它们时,html实体仍然存在,我看不出我的代码有什么问题。

public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>{
    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();

    public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
        StringTokenizer itr = new StringTokenizer(value.toString(),".,;:!?()[]\t\n\r",true);
        while (itr.hasMoreTokens()) {
            String next_word = itr.nextToken();
            if(next_word.contains("&")){

                next_word = StringEscapeUtils.unescapeHtml(next_word);
            }
                            word.set(next_word);
                            context.write(word, one);
        }
    }
}

有人能解释一下有什么问题吗?

dluptydi

dluptydi1#

这是一个使用正则表达式从输入文件的文本中过滤出html实体和标点符号的经典例子。
为了做到这一点,我们需要创建两个正则表达式,分别用来匹配html实体和标点符号,并将它们从文本中删除,最后将剩余的有效单词设置为键值对。
从html实体开始,比如 &nbsp; , &lt; ,和 &gt; ,我们可以发现这些标记总是以 & 字符并以 ; 中间有许多字母字符的字符。因此,基于regex语法(您可以自己学习,如果您还没有学习,那么它非常有价值),下面的表达式匹配所有这些标记:

&.*?\w+;

(我们也可以在这里使用在线regex测试仪进行测试):

接下来,对于标点符号,我们可以简单地通过简单地查找既不是字母也不是数字(当然也不是空格)的字符来匹配它们,例如下一个正则表达式:

[^a-zA-Z0-9 ]

(在删除与上一个正则表达式匹配的html实体后,再次使用在线regex测试仪进行测试):

所以为了使用这些正则表达式,我们可以简单地使用 replaceAll() 方法,该方法基于第一个参数的regex将与之匹配的所有标记更改为第二个参数字符串的值。在这里,我们可以将所有匹配的标记更改为一个简单的空格,并继续在最后删除所有的双空格,以便只保留有效的单词作为键放入Map器的键值对中。
所以程序现在看起来是这样的:

import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class WordCount 
{
  public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>
  {

    private final static IntWritable one = new IntWritable(1);

    public void map(Object key, Text value, Context context) throws IOException, InterruptedException 
    {
      String line = value.toString();

       // clean up the text of the line by removing...
      line = line.replaceAll("&.*?\\w+;", " ")               // HTML entities...
                  .replaceAll("[^a-zA-Z0-9 ]", " ")         // punctuation...
                  .replaceAll("\\s+", " ");               // and getting rid of double spaces

      // if the line has remaining words after the cleanup...
      if(line != null && !line.trim().isEmpty())
      {
          String[] words = line.split(" ");   // split the text to words

          // set each word as key to the key-value pair
          for(String word : words)
              context.write(new Text(word), one);
      }  
    }
  }

  public static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable> 
  {
    private IntWritable result = new IntWritable();

    public void reduce(Text key, Iterable<IntWritable> values,Context context) throws IOException, InterruptedException 
    {
      int sum = 0;
      for (IntWritable val : values) 
      {
        sum += val.get();
      }

      result.set(sum);
      context.write(key, result);
    }
  }

  public static void main(String[] args) throws Exception 
  {
    Configuration conf = new Configuration();
    Job job = Job.getInstance(conf, "word count");
    job.setJarByClass(WordCount.class);
    job.setMapperClass(TokenizerMapper.class);
    job.setCombinerClass(IntSumReducer.class);
    job.setReducerClass(IntSumReducer.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);
    FileInputFormat.addInputPath(job, new Path(args[0]));
    FileOutputFormat.setOutputPath(job, new Path(args[1]));
    System.exit(job.waitForCompletion(true) ? 0 : 1);
  }
}

并使用以下文本文件内容作为输入:

hello &nbsp; people! how are you?
i am better than ever how about &lt; you &gt;?
i just found three &euro; on the floor....
so damn lucky good for you..!
thank you @@@@@ :)

这是给定的输出:

相关问题