1 识别nutch-1.7的编码,完成

以前1.2是在 org.apache.nutch.parse.html.HtmlParser
EncodingDetector detector = new EncodingDetector(conf);
detector.autoDetectClues(content, true);
detector.addClue(sniffCharacterEncoding(contentInOctets), "sniffed");
String encoding = detector.guessEncoding(content,
defaultCharEncoding); metadata.set(Metadata.ORIGINAL_CHAR_ENCODING, encoding);
metadata.set(Metadata.CHAR_ENCODING_FOR_CONVERSION, encoding); ------在这里讲识别出来的编码写进content的metadata中
System.out.println("set charset in Content " + encoding);
content.getMetadata().add("charset", encoding); 但是在1.7中却不行了。原因不详 目前的策略是,org.apache.nutch.protocol.http.api.HttpBase
位置:src/plugin/lib-http/src/java下的org.apache.nutch.protocol.http.api包中
中进行修改。 做法是将HtmlParser中的代码搬过来。 EncodingDetector detector = new EncodingDetector(conf);
detector.autoDetectClues(c, true);
detector
.addClue(sniffCharacterEncoding(c.getContent()), "sniffed");
String encoding = detector.guessEncoding(c, "windows-1252"); System.out.println("set charset in Content " + encoding);
c.getMetadata().add("charset", encoding); 同时将相关函数字段拷贝过来:
// NUTCH-1006 Meta equiv with single quotes not accepted
private static Pattern metaPattern = Pattern.compile(
"<meta\\s+([^>]*http-equiv=(\"|')?content-type(\"|')?[^>]*)>",
Pattern.CASE_INSENSITIVE);
private static Pattern charsetPattern = Pattern.compile(
"charset=\\s*([a-z][_\\-0-9a-z]*)", Pattern.CASE_INSENSITIVE); // I used 1000 bytes at first, but found that some documents have
// meta tag well past the first 1000 bytes.
// (e.g. http://cn.promo.yahoo.com/customcare/music.html)
private static final int CHUNK_SIZE = 2000; /**
* Given a <code>byte[]</code> representing an html file of an
* <em>unknown</em> encoding, read out 'charset' parameter in the meta tag
* from the first <code>CHUNK_SIZE</code> bytes. If there's no meta tag for
* Content-Type or no charset is specified, <code>null</code> is returned. <br />
* FIXME: non-byte oriented character encodings (UTF-16, UTF-32) can't be
* handled with this. We need to do something similar to what's done by
* mozilla
* (http://lxr.mozilla.org/seamonkey/source/parser/htmlparser/src/nsParser
* .cpp#1993). See also http://www.w3.org/TR/REC-xml/#sec-guessing <br />
*
* @param content
* <code>byte[]</code> representation of an html file
*/ private static String sniffCharacterEncoding(byte[] content) {
int length = content.length < CHUNK_SIZE ? content.length : CHUNK_SIZE; // We don't care about non-ASCII parts so that it's sufficient
// to just inflate each byte to a 16-bit value by padding.
// For instance, the sequence {0x41, 0x82, 0xb7} will be turned into
// {U+0041, U+0082, U+00B7}.
String str = "";
try {
str = new String(content, 0, length, Charset.forName("ASCII")
.toString());
} catch (UnsupportedEncodingException e) {
// code should never come here, but just in case...
return null;
} Matcher metaMatcher = metaPattern.matcher(str);
String encoding = null;
if (metaMatcher.find()) {
Matcher charsetMatcher = charsetPattern.matcher(metaMatcher
.group(1));
if (charsetMatcher.find())
encoding = new String(charsetMatcher.group(1));
} return encoding;
}
同时修改:SegmentReader的中的方法
public void reduce(Text key, Iterator<NutchWritable> values,
OutputCollector<Text, Text> output, Reporter reporter)
throws IOException {
StringBuffer dump = new StringBuffer(); dump.append("\nRecno:: ").append(recNo++).append("\n");
dump.append("URL:: " + key.toString() + "\n");
while (values.hasNext()) {
Writable value = values.next().get(); // unwrap
if (value instanceof CrawlDatum) {
dump.append("\nCrawlDatum::\n").append(((CrawlDatum) value).toString());
} else if (value instanceof Content) {
Content ct = (Content)value;
String charset = ct.getMetadata().get("charset");
dump.append("\nContent::\n"
).append(ct.toString(charset));
} else if (value instanceof ParseData) {
dump.append("\nParseData::\n").append(((ParseData) value).toString());
} else if (value instanceof ParseText) {
dump.append("\nParseText::\n").append(((ParseText) value).toString());
} else if (LOG.isWarnEnabled()) {
LOG.warn("Unrecognized type: " + value.getClass());
}
}
output.collect(key, new Text(dump.toString()));
}

