This commit is contained in:
parent
c33a997dc5
commit
57507756ec
14 changed files with 864 additions and 97 deletions
83
ia-main.go
Normal file
83
ia-main.go
Normal file
|
@ -0,0 +1,83 @@
|
|||
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()
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue