From 8f6111364a00351d859e9cdeeed081aea4901902 Mon Sep 17 00:00:00 2001 From: "Peter M. Stahl" Date: Mon, 16 May 2022 17:29:19 +0200 Subject: [PATCH] Use inline class for ngrams to reduce memory footprint (#101) --- .../github/pemistahl/lingua/internal/Ngram.kt | 3 ++- .../lingua/api/LanguageDetectorTest.kt | 22 +++++++++---------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/com/github/pemistahl/lingua/internal/Ngram.kt b/src/main/kotlin/com/github/pemistahl/lingua/internal/Ngram.kt index 48dfc772..c228f960 100644 --- a/src/main/kotlin/com/github/pemistahl/lingua/internal/Ngram.kt +++ b/src/main/kotlin/com/github/pemistahl/lingua/internal/Ngram.kt @@ -23,8 +23,9 @@ import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Encoder +@JvmInline @Serializable(with = NgramSerializer::class) -internal data class Ngram(val value: String) : Comparable { +internal value class Ngram(val value: String) : Comparable { init { require(value.length in 0..5) { "length of ngram '$value' is not in range 0..5" diff --git a/src/test/kotlin/com/github/pemistahl/lingua/api/LanguageDetectorTest.kt b/src/test/kotlin/com/github/pemistahl/lingua/api/LanguageDetectorTest.kt index b00fb266..d88ea570 100644 --- a/src/test/kotlin/com/github/pemistahl/lingua/api/LanguageDetectorTest.kt +++ b/src/test/kotlin/com/github/pemistahl/lingua/api/LanguageDetectorTest.kt @@ -212,17 +212,17 @@ class LanguageDetectorTest { // ngram probability lookup private fun ngramProbabilityProvider() = listOf( - arguments(ENGLISH, Ngram("a"), 0.01), - arguments(ENGLISH, Ngram("lt"), 0.12), - arguments(ENGLISH, Ngram("ter"), 0.21), - arguments(ENGLISH, Ngram("alte"), 0.25), - arguments(ENGLISH, Ngram("alter"), 0.29), - - arguments(GERMAN, Ngram("t"), 0.08), - arguments(GERMAN, Ngram("er"), 0.18), - arguments(GERMAN, Ngram("alt"), 0.22), - arguments(GERMAN, Ngram("lter"), 0.28), - arguments(GERMAN, Ngram("alter"), 0.30) + arguments(ENGLISH, "a", 0.01), + arguments(ENGLISH, "lt", 0.12), + arguments(ENGLISH, "ter", 0.21), + arguments(ENGLISH, "alte", 0.25), + arguments(ENGLISH, "alter", 0.29), + + arguments(GERMAN, "t", 0.08), + arguments(GERMAN, "er", 0.18), + arguments(GERMAN, "alt", 0.22), + arguments(GERMAN, "lter", 0.28), + arguments(GERMAN, "alter", 0.30) ) @ParameterizedTest