예제 #1
0
        private static bool loadMainDictionary(String mainPath)
        {
            logger.info("自定义词典开始加载:" + mainPath);
            if (loadDat(mainPath))
            {
                return(true);
            }
            Dictionary <String, CoreDictionary.Attribute> map = new Dictionary <string, CoreDictionary.Attribute>();
            HashSet <Nature> customNatureCollector            = new HashSet <Nature>();

            try
            {
                foreach (String p in path)
                {
                    Nature defaultNature = Nature.n;
                    int    cut           = p.IndexOf(' ');
                    if (cut > 0)
                    {
                        // 有默认词性
                        String nature = p.Substring(cut + 1);
                        p = p.Substring(0, cut);
                        try
                        {
                            defaultNature = LexiconUtility.convertStringToNature(nature, customNatureCollector);
                        }
                        catch (Exception e)
                        {
                            logger.severe("配置文件【" + p + "】写错了!" + e);
                            continue;
                        }
                    }
                    logger.info("以默认词性[" + defaultNature + "]加载自定义词典" + p + "中……");
                    bool success = load(p, defaultNature, map, customNatureCollector);
                    if (!success)
                    {
                        logger.warning("失败:" + p);
                    }
                }
                if (map.Count == 0)
                {
                    logger.warning("没有加载到任何词条");
                    map.put(Predefine.TAG_OTHER, null); // 当作空白占位符
                }
                logger.info("正在构建DoubleArrayTrie……");
                dat.build(map);
                // 缓存成dat文件,下次加载会快很多
                logger.info("正在缓存词典为dat文件……");
                // 缓存值文件
                List <CoreDictionary.Attribute> attributeList = new List <CoreDictionary.Attribute>();
                foreach (KeyValuePair <String, CoreDictionary.Attribute> entry in map)
                {
                    attributeList.Add(entry.Value);
                }
                DataOutputStream out = new DataOutputStream(new FileOutputStream(mainPath + Predefine.BIN_EXT));
                // 缓存用户词性
                IOUtil.writeCustomNature(out, customNatureCollector);
                // 缓存正文
                out.writeInt(attributeList.size());
                for (CoreDictionary.Attribute attribute : attributeList)
                {
                    out.writeInt(attribute.totalFrequency);
                    out.writeInt(attribute.nature.length);