307 lines
16 KiB
C#
307 lines
16 KiB
C#
|
|
using FutureMailAPI.DTOs;
|
|||
|
|
|
|||
|
|
namespace FutureMailAPI.Services
|
|||
|
|
{
|
|||
|
|
public class AIAssistantService : IAIAssistantService
|
|||
|
|
{
|
|||
|
|
private readonly ILogger<AIAssistantService> _logger;
|
|||
|
|
|
|||
|
|
public AIAssistantService(ILogger<AIAssistantService> logger)
|
|||
|
|
{
|
|||
|
|
_logger = logger;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public async Task<ApiResponse<WritingAssistantResponseDto>> GetWritingAssistanceAsync(WritingAssistantRequestDto request)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
// 在实际应用中,这里会调用真实的AI服务(如OpenAI GPT)
|
|||
|
|
// 目前我们使用模拟数据
|
|||
|
|
|
|||
|
|
var response = new WritingAssistantResponseDto
|
|||
|
|
{
|
|||
|
|
Content = GenerateWritingContent(request),
|
|||
|
|
Suggestions = GenerateWritingSuggestions(request),
|
|||
|
|
EstimatedTime = EstimateWritingTime(request)
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
return ApiResponse<WritingAssistantResponseDto>.SuccessResult(response);
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
_logger.LogError(ex, "获取写作辅助时发生错误");
|
|||
|
|
return ApiResponse<WritingAssistantResponseDto>.ErrorResult("获取写作辅助失败");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public async Task<ApiResponse<SentimentAnalysisResponseDto>> AnalyzeSentimentAsync(SentimentAnalysisRequestDto request)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
// 在实际应用中,这里会调用真实的情感分析服务
|
|||
|
|
// 目前我们使用模拟数据
|
|||
|
|
|
|||
|
|
var response = AnalyzeSentiment(request.Content);
|
|||
|
|
|
|||
|
|
return ApiResponse<SentimentAnalysisResponseDto>.SuccessResult(response);
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
_logger.LogError(ex, "分析情感时发生错误");
|
|||
|
|
return ApiResponse<SentimentAnalysisResponseDto>.ErrorResult("情感分析失败");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public async Task<ApiResponse<FuturePredictionResponseDto>> PredictFutureAsync(FuturePredictionRequestDto request)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
// 在实际应用中,这里会调用真实的预测服务
|
|||
|
|
// 目前我们使用模拟数据
|
|||
|
|
|
|||
|
|
var response = GenerateFuturePrediction(request);
|
|||
|
|
|
|||
|
|
return ApiResponse<FuturePredictionResponseDto>.SuccessResult(response);
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
_logger.LogError(ex, "预测未来时发生错误");
|
|||
|
|
return ApiResponse<FuturePredictionResponseDto>.ErrorResult("未来预测失败");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private string GenerateWritingContent(WritingAssistantRequestDto request)
|
|||
|
|
{
|
|||
|
|
// 模拟AI生成内容
|
|||
|
|
return request.Type switch
|
|||
|
|
{
|
|||
|
|
WritingAssistantType.OUTLINE => GenerateOutline(request.Prompt, request.Tone, request.Length),
|
|||
|
|
WritingAssistantType.DRAFT => GenerateDraft(request.Prompt, request.Tone, request.Length),
|
|||
|
|
WritingAssistantType.COMPLETE => GenerateCompleteContent(request.Prompt, request.Tone, request.Length),
|
|||
|
|
_ => "抱歉,无法生成内容。"
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private string GenerateOutline(string prompt, WritingTone tone, WritingLength length)
|
|||
|
|
{
|
|||
|
|
return $"基于您的提示\"{prompt}\",我为您生成了以下大纲:\n\n1. 引言\n2. 主要观点\n3. 支持论据\n4. 结论\n\n这个大纲适合{GetToneDescription(tone)}的写作风格,预计可以写成{GetLengthDescription(length)}的内容。";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private string GenerateDraft(string prompt, WritingTone tone, WritingLength length)
|
|||
|
|
{
|
|||
|
|
return $"关于\"{prompt}\"的草稿:\n\n{GetToneDescription(tone)}的开场白...\n\n主要内容的初步构思...\n\n需要进一步完善的结尾部分。\n\n这是一个初步草稿,您可以根据需要进一步修改和完善。";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private string GenerateCompleteContent(string prompt, WritingTone tone, WritingLength length)
|
|||
|
|
{
|
|||
|
|
return $"关于\"{prompt}\"的完整内容:\n\n{GetToneDescription(tone)}的开场白,引出主题...\n\n详细阐述主要观点,包含丰富的细节和例子...\n\n深入分析并提供有力的支持论据...\n\n{GetToneDescription(tone)}的结尾,总结全文并留下深刻印象。\n\n这是一篇完整的{GetLengthDescription(length)}文章,您可以直接使用或根据需要进行微调。";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private List<string> GenerateWritingSuggestions(WritingAssistantRequestDto request)
|
|||
|
|
{
|
|||
|
|
var suggestions = new List<string>();
|
|||
|
|
|
|||
|
|
switch (request.Type)
|
|||
|
|
{
|
|||
|
|
case WritingAssistantType.OUTLINE:
|
|||
|
|
suggestions.Add("考虑添加更多子论点来丰富大纲结构");
|
|||
|
|
suggestions.Add("为每个主要观点添加关键词或简短描述");
|
|||
|
|
break;
|
|||
|
|
case WritingAssistantType.DRAFT:
|
|||
|
|
suggestions.Add("添加更多具体例子来支持您的观点");
|
|||
|
|
suggestions.Add("考虑调整段落顺序以改善逻辑流程");
|
|||
|
|
break;
|
|||
|
|
case WritingAssistantType.COMPLETE:
|
|||
|
|
suggestions.Add("检查语法和拼写错误");
|
|||
|
|
suggestions.Add("考虑添加过渡词来改善段落间的连接");
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
suggestions.Add($"尝试使用{GetToneDescription(request.Tone)}的表达方式");
|
|||
|
|
suggestions.Add($"考虑将内容调整到{GetLengthDescription(request.Length)}的长度");
|
|||
|
|
|
|||
|
|
return suggestions;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private int EstimateWritingTime(WritingAssistantRequestDto request)
|
|||
|
|
{
|
|||
|
|
return request.Type switch
|
|||
|
|
{
|
|||
|
|
WritingAssistantType.OUTLINE => 5,
|
|||
|
|
WritingAssistantType.DRAFT => 15,
|
|||
|
|
WritingAssistantType.COMPLETE => 30,
|
|||
|
|
_ => 10
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private string GetToneDescription(WritingTone tone)
|
|||
|
|
{
|
|||
|
|
return tone switch
|
|||
|
|
{
|
|||
|
|
WritingTone.FORMAL => "正式",
|
|||
|
|
WritingTone.CASUAL => "轻松随意",
|
|||
|
|
WritingTone.EMOTIONAL => "情感丰富",
|
|||
|
|
WritingTone.INSPIRATIONAL => "鼓舞人心",
|
|||
|
|
_ => "中性"
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private string GetLengthDescription(WritingLength length)
|
|||
|
|
{
|
|||
|
|
return length switch
|
|||
|
|
{
|
|||
|
|
WritingLength.SHORT => "简短",
|
|||
|
|
WritingLength.MEDIUM => "中等长度",
|
|||
|
|
WritingLength.LONG => "长篇",
|
|||
|
|
_ => "适中"
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private SentimentAnalysisResponseDto AnalyzeSentiment(string content)
|
|||
|
|
{
|
|||
|
|
// 模拟情感分析
|
|||
|
|
var random = new Random();
|
|||
|
|
|
|||
|
|
// 简单的关键词分析(实际应用中应使用更复杂的算法)
|
|||
|
|
var positiveKeywords = new[] { "开心", "快乐", "爱", "美好", "成功", "希望", "感谢", "幸福" };
|
|||
|
|
var negativeKeywords = new[] { "悲伤", "难过", "失败", "痛苦", "失望", "愤怒", "焦虑", "恐惧" };
|
|||
|
|
|
|||
|
|
var positiveCount = positiveKeywords.Count(keyword => content.Contains(keyword));
|
|||
|
|
var negativeCount = negativeKeywords.Count(keyword => content.Contains(keyword));
|
|||
|
|
|
|||
|
|
SentimentType sentiment;
|
|||
|
|
if (positiveCount > negativeCount)
|
|||
|
|
sentiment = SentimentType.POSITIVE;
|
|||
|
|
else if (negativeCount > positiveCount)
|
|||
|
|
sentiment = SentimentType.NEGATIVE;
|
|||
|
|
else if (positiveCount > 0 && negativeCount > 0)
|
|||
|
|
sentiment = SentimentType.MIXED;
|
|||
|
|
else
|
|||
|
|
sentiment = SentimentType.NEUTRAL;
|
|||
|
|
|
|||
|
|
var emotions = new List<EmotionScore>();
|
|||
|
|
|
|||
|
|
// 根据情感类型生成情绪分数
|
|||
|
|
switch (sentiment)
|
|||
|
|
{
|
|||
|
|
case SentimentType.POSITIVE:
|
|||
|
|
emotions.Add(new EmotionScore { Type = EmotionType.HAPPY, Score = 0.8 });
|
|||
|
|
emotions.Add(new EmotionScore { Type = EmotionType.HOPEFUL, Score = 0.6 });
|
|||
|
|
emotions.Add(new EmotionScore { Type = EmotionType.GRATEFUL, Score = 0.5 });
|
|||
|
|
break;
|
|||
|
|
case SentimentType.NEGATIVE:
|
|||
|
|
emotions.Add(new EmotionScore { Type = EmotionType.SAD, Score = 0.8 });
|
|||
|
|
emotions.Add(new EmotionScore { Type = EmotionType.ANXIOUS, Score = 0.6 });
|
|||
|
|
emotions.Add(new EmotionScore { Type = EmotionType.CONFUSED, Score = 0.4 });
|
|||
|
|
break;
|
|||
|
|
case SentimentType.MIXED:
|
|||
|
|
emotions.Add(new EmotionScore { Type = EmotionType.NOSTALGIC, Score = 0.7 });
|
|||
|
|
emotions.Add(new EmotionScore { Type = EmotionType.HOPEFUL, Score = 0.5 });
|
|||
|
|
emotions.Add(new EmotionScore { Type = EmotionType.SAD, Score = 0.4 });
|
|||
|
|
break;
|
|||
|
|
default:
|
|||
|
|
emotions.Add(new EmotionScore { Type = EmotionType.CONFUSED, Score = 0.3 });
|
|||
|
|
emotions.Add(new EmotionScore { Type = EmotionType.HOPEFUL, Score = 0.3 });
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 提取关键词(简单实现)
|
|||
|
|
var words = content.Split(new[] { ' ', ',', '.', '!', '?', ';', ':', ',', '。', '!', '?', ';', ':' }, StringSplitOptions.RemoveEmptyEntries);
|
|||
|
|
var keywords = words.Where(word => word.Length > 3).GroupBy(word => word)
|
|||
|
|
.OrderByDescending(g => g.Count())
|
|||
|
|
.Take(5)
|
|||
|
|
.Select(g => g.Key)
|
|||
|
|
.ToList();
|
|||
|
|
|
|||
|
|
// 生成摘要
|
|||
|
|
var summary = content.Length > 100 ? content.Substring(0, 100) + "..." : content;
|
|||
|
|
|
|||
|
|
return new SentimentAnalysisResponseDto
|
|||
|
|
{
|
|||
|
|
Sentiment = sentiment,
|
|||
|
|
Confidence = 0.7 + random.NextDouble() * 0.3, // 0.7-1.0之间的随机数
|
|||
|
|
Emotions = emotions,
|
|||
|
|
Keywords = keywords,
|
|||
|
|
Summary = summary
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private FuturePredictionResponseDto GenerateFuturePrediction(FuturePredictionRequestDto request)
|
|||
|
|
{
|
|||
|
|
// 模拟未来预测
|
|||
|
|
var random = new Random();
|
|||
|
|
|
|||
|
|
var prediction = request.Type switch
|
|||
|
|
{
|
|||
|
|
PredictionType.CAREER => GenerateCareerPrediction(request.Content, request.DaysAhead),
|
|||
|
|
PredictionType.RELATIONSHIP => GenerateRelationshipPrediction(request.Content, request.DaysAhead),
|
|||
|
|
PredictionType.HEALTH => GenerateHealthPrediction(request.Content, request.DaysAhead),
|
|||
|
|
PredictionType.FINANCIAL => GenerateFinancialPrediction(request.Content, request.DaysAhead),
|
|||
|
|
PredictionType.PERSONAL_GROWTH => GeneratePersonalGrowthPrediction(request.Content, request.DaysAhead),
|
|||
|
|
_ => "无法进行预测。"
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
var factors = GeneratePredictionFactors(request.Type);
|
|||
|
|
var suggestions = GeneratePredictionSuggestions(request.Type);
|
|||
|
|
|
|||
|
|
return new FuturePredictionResponseDto
|
|||
|
|
{
|
|||
|
|
Prediction = prediction,
|
|||
|
|
Confidence = 0.6 + random.NextDouble() * 0.4, // 0.6-1.0之间的随机数
|
|||
|
|
Factors = factors,
|
|||
|
|
Suggestions = suggestions
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private string GenerateCareerPrediction(string content, int daysAhead)
|
|||
|
|
{
|
|||
|
|
return $"基于您提供的信息\"{content}\",预测在未来{daysAhead}天内,您可能会遇到新的职业机会。这可能是一个晋升机会、一个新项目或是一个学习新技能的机会。建议您保持开放的心态,积极接受挑战,这将有助于您的职业发展。";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private string GenerateRelationshipPrediction(string content, int daysAhead)
|
|||
|
|
{
|
|||
|
|
return $"根据您描述的\"{content}\",预测在未来{daysAhead}天内,您的人际关系可能会有积极的变化。可能会与老朋友重新联系,或者结识新的朋友。建议您保持真诚和开放的态度,这将有助于建立更深厚的人际关系。";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private string GenerateHealthPrediction(string content, int daysAhead)
|
|||
|
|
{
|
|||
|
|
return $"基于您提供的\"{content}\"信息,预测在未来{daysAhead}天内,您的健康状况可能会有所改善。建议您保持良好的作息习惯,适当运动,并注意饮食均衡。这些小的改变可能会带来显著的健康效益。";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private string GenerateFinancialPrediction(string content, int daysAhead)
|
|||
|
|
{
|
|||
|
|
return $"根据您描述的\"{content}\",预测在未来{daysAhead}天内,您的财务状况可能会趋于稳定。可能会有意外的收入或节省开支的机会。建议您制定合理的预算计划,并考虑长期投资策略。";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private string GeneratePersonalGrowthPrediction(string content, int daysAhead)
|
|||
|
|
{
|
|||
|
|
return $"基于您分享的\"{content}\",预测在未来{daysAhead}天内,您将有机会在个人成长方面取得进展。可能会发现新的兴趣爱好,或者在学习新技能方面取得突破。建议您保持好奇心,勇于尝试新事物。";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private List<string> GeneratePredictionFactors(PredictionType type)
|
|||
|
|
{
|
|||
|
|
return type switch
|
|||
|
|
{
|
|||
|
|
PredictionType.CAREER => new List<string> { "行业趋势", "个人技能", "经济环境", "人脉资源" },
|
|||
|
|
PredictionType.RELATIONSHIP => new List<string> { "沟通方式", "共同兴趣", "价值观", "情感需求" },
|
|||
|
|
PredictionType.HEALTH => new List<string> { "生活习惯", "遗传因素", "环境因素", "心理状态" },
|
|||
|
|
PredictionType.FINANCIAL => new List<string> { "收入水平", "消费习惯", "投资决策", "市场环境" },
|
|||
|
|
PredictionType.PERSONAL_GROWTH => new List<string> { "学习能力", "自我认知", "生活经历", "目标设定" },
|
|||
|
|
_ => new List<string> { "未知因素" }
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private List<string> GeneratePredictionSuggestions(PredictionType type)
|
|||
|
|
{
|
|||
|
|
return type switch
|
|||
|
|
{
|
|||
|
|
PredictionType.CAREER => new List<string> { "持续学习新技能", "扩展人脉网络", "设定明确的职业目标", "保持积极的工作态度" },
|
|||
|
|
PredictionType.RELATIONSHIP => new List<string> { "保持真诚沟通", "尊重他人观点", "定期维护关系", "表达感激之情" },
|
|||
|
|
PredictionType.HEALTH => new List<string> { "保持规律作息", "均衡饮食", "适量运动", "定期体检" },
|
|||
|
|
PredictionType.FINANCIAL => new List<string> { "制定预算计划", "减少不必要开支", "考虑长期投资", "建立应急基金" },
|
|||
|
|
PredictionType.PERSONAL_GROWTH => new List<string> { "设定学习目标", "尝试新体验", "反思自我", "寻求反馈" },
|
|||
|
|
_ => new List<string> { "保持积极态度" }
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|