-
Notifications
You must be signed in to change notification settings - Fork 463
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Lingua as language detection library
- Loading branch information
Showing
5 changed files
with
63 additions
and
3 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
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
31 changes: 31 additions & 0 deletions
31
grobid-core/src/main/kotlin/org/grobid/core/lang/impl/LinguaLanguageDetector.kt
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.grobid.core.lang.impl | ||
|
||
import com.github.pemistahl.lingua.api.LanguageDetectorBuilder | ||
import org.grobid.core.lang.Language | ||
import org.grobid.core.lang.LanguageDetector | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
|
||
class LinguaLanguageDetector : LanguageDetector { | ||
private val detector: com.github.pemistahl.lingua.api.LanguageDetector = LanguageDetectorBuilder | ||
.fromAllLanguages() | ||
// .withPreloadedLanguageModels() | ||
.build() | ||
|
||
override fun detect(text: String): Language { | ||
val languages = detector.computeLanguageConfidenceValues(text = text) | ||
|
||
if (LOGGER.isDebugEnabled) { | ||
LOGGER.debug(languages.toString()) | ||
} | ||
|
||
val l = languages.firstKey() | ||
val p = languages[l] ?: 0.0 | ||
|
||
return Language(l.isoCode639_1.toString(), p) | ||
} | ||
|
||
companion object { | ||
private val LOGGER: Logger = LoggerFactory.getLogger(LinguaLanguageDetector::class.java) | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
grobid-core/src/main/kotlin/org/grobid/core/lang/impl/LinguaLanguageDetectorFactory.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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.grobid.core.lang.impl; | ||
|
||
import org.grobid.core.lang.LanguageDetector; | ||
import org.grobid.core.lang.LanguageDetectorFactory; | ||
|
||
/** | ||
* Implementation of a language detector factory with Lingua language identifier | ||
*/ | ||
public class LinguaLanguageDetectorFactory implements LanguageDetectorFactory { | ||
private static volatile LanguageDetector instance = null; | ||
|
||
private static void init() { | ||
|
||
} | ||
|
||
public LanguageDetector getInstance() { | ||
if (instance == null) { | ||
synchronized (this) { | ||
if(instance == null) { | ||
init(); | ||
instance = new LinguaLanguageDetector(); | ||
} | ||
} | ||
|
||
} | ||
return instance; | ||
} | ||
|
||
} |
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