Application of Language-detection language Detection Tool Demo

Source: Internet
Author: User

This article basically borrows from the big guy article: http://www.cnblogs.com/makemelaugh/archive/2012/09/26/2704802.html

On this basis to add some of their own additions to facilitate access.

A jar package that needs to be downloaded in advance:

Google Code's Language-detection:https://code.google.com/archive/p/language-detection/downloads

Language-detection on GitHub (this is a corpus of national languages): Https://github.com/optimaize/language-detector

Jsonic jar Package: https://zh.osdn.net/projects/jsonic/

The project needs to refer to Langdetect.jar and its dependent Jsonic.jar (just take the jar and corpus in the project)

A simple example is as follows:

Demo (red circle refers to the jar package, corpus, and get the configuration file key of the tool Class):

Example stickers on:

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 {            //load corpus based on path ( Propertiesutil is the corpus path used to get the configuration in the configuration file)            Detectorfactory.loadprofile (Propertiesutil.getproperty (" Config.properties "," Langdir "));        } catch (Langdetectexception e) {            e.printstacktrace ();        }        Detector detect;        try {            Detect = Detectorfactory.create ();            The language to be detected            detect.append ("This is a Chinese");            System.out.println (Detect.detect ());        } catch (Langdetectexception e) {            e.printstacktrace ();}}    }
The Propertiesutil tool class is affixed with:
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 Ma p<string,properties> propertiesmap = new hashmap<string,properties> ();/** * Initialize Properties file * @Title: INITP roperties * @data: 2016-6-27 pm 12:44:19 * @author: Bagen * * @param file */private static void InitProperties (String file) {p roperties = 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 ();}} /** * Get value from cache map * @Title: GetProperties * @data: 2016-6-27 pm 12:59:01 * @author: Bagen * * @param file * @return */public STA Tic Properties getProperties (String file) {Properties properties = null;if (Propertiesmap.containskey (file)) {properties = (properties) propertiesmap.get (file);} return properties;} /** * Get values in Properties * @Title: GetProperty * @data: 2016-6-27 pm 12:51:03 * @author: Bagen * * @param file * @param key * @re Turn */public static string GetProperty (string file, string key) {if (!propertiesmap.containskey (file)) InitProperties ( file); return getProperties (file). GetProperty (key);}}

Operation Result:

Above.

Application of Language-detection language Detection Tool Demo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.