68 lines
2.6 KiB
Go
68 lines
2.6 KiB
Go
package weather
|
||
|
||
import (
|
||
"regexp"
|
||
"strings"
|
||
)
|
||
|
||
// NormalizeCondition converts Environment Canada’s verbose condition text
|
||
// into a consistent, title-cased string suitable for display or icon mapping.
|
||
func NormalizeCondition(raw string) string {
|
||
raw = strings.TrimSpace(strings.ToLower(raw))
|
||
if raw == "" {
|
||
return "Unknown"
|
||
}
|
||
|
||
// Common regex cleanup patterns
|
||
replacements := map[*regexp.Regexp]string{
|
||
regexp.MustCompile(`(?i)snowshower|snow showers?`): "Snow",
|
||
regexp.MustCompile(`(?i)rainshower|rain showers?`): "Rain",
|
||
regexp.MustCompile(`(?i)freezing rain|ice pellets|sleet`): "Freezing Rain",
|
||
regexp.MustCompile(`(?i)flurries?`): "Light Snow",
|
||
regexp.MustCompile(`(?i)thunderstorms?|tstorms?`): "Thunderstorm",
|
||
regexp.MustCompile(`(?i)drizzle`): "Drizzle",
|
||
regexp.MustCompile(`(?i)fog patches?|mist|haze|smoke|ash`): "Fog",
|
||
regexp.MustCompile(`(?i)blowing snow|drifting snow`): "Blowing Snow",
|
||
regexp.MustCompile(`(?i)freezing drizzle`): "Freezing Drizzle",
|
||
regexp.MustCompile(`(?i)showers? mixed with flurries?`): "Mixed Rain/Snow",
|
||
regexp.MustCompile(`(?i)snow mixed with rain`): "Mixed Rain/Snow",
|
||
regexp.MustCompile(`(?i)mainly cloudy|mostly cloudy`): "Cloudy",
|
||
regexp.MustCompile(`(?i)mainly sunny|mostly sunny|sunny`): "Sunny",
|
||
regexp.MustCompile(`(?i)partly cloudy|a few clouds`): "Partly Cloudy",
|
||
regexp.MustCompile(`(?i)clear|fair`): "Clear",
|
||
regexp.MustCompile(`(?i)rain and snow|rain/snow`): "Mixed Rain/Snow",
|
||
regexp.MustCompile(`(?i)light rain`): "Light Rain",
|
||
regexp.MustCompile(`(?i)heavy rain`): "Heavy Rain",
|
||
regexp.MustCompile(`(?i)light snow`): "Light Snow",
|
||
regexp.MustCompile(`(?i)heavy snow`): "Heavy Snow",
|
||
}
|
||
|
||
for pattern, replacement := range replacements {
|
||
if pattern.MatchString(raw) {
|
||
raw = pattern.ReplaceAllString(raw, replacement)
|
||
}
|
||
}
|
||
|
||
// Collapse multiple spaces and ensure proper capitalization
|
||
raw = strings.Join(strings.Fields(raw), " ")
|
||
raw = strings.Title(raw)
|
||
|
||
// Final normalization
|
||
switch raw {
|
||
case "Cloudy Periods":
|
||
raw = "Cloudy"
|
||
case "Mostly Cloudy":
|
||
raw = "Cloudy"
|
||
case "Mostly Sunny":
|
||
raw = "Sunny"
|
||
}
|
||
|
||
return raw
|
||
}
|
||
|
||
// NormalizeWeatherData standardizes the Condition field in WeatherData.
|
||
func NormalizeWeatherData(data WeatherData) WeatherData {
|
||
data.Condition = NormalizeCondition(data.Condition)
|
||
return data
|
||
}
|