Search/ia-main.go

84 lines
1.8 KiB
Go
Raw Normal View History

2025-06-25 23:23:33 +02:00
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 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()
}
}
}()
}