Java Reference
In-Depth Information
String text = "An overly sentimental film with a somewhat "
+ "problematic message, but its sweetness and charm "
+ "are occasionally enough to approximate true depth "
+ "and grace. ";
System.out.println("Text: " + text);
The LMClassifier instance is created using the langid-leipzig.classifier
file:
LMClassifier classifier = null;
try {
classifier = (LMClassifier)
AbstractExternalizable.readObject(
new File(".../langid-leipzig.classifier"));
} catch (IOException | ClassNotFoundException ex) {
// Handle exceptions
}
The classify method is used followed by the application of the bestCategory
method to obtain the best language fit, as shown here:
Classification classification = classifier.classify(text);
String bestCategory = classification.bestCategory();
System.out.println("Best Language: " + bestCategory);
The output is as follows with English being chosen:
Text: An overly sentimental film with a somewhat
problematic message, but its sweetness and charm are
occasionally enough to approximate true depth and grace.
Best Language: en
The following code example uses the first sentence of the Swedish Wikipedia entry in
Swedish ( http://sv.wikipedia.org/wiki/Svenska ) for the text:
text = "Svenska är ett östnordiskt språk som talas av cirka
"
+ "tio miljoner personer[1], främst i Finland "
+ "och Sverige.";
Search WWH ::




Custom Search