public class LangDetector
extends java.lang.Object
LangDetector class is to detect language from specified text.
Its instance is able to be constructed via the factory class LangDetectorFactory.
After appending a target text to the LangDetector instance with append(Reader) or append(String),
the detector provides the language detection results for target text via detect() or getProbabilities().
detect() method returns a single language name which has the highest probability.
getProbabilities() methods returns a list of multiple languages and their probabilities.
The detector has some parameters for language detection.
See setAlpha(double), setMaxTextLength(int) and setPriorMap(Map).
import java.util.ArrayList;
import org.codelibs.elasticsearch.langfield.detect.LangDetector;
import org.codelibs.elasticsearch.langfield.detect.LangDetectorFactory;
import org.codelibs.elasticsearch.langfield.detect.Language;
class LangDetectSample {
public void init(String profileDirectory) {
LangDetectorFactory.loadProfile(profileDirectory);
}
public String detect(String text) {
LangDetector detector = LangDetectorFactory.create();
detector.append(text);
return detector.detect();
}
public List<Language> detectLangs(String text) {
LangDetector detector = LangDetectorFactory.create();
detector.append(text);
return detector.getProbabilities();
}
}
LangDetectorFactory| 修飾子とタイプ | フィールドと説明 |
|---|---|
static java.lang.String |
UNKNOWN_LANG |
| コンストラクタと説明 |
|---|
LangDetector(LangDetectorFactory factory)
Constructor.
|
| 修飾子とタイプ | メソッドと説明 |
|---|---|
void |
append(java.io.Reader reader)
Append the target text for language detection.
|
void |
append(java.lang.String text)
Append the target text for language detection.
|
java.lang.String |
detect()
Detect language of the target text and return the language name which has the highest probability.
|
java.util.List<Language> |
getProbabilities()
Get language candidates which have high probabilities
|
void |
setAlpha(double alpha)
Set smoothing parameter.
|
void |
setMaxTextLength(int maxTextLength)
Specify max size of target text to use for language detection.
|
void |
setPriorMap(java.util.Map<java.lang.String,java.lang.Double> priorMap)
Set prior information about language probabilities.
|
void |
setVerbose()
Set Verbose Mode(use for debug).
|
public static final java.lang.String UNKNOWN_LANG
public LangDetector(LangDetectorFactory factory)
LangDetectorFactory.getLangDetector().factory - LangDetectorFactory instance (only LangDetectorFactory inside)public void setVerbose()
public void setAlpha(double alpha)
alpha - the smoothing parameterpublic void setPriorMap(java.util.Map<java.lang.String,java.lang.Double> priorMap)
priorMap - the priorMap to setpublic void setMaxTextLength(int maxTextLength)
maxTextLength - the maxTextLength to setpublic void append(java.io.Reader reader)
throws java.io.IOException
setMaxTextLength(int),
the rest is cut down.reader - the input reader (BufferedReader as usual)java.io.IOException - Can't read the reader.public void append(java.lang.String text)
setMaxTextLength(int),
the rest is cut down.text - the target text to appendpublic java.lang.String detect()
public java.util.List<Language> getProbabilities()
Copyright © 2011-2017. All Rights Reserved.