Skip to content

Commit

Permalink
Replace mid-token periods with spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
dharmab committed Dec 19, 2024
1 parent 25fc46d commit 9229403
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
16 changes: 16 additions & 0 deletions pkg/parser/declare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,18 @@ func TestParserDeclare(t *testing.T) {
IsBRAA: true,
},
},
{
text: "anyface mobius1, declare 177.29",
expected: &brevity.DeclareRequest{
Callsign: "mobius 1",
Bullseye: *brevity.NewBullseye(
bearings.NewMagneticBearing(177*unit.Degree),
29*unit.NauticalMile,
),
Range: 29 * unit.NauticalMile,
Track: brevity.UnknownDirection,
},
},
}
runParserTestCases(t, New(TestCallsign, true), testCases, func(t *testing.T, test parserTestCase, request any) {
t.Helper()
Expand All @@ -206,14 +218,18 @@ func TestParserDeclare(t *testing.T) {
assert.True(t, actual.IsBRAA)
require.NotNil(t, actual)
require.NotNil(t, actual.Bearing)
require.NotNil(t, expected.Bearing)
assert.InDelta(t, expected.Bearing.Degrees(), actual.Bearing.Degrees(), 0.5)
require.NotNil(t, actual.Range)
require.NotNil(t, expected.Range)
assert.InDelta(t, expected.Range.NauticalMiles(), actual.Range.NauticalMiles(), 0.5)
} else {
assert.False(t, actual.IsBRAA)
require.NotNil(t, actual)
require.NotNil(t, actual.Bullseye)
require.NotNil(t, actual.Bullseye.Bearing())
require.NotNil(t, expected.Bullseye)
require.NotNil(t, expected.Bullseye.Bearing())
assert.InDelta(t, expected.Bullseye.Bearing().Degrees(), actual.Bullseye.Bearing().Degrees(), 0.5)
assert.InDelta(t, expected.Bullseye.Distance().NauticalMiles(), actual.Bullseye.Distance().NauticalMiles(), 1)
}
Expand Down
15 changes: 10 additions & 5 deletions pkg/parser/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ func isSimilar(a, b string) bool {
//
// - Split on any "|" character and discard the tail.
// - Convert to lowercase.
// - Replace hyphens and underscores with spaces. Remove any other characters
// - Replace hyphens and underscores with spaces.
// - Replace a period followed by a non-space character with a space.
// - Remove any other characters
// that are not letters, digits, or spaces.
// - Insert a space between any letter immediately followed by a digit.
// - Trim leading and trailing whitespace.
Expand All @@ -42,12 +44,15 @@ func normalize(tx string) string {
return tx
}

// removeSymbols removes any characters that are not letters, digits, or spaces.
// Hyphens and underscores are replaced with spaces. Other symbols are removed.
// removeSymbols removes any characters that are not letters, digits, or
// spaces. Hyphens and underscores are replaced with spaces. A period followed
// by a non-space character is replaced with a space. Other symbols are
// removed.
func removeSymbols(tx string) string {
var builder strings.Builder
for _, r := range tx {
if r == '-' || r == '_' {
for i, r := range tx {
isPeriodBeforeNonSpace := r == '.' && i+1 < len(tx) && !unicode.IsSpace(rune(tx[i+1]))
if r == '-' || r == '_' || isPeriodBeforeNonSpace {
_, _ = builder.WriteRune(' ')
} else if unicode.IsLetter(r) || unicode.IsDigit(r) || unicode.IsSpace(r) {
_, _ = builder.WriteRune(r)
Expand Down
2 changes: 2 additions & 0 deletions pkg/parser/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ func TestRemoveSymbols(t *testing.T) {
}{
{"Eagle-12", "Eagle 12"},
{"Hello, World!", "Hello World"},
{"180.", "180"},
{"180.3", "180 3"},
}

for _, test := range tests {
Expand Down

0 comments on commit 9229403

Please sign in to comment.