-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
209 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
domain-generator/src/main/kotlin/com/few/domain/generator/core/GroupNewsModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.few.domain.generator.core | ||
|
||
data class GroupNewsModel( | ||
val topic: String = "", | ||
val news: List<NewsModel> = listOf(), | ||
val section: SectionContentModel = SectionContentModel(), | ||
) { | ||
fun toMap(): Map<String, Any> { | ||
return mapOf( | ||
"topic" to topic, | ||
"news" to news.map { it.toMap() }, | ||
"section" to section.toDict() | ||
) | ||
} | ||
|
||
companion object { | ||
fun fromMap(data: Map<String, Any>): GroupNewsModel { | ||
val newsList = (data["news"] as List<Map<String, Any>>).map { NewsModel.fromMap(it) } | ||
val sectionData = SectionContentModel.fromDict(data["section"] as Map<String, Any>? ?: emptyMap()) | ||
return GroupNewsModel( | ||
topic = data["topic"] as String, | ||
news = newsList, | ||
section = sectionData | ||
) | ||
} | ||
} | ||
} | ||
|
||
data class SectionContentModel( | ||
val title: String = "", | ||
val contents: List<ContentModel> = listOf(), | ||
) { | ||
fun toDict(): Map<String, Any> { | ||
return mapOf( | ||
"title" to title, | ||
"contents" to contents.map { it.toDict() } | ||
) | ||
} | ||
|
||
companion object { | ||
fun fromDict(data: Map<String, Any>): SectionContentModel { | ||
val contentsList = | ||
(data["contents"] as? List<Map<String, Any>>)?.map { ContentModel.fromDict(it) } ?: emptyList() | ||
return SectionContentModel( | ||
title = data["title"] as? String ?: "", | ||
contents = contentsList | ||
) | ||
} | ||
} | ||
} | ||
|
||
data class ContentModel( | ||
val subTitle: String = "", | ||
val body: String = "", | ||
) { | ||
fun toDict(): Map<String, Any> { | ||
return mapOf( | ||
"subTitle" to subTitle, | ||
"body" to body | ||
) | ||
} | ||
|
||
companion object { | ||
fun fromDict(data: Map<String, Any>): ContentModel { | ||
return ContentModel( | ||
subTitle = data["subTitle"] as? String ?: "", | ||
body = data["body"] as? String ?: "" | ||
) | ||
} | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
domain-generator/src/main/kotlin/com/few/domain/generator/core/NewsGrouper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.few.domain.generator.core | ||
|
||
import java.io.File | ||
import com.google.gson.Gson | ||
import com.google.gson.JsonObject | ||
import com.google.gson.reflect.TypeToken | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class NewsGrouper( | ||
private val chatGpt: ChatGpt, | ||
private val fewGson: Gson, | ||
) { | ||
private val log = KotlinLogging.logger {} | ||
|
||
fun groupAndSaveNews(inputFilePath: String, outputFilePath: String) { | ||
val newsList = loadSummarizedNews(inputFilePath) | ||
|
||
log.info { "뉴스 그룹화 진행 중..." } | ||
|
||
val groupedNews = chatGpt.groupNewsWithChatGPT(newsList) | ||
|
||
log.info { "그룹화된 뉴스 저장 중..." } | ||
saveGroupedNewsToJson(groupedNews, newsList, outputFilePath) | ||
|
||
log.info { "뉴스 그룹화 완료." } | ||
log.info { "${groupedNews.size()}개의 그룹으로 뉴스가 분류되어 '$outputFilePath' 파일로 저장되었습니다." } | ||
} | ||
|
||
private fun loadSummarizedNews(inputFilePath: String): List<NewsModel> { | ||
val fileContent = File(inputFilePath).readText(Charsets.UTF_8) | ||
|
||
// JSON 문자열을 List<Map<String, Any>> 형태로 변환 | ||
val typeToken = object : TypeToken<List<Map<String, Any>>>() {}.type | ||
val data: List<Map<String, Any>> = fewGson.fromJson(fileContent, typeToken) | ||
|
||
// 각 항목을 NewsModel 객체로 변환 | ||
return data.map { NewsModel.fromMap(it) } | ||
} | ||
|
||
private fun saveGroupedNewsToJson( | ||
groupedNews: JsonObject, | ||
newsList: List<NewsModel>, | ||
outputFilePath: String, | ||
) { | ||
val result = mutableListOf<GroupNewsModel>() | ||
|
||
// "groups" 필드를 JsonArray로 추출 | ||
val groupElements = groupedNews.getAsJsonArray("groups") | ||
|
||
for (groupElement in groupElements) { | ||
val group = groupElement.asJsonObject | ||
|
||
// "news_ids"를 JsonArray로 추출하고 String 리스트로 변환 | ||
val groupNewsIds = group.getAsJsonArray("news_ids").map { it.asString } | ||
|
||
// 뉴스 ID가 그룹에 포함된 뉴스 필터링 | ||
val newsInGroup = newsList.filter { it.id in groupNewsIds } | ||
|
||
// 뉴스가 3개 이상인 경우만 추가 | ||
if (newsInGroup.size >= 3) { | ||
val groupNews = GroupNewsModel( | ||
topic = group.getAsJsonPrimitive("topic").asString, | ||
news = newsInGroup | ||
) | ||
result.add(groupNews) | ||
println(groupNewsIds) | ||
} | ||
} | ||
|
||
// JSON 직렬화 및 파일 저장 | ||
val groupNewsData = result.map { it.toMap() } | ||
File(outputFilePath).writeText(fewGson.toJson(groupNewsData), Charsets.UTF_8) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters