Skip to content

Commit cfddcea

Browse files
committed
[Navigation] Combine single-digits nums/letters together for address
E.g. postcodes, see #387
1 parent 283debd commit cfddcea

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

app/src/main/kotlin/org/stypox/dicio/skills/navigation/NavigationSkill.kt

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,18 @@ class NavigationSkill(correspondingSkillInfo: SkillInfo, data: StandardRecognize
2323
// No number parser available, feed the spoken input directly to the map application.
2424
placeToNavigate.trim { it <= ' ' }
2525
} else {
26-
val textWithNumbers: List<Any> = npf
26+
val strNums: List<Any> = npf
2727
.extractNumber(placeToNavigate)
2828
.preferOrdinal(true)
2929
.mixedWithText
30+
.flatMap { item ->
31+
if (item is String) {
32+
// allows checking whether there are two single letters next to each other
33+
splitWordsAndKeepInBetween.findAll(item).map { it.value }
34+
} else {
35+
sequenceOf(item)
36+
}
37+
}
3038

3139
// Given an address of "9546 19 avenue", the building number is 9546 and the street
3240
// number is 19.
@@ -41,15 +49,27 @@ class NavigationSkill(correspondingSkillInfo: SkillInfo, data: StandardRecognize
4149
// Based on these known issues, for the example address given above, the speech provided
4250
// by the user should be "nine thousand five hundred forty six nineteen(th) avenue".
4351
val placeToNavigateSB = StringBuilder()
44-
for (currentItem in textWithNumbers) {
45-
if (currentItem is String) {
46-
placeToNavigateSB.append(currentItem)
47-
} else if (currentItem is Number) {
48-
if (currentItem.isInteger) {
49-
placeToNavigateSB.append(currentItem.integerValue())
52+
for (i in strNums.indices) {
53+
val curr = strNums[i]
54+
if (curr is String) {
55+
if (i in 1..<(strNums.size - 1)
56+
&& isSingleDigit(strNums[i - 1])
57+
&& curr.isBlank()
58+
&& isSingleDigit(strNums[i + 1])
59+
) {
60+
// two consecutive digits or single letters likely go together,
61+
// so don't add the space
62+
continue
63+
}
64+
placeToNavigateSB.append(curr)
65+
} else if (curr is Number) {
66+
if (curr.isInteger) {
67+
placeToNavigateSB.append(curr.integerValue())
5068
} else {
51-
placeToNavigateSB.append(currentItem.decimalValue())
69+
placeToNavigateSB.append(curr.decimalValue())
5270
}
71+
} else {
72+
throw RuntimeException("Item $curr is neither String nor Number")
5373
}
5474
}
5575
placeToNavigateSB.toString().trim { it <= ' ' }
@@ -62,4 +82,16 @@ class NavigationSkill(correspondingSkillInfo: SkillInfo, data: StandardRecognize
6282

6383
return NavigationOutput(cleanPlaceToNavigate)
6484
}
85+
86+
companion object {
87+
val splitWordsAndKeepInBetween = Regex("""\p{L}+|[^\p{L}]""")
88+
89+
fun isSingleDigit(strOrNum: Any): Boolean {
90+
return when (strOrNum) {
91+
is String -> strOrNum.length <= 1
92+
is Number -> strOrNum.isInteger && strOrNum.integerValue() in 0..<10
93+
else -> throw RuntimeException("Item $strOrNum is neither String nor Number")
94+
}
95+
}
96+
}
6597
}

0 commit comments

Comments
 (0)