字典

来源:互联网 发布:windows终端 编辑:程序博客网 时间:2024/04/29 10:59

// 中文分词词典类
// Class for Dictionary
//
import java.util.*;
import java.io.*;
import java.lang.*;

public class Dictionary
{
        HashMap hm;  //a word set

        public Dictionary()
        {
                hm = new HashMap();
        }

        public Dictionary(String fileName)
        {
                hm = new HashMap();
                Load(fileName);
        }

        public void Load(String fileName)
        {
                try
                {
                        BufferedReader in=
                                new BufferedReader(
                                        new FileReader(fileName) );

                        String s;
                        String []words;
                        while((s = in.readLine()) != null)
                        {
                                words = s.split("/t");
                                Integer freq = new Integer(words[1]);
                                hm.put(words[0], freq );

                        }
                }
                catch(IOException e)
                {
                        System.out.println("Error: " + e);
                }
        }

        public boolean Find(String word)
        {
                return hm.containsKey(word);
        }
        public boolean findto(String s)
        {
            int len = s.length();
            for (int i=0; i<len; ++i)
            {
                char ch = s.charAt(i);
                if (!(((ch > 'a') && (ch < 'z')) || ((ch>'A') && (ch < 'Z')) || ((ch > '0') && (ch < '9'))))
               return true;
            }
             return false;
       }
     

      
       
        public Integer GetFreq(String word)
        {
                if(Find(word) == false)
                {
                        return new Integer(0);
                }
                return (Integer)hm.get(word);
        }
}