All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
delphi打开已经存在的映像文件
在c#中执行sql语句时传递参数的小经验_[Asp.Net教程]
从,PHP,迁移到,ASP.NET(3)_[Asp.Net教程]
ASP.NET技巧:DataGrid传统分页方式_[Asp.Net教程]
C#教程:WebResponse类使用实例
SQL Server 2005数据库升级技巧_[SQL Server教程]
asp.net2.0中使用session进行用户登录验证
Asp.Net,构架
.Net学习,使用IronPython分析Lambda表达式_[Asp.Net教程]
经典:10个重要的PHP网络信息函数说明_php资料_编程技术
Reading number is top 10 pictures
一万二一支的万珂,用得真心肉疼。
徐若瑄展示美丽胸围1
NeedWallpaper14
中国文革时期的色情图片1
生活中总有些低调的人,不经意间散发出土豪的气质
一千块钱如何创业
这才是真正的人体艺术1
Thrilling English baby
来几张有特色的图片
Soldier saw beauty after the reaction
Download software ranking
网络管理员第三版
Tram sex maniac 2 (H) rar bag10
Boxer's Top ten classic battle3
VC++6.0简体中文版
都市狐狸姑娘传
linux高级编程
Unix video tutorial7
Tram sex maniac 2 (H) rar bag12
Jinling thirteen stock
C++教程第四版
aaa published in(发表于) 2013/12/15 8:38:23 Edit(编辑)
.NET自动字符编码识别程序库,NChardet_.net资料_编程技术

.NET自动字符编码识别程序库,NChardet_.net资料_编程技术

.NET自动字符编码识别程序库 NChardet_.net资料_编程技术-你的首页-uuhomepage.com

  什么是NChardet


NChardet是mozilla自动字符编码识别程序库chardet的.NET实现,它移植自jchardet,chardet的java版实现,可实现对给定字符流的编码探测。


NChardet是如何工作的


NChardet通过逐个比较输入字符来猜测编码;由于是猜测,所以可能会有不能完全识别的情况;如果输入字符不能确定正确的编码,那么NChardet会给出一组可能的编码值。


如何使用NChardet


要使用NChardet来探测编码,需要进行如下步骤。


1、使用制定的语言线索来构造Detector类的实例对象。
2、用实现了ICharsetDetectionObserver接口的对象作为参数来调用Detector类的Init方法。
3、传入要探测的字符流进行编码探测。
4、调用Detector类的DataEnd方法。
5、得到结果或可能的结果集。


语言线索是一个整数,可用的语言线索有如下几个:


1. Japanese
2. Chinese
3. Simplified Chinese
4. Traditional Chinese
5. Korean
6. Dont know (默认)



ICharsetDetectionObserver接口只有一个Notify方法,当NChardet引擎认为自己已经探测出正确的编码时,它就会调用这个Notify方法,用户程序可以从这个Nodify方法中得到通知(重写ICharsetDetectionObserver接口的Notify实现)。


代码实例:



//实现ICharsetDetectionObserver接口
public class MyCharsetDetectionObserver :
NChardet.ICharsetDetectionObserver
{
public string Charset = null;

public void Notify(string charset)
{
Charset = charset;
}
}



int lang = 2 ;//
//用指定的语参数实例化Detector
Detector det = new Detector(lang) ;
//初始化
MyCharsetDetectionObserver cdo = new MyCharsetDetectionObserver();
det.Init(cdo);


//输入字符流
Uri url = new Uri(“http://cn.yahoo.com”);
HttpWebRequest request =
HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response =
(HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();

byte[] buf = new byte[1024] ;
int len;
bool done = false ;
bool isAscii = true ;


while( (len=stream.Read(buf,0,buf.Length)) != 0) {
// 探测是否为Ascii编码
if (isAscii)
isAscii = det.isAscii(buf,len);


// 如果不是Ascii编码,并且编码未确定,则继续探测
if (!isAscii && !done)
done = det.DoIt(buf,len, false);


}
stream.Close();
stream.Dispose();
//调用DatEnd方法,
//如果引擎认为已经探测出了正确的编码,
//则会在此时调用ICharsetDetectionObserver的Notify方法
det.DataEnd();


if (isAscii) {
Console.WriteLine("CHARSET = ASCII");
found = true ;
}
else if (cdo.Charset != null)
{
Console.WriteLine("CHARSET = {0}",cdo.Charset);
found = true;
}

if (!found) {
string[] prob = det.getProbableCharsets() ;
for(int i=0; i Console.WriteLine("Probable Charset = " + prob[i]);
}
}
Console.ReadLine();





添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.