码迷,mamicode.com
首页 > 编程语言 > 详细

language-detection 语言检测工具的应用demo

时间:2017-07-20 16:27:56      阅读:2032      评论:0      收藏:0      [点我收藏+]

标签:logs   import   arch   应用   git   tool   puts   image   获取   

本文基本借鉴大佬文章:http://www.cnblogs.com/makemelaugh/archive/2012/09/26/2704802.html

在此基础上添加一些自己的补充,方便查阅。

 

提前需要下载的jar包:

谷歌code的 language-detection下载地址:https://code.google.com/archive/p/language-detection/downloads

github上的 language-detection下载地址(这个里面有各国语言的语料包):https://github.com/optimaize/language-detector

JSONIC的jar包下载地址: https://zh.osdn.net/projects/jsonic/

项目中需要引用langdetect.jar和其依赖的jsonic.jar  (项目中只需要把jar包和语料包拿过来就可以了)

 

简单例子如下:

Demo截图(红圈内分别为引用的jar包,语料包以及获取配置文件key的工具类):

技术分享

例子贴上:

package langDetectionTest.langDetection;

import com.cybozu.labs.langdetect.Detector;
import com.cybozu.labs.langdetect.DetectorFactory;
import com.cybozu.labs.langdetect.LangDetectException;
import langDetectionTest.tools.PropertiesUtil;

/**
 * Created by xinlian on 2017/7/20.
 */
public class Test {
    public static void main(String[] args) {

        try {
            //根据路径加载语料包(PropertiesUtil是用来获取配置文件中的配置的语料包路径)
            DetectorFactory.loadProfile(PropertiesUtil.getProperty("config.properties ", "langdir"));
        } catch (LangDetectException e) {
            e.printStackTrace();
        }

        Detector detect;
        try {
            detect = DetectorFactory.create();
            //需要检测的语言
            detect.append("这是一段中文");
            System.out.println(detect.detect());
        } catch (LangDetectException e) {
            e.printStackTrace();
        }
    }
}
PropertiesUtil工具类贴上:
package langDetectionTest.tools;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * ?????????????
 * 
 * @author zhuf
 * @time 2011-08-26
 */
public class PropertiesUtil {

private static Properties properties;
	
	private static Map<String ,Properties> propertiesMap = new HashMap<String ,Properties>();
	
	/**
	 * 初始化properties文件
	 * @Title: initProperties
	 * @data:2016-6-27下午12:44:19
	 * @author:bagen
	 *
	 * @param file
	 */
	private static void initProperties(String file) {
		properties = new Properties();
		try {
			ClassLoader loader = PropertiesUtil.class.getClassLoader();
			java.io.InputStream inStream = loader.getResourceAsStream(file);
			if(inStream != null) {
				properties.load(inStream);
			}
			propertiesMap.put(file, properties);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 从缓存Map获取值
	 * @Title: getProperties
	 * @data:2016-6-27下午12:59:01
	 * @author:bagen
	 *
	 * @param file
	 * @return
	 */
	public static Properties getProperties(String file){
		Properties properties = null;
		if(propertiesMap.containsKey(file)){
			properties = (Properties) propertiesMap.get(file);
		}
		return properties;
	}
	
	/**
	 * 获取properties中的值
	 * @Title: getProperty
	 * @data:2016-6-27下午12:51:03
	 * @author:bagen
	 *
	 * @param file
	 * @param key
	 * @return
	 */
	public static String getProperty(String file, String key) {
		if(!propertiesMap.containsKey(file))
			initProperties(file);
		return getProperties(file).getProperty(key);
	}
}

  运行结果:

技术分享

以上。

language-detection 语言检测工具的应用demo

标签:logs   import   arch   应用   git   tool   puts   image   获取   

原文地址:http://www.cnblogs.com/Alex-zqzy/p/7211797.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!