.NET自动字符编码识别程序库 NChardet

1 篇文章 0 订阅
什么是NChardet

     NChardetmozilla自动字符编码识别程序chardet.NET实现,它移植自jchardetchardetjava版实现,可实现对给定字符流的编码探测

 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<prob.Length; i++) {
            Console.WriteLine(
"Probable Charset = " + prob[i]);
        }
    }
    Console.ReadLine();

  

获得NChardet

    NChardet Library (.NET 1.1 and .NET 2.0 ,DLL)

    NChardet Source Code (开发环境为SharpDevelop 2.0)

 版权说明

     本程序移植自jchardet,版权说明请参照jchardetC#代码采用BSD授权。

 相关链接

    mozilla chardet http://www.mozilla.org/projects/intl/chardet.html
    mozilla chardet 下载  http://lxr.mozilla.org/mozilla/source/intl/chardet/
    jchardet http://jchardet.sourceforge.net/
    SharpDevelop http://www.icsharpcode.net/OpenSource/SD/
    字符集编码的自动识别jchardet http://hedong.3322.org/archives/000361.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
NChardet文本文件编码探测库源码,C#,txt文件编码自动探测 用于自动探测TXT文本文件编码,支持GB2312、UTF-8、ASCII等几乎所有主流编码的探测,使用方法如下: static public Encoding GetEncoding(string bookPath, ref string charsetName) { charsetName = ""; //1. Japanese //2. Chinese //3. Simplified Chinese //4. Traditional Chinese //5. Korean //6. Dont know (默认) 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 = File.OpenRead(bookPath); byte[] buf = new byte[1024]; int len; bool done = false; bool isAscii = true; bool found = false; 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) { charsetName = cdo.Charset; return GetEncodingFromEncodingName(cdo.Charset); } if (!found) { charsetName = ""; string[] prob = det.getProbableCharsets(); for (int i = 0; i < prob.Length; i++) { //Console.WriteLine("Probable Charset = " + prob[i]); } return Encoding.Default; } return Encoding.Default; } static public Encoding GetEncodingFromEncodingName(string charset) { if (string.IsNullOrWhiteSpace(charset)) { charset = "gb2312"; } return Encoding.GetEncoding(charset); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值