I will give you a piece of text to check the language? Two open-source projects are available. One is Apache Tika and the other is language-detection. Language-detection is an open-source software package for language detection on Google Code. It is ready for use on a daily basis. Its Project LINK is http://code.google.com/p/?age-detection. Basically, you just need to reference langdetect. jar and its dependent jsonic-1.3.0.jar (also Japanese). Below is a simple example.
Create a Java project, introduce the above two jar packages into the project, and create a test class, as shown below:
Import Java.net. urisyntaxexception;
ImportCom. cybozu. Labs. langdetect .*;
/**
*@ AuthorXxx
*
*/
Public ClassLangtest
{
/**
* @ Param ARGs
*/
Public Static Void Main (string [] ARGs)
{
Try
{
Detectorfactory. loadprofile (thread. currentthread (). getcontextclassloader (). getresource ("Lang"). getpath ());
} Catch (Langdetectexception E)
{
E. printstacktrace ();
}
Detector detect;
Try
{
Detect = detectorfactory. Create ();
Detect. append ("A depends on ");
System. Out. println (detect. Detect ());
} Catch (Langdetectexception E)
{
E. printstacktrace ();
}
}
}
The result of this text is ZH-CN, which is very simple.
The basic initialization of language-detection is completed by detectorfactory. Before testing, you need to load the Language Pack (in fact, it is a sample of each language, you can add it yourself ). The Language Pack was initially added using the addprofile method. Its prototype is addprofile (langprofile profile, int index, int langsize). You can build your own vocabulary and then add it using the addprofile method. You can also use the loadprofile method to load all the language files in a directory (according to the required format, the downloaded jar package has a sample) at one time. The next step is simple. Create a detector through detectorfactory and append the text to be detected. After detect, the system returns the language category and closes the job.