Merge pull request 'Return Sequence instead of List' (#6) from update-versions into main
All checks were successful
Publish package / publish (push) Successful in 1m36s
All checks were successful
Publish package / publish (push) Successful in 1m36s
Reviewed-on: #6
This commit is contained in:
commit
c6347a6b13
@ -5,7 +5,7 @@ plugins {
|
||||
}
|
||||
|
||||
group = "com.marvinelsen"
|
||||
version = "1.0.1"
|
||||
version = "2.0.0"
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
|
@ -1,12 +1,12 @@
|
||||
package com.marvinelsen.cedict.api
|
||||
|
||||
import com.marvinelsen.cedict.internal.CedictParserImpl
|
||||
import com.marvinelsen.cedict.internal.RegexCedictParser
|
||||
import java.io.InputStream
|
||||
|
||||
interface CedictParser {
|
||||
fun parseCedict(inputStream: InputStream): List<CedictEntry>
|
||||
fun parseCedict(inputStream: InputStream): Sequence<CedictEntry>
|
||||
|
||||
companion object {
|
||||
val instance: CedictParser by lazy { CedictParserImpl() }
|
||||
val instance: CedictParser by lazy { RegexCedictParser() }
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ import com.marvinelsen.cedict.api.CedictParser
|
||||
import com.marvinelsen.chinese.transliteration.Syllable
|
||||
import java.io.InputStream
|
||||
|
||||
internal class CedictParserImpl : CedictParser {
|
||||
internal class RegexCedictParser : CedictParser {
|
||||
companion object {
|
||||
private const val DEFINITION_SEPARATOR = '/'
|
||||
private const val GLOSS_SEPARATOR = ';'
|
||||
@ -20,11 +20,11 @@ internal class CedictParserImpl : CedictParser {
|
||||
}
|
||||
|
||||
override fun parseCedict(inputStream: InputStream) =
|
||||
inputStream.bufferedReader().useLines { lines ->
|
||||
lines.filterNot(::isComment)
|
||||
.map(::toCedictEntry)
|
||||
.toList()
|
||||
}
|
||||
inputStream
|
||||
.bufferedReader()
|
||||
.lineSequence()
|
||||
.filterNot(::isComment)
|
||||
.map(::toCedictEntry)
|
||||
|
||||
private fun isComment(line: String) = line[0] == COMMENT_MARKER
|
||||
|
||||
@ -48,9 +48,5 @@ internal class CedictParserImpl : CedictParser {
|
||||
|
||||
private fun toCedictDefinitions(definitions: String) = definitions
|
||||
.split(DEFINITION_SEPARATOR)
|
||||
.map {
|
||||
CedictDefinition(
|
||||
glosses = it.split(GLOSS_SEPARATOR).map(String::trim)
|
||||
)
|
||||
}
|
||||
.map { CedictDefinition(it.split(GLOSS_SEPARATOR).map(String::trim)) }
|
||||
}
|
@ -7,9 +7,9 @@ import io.kotest.core.spec.style.ShouldSpec
|
||||
import io.kotest.matchers.shouldBe
|
||||
import java.util.zip.GZIPInputStream
|
||||
|
||||
class CedictParserImplTest : ShouldSpec({
|
||||
class RegexCedictParserTest : ShouldSpec({
|
||||
should("parse lines correctly") {
|
||||
val cedictParser = CedictParserImpl()
|
||||
val cedictParser = RegexCedictParser()
|
||||
val cedictEntry =
|
||||
cedictParser.toCedictEntry("皮實 皮实 [pi2 shi5] /(of things) durable/(of people) sturdy; tough/")
|
||||
|
||||
@ -29,9 +29,12 @@ class CedictParserImplTest : ShouldSpec({
|
||||
val cedictFilePath = "/cedict_1_0_ts_utf-8_mdbg.txt.gz"
|
||||
val cedictFileStream = GZIPInputStream(javaClass.getResourceAsStream(cedictFilePath))
|
||||
|
||||
val cedictParser = CedictParserImpl()
|
||||
val cedictEntries = cedictParser.parseCedict(cedictFileStream)
|
||||
val cedictParser = RegexCedictParser()
|
||||
|
||||
cedictEntries.size shouldBe 122_508
|
||||
cedictFileStream.use {
|
||||
val cedictEntries = cedictParser.parseCedict(cedictFileStream)
|
||||
|
||||
cedictEntries.toList().size shouldBe 122_508
|
||||
}
|
||||
}
|
||||
})
|
Loading…
Reference in New Issue
Block a user