Skip to content

Instantly share code, notes, and snippets.

@missinglink
Last active January 12, 2016 18:48
Show Gist options
  • Select an option

  • Save missinglink/308f6704519564cce39a to your computer and use it in GitHub Desktop.

Select an option

Save missinglink/308f6704519564cce39a to your computer and use it in GitHub Desktop.
remove ordinals eg. '21st' -> '21', strict in enforcing rules and supports autocomplete
package token
import "regexp"
// Ordinals - remove ordinals (such as the 'st' from '21st')
func Ordinals(tokens []string) []string {
r := []string{}
var reg = ""
reg += "(?i)" // case insensitive
reg += "((^|\\s)" // start anchor (group $2)
reg += "((1)st?|(2)nd?|(3)rd?|([4-9])th?)" // singles (groups $4$5$6$7)
reg += "|" // or
reg += "(0*([0-9]*)(1[0-9])th?)" // teens (groups $9$10), trim leading zeros
reg += "|" // or
reg += "(0*([0-9]*[02-9])((1)st?|(2)nd?|(3)rd?|([04-9])th?))" // the rest (groups $12$14$15$16$17), trim leading zeros
reg += "($|\\s))" // end anchor (group $18)
exp := regexp.MustCompile(reg)
// regex replace all instances
for _, token := range tokens {
r = append(r, exp.ReplaceAllString(token, "$2$4$5$6$7$9$10$12$14$15$16$17$18"))
}
return r
}
package token
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestOrdinals(t *testing.T) {
var text, expected []string
// multiple in same string
text = []string{"1st 2nd 3rd 4th 5th"}
expected = []string{"1 2 3 4 5"}
assert.Equal(t, expected, Ordinals(text))
// singles
text = []string{"1st", "2nd", "3rd", "4th", "5th"}
expected = []string{"1", "2", "3", "4", "5"}
assert.Equal(t, expected, Ordinals(text))
// ordindals
text = []string{"1st", "22nd", "333rd", "4444th", "2500th"}
expected = []string{"1", "22", "333", "4444", "2500"}
assert.Equal(t, expected, Ordinals(text))
// teens
text = []string{"11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", "20th"}
expected = []string{"11", "12", "13", "14", "15", "16", "17", "18", "19", "20"}
assert.Equal(t, expected, Ordinals(text))
// teens (hundreds)
text = []string{"111th", "112th", "113th", "114th", "115th", "116th", "117th", "118th", "119th", "120th"}
expected = []string{"111", "112", "113", "114", "115", "116", "117", "118", "119", "120"}
assert.Equal(t, expected, Ordinals(text))
// teens (wrong suffix)
text = []string{"11st", "12nd", "13rd", "111st", "112nd", "113rd"}
expected = []string{"11st", "12nd", "13rd", "111st", "112nd", "113rd"}
assert.Equal(t, expected, Ordinals(text))
// uppercase
text = []string{"1ST", "22ND", "333RD", "4444TH"}
expected = []string{"1", "22", "333", "4444"}
assert.Equal(t, expected, Ordinals(text))
// autocomplete
text = []string{"26", "26t", "26th", "3", "3r", "3rd"}
expected = []string{"26", "26", "26", "3", "3", "3"}
assert.Equal(t, expected, Ordinals(text))
// wrong suffix (do nothing)
text = []string{"0th", "26s", "26st", "31t", "31th", "21r", "21rd", "29n", "29nd"}
expected = []string{"0th", "26s", "26st", "31t", "31th", "21r", "21rd", "29n", "29nd"}
assert.Equal(t, expected, Ordinals(text))
// trim leading zeros
text = []string{"011th", "0000201st"}
expected = []string{"11", "201"}
assert.Equal(t, expected, Ordinals(text))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment