stardict字典文件格式识别类

这个类用于处理Stardict字典文件,提供按关键字查找单词和获取原文对应翻译的功能。通过FileStream和BinaryReader读取idx和dict文件,识别以特定结束符分隔的单词,并进行字节操作获取单词位置和大小,最终解码为UTF8字符串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using  System;
using  System.Collections.Generic;
using  System.IO;
using  System.Linq;
using  System.Text;

namespace  cn.Laiyunqing
{
   
public   class  nStarDict
    {
       
private   string  idxFile;
       
private   string  dictFile;

       
public  nStarDict( string  idx,  string  dict)
       {
           
this .idxFile  =  idx;
           
this .dictFile  =  dict;
       }

       
public  List < string >  Words( string  key)
       {
           List
< string >  words  =   new  List < string > ();
           FileStream fsIdx 
=   new  FileStream( this .idxFile, FileMode.Open, FileAccess.Read);
           BinaryReader brIdx 
=   new  BinaryReader(fsIdx, Encoding.UTF8);
           
byte [] ch  =   new   byte [ 4096 ];
           
byte  end  =  ( byte ) ' ' ;
           
int  index  =   0 ;
           
for  (index  =   0 ; index  <   4096 ; index ++ )
               ch[index] 
=  ( byte ) ' ' ;
           index 
=   0 ;
           
while  ( true )
           {
               
try
               {
                   ch[index] 
=  brIdx.ReadByte();
                   index
++ ;
               }
               
catch  (EndOfStreamException)
               {
                   
break ;
               }
               
if  (ch[index  -   1 ==  end)
               {
                   
byte [] word  =   new   byte [index  -   1 ];
                   
for  ( int  i  =   0 ; i  <  index  -   1 ; i ++ )
                   {
                       word[i] 
=  ch[i];
                       ch[i] 
=   0 ;
                   }
                 
string  queryString  =  Encoding.UTF8.GetString(word);
                 
if  (queryString.StartsWith(key,  true , System.Globalization.CultureInfo.CurrentCulture))
                 {
                     words.Add(queryString);
                 }
               }
           }
           brIdx.Close();
           fsIdx.Close();
           
return  words;
       }

       
public   string  Result( string  originalText)
       {
           
string  translatedText  =   string .Empty;
           FileStream fsIdx 
=   new  FileStream( this .idxFile, FileMode.Open, FileAccess.Read);
           BinaryReader brIdx 
=   new  BinaryReader(fsIdx, Encoding.UTF8);
           FileStream fsDict 
=   new  FileStream( this .dictFile, FileMode.Open, FileAccess.Read);
           BinaryReader brDict 
=   new  BinaryReader(fsDict, Encoding.UTF8);
           
byte [] ch  =   new   byte [ 4096 ];
           
byte  end  =  ( byte ) ' ' ;
           
int  pos, size;
           
int  index  =   0 ;
           
for  (index  =   0 ; index  <   4096 ; index ++ )
               ch[index] 
=  ( byte ) ' ' ;
           index 
=   0 ;
           
while  ( true )
           {
               
try
               {
                   ch[index] 
=  brIdx.ReadByte();
                   index
++ ;
               }
               
catch  (EndOfStreamException)
               {
                   
break ;
               }
               
if  (ch[index  -   1 ==  end)
               {
                   
byte [] word  =   new   byte [index  -   1 ];
                   
for  ( int  i  =   0 ; i  <  index  -   1 ; i ++ )
                   {
                       word[i] 
=  ch[i];
                       ch[i] 
=   0 ;
                   }
                   
string  queryString  =  Encoding.UTF8.GetString(word);

                   index 
=   0 ;
                   
byte [] tmp  =  brIdx.ReadBytes( 4 );
                   pos 
=  tmp[ 3 +  (tmp[ 2 <<   8 +  (tmp[ 1 <<   16 +  (tmp[ 0 <<   24 ); 
                   tmp 
=  brIdx.ReadBytes( 4 );
                   size 
=  tmp[ 3 +  (tmp[ 2 <<   8 +  (tmp[ 1 <<   16 +  (tmp[ 0 <<   24 ); 
                   
byte [] content  =  brDict.ReadBytes(size);
                   
string  outText  =  Encoding.UTF8.GetString(content).Replace( " " " </br> " +   " </br> " ;
                   
// 是查询的单词
                    if  (queryString  ==  originalText)
                   {
                       translatedText 
=  outText;
                       
break ;
                   }
               }
           }
           brDict.Close();
           fsDict.Close();
           brIdx.Close();
           fsIdx.Close();
           
return  translatedText;
       }

    }
}
 
安装说明:http://blog.youkuaiyun.com/love254443233/article/details/39056173 安装说明: 1:sudo apt-get install stardict 2:全部解压到temp文件夹中 3:sudo mv temp/* /usr/share/stardict/dic 3:重启星际译王 包含的词库zh_CN 简体中文词典.html中所有汉语+英汉词库 cdict-gb dictionary(en - zh_CN) tarbal GPL, 4.5M, 57510 words stardict1.3 dictionary(en - zh_CN) tarbal GPL, 1.1M, 51214 words Sun Dict(en - zh_CN) tarbal GPL, 1M, 33042 words xdict-ec-gb dictionary(en - zh_CN) tarbal GPL, 3.6M, 177817 words xdict-ce-gb dictionary(zh_CN - en) tarbal GPL, 2.4M, 160995 words. cedict-gb dictionary(zh_CN - en) tarbal CEDICT LICENCE, 0.7M, 31200 words. 推荐! langdao-ec-gb dictionary(en - zh_CN) 朗道英汉字典 tarbal GPL, 9.1M, 435468 words. 推荐! langdao-ce-gb dictionary(zh_CN - en) 朗道汉英字典 tarbal GPL, 7.8M, 405719 words. 推荐! lazyworm-ec dictionary(en - zh_CN) 懒虫简明英汉词典 tarbal GPL, 10M, 452185 words. lazyworm-ce dictionary(zh_CN - en) 懒虫简明汉英词典 tarbal GPL, 1.7M, 119592 words. 新华字典 tarbal GPL, 8.9M, 74025 words. ProECCE dictionary 英汉汉英专业词典 tarbal GPL, 8.6M, 571998 words. Hanzi Master (zh_CN - en) tarbal GPL, 427K, 25992 words XiangYa Medical dictionary 湘雅医学专业词典 tarbal GPL, 28M, 1479322 words Advance Chinese big dictionary (zh_CN - zh_CN) 高级汉语大词典 tarbal GPL, 6.3M, 51675 words Advance Chinese big dictionary (zh_CN - zh_CN) 高级汉语大词典(修正版) tarbal GPL, 6.3M, 51675 words International standard Chinese big dictionary (zh_CN - zh_CN) 国际标准汉字大辞典 tarbal GPL, 1.5M, 45053 words Chinese idiom dictionary (zh_CN - zh_CN) 汉语成语词典 tarbal GPL, 2M, 24702 words Chinese idiom dictionary (zh_CN - zh_CN) 汉语成语词典(修正版) tarbal GPL, 1.9M, 13001 words 汉语成语词典(美化版) tarbal GPL, 2.8M, 13305 words Modern Chinese dictionary (zh_CN - zh_CN) 现代汉语词典 tarbal GPL, 3.3M, 57643 words Modern Chinese dictionary (zh_CN - zh_CN) 现代汉语词典(修正版) tarbal GPL, 3.2M, 57691 words 21 century bidirectional dictionary 21世纪英汉汉英双向词典 tarbal GPL, 13M, 213723 words quick_eng-zh_CN 英汉专业词典 tarball GPL, 10.7M, 634008 words 21 century bidirectional science and technology dictionary 21世纪双语科技词典 tarball GPL, 15M, 809486 words nc
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值