Beispiel #1
0
 /**
  * 将属性的词性锁定为nature
  *
  * @param nature 词性
  * @return 如果锁定词性在词性列表中,返回真,否则返回假
  */
 public boolean confirmNature(Nature nature) {
   if (attribute.nature.length == 1 && attribute.nature[0] == nature) {
     return true;
   }
   boolean result = true;
   int frequency = attribute.getNatureFrequency(nature);
   if (frequency == 0) {
     frequency = 1000;
     result = false;
   }
   attribute = new CoreDictionary.Attribute(nature, frequency);
   return result;
 }
Beispiel #2
0
 public boolean hasNature(Nature nature) {
   return attribute.getNatureFrequency(nature) > 0;
 }
Beispiel #3
0
  private static boolean load(String path) {
    logger.info("核心词典开始加载:" + path);
    if (loadDat(path)) return true;
    TreeMap<String, CoreDictionary.Attribute> map = new TreeMap<String, Attribute>();
    BufferedReader br = null;
    try {
      br = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"));
      String line;
      int MAX_FREQUENCY = 0;
      long start = System.currentTimeMillis();
      while ((line = br.readLine()) != null) {
        String param[] = line.split("\\s");
        int natureCount = (param.length - 1) / 2;
        CoreDictionary.Attribute attribute = new CoreDictionary.Attribute(natureCount);
        for (int i = 0; i < natureCount; ++i) {
          attribute.nature[i] = Enum.valueOf(Nature.class, param[1 + 2 * i]);
          attribute.frequency[i] = Integer.parseInt(param[2 + 2 * i]);
          attribute.totalFrequency += attribute.frequency[i];
        }
        map.put(param[0], attribute);
        MAX_FREQUENCY += attribute.totalFrequency;
      }
      logger.info(
          "核心词典读入词条"
              + map.size()
              + " 全部频次"
              + MAX_FREQUENCY
              + ",耗时"
              + (System.currentTimeMillis() - start)
              + "ms");
      br.close();
      trie.build(map);
      logger.info("核心词典加载成功:" + trie.size() + "个词条,下面将写入缓存……");
      try {
        DataOutputStream out = new DataOutputStream(new FileOutputStream(path + Predefine.BIN_EXT));
        Collection<CoreDictionary.Attribute> attributeList = map.values();
        out.writeInt(attributeList.size());
        for (CoreDictionary.Attribute attribute : attributeList) {
          out.writeInt(attribute.totalFrequency);
          out.writeInt(attribute.nature.length);
          for (int i = 0; i < attribute.nature.length; ++i) {
            out.writeInt(attribute.nature[i].ordinal());
            out.writeInt(attribute.frequency[i]);
          }
        }
        trie.save(out);
        out.close();
      } catch (Exception e) {
        logger.warning("保存失败" + e);
        return false;
      }
    } catch (FileNotFoundException e) {
      logger.warning("核心词典" + path + "不存在!" + e);
      return false;
    } catch (IOException e) {
      logger.warning("核心词典" + path + "读取错误!" + e);
      return false;
    }

    return true;
  }