Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java中的hadoop Wordcount删除标点符号和HTML实体_Java_Hadoop_Html Entities - Fatal编程技术网

使用java中的hadoop Wordcount删除标点符号和HTML实体

使用java中的hadoop Wordcount删除标点符号和HTML实体,java,hadoop,html-entities,Java,Hadoop,Html Entities,我尝试从hadoop Apache()中使用java中的Wordcount代码删除所有标点(,;:!?()[])以及所有HTML实体(&…)。如果我只删除带有分隔符的标点符号,它的工作效果非常好,就像我从StringEscapeUtils包中删除带有unescapeHtml(word)的HTML实体一样 但是当我同时运行它们时,HTML实体仍然存在,我看不出我的代码有什么问题 public static class TokenizerMapper extends Mapper<Ob

我尝试从hadoop Apache()中使用java中的Wordcount代码删除所有标点(,;:!?()[])以及所有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);
        }
    }
}
公共静态类TokenizerMapper扩展映射器{
私有最终静态IntWritable one=新的IntWritable(1);
私有文本字=新文本();
公共void映射(对象键、文本值、上下文上下文)引发IOException、InterruptedException{
StringTokenizer itr=新的StringTokenizer(value.toString(),“;:!?()[]\t\n\r”,true);
而(itr.hasMoreTokens()){
字符串next_word=itr.nextToken();
if(下一个单词包含(&)){
next\u word=StringEscapeUtils.unescapeHtml(next\u word);
}
word.set(下一个单词);
上下文。写(单词,一);
}
}
}

有人能解释一下问题出在哪里吗?

这是一个典型的示例,用于从输入文件中的文本中过滤出HTML实体和标点符号

为了做到这一点,我们需要创建两个正则表达式,分别用于匹配HTML实体和标点符号,并将它们从文本中删除,最终将剩余的有效单词设置为键值对

从像
这样的HTML实体开始,我们可以发现这些标记总是以
&
字符开头,以
结尾字符,中间有许多字母字符。因此,基于RegEx语法(您可以自己研究,如果您还没有研究过,它确实很有价值),以下表达式匹配所有这些标记:

&.*?\w+;
(我们也可以使用在线正则表达式测试仪进行测试):

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

[^a-zA-Z0-9 ]
(删除与上一个正则表达式匹配的HTML实体后,再次使用online regex tester进行测试):

因此,为了使用这些正则表达式,我们只需使用
replaceAll()
方法,该方法基于第一个参数的正则表达式,将与之匹配的所有标记更改为第二个参数字符串的值。在这里,我们可以将所有匹配的标记更改为一个简单的空格,并继续在最后删除所有双空格,以便只剩下有效的单词作为键放入映射器的键值对中

所以这个程序现在看起来是这样的:

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 @@@@@ :)