From 95b94e0c652728244b6f4ded83d1e3d8ffde48b9 Mon Sep 17 00:00:00 2001 From: smoh Date: Sun, 11 Jan 2026 22:48:42 +0900 Subject: [PATCH] =?UTF-8?q?=EC=8B=A0=EA=B7=9C=20=EC=9A=94=EC=95=BD=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/SummarizeService.cs | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/MessageSummarizer/Services/SummarizeService.cs b/MessageSummarizer/Services/SummarizeService.cs index 4f41dd6..116573f 100644 --- a/MessageSummarizer/Services/SummarizeService.cs +++ b/MessageSummarizer/Services/SummarizeService.cs @@ -8,6 +8,28 @@ namespace MessageSummarizer.Services; public class SummarizeService : ISummarizeService { public async Task SummarizeAsync(string input, CancellationToken cancellationToken = default) + { + if (string.IsNullOrWhiteSpace(input)) + return string.Empty; + + // 첫 줄 추출 + var firstLine = GetFirstLine(input); + + if (firstLine.Contains("핵심 요약", StringComparison.OrdinalIgnoreCase)) + { + return await SummarizeArticleAsync(input, cancellationToken); + } + + return await SummarizeMessageAsync(input, cancellationToken); + } + + private string GetFirstLine(string input) + { + using var reader = new StringReader(input); + return reader.ReadLine() ?? string.Empty; + } + + private async Task SummarizeMessageAsync(string input, CancellationToken cancellationToken = default) { if (string.IsNullOrWhiteSpace(input)) return string.Empty; @@ -38,4 +60,53 @@ public class SummarizeService : ISummarizeService var summary = $"{date},{ticker},{stockName},{amount}"; return summary; } + + public async Task SummarizeArticleAsync(string input, CancellationToken cancellationToken = default) + { + var lines = Regex + .Split(input, @"\r\n|\r|\n|\u2028|\u2029") + .Select(l => l.Trim()) + .Where(l => !string.IsNullOrEmpty(l)) + .ToList(); + + var sb = new StringBuilder(); + + bool articleHeaderWritten = false; + + foreach (var line in lines) + { + cancellationToken.ThrowIfCancellationRequested(); + + // 1️.숫자만 있는 라인 제거 (1, 2, 3 ...) + if (Regex.IsMatch(line, @"^\d+$")) + continue; + + // 2️. 타임라인 (MM:SS) 제거 + if (Regex.IsMatch(line, @"^\d{1,2}:\d{2}$")) + continue; + + // 3️. 핵심 요약 헤더 + if (line == "핵심 요약") + { + sb.AppendLine("## 핵심 요약"); + continue; + } + + // 4️. 아티클 관련 헤더 통합 + if (line is "타임라인" or "아티클" or "AI 채팅") + { + if (!articleHeaderWritten) + { + sb.AppendLine("## 아티클"); + articleHeaderWritten = true; + } + continue; + } + + // 5️. 그 외 모든 텍스트는 bullet 처리 + sb.AppendLine($"* {line}"); + } + + return sb.ToString().TrimEnd(); + } }