-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from mielvds/language-detection
Replace Language detection library with the more accurate lingua
- Loading branch information
Showing
3 changed files
with
36 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 17 additions & 22 deletions
39
src/main/java/de/gwdg/metadataqa/api/util/QALanguageDetector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,37 @@ | ||
package de.gwdg.metadataqa.api.util; | ||
|
||
import com.google.common.base.Optional; | ||
import com.optimaize.langdetect.LanguageDetector; | ||
import com.optimaize.langdetect.LanguageDetectorBuilder; | ||
import com.optimaize.langdetect.i18n.LdLocale; | ||
import com.optimaize.langdetect.ngram.NgramExtractors; | ||
import com.optimaize.langdetect.profiles.LanguageProfile; | ||
import com.optimaize.langdetect.profiles.LanguageProfileReader; | ||
import com.optimaize.langdetect.text.CommonTextObjectFactories; | ||
import com.optimaize.langdetect.text.TextObjectFactory; | ||
import com.github.pemistahl.lingua.api.IsoCode639_1; | ||
import com.github.pemistahl.lingua.api.Language; | ||
import com.github.pemistahl.lingua.api.LanguageDetector; | ||
import com.github.pemistahl.lingua.api.LanguageDetectorBuilder; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.SortedMap; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* | ||
* @author Péter Király <peter.kiraly at gwdg.de> | ||
*/ | ||
class QALanguageDetector { | ||
|
||
private List<LanguageProfile> languageProfiles; | ||
private LanguageDetector languageDetector; | ||
private TextObjectFactory textObjectFactory; | ||
|
||
QALanguageDetector() throws IOException { | ||
languageProfiles = new LanguageProfileReader().readAllBuiltIn(); | ||
|
||
//build language detector: | ||
languageDetector = LanguageDetectorBuilder | ||
.create(NgramExtractors.standard()) | ||
.withProfiles(languageProfiles) | ||
.build(); | ||
.fromAllSpokenLanguages() | ||
.build(); | ||
} | ||
|
||
//create a text object factory | ||
textObjectFactory = CommonTextObjectFactories.forDetectingOnLargeText(); | ||
public Language detect(String text) { | ||
final Language detectedLanguage = languageDetector.detectLanguageOf(text); | ||
return detectedLanguage; | ||
} | ||
|
||
public Optional<LdLocale> detect(String text) { | ||
var textObject = textObjectFactory.forText(text); | ||
return languageDetector.detect(textObject); | ||
public SortedMap<Language, Double> detectWithConfidence(String text) { | ||
final SortedMap<Language, Double> detectedLanguages = languageDetector.computeLanguageConfidenceValues(text); | ||
return detectedLanguages; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters