After appending a target text to the {@link Detector} instance with {@link #append(Reader)} or {@link #append(String)}, the detector provides the language detection results for target text via {@link #detect()} or {@link #getProbabilities()}. {@link #detect()} method returns a single language name which has the highest probability.{@link #getProbabilities()} methods returns a list of multiple languages and their probabilities.
The detector has some parameters for language detection. See {@link #setAlpha(double)}, {@link #setMaxTextLength(int)} and {@link #setPriorMap(HashMap)}.
import java.util.ArrayList; import com.cybozu.labs.langdetect.Detector; import com.cybozu.labs.langdetect.DetectorFactory; import com.cybozu.labs.langdetect.Language; class LangDetectSample { public void init(String profileDirectory) throws LangDetectException { DetectorFactory.loadProfile(profileDirectory); } public String detect(String text) throws LangDetectException { Detector detector = DetectorFactory.create(); detector.append(text); return detector.detect(); } public ArrayListdetectLangs(String text) throws LangDetectException { Detector detector = DetectorFactory.create(); detector.append(text); return detector.getProbabilities(); } }
|
|
|
|