Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,48 +47,78 @@ public QuizCreateResponseDto quizCreate(String graphIdStr, String mode) {

// listenUp ํ€ด์ฆˆ ์ƒ์„ฑ ๋ฉ”์„œ๋“œ
private ListenUpQuizDto listenUpQuizCreate(Graph graph) {
// ์ตœ์ข… ๋ฆฌํ„ดํ•  ํ€ด์ฆˆ ๋ฆฌ์ŠคํŠธ (3๊ฐœ)
Random random = new Random();
List<ListenUpQuizDto.ListenUpQuiz> quizzes = new ArrayList<>();
Set<String> usedSentences = new HashSet<>();
List<String> candidates = new ArrayList<>();

Random random = new Random();
// 1. ๊ทธ๋ž˜ํ”„ ๋…ธ๋“œ์—์„œ ๋ฌธ์žฅ ์ถ”์ถœ
for (GraphNode node : graph.getNodes()) {
if (node.getIncludeSentence() == null || node.getIncludeSentence().isBlank()) continue;

// ๋ช‡ ๊ฐœ ํ€ด์ฆˆ ๋งŒ๋“ค์—ˆ๋Š”์ง€ ์„ธ๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜ (3๊ฐœ๊นŒ์ง€๋งŒ ๋งŒ๋“ค ๊ฑฐ์ž„)
int count = 0;
// "." ์œผ๋กœ ๋ฌธ์žฅ ๋‚˜๋ˆ„๊ธฐ
String[] splitSentences = node.getIncludeSentence().split("\\.");

// ๊ทธ๋ž˜ํ”„์— ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋ฅผ ํ•˜๋‚˜์”ฉ ํ™•์ธ
for (GraphNode node : graph.getNodes()) {
for (String rawSentence : splitSentences) {
String sentence = rawSentence.trim();
if (sentence.isBlank()) continue; // ๊ณต๋ฐฑ์€ ์Šคํ‚ต
if (usedSentences.contains(sentence)) continue;

// includeSentence๊ฐ€ null์ด๊ฑฐ๋‚˜ ๊ณต๋ฐฑ์ด๋ฉด ๊ฑด๋„ˆ๋›ฐ๊ธฐ
if (node.getIncludeSentence() == null || node.getIncludeSentence().isBlank()) {
continue;
String[] words = sentence.split("\\s+");
if (words.length < 5) continue; // 5๋‹จ์–ด ๋ฏธ๋งŒ์€ ์Šคํ‚ต

candidates.add(sentence);
}
}

// 2. ๋‹จ์–ด ์ˆ˜ ๊ธฐ์ค€ ์ •๋ ฌ (5๋‹จ์–ด์— ๊ฐ€๊นŒ์šด ์ˆœ์„œ)
candidates.sort(Comparator.comparingInt(
s -> Math.abs(s.trim().split("\\s+").length - 5)
));

int count = 0;

for (String sentence : candidates) {
if (count >= 3) break;

// includeSentence ๋ฌธ์žฅ์„ ๋„์–ด์“ฐ๊ธฐ ๊ธฐ์ค€์œผ๋กœ ๋‹จ์–ด ๋ถ„๋ฆฌ
String[] words = node.getIncludeSentence().trim().split("\\s+");
String[] words = sentence.split("\\s+");

// ๋‹จ์–ด ๋ฆฌ์ŠคํŠธ๋ฅผ ์ •๋‹ต ๋ฆฌ์ŠคํŠธ๋กœ ์ €์žฅ
List<String> answer = Arrays.asList(words);
List<String> answer = new ArrayList<>();

if (words.length == 5) { // 5๋‹จ์–ด๋ฉด ๊ทธ๋Œ€๋กœ
answer = Arrays.asList(words);
} else {
// 6๋‹จ์–ด ์ด์ƒ์ด๋ฉด ๋žœ๋คํ•˜๊ฒŒ 5๊ฐœ๋กœ ์••์ถ•
int mergeCount = words.length - 5; // ํ•ฉ์ณ์•ผ ํ•  ํšŸ์ˆ˜
List<String> wordList = new ArrayList<>(Arrays.asList(words));

for (int i = 0; i < mergeCount; i++) {
int mergeIdx = random.nextInt(wordList.size() - 1); // ๋งˆ์ง€๋ง‰ ๋‹จ์–ด๋Š” ์ œ์™ธ
String merged = wordList.get(mergeIdx) + " " + wordList.get(mergeIdx + 1);
wordList.set(mergeIdx, merged);
wordList.remove(mergeIdx + 1);
}
answer = wordList;
}

if (answer.size() != 5) continue; // ์•ˆ์ „๋ง

// ์ •๋‹ต ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณต์‚ฌํ•ด์„œ ์…”ํ”Œํ•  ๋ฆฌ์ŠคํŠธ๋กœ ์‚ฌ์šฉ
List<String> shuffled = new ArrayList<>(answer);
Collections.shuffle(shuffled, random); // ๋ฌด์ž‘์œ„๋กœ ๋‹จ์–ด ์ˆœ์„œ ์„ž๊ธฐ
Collections.shuffle(shuffled, random);

// ํ•˜๋‚˜์˜ ํ€ด์ฆˆ ์ƒ์„ฑ
// ํ€ด์ฆˆ ์ƒ์„ฑ
ListenUpQuizDto.ListenUpQuiz quiz = ListenUpQuizDto.ListenUpQuiz.builder()
.answer(answer) // ์ •๋‹ต ๋ฆฌ์ŠคํŠธ
.shuffled(shuffled) // ์„ž์ธ(๋ฌธ์ œ) ๋ฆฌ์ŠคํŠธ
.description(node.getIncludeSentence()) // TTS๋กœ ์ฝ์–ด์ค„ ์›๋ฌธ ๋ฌธ์žฅ
.answer(answer)
.shuffled(shuffled)
.description(sentence) // ์ด ๋ฌธ์žฅ ์ „์ฒด๊ฐ€ TTS๋กœ ์ฝํž ๋ฌธ์žฅ
.build();

// ํ€ด์ฆˆ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€
quizzes.add(quiz);
usedSentences.add(sentence);
count++;

// 3๊ฐœ ๋งŒ๋“ค์—ˆ์œผ๋ฉด ์ข…๋ฃŒ
if (count >= 3) break;
}

// ์ตœ์ข… ํ€ด์ฆˆ DTO์— ๋‹ด์•„์„œ ๋ฆฌํ„ด
// ์ตœ์ข… ํ€ด์ฆˆ DTO์— ๋‹ด์•„์„œ ๋ฒˆํ™˜
return ListenUpQuizDto.builder()
.quizzes(quizzes)
.build();
Expand Down