100 lines
2.3 KiB
Go
100 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
type InstantAnswer struct {
|
|
Type string // "calc", "unit_convert", "wiki", ...
|
|
Title string
|
|
Content interface{}
|
|
}
|
|
|
|
func detectInstantAnswer(query string) *InstantAnswer {
|
|
// Try currency conversion first (more specific)
|
|
if amount, from, to, ok := ParseCurrencyConversion(query); ok {
|
|
if result, ok := ConvertCurrency(amount, from, to); ok {
|
|
return &InstantAnswer{
|
|
Type: "currency",
|
|
Title: "Currency Conversion",
|
|
Content: map[string]interface{}{
|
|
"from": from,
|
|
"to": to,
|
|
"amount": amount,
|
|
"result": result,
|
|
"display": fmt.Sprintf("%.2f %s = %.2f %s", amount, from, result, to),
|
|
},
|
|
}
|
|
}
|
|
}
|
|
|
|
// Try math expression
|
|
if result, ok := parseMathExpression(query); ok {
|
|
return &InstantAnswer{
|
|
Type: "calc",
|
|
Title: "Calculation Result",
|
|
Content: result,
|
|
}
|
|
}
|
|
|
|
// Try weather instant answer
|
|
if city, forecast, ok := getWeatherForQuery(query); ok {
|
|
return &InstantAnswer{
|
|
Type: "weather",
|
|
Title: fmt.Sprintf("Weather in %s", city.Name),
|
|
Content: map[string]interface{}{
|
|
"city": city.Name,
|
|
"country": city.Country,
|
|
"lat": city.Lat,
|
|
"lon": city.Lon,
|
|
"current": forecast.Current,
|
|
"forecast": forecast.Forecast,
|
|
"display": fmt.Sprintf("%.1f°C, %s", forecast.Current.Temperature, forecast.Current.Condition),
|
|
},
|
|
}
|
|
}
|
|
|
|
// Try Wikipedia search
|
|
if title, text, link, ok := getWikipediaSummary(query); ok {
|
|
return &InstantAnswer{
|
|
Type: "wiki",
|
|
Title: title,
|
|
Content: map[string]string{
|
|
"text": text,
|
|
"link": link,
|
|
},
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func initExchangeRates() {
|
|
// Initial synchronous load
|
|
if err := UpdateExchangeRates(); err != nil {
|
|
printErr("Initial exchange rate update failed: %v", err)
|
|
} else {
|
|
PrecacheAllCurrencyPairs()
|
|
}
|
|
|
|
// Pre-cache common wiki terms in background
|
|
go func() {
|
|
commonTerms := []string{"United States", "Europe", "Technology", "Science", "Mathematics"}
|
|
for _, term := range commonTerms {
|
|
getWikipediaSummary(term)
|
|
}
|
|
}()
|
|
|
|
// Periodically update cache
|
|
ticker := time.NewTicker(30 * time.Minute)
|
|
go func() {
|
|
for range ticker.C {
|
|
if err := UpdateExchangeRates(); err != nil {
|
|
printWarn("Periodic exchange rate update failed: %v", err)
|
|
} else {
|
|
PrecacheAllCurrencyPairs()
|
|
}
|
|
}
|
|
}()
|
|
}
|