关于IKAnalyzer自定义分词的切换主词典的方法

定义一个MYCONFIG类实现Configuration接口

package util;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.InvalidPropertiesFormatException;
import java.util.List;
import java.util.Properties;

import org.wltea.analyzer.cfg.Configuration;

public class MyConfig  implements Configuration
{
  private static final Configuration CFG=new MyConfig();
  /**
   * 分词器默认字典路径
   */
  private  String PATH_DIC_MAIN = "org/wltea/analyzer/dic/main2012.dic";
  private static final String PATH_DIC_QUANTIFIER = "org/wltea/analyzer/dic/quantifier.dic";
  
  public static String getEXT_DICT() {
    return EXT_DICT;
}


public static void setEXT_DICT(String eXT_DICT) {
    EXT_DICT = eXT_DICT;
}


private static final String FILE_NAME = "IKAnalyzer.cfg.xml";//保留静态自定义词库的功能
  private static  String EXT_DICT = "ext_dict";//配置扩展词典
  private static final String EXT_STOP = "ext_stopwords";//配置扩展停止词典
  
  private Properties props;
  
  private boolean useSmart;//是否使用Smart方式

  public static Configuration getInstance()
  {
    return CFG;
  }

  

/**
 * 初始化配置文件
 */
public MyConfig()
  {
    this.props = new Properties();

    //InputStream input = getClass().getClassLoader().getResourceAsStream("IKAnalyzer.cfg.xml");
    InputStream input = getClass().getClassLoader().getResourceAsStream(FILE_NAME);
    if (input != null)
      try {
        this.props.loadFromXML(input);
      } catch (InvalidPropertiesFormatException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
  }

  public boolean useSmart()
  {
    return this.useSmart;
  }
  
//设置useSmart标志位
  public void setUseSmart(boolean useSmart)
  {
    this.useSmart = useSmart;
  }

  
  /**
   * 设置主词典路径
   */
  public void setMainDictionary(String path){
      this.PATH_DIC_MAIN=path;
  }
  public String getMainDictionary()
  {
    return PATH_DIC_MAIN;
  }

  /**
   * 获取量词词典路径
   * 
   */
  public String getQuantifierDicionary()
  {
    return PATH_DIC_QUANTIFIER;
  }

  /**
   * 获取扩展字典配置路径
   */
  public List<String> getExtDictionarys()
  {
    List<String> extDictFiles = new ArrayList<String>(2);
    
    String extDictCfg = this.props.getProperty(EXT_DICT);
    if (extDictCfg != null)
    {
      String[] filePaths = extDictCfg.split(";");
      if (filePaths != null) {
        for (String filePath : filePaths) {
          if ((filePath != null) && (!"".equals(filePath.trim()))) {
            extDictFiles.add(filePath.trim());
          }
        }
      }
    }
    return extDictFiles;
  }

  
  /**
   * 获取扩展停止词典配置路径
   */
  public List<String> getExtStopWordDictionarys()
  {
    List<String> extStopWordDictFiles = new ArrayList<String>(2);
    String extStopWordDictCfg = this.props.getProperty(EXT_STOP);
    if (extStopWordDictCfg != null)
    {
        //使用分隔多个扩展字典配置
      String[] filePaths = extStopWordDictCfg.split(";");
      if (filePaths != null) {
        for (String filePath : filePaths) {
          if ((filePath != null) && (!"".equals(filePath.trim()))) {
            extStopWordDictFiles.add(filePath.trim());
          }
        }
      }
    }
    return extStopWordDictFiles;
  }
}

实例化词典时:

MyConfig cfg = new MyConfig.getInstance();

MyConfig.setPath_DIC_MAIN("org/wltea/analyzer/dic/zwm.dic");

Dictionary.initial(cfg);

 

转载于:https://my.oschina.net/xiaoshoubingliang/blog/740880

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值