add lookback on start, added parsing for ','s
This commit is contained in:
@@ -9,22 +9,25 @@ import (
|
||||
|
||||
const miToKM = 1.60934
|
||||
|
||||
// thousandsRe detects numbers where comma is a thousands separator (e.g. 1,000 or 5,981.9)
|
||||
var thousandsRe = regexp.MustCompile(`^\d{1,3}(,\d{3})+`)
|
||||
|
||||
var (
|
||||
// Matches: 25km, 25.5 km, 25,5km, 25KM, 25 kilometers, 25 kilometres
|
||||
kmPattern = regexp.MustCompile(
|
||||
`(?i)\b(\d+(?:[.,]\d+)?)\s*(?:km|kms|kilometer|kilometers|kilometre|kilometres)\b`,
|
||||
`(?i)\b(\d{1,3}(?:,\d{3})*(?:\.\d+)?|\d+(?:[.,]\d+)?)\s*(?:km|kms|kilometer|kilometers|kilometre|kilometres)\b`,
|
||||
)
|
||||
|
||||
// Matches standalone "k" used in cycling context: "did a 100k", "50k ride"
|
||||
// Only match when followed by a word boundary and a non-unit word (ride, loop, etc.)
|
||||
// or preceded by cycling verbs.
|
||||
kPattern = regexp.MustCompile(
|
||||
`(?i)\b(\d+(?:[.,]\d+)?)\s*k\b`,
|
||||
`(?i)\b(\d{1,3}(?:,\d{3})*(?:\.\d+)?|\d+(?:[.,]\d+)?)\s*k\b`,
|
||||
)
|
||||
|
||||
// Matches: 25mi, 25 mi, 25 miles, 25mile
|
||||
miPattern = regexp.MustCompile(
|
||||
`(?i)\b(\d+(?:[.,]\d+)?)\s*(?:mi|mile|miles)\b`,
|
||||
`(?i)\b(\d{1,3}(?:,\d{3})*(?:\.\d+)?|\d+(?:[.,]\d+)?)\s*(?:mi|mile|miles)\b`,
|
||||
)
|
||||
)
|
||||
|
||||
@@ -51,8 +54,16 @@ func firstMatch(re *regexp.Regexp, text string, multiplier float64) (float64, bo
|
||||
if m == nil {
|
||||
return 0, false
|
||||
}
|
||||
// Normalise comma decimal separator
|
||||
numStr := strings.ReplaceAll(m[1], ",", ".")
|
||||
numStr := m[1]
|
||||
if strings.Contains(numStr, ",") {
|
||||
if thousandsRe.MatchString(numStr) {
|
||||
// e.g. "1,000" or "5,981.9" — comma is thousands separator
|
||||
numStr = strings.ReplaceAll(numStr, ",", "")
|
||||
} else {
|
||||
// e.g. "25,5" — comma is decimal separator
|
||||
numStr = strings.ReplaceAll(numStr, ",", ".")
|
||||
}
|
||||
}
|
||||
v, err := strconv.ParseFloat(numStr, 64)
|
||||
if err != nil {
|
||||
return 0, false
|
||||
|
||||
Reference in New Issue
Block a user