Skip to content

Commit

Permalink
[BTD-292] Added keyword search
Browse files Browse the repository at this point in the history
  • Loading branch information
paddynski-moj committed Feb 27, 2025
1 parent df98c58 commit a107d2e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package uk.gov.justice.digital.hmpps.learnerrecordsapi.config

object Synonyms {

private val synonyms = mapOf(
"maths" to "mathematics",
)

fun getSynonyms(keywords: List<String>): List<String> {
val list = mutableListOf<String>()
synonyms.forEach { (key, value) ->
if (keywords.contains(key)) list.add(value)
if (keywords.contains(value)) list.add(key)
}
return list + keywords
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ data class LearningEvent(
} else if (keywords.isEmpty()) {
return true
}
return keywords.any { keyword -> subject.contains(keyword, ignoreCase = true) }
return keywords.any { keyword -> subject.containsWholeWord(keyword) }
}
}

fun String.containsWholeWord(word: String): Boolean = (" $this ").contains(" $word ", ignoreCase = true)
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import uk.gov.justice.digital.hmpps.learnerrecordsapi.config.HttpClientConfiguration
import uk.gov.justice.digital.hmpps.learnerrecordsapi.config.LRSConfiguration
import uk.gov.justice.digital.hmpps.learnerrecordsapi.config.Synonyms.getSynonyms
import uk.gov.justice.digital.hmpps.learnerrecordsapi.logging.LoggerUtil
import uk.gov.justice.digital.hmpps.learnerrecordsapi.logging.LoggerUtil.debugLog
import uk.gov.justice.digital.hmpps.learnerrecordsapi.models.lrsapi.response.LearningEventsResponse
Expand Down Expand Up @@ -50,7 +51,7 @@ class LearnerEventsService(
incomingUln = learningEventsResult.incomingUln,
foundUln = learningEventsResult.foundUln,
learnerRecord = learningEventsResult.learnerRecord.filter {
it.isSubjectOneOf(request.keywords)
it.isSubjectOneOf(getSynonyms(request.keywords))
},
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class LearnerEventsServiceTest {
learningEventsResult = LearningEventsResult(
learnerRecord = listOf(
LearningEvent(
subject = "GSCE in Mathematics",
subject = "GCSE in Mathematics",
),
),
),
Expand Down Expand Up @@ -184,4 +184,19 @@ class LearnerEventsServiceTest {
fun `should return learner event as no keyword specified`() {
checkLearnerEvents(null, true)
}

@Test
fun `should return learner event as keyword synonym match`() {
checkLearnerEvents("maths", true)
}

@Test
fun `should return learner event as keyword at start`() {
checkLearnerEvents("gcse", true)
}

@Test
fun `should return no learner events as keyword not whole word`() {
checkLearnerEvents("gc", false)
}
}

0 comments on commit a107d2e

Please sign in to comment.