nutch-1.7-二次开发-Content中增加编码
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中增加编码的更多相关文章
- NX二次开发-NXOpen中Point3d类型转换成point类型
NX9+VS2012 #include <NXOpen/NXObject.hxx> #include <NXOpen/Part.hxx> #include <NXOpen ...
- AE二次开发中几个功能速成归纳(符号设计器、创建要素、图形编辑、属性表编辑、缓冲区分析)
/* * 实习课上讲进阶功能所用文档,因为赶时间从网上抄抄改改,凑合能用,记录一下以备个人后用. * * ----------------------------------------------- ...
- SPSS二次开发
在以前关于SPSS二次开发文章中留下过自己联系方式,差不多一年的时间,零零散散的和我取得联系的人也有几十位,看来对于SPSS二次开发的需求不少. Web SPSS系统是利用SPSS二次开发技术,使用户 ...
- 齐博x1 二次开发的灵魂fun函数
X1最强大之处,体现在灵活,扩展性强,在使用过程中,你会发现灵活之处非常之多. 现在跟大家讲一下,灵魂函数 fun() X1的核心函数文件是 application/common.php 随着模块频道 ...
- Nutch 二次开发parse纸
大约nutch基础知识可以参考lemo柱 nutch支持二次开发,为了满足搜索的准确性的问题,内容提取出来作为索引的内容,相应的是parse_text的数据.我使用的事nutch1.4 版本号,在cy ...
- CAD二次开发中浮动面板不浮动的问题
CAD二次开发中创建了一个浮动面板,想让它创建出来后以浮动状态显示, 但是DockSides.None设置完后,面板还是不浮动.搞了很久,最后原来是 需要先设置Visible,再设置DockSides ...
- 在AE二次开发中出“正试图在 OS 加载程序锁内执行托管代码。不要尝试在 DllMain 或映像初始化函数内运行托管代码,这样做会导致应用程序挂起。”异常解决方案
今天的一个项目总用到了AE的开发组件,也就是ESRI公司提供的一系列的开发包(组件)都是以dll(动态链接库的形式)然后今天在调试的时候却出现了“正试图在 OS 加载程序锁内执行托管代码.不要尝试在 ...
- visual studio2010中C#生成的,ArcGIS二次开发的basetool的dll,注册为COM组件tlb文件,并在arcmap中加载使用
写了个标题好长啊~~~~ 这两天又认识了一个新玩意,记录一下下,啦啦啦~~~~~ 话说,认识arcgis快十年了,从桌面版到engine的二次开发,其实不过才认识到它的冰山一角, 它总是能带来很多还未 ...
- NX二次开发-Block UI C++界面关于 在Block UI中UF_initialize();和UF_terminate();的使用
关于 在Block UI中UF_initialize();和UF_terminate();的使用 用Block UI作NX二次开发的时候,不需要在使用UFUN函数的时候加UF_initialize() ...
随机推荐
- MYSQL 索引页 结构图
create table t( a ) not ) default null,primary key(a)); mysql> select * from t; +----+------+ | a ...
- Qemu下安装Sun Solairs8简明教程 转
http://blog.csdn.net/stonesharp/article/details/8928393 Qemu下安装Sun Solairs8简明教程(Centos6. / Win7) 作者: ...
- 注释驱动的 Spring cache 缓存介绍--转载
概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...
- 如何查看电脑的链接WIFI密码
这个查看起来确实有难度,一般软件也难以破解.不如你试试在浏览器输入192.168.1.1,账户密码都是admin然后进入路由设置里.无线安全设置里有.
- [转载]js中__proto__和prototype的区别和关系
首先,要明确几个点:1.在JS里,万物皆对象.方法(Function)是对象,方法的原型(Function.prototype)是对象.因此,它们都会具有对象共有的特点.即:对象具有属性_ ...
- java 百分比显示
DecimalFormat percent = new DecimalFormat("0.00%"); completed_num = (double) involvedTask_ ...
- CenOs安装中文输入法
http://jingyan.baidu.com/album/d8072ac4434666ec95cefda1.html?picindex=2 查看链接
- BroadcastReceiver和Intetnt的理解 Day34
BroadcastReceiver和Intetnt的理解 Day34 mobile4.0 短信监控 问题堆栈 1. 下载开源项目View.网址自己fork一下 2. ContentProvider原理 ...
- setTimeout 和 setInterval区别
setTimeout和setIntelval都有定时的功能!!!取消定时功能的时候,都有对应的clearTimeout以及clearInterval与之对应. 但是他们之间是有区别的! setTime ...
- (八)Struts2 文件上传和下载
所有的学习我们必须先搭建好Struts2的环境(1.导入对应的jar包,2.web.xml,3.struts.xml) 第一节:Struts2 文件上传 Struts2 文件上传基于Struts2 拦 ...