import json
# 本地宠物禁忌食物数据库(简化版)
TOXIC_FOODS = {
"dog": {
"chocolate": {"toxic": True, "symptoms": "呕吐、腹泻、心跳加速", "action": "立即催吐并送医"},
"grapes": {"toxic": True, "symptoms": "肾衰竭", "action": "立即送医,不可催吐"},
"xylitol": {"toxic": True, "symptoms": "低血糖、肝损伤", "action": "立即送医"},
"onion": {"toxic": True, "symptoms": "溶血性贫血", "action": "立即送医"},
"apple_seeds": {"toxic": True, "symptoms": "氰化物中毒", "action": "少量无害,大量需就医"},
},
"cat": {
"onion": {"toxic": True, "symptoms": "溶血性贫血", "action": "立即送医"},
"garlic": {"toxic": True, "symptoms": "贫血、虚弱", "action": "立即送医"},
"chocolate": {"toxic": True, "symptoms": "呕吐、心律失常", "action": "立即送医"},
"grapes": {"toxic": True, "symptoms": "肾损伤", "action": "立即送医"},
"raw_eggs": {"toxic": True, "symptoms": "沙门氏菌感染、生物素缺乏", "action": "煮熟后可少量喂食"},
}
}
def check_food(pet_type: str, food: str) -> dict:
"""查询某种宠物能否吃某种食物"""
pet = TOXIC_FOODS.get(pet_type.lower())
if not pet:
return {"error": f"不支持的宠物类型: {pet_type},仅支持 dog/cat"}
result = pet.get(food.lower())
if result is None:
return {"safe": True, "message": f"{food} 不在已知危险食物列表中,但建议咨询兽医"}
return {
"safe": not result["toxic"],
"symptoms": result["symptoms"],
"action": result["action"]
}
# 示例:查询狗吃巧克力
print(json.dumps(check_food("dog", "chocolate"), ensure_ascii=False, indent=2))
# 输出:{"safe": false, "symptoms": "呕吐、腹泻、心跳加速", "action": "立即催吐并送医"}
# 示例:查询猫吃苹果
print(json.dumps(check_food("cat", "apple"), ensure_ascii=False, indent=2))
# 输出:{"safe": true, "message": "apple 不在已知危险食物列表中,但建议咨询兽医"}
package main
import (
"encoding/json"
"fmt"
"strings"
)
// FoodInfo 食物毒性信息
type FoodInfo struct {
Toxic bool `json:"toxic"`
Symptoms string `json:"symptoms"`
Action string `json:"action"`
}
// 宠物-食物数据库
var toxicDB = map[string]map[string]FoodInfo{
"dog": {
"chocolate": {true, "呕吐、腹泻、心跳加速", "立即催吐并送医"},
"grapes": {true, "肾衰竭", "立即送医,不可催吐"},
"xylitol": {true, "低血糖、肝损伤", "立即送医"},
"onion": {true, "溶血性贫血", "立即送医"},
"macadamia": {true, "虚弱、呕吐、体温升高", "立即送医"},
},
"cat": {
"onion": {true, "溶血性贫血", "立即送医"},
"garlic": {true, "贫血、虚弱", "立即送医"},
"chocolate": {true, "呕吐、心律失常", "立即送医"},
"grapes": {true, "肾损伤", "立即送医"},
"milk": {false, "乳糖不耐受(腹泻)", "少量无害,大量需避免"},
},
}
// CheckFood 查询食物安全性
func CheckFood(petType, food string) (interface{}, error) {
pet, ok := toxicDB[strings.ToLower(petType)]
if !ok {
return nil, fmt.Errorf("不支持的宠物类型: %s(仅支持 dog/cat)", petType)
}
info, found := pet[strings.ToLower(food)]
if !found {
return map[string]interface{}{
"safe": true,
"message": fmt.Sprintf("%s 不在已知危险食物列表中,但建议咨询兽医", food),
}, nil
}
return map[string]interface{}{
"safe": !info.Toxic,
"symptoms": info.Symptoms,
"action": info.Action,
}, nil
}
func main() {
// 示例:狗吃巧克力
result, _ := CheckFood("dog", "chocolate")
b, _ := json.MarshalIndent(result, "", " ")
fmt.Println(string(b))
// 输出:{"action":"立即催吐并送医","safe":false,"symptoms":"呕吐、腹泻、心跳加速"}
// 示例:猫吃牛奶
result2, _ := CheckFood("cat", "milk")
b2, _ := json.MarshalIndent(result2, "", " ")
fmt.Println(string(b2))
// 输出:{"action":"少量无害,大量需避免","safe":true,"symptoms":"乳糖不耐受(腹泻)"}
}
// 宠物禁忌食物数据库
const TOXIC_FOODS = {
dog: {
chocolate: { toxic: true, symptoms: '呕吐、腹泻、心跳加速', action: '立即催吐并送医' },
grapes: { toxic: true, symptoms: '肾衰竭', action: '立即送医,不可催吐' },
xylitol: { toxic: true, symptoms: '低血糖、肝损伤', action: '立即送医' },
onion: { toxic: true, symptoms: '溶血性贫血', action: '立即送医' },
avocado: { toxic: true, symptoms: '呼吸困难、心肌损伤', action: '立即送医' },
},
cat: {
onion: { toxic: true, symptoms: '溶血性贫血', action: '立即送医' },
garlic: { toxic: true, symptoms: '贫血、虚弱', action: '立即送医' },
chocolate: { toxic: true, symptoms: '呕吐、心律失常', action: '立即送医' },
grapes: { toxic: true, symptoms: '肾损伤', action: '立即送医' },
raw_fish: { toxic: false, symptoms: '硫胺素缺乏(长期大量)', action: '煮熟后少量喂食' },
}
};
/**
* 查询食物安全性
* @param {'dog'|'cat'} petType 宠物类型
* @param {string} food 食物名称
* @returns {object} 查询结果
*/
function checkFood(petType, food) {
const pet = TOXIC_FOODS[petType.toLowerCase()];
if (!pet) {
return { error: `不支持的宠物类型: ${petType},仅支持 dog/cat` };
}
const result = pet[food.toLowerCase()];
if (!result) {
return { safe: true, message: `${food} 不在已知危险食物列表中,但建议咨询兽医` };
}
return {
safe: !result.toxic,
symptoms: result.symptoms,
action: result.action
};
}
// 示例:狗吃巧克力
console.log(JSON.stringify(checkFood('dog', 'chocolate'), null, 2));
// 输出:{"safe":false,"symptoms":"呕吐、腹泻、心跳加速","action":"立即催吐并送医"}
// 示例:猫吃生鱼
console.log(JSON.stringify(checkFood('cat', 'raw_fish'), null, 2));
// 输出:{"safe":true,"symptoms":"硫胺素缺乏(长期大量)","action":"煮熟后少量喂食"}