nutch-1.7-二次开发-Content中增加编码的更多相关文章

  1. NX二次开发-NXOpen中Point3d类型转换成point类型

    NX9+VS2012 #include <NXOpen/NXObject.hxx> #include <NXOpen/Part.hxx> #include <NXOpen ...

  2. AE二次开发中几个功能速成归纳(符号设计器、创建要素、图形编辑、属性表编辑、缓冲区分析)

    /* * 实习课上讲进阶功能所用文档,因为赶时间从网上抄抄改改,凑合能用,记录一下以备个人后用. * * ----------------------------------------------- ...

  3. SPSS二次开发

    在以前关于SPSS二次开发文章中留下过自己联系方式,差不多一年的时间,零零散散的和我取得联系的人也有几十位,看来对于SPSS二次开发的需求不少. Web SPSS系统是利用SPSS二次开发技术,使用户 ...

  4. 齐博x1 二次开发的灵魂fun函数

    X1最强大之处,体现在灵活,扩展性强,在使用过程中,你会发现灵活之处非常之多. 现在跟大家讲一下,灵魂函数 fun() X1的核心函数文件是 application/common.php 随着模块频道 ...

  5. Nutch 二次开发parse纸

    大约nutch基础知识可以参考lemo柱 nutch支持二次开发,为了满足搜索的准确性的问题,内容提取出来作为索引的内容,相应的是parse_text的数据.我使用的事nutch1.4 版本号,在cy ...

  6. CAD二次开发中浮动面板不浮动的问题

    CAD二次开发中创建了一个浮动面板,想让它创建出来后以浮动状态显示, 但是DockSides.None设置完后,面板还是不浮动.搞了很久,最后原来是 需要先设置Visible,再设置DockSides ...

  7. 在AE二次开发中出“正试图在 OS 加载程序锁内执行托管代码。不要尝试在 DllMain 或映像初始化函数内运行托管代码,这样做会导致应用程序挂起。”异常解决方案

    今天的一个项目总用到了AE的开发组件,也就是ESRI公司提供的一系列的开发包(组件)都是以dll(动态链接库的形式)然后今天在调试的时候却出现了“正试图在 OS 加载程序锁内执行托管代码.不要尝试在 ...

  8. visual studio2010中C#生成的,ArcGIS二次开发的basetool的dll,注册为COM组件tlb文件,并在arcmap中加载使用

    写了个标题好长啊~~~~ 这两天又认识了一个新玩意,记录一下下,啦啦啦~~~~~ 话说,认识arcgis快十年了,从桌面版到engine的二次开发,其实不过才认识到它的冰山一角, 它总是能带来很多还未 ...

  9. NX二次开发-Block UI C++界面关于 在Block UI中UF_initialize();和UF_terminate();的使用

    关于 在Block UI中UF_initialize();和UF_terminate();的使用 用Block UI作NX二次开发的时候,不需要在使用UFUN函数的时候加UF_initialize() ...

随机推荐

  1. CreateEvent的使用方法

     CreateEvent的使用方法收藏 新一篇: PreCreateWindow的作用和用法 | 旧一篇: VC中_T()的作用 function StorePage(){d=document;t=d ...

  2. richTextBox插入表格

    附件:http://files.cnblogs.com/xe2011/richTextBox_InsertTable.rar 插入表格 /// <summary> /// 插入表格 /// ...

  3. LVM物理卷命令

    1. 物理卷命令  一般维护命令:  #pvscan //在系统的全部磁盘中搜索已存在的物理卷  #pvdisplay 物理卷全路径名称 //用于显示指定物理卷的属性. #pvdata 物理卷全路径名 ...

  4. the Linux Kernel: Traffic Control, Shaping and QoS

    −Table of Contents Journey to the Center of the Linux Kernel: Traffic Control, Shaping and QoS 1 Int ...

  5. LabVIEW系列——生产现场故障邮件通知

    第一步.建立邮件服务器,winmail做邮件服务器是个不错的选择.不过在使用前需要注册. 第二步.创建修改域名.这里创建的域名不是真正的域名,只能用于局域网内.假设:lhy.com.cn 第三步.创建 ...

  6. pcap 安装(debian7 linux) qt 使用pcap.h

    安装方法一.sudo apt-get install libpcap-dev 安装方法二. 去http://www.tcpdump.org/下载最新的libpcap.tar.gz包 解压以后 ./co ...

  7. HDU2054JAVA

    (包括2016级新生)除了校赛,还有什么途径可以申请加入ACM校队? A == B ? Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: ...

  8. AS3性能及Flex-Formatting设置问题

    1.支持Vector 2.for each in更是从Flash Player 9 3.Flash原生的bitmap.encode 4.如打开位图缓存:使用bitmapData.lock:把bitma ...

  9. Mybatis下配置调用Oracle自定义函数返回的游标结果集

    在ibatis和Mybatis对存储过程和函数函数的调用的配置Xml是不一样的,以下是针对Mybatis 3.2的环境进行操作的. 第一步配置Mapper的xml内容 <mapper names ...

  10. jsp获取服务端的访问信息

    获取服务端访问信息 public static String getUrl(HttpServletRequest request){ String url = ""; if(req ...