Skip to content

Commit

Permalink
shorten length of the patient name
Browse files Browse the repository at this point in the history
Signed-off-by: Kengo TODA <toda_k@henry.jp>
  • Loading branch information
KengoTODA committed Apr 11, 2023
1 parent 08778c4 commit a0c1b6d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
17 changes: 9 additions & 8 deletions app/src/main/kotlin/jp/henry/uke/mask/MaskingEngine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,20 @@ class MaskingEngine(seed: Int) {
}

if (computeAge(birthDay, termStartDate) <= 6) {
return "${maskName("患者名", raw)}${age}歳, 未就学児"
return "${maskName("患者", raw)}(${age}歳,未就学児)"
}

val ageAtEndOfLastMonth = computeAge(birthDay, base.minusDays(1))
val ageAtEndOfThisMonth = computeAge(birthDay, base.atEndOfMonth())
return if (ageAtEndOfThisMonth == 75 && ageAtEndOfLastMonth == 74) {
if (birthDay.dayOfMonth == 1) {
"${maskName("患者名", raw)}${age}歳, 1日生まれの為75歳到達月対象外)"
} else {
if (birthDay.dayOfMonth != 1) {
// 75歳の誕生日当日から後期高齢に移行し、その月の自己負担額が半額となる制度のため、これを表示
"${maskName("患者名", raw)}${age}歳, 75歳到達月)"
"${maskName("患者", raw)}(${age}歳,75歳到達月)"
} else {
"${maskName("患者", raw)}(${age}歳,75歳到達月特例対象外)"
}
} else {
"${maskName("患者名", raw)}${age}"
"${maskName("患者", raw)}(${age})"
}
}

Expand Down Expand Up @@ -115,7 +115,7 @@ class MaskingEngine(seed: Int) {
line.split(",").withIndex().joinToString(",") {
when (it.index) {
4 -> maskNumber(it.value, 7)
6 -> maskName("医療機関名", it.value)
6 -> maskName("医療機関", it.value)
9 -> maskTelNum(it.value)
else -> it.value
}
Expand All @@ -124,7 +124,8 @@ class MaskingEngine(seed: Int) {
fun maskName(prefix: String, name: String) = names.getOrPut(prefix) {
HashMap()
}.getOrPut(name) {
"$prefix${random.nextInt(Integer.MAX_VALUE)}"
// 長すぎると患者名が全角20文字を超えるため6桁に抑える
"$prefix${random.nextInt(999_999)}"
}

fun maskNumber(text: String, length: Int): String = numbers.getOrPut(length) {
Expand Down
39 changes: 29 additions & 10 deletions app/src/test/kotlin/jp/henry/uke/mask/MaskingEngineSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package jp.henry.uke.mask
import io.kotest.assertions.withClue
import io.kotest.core.spec.style.DescribeSpec
import io.kotest.matchers.ints.shouldBeGreaterThanOrEqual
import io.kotest.matchers.ints.shouldBeLessThanOrEqual
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldContain
import io.kotest.matchers.string.shouldNotContain
Expand All @@ -12,6 +13,7 @@ import io.kotest.property.arbitrary.localDate
import io.kotest.property.arbitrary.string
import io.kotest.property.checkAll
import jp.henry.uke.mask.MaskingEngine.Companion.computeAge
import java.nio.charset.Charset
import java.time.LocalDate

class MaskingEngineSpec : DescribeSpec({
Expand Down Expand Up @@ -64,9 +66,9 @@ class MaskingEngineSpec : DescribeSpec({
val endOfTheLastMonth = LocalDate.of(2021, 1, 31)
val beginningOfThisMonth = LocalDate.of(2021, 2, 1)

MaskingEngine(0).maskPatientName("患者", theDayBefore, birthDay) shouldContain "0歳"
MaskingEngine(0).maskPatientName("患者", endOfTheLastMonth, birthDay) shouldContain "0歳"
MaskingEngine(0).maskPatientName("患者", beginningOfThisMonth, birthDay) shouldContain "1歳"
MaskingEngine(0).maskPatientName("患者", theDayBefore, birthDay) shouldContain "(0歳"
MaskingEngine(0).maskPatientName("患者", endOfTheLastMonth, birthDay) shouldContain "(0歳"
MaskingEngine(0).maskPatientName("患者", beginningOfThisMonth, birthDay) shouldContain "(1歳"
}
it("誕生日がそれ以外の場合") {
checkAll(Arb.int(2, 31)) {
Expand All @@ -76,12 +78,12 @@ class MaskingEngineSpec : DescribeSpec({
val endOfTheMonth = LocalDate.of(2020, 3, 31)
val beginningOfTheNextMonth = LocalDate.of(2020, 4, 1)

MaskingEngine(0).maskPatientName("患者", oneDayBefore, birthDay) shouldContain "0歳"
MaskingEngine(0).maskPatientName("患者", today, birthDay) shouldContain "0歳"
MaskingEngine(0).maskPatientName("患者", endOfTheMonth, birthDay) shouldContain "0歳"
MaskingEngine(0).maskPatientName("患者", oneDayBefore, birthDay) shouldContain "(0歳"
MaskingEngine(0).maskPatientName("患者", today, birthDay) shouldContain "(0歳"
MaskingEngine(0).maskPatientName("患者", endOfTheMonth, birthDay) shouldContain "(0歳"

// 次の月から年齢が加算される
MaskingEngine(0).maskPatientName("患者", beginningOfTheNextMonth, birthDay) shouldContain "1歳"
MaskingEngine(0).maskPatientName("患者", beginningOfTheNextMonth, birthDay) shouldContain "(1歳"
}
}
}
Expand Down Expand Up @@ -150,7 +152,6 @@ class MaskingEngineSpec : DescribeSpec({
}
val engine = MaskingEngine(0)
do {
println("${birthDay}生まれの患者は${today}において${computeAge(birthDay, today)}歳")
engine.maskPatientName("患者", today, birthDay) shouldNotContain "未就学児"
today = today.plusDays(1)
} while (computeAge(birthDay, today) == 6)
Expand Down Expand Up @@ -196,12 +197,30 @@ class MaskingEngineSpec : DescribeSpec({
val birthDay = LocalDate.of(1955, month, 1)
checkAll(Arb.int(1, birthDay.lengthOfMonth())) { day ->
val thisMonth = LocalDate.of(2030, month, day)
// 14.5文字
MaskingEngine(0).maskPatientName("患者", thisMonth, birthDay) shouldContain "1日生まれの為75歳到達月対象外"
MaskingEngine(0).maskPatientName("患者", thisMonth, birthDay) shouldContain "75歳到達月特例対象外"
}
}
}
}
}

/**
* Shift-JISにおける文字数を計算する。全角は2文字、半角は1文字として数える。
*/
fun String.countSjisChars(): Int {
val charset = Charset.forName("SJIS")
return this.toByteArray(charset).size
}
it("半角で40文字、全角で20文字を超えない文字列を生成する") {
checkAll(Arb.int(), Arb.string(), Arb.int(1, 80)) { seed, name, age ->
val birthDay = LocalDate.of(1930, 1, 1)
val today = LocalDate.of(1930 + age, 1, 1)
val maskedName = MaskingEngine(seed).maskPatientName(name, today, birthDay)
val sjisLength = maskedName.countSjisChars()
withClue("生成された文字列($maskedName)の長さが半角で40文字を超えるべきではないが $sjisLength 文字となった") {
sjisLength shouldBeLessThanOrEqual 40
}
}
}
}
})

0 comments on commit a0c1b6d

Please sign in to comment.