ITeye博客

读取StarDict里的字典文件

msino · 2009-04-05 20:47 · HTML · .net · OSGI · 360 · thread · Markdown

搜集了些资料,并重新整理了源代码

 

1. 下载stardict字典文件, 以“langdao-ec-gb dictionary(en - zh_CN) 朗道英汉字典”为例

 

http://prdownloads.sourceforge.net/stardict/stardict-langdao-ec-gb-2.4.2.tar.bz2?download

 

2. 将附件的两个jar包加入到build path中

 

3. 将附件的config.properties放在你的项目根目录下, 并配置字典路径和字典文件名。如下:

 

dictPath=./stardict-langdao-ec-gb-2.4.2
dictName=langdao-ec-gb

 

4. 使用。 看代码:

 

package com.msino.dict.test;

import com.msino.dict.StarDictReader;
import com.msino.dict.manager.DictsManager;
import com.msino.dict.service.IDictsManager;
import com.msino.dict.service.core.Word;
import java.io.FileNotFoundException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Test case of class <code>StarDictReader</code>
 * @author XuDong.Su
 * @version 1.0,  Apr 5, 2009
 * @see com.msino.dict.StarDictReader
 */
public class StarDictReaderTest {
    private StarDictReader instance;
    private IDictsManager dictsManager = new DictsManager();

    private String dictsProp = dictsManager.getDictProps();

    String dictFileDir = "";
    String dictName = "";
	
    /**
     * Default constructor.
     */
    public StarDictReaderTest() {
    	
    	dictFileDir = dictsProp.substring(0, dictsProp.indexOf("||"));
    	dictName = dictsProp.substring(dictsProp.indexOf("||") + 2, dictsProp.length());
    	
    	System.out.println("dictFileDir : " + dictFileDir);
	System.out.println("dictName : " + dictName);
		
		
        try {
            instance =  new StarDictReader(dictFileDir, dictName);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(StarDictReaderTest.class.getName()).
                    log(Level.SEVERE, null, ex);
        }
    }
    
    public static void main(String[] args) throws InterruptedException {
    	StarDictReaderTest test = new StarDictReaderTest();
    	test.lookup();
    }

    /**
     * Test of lookup method, of class DictEngine.
     * @throws java.lang.InterruptedException Thread.sleep(long)
     */
    public void lookup() throws InterruptedException {
        System.out.println("lookup");

        String word = "A Programming Language on the 360";
        String word2 = "crystal";
        String word3 = "trailer";
        String word4 = "not exsits";

        try {
            Word result = instance.lookup(word);
            System.out.println(result.self + ": " + result.definition);

            Thread.sleep(1000);

            long start = System.currentTimeMillis();

            System.out.println("Time: " + (start) + "ms");
            result = instance.lookup(word2);
            System.out.println(result.self + ": " + result.definition);

            long end1 = System.currentTimeMillis();
            System.out.println("Time: " + (end1) + "ms");
            System.out.println("Space Time: " + (end1 - start) + "ms");

            result = instance.lookup(word3);
            System.out.println(result.self + ": " + result.definition);
            
            long end2 = System.currentTimeMillis();
            System.out.println("Time: " + (end2 - end1) + "ms");
            
            result = instance.lookup(word4);
            System.out.println(result.self + ": " + result.definition);
            
            long end3 = System.currentTimeMillis();
            System.out.println("Time: " + (end3 - end2) + "ms");
            
        } catch (Exception ex) {
        	System.out.println(ex.getMessage());
            //Logger.getLogger(StarDictReaderTest.class.getName()).log(Level.INFO, ex.getMessage());
        }
    }

}

评论(6)

  1. #6 楼 · msino · 2009-04-09 21:31

    halk writes:

    msino 写道halk 写道msino 写道halk 写道两个分别由什么作用?stardict_tools.jar 用来读取gzip格式的字典文件, 由传入的单词返回翻译结果knopflerfish-osgi.jar看这里 http://www.knopflerfish.org/download.html还是想看你stardict_tools.jar 包里的代码,那才是精华..哈哈,反编译哈哈,试过了 全是自己写的?见GoogleCode有相关的工程,但是是C的

    No,no CSDN上下的,不过原代码有点问题,HardCode了文件路径,我重写了那个方法,顺便整理了下代码

  2. #5 楼 · halk · 2009-04-08 18:46

    msino writes:

    halk 写道 msino 写道 halk 写道两个分别由什么作用?stardict_tools.jar 用来读取gzip格式的字典文件, 由传入的单词返回翻译结果knopflerfish-osgi.jar看这里 http://www.knopflerfish.org/download.html还是想看你stardict_tools.jar 包里的代码,那才是精华..

    哈哈,反编译

    哈哈,试过了 :!: 全是自己写的? 见GoogleCode有相关的工程,但是是C的

  3. #4 楼 · msino · 2009-04-08 11:31

    halk writes:

    msino 写道 halk 写道两个分别由什么作用?stardict_tools.jar 用来读取gzip格式的字典文件, 由传入的单词返回翻译结果knopflerfish-osgi.jar看这里 http://www.knopflerfish.org/download.html还是想看你stardict_tools.jar 包里的代码,那才是精华..

    哈哈,反编译

  4. #3 楼 · halk · 2009-04-07 09:40

    msino writes:

    halk 写道两个分别由什么作用?stardict_tools.jar 用来读取gzip格式的字典文件, 由传入的单词返回翻译结果knopflerfish-osgi.jar看这里 http://www.knopflerfish.org/download.html

    还是想看你stardict_tools.jar 包里的代码,那才是精华..

  5. #2 楼 · msino · 2009-04-06 22:16

    halk writes:

    两个分别由什么作用?

    stardict_tools.jar 用来读取gzip格式的字典文件, 由传入的单词返回翻译结果

    knopflerfish-osgi.jar看这里 http://www.knopflerfish.org/download.html

  6. #1 楼 · halk · 2009-04-06 17:23

    两个分别由什么作用?

评论功能维护中,当前仅展示历史评论。