Skip to content

Commit 5f3b64b

Browse files
committed
fixed tests to allow for source word
1 parent c3ab272 commit 5f3b64b

File tree

6 files changed

+35
-30
lines changed

6 files changed

+35
-30
lines changed

src/main/java/com/jakegodsall/services/flashcard/FlashcardService.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,55 +26,60 @@ public interface FlashcardService {
2626
*
2727
* @param targetWord the word for which the flashcard is generated
2828
* @param flashcardType the type of the flashcard (e.g., multiple choice, fill-in-the-blank)
29-
* @param language the language of the flashcard content
29+
* @param sourceLanguage the language of the flashcard content
30+
* @param targetLanguage the language of the flashcard content
3031
* @param options additional options to customize flashcard generation
3132
* @return the generated flashcard
3233
*/
33-
Flashcard generateFlashcard(String targetWord, FlashcardType flashcardType, Language language, Options options);
34+
Flashcard generateFlashcard(String targetWord, FlashcardType flashcardType, Language sourceLanguage, Language targetLanguage, Options options);
3435

3536
/**
3637
* Generates multiple flashcards for a single target word in one API call.
3738
*
3839
* @param targetWord the word for which multiple flashcards are generated
3940
* @param flashcardType the type of the flashcards (e.g., multiple choice, fill-in-the-blank)
40-
* @param language the language of the flashcards
41+
* @param sourceLanguage the language of the flashcards
42+
* @param targetLanguage the language of the flashcards
4143
* @param options additional options to customize flashcard generation
4244
* @param count the number of flashcards to generate
4345
* @return a list of generated flashcards
4446
*/
45-
List<Flashcard> generateMultipleFlashcardsForWord(String targetWord, FlashcardType flashcardType, Language language, Options options, int count);
47+
List<Flashcard> generateMultipleFlashcardsForWord(String targetWord, FlashcardType flashcardType, Language sourceLanguage, Language targetLanguage, Options options, int count);
4648

4749
/**
4850
* Generates flashcards interactively by prompting the user for words until they exit.
4951
*
5052
* @param flashcardType the type of flashcards (e.g., multiple choice, fill-in-the-blank)
51-
* @param language the language of the flashcards
53+
* @param sourceLanguage the language of the flashcards
54+
* @param targetLanguage the language of the flashcards
5255
* @param options additional options to customize flashcard generation
5356
* @return a list of generated flashcards
5457
* @throws IOException if there is an error in reading user input
5558
*/
56-
List<Flashcard> generateFlashcardsInteractively(FlashcardType flashcardType, Language language, Options options) throws IOException;
59+
List<Flashcard> generateFlashcardsInteractively(FlashcardType flashcardType, Language sourceLanguage, Language targetLanguage, Options options) throws IOException;
5760

5861
/**
5962
* Generates flashcards sequentially for a list of target words.
6063
*
6164
* @param targetWords the list of words for which flashcards are generated
6265
* @param flashcardType the type of the flashcards (e.g., multiple choice, fill-in-the-blank)
63-
* @param language the language of the flashcards
66+
* @param sourceLanguage the language of the flashcards
67+
* @param targetLanguage the language of the flashcards
6468
* @param options additional options to customize flashcard generation
6569
* @return a list of generated flashcards
6670
*/
67-
List<Flashcard> generateFlashcardsSequentially(List<String> targetWords, FlashcardType flashcardType, Language language, Options options);
71+
List<Flashcard> generateFlashcardsSequentially(List<String> targetWords, FlashcardType flashcardType, Language sourceLanguage, Language targetLanguage, Options options);
6872

6973
/**
7074
* Generates flashcards concurrently for a list of target words.
7175
*
7276
* @param targetWords the list of words for which flashcards are generated
73-
* @param language the language of the flashcards
77+
* @param sourceLanguage the language of the flashcards
78+
* @param targetLanguage the language of the flashcards
7479
* @param options additional options to customize flashcard generation
7580
* @return a list of generated flashcards
7681
* @throws InterruptedException if the execution is interrupted during concurrent generation
7782
* @throws ExecutionException if an exception occurs during concurrent execution
7883
*/
79-
List<Flashcard> generateFlashcardsConcurrently(List<String> targetWords, FlashcardType flashcardType, Language language, Options options) throws InterruptedException, ExecutionException;
84+
List<Flashcard> generateFlashcardsConcurrently(List<String> targetWords, FlashcardType flashcardType, Language sourceLanguage, Language targetLanguage, Options options) throws InterruptedException, ExecutionException;
8085
}

src/main/java/com/jakegodsall/services/flashcard/impl/FlashcardServiceGPTImpl.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ public List<String> getAvailableModels() {
5252
}
5353

5454
@Override
55-
public Flashcard generateFlashcard(String targetWord, FlashcardType flashcardType, Language language, Options options) {
55+
public Flashcard generateFlashcard(String targetWord, FlashcardType flashcardType, Language sourceLanguage, Language targetLanguage, Options options) {
5656
try {
5757
// Generate the prompt based on the flashcard type
58-
String prompt = promptGenerator.generatePrompt(targetWord, flashcardType, language, options);
58+
String prompt = promptGenerator.generatePrompt(targetWord, flashcardType, sourceLanguage, targetLanguage, options);
5959
// Generate HTTP POST request body
6060
String requestBody = promptGenerator.generateRequestBody(prompt);
6161
// Send the POST request to the GPT API
@@ -73,10 +73,10 @@ public Flashcard generateFlashcard(String targetWord, FlashcardType flashcardTyp
7373
}
7474

7575
@Override
76-
public List<Flashcard> generateMultipleFlashcardsForWord(String targetWord, FlashcardType flashcardType, Language language, Options options, int count) {
76+
public List<Flashcard> generateMultipleFlashcardsForWord(String targetWord, FlashcardType flashcardType, Language sourceLanguage, Language targetLanguage, Options options, int count) {
7777
try {
7878
// Generate the prompt for multiple flashcards
79-
String prompt = promptGenerator.generatePromptForMultipleFlashcards(targetWord, flashcardType, language, options, count);
79+
String prompt = promptGenerator.generatePromptForMultipleFlashcards(targetWord, flashcardType, sourceLanguage, targetLanguage, options, count);
8080
// Generate HTTP POST request body
8181
String requestBody = promptGenerator.generateRequestBody(prompt);
8282
// Send the POST request to the GPT API
@@ -94,7 +94,7 @@ public List<Flashcard> generateMultipleFlashcardsForWord(String targetWord, Flas
9494
}
9595

9696
@Override
97-
public List<Flashcard> generateFlashcardsInteractively(FlashcardType flashcardType, Language language, Options options) throws IOException {
97+
public List<Flashcard> generateFlashcardsInteractively(FlashcardType flashcardType, Language sourceLanguage, Language targetLanguage, Options options) throws IOException {
9898
List<Flashcard> flashcards = new ArrayList<>();
9999

100100
// Instantiate the interactive mode input service
@@ -118,7 +118,7 @@ public List<Flashcard> generateFlashcardsInteractively(FlashcardType flashcardTy
118118
}
119119

120120
// Otherwise generate the flashcard and ad it to the List
121-
Flashcard flashcard = generateFlashcard(input, flashcardType, language, options);
121+
Flashcard flashcard = generateFlashcard(input, flashcardType, sourceLanguage, targetLanguage, options);
122122
flashcards.add(flashcard);
123123

124124
System.out.println(flashcard);
@@ -130,15 +130,15 @@ public List<Flashcard> generateFlashcardsInteractively(FlashcardType flashcardTy
130130
}
131131

132132
@Override
133-
public List<Flashcard> generateFlashcardsSequentially(List<String> targetWords, FlashcardType flashcardType, Language language, Options options) {
133+
public List<Flashcard> generateFlashcardsSequentially(List<String> targetWords, FlashcardType flashcardType, Language sourceLanguage, Language targetLanguage, Options options) {
134134
List<Flashcard> flashcards = new ArrayList<>();
135135

136136
// Track the start time
137137
long startTime = System.currentTimeMillis();
138138

139139
// Iterate through words, generate flashcards and store them in the List<Flashcard>
140140
for (int i = 0; i < targetWords.size(); i++) {
141-
flashcards.add(generateFlashcard(targetWords.get(i), flashcardType, language, options));
141+
flashcards.add(generateFlashcard(targetWords.get(i), flashcardType, sourceLanguage, targetLanguage, options));
142142
System.out.println("Flashcard " + (i + 1) + " of " + targetWords.size() + " generated.");
143143
}
144144

@@ -152,14 +152,14 @@ public List<Flashcard> generateFlashcardsSequentially(List<String> targetWords,
152152
}
153153

154154
@Override
155-
public List<Flashcard> generateFlashcardsConcurrently(List<String> targetWords, FlashcardType flashcardType, Language language, Options options) throws InterruptedException, ExecutionException {
155+
public List<Flashcard> generateFlashcardsConcurrently(List<String> targetWords, FlashcardType flashcardType, Language sourceLanguage, Language targetLanguage, Options options) throws InterruptedException, ExecutionException {
156156
List<Future<Flashcard>> futures = new ArrayList<>();
157157

158158
long startTime = System.currentTimeMillis();
159159

160160
// Submit a task for each word and inform the client
161161
for (String targetWord : targetWords) {
162-
futures.add(executorService.submit(() -> generateFlashcard(targetWord, flashcardType, language, options)));
162+
futures.add(executorService.submit(() -> generateFlashcard(targetWord, flashcardType, sourceLanguage, targetLanguage, options)));
163163
}
164164

165165
System.out.println("All tasks submitted. Waiting for results...");

src/main/java/com/jakegodsall/services/json/impl/JsonParseServiceGPTImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,12 @@ public List<Flashcard> parseMultipleFlashcards(String responseBody, FlashcardTyp
9797
* Parses a WordFlashcard from the given JsonNode.
9898
*/
9999
private WordFlashcard parseWordFlashcardFromJson(JsonNode rootNode) {
100-
JsonNode nativeWordNode = getJsonNode(rootNode, "nativeWord");
100+
JsonNode sourceWordNode = getJsonNode(rootNode, "sourceWord");
101101
JsonNode targetWordNode = getJsonNode(rootNode, "targetWord");
102102
JsonNode targetSentenceNode = getJsonNode(rootNode, "targetSentence");
103103

104104
return new WordFlashcard(
105-
nativeWordNode.asText(),
105+
sourceWordNode.asText(),
106106
targetWordNode.asText(),
107107
targetSentenceNode.asText()
108108
);
@@ -112,11 +112,11 @@ private WordFlashcard parseWordFlashcardFromJson(JsonNode rootNode) {
112112
* Parses a SentenceFlashcard from the given JsonNode.
113113
*/
114114
private SentenceFlashcard parseSentenceFlashcardFromJson(JsonNode rootNode) {
115-
JsonNode nativeSentenceNode = getJsonNode(rootNode, "nativeSentence");
115+
JsonNode sourceSentenceNode = getJsonNode(rootNode, "sourceSentence");
116116
JsonNode targetSentenceNode = getJsonNode(rootNode, "targetSentence");
117117

118118
return new SentenceFlashcard(
119-
nativeSentenceNode.asText(),
119+
sourceSentenceNode.asText(),
120120
targetSentenceNode.asText()
121121
);
122122
}

src/main/java/com/jakegodsall/services/prompt/impl/PromptServiceGPTImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public String generatePromptForMultipleFlashcards(String targetWord, FlashcardTy
5656
}
5757

5858
prompt.append("You are a language learning assistant.\n")
59-
.append("You are given a word in a target language and you need to generate flashcards for it.\n")
59+
.append("You are given a word in a target language and you need to generate flashcards for it.\n");
6060

6161
prompt.append("Generate ")
6262
.append(count)

src/test/java/com/jakegodsall/services/json/impl/JsonParseServiceGPTImplTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public void setUp() {
2828
// PARSE WORD FLASHCARD
2929

3030
@Test
31-
public void parseWordFlashcard_missingNativeWordField() throws Exception {
31+
public void parseWordFlashcard_missingSourceWordField() throws Exception {
3232
String responseBody = "{ \"choices\": [{ \"message\": { \"content\": " +
3333
"{ \"targetWord\": \"Hola\", \"targetSentence\": \"Hola, ¿cómo estás?\" }" +
3434
"} }] }";
@@ -37,7 +37,7 @@ public void parseWordFlashcard_missingNativeWordField() throws Exception {
3737
NoSuchElementException exception = assertThrows(NoSuchElementException.class, () -> {
3838
jsonParseService.parseFlashcard(responseBody, FlashcardType.WORD);
3939
});
40-
assertEquals("Missing 'nativeWord' field in the JSON response", exception.getMessage());
40+
assertEquals("Missing 'sourceWord' field in the JSON response", exception.getMessage());
4141
}
4242

4343
@Test

src/test/java/com/jakegodsall/services/output/impl/OutputServiceJsonModeTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void serialiseToOutputFormat_validInput() throws JsonProcessingException
2929
String result = outputService.serialiseToOutputFormat(testFlashcards);
3030

3131
assertThat(result)
32-
.isEqualTo("[{\"nativeWord\":\"book\",\"targetWord\":\"libro\",\"exampleTargetSentence\":\"El libro.\"},{\"nativeWord\":\"car\",\"targetWord\":\"coche\",\"exampleTargetSentence\":\"El coche.\"}]");
32+
.isEqualTo("[{\"sourceWord\":\"book\",\"targetWord\":\"libro\",\"exampleTargetSentence\":\"El libro.\"},{\"sourceWord\":\"car\",\"targetWord\":\"coche\",\"exampleTargetSentence\":\"El coche.\"}]");
3333
}
3434

3535
@Test
@@ -44,12 +44,12 @@ public void serialiseToOutputFormat_emptyList() throws JsonProcessingException {
4444
private List<Flashcard> generateDummyFlashcardList() {
4545
List<Flashcard> flashcards = new ArrayList<>();
4646
flashcards.add(WordFlashcard.builder()
47-
.nativeWord("book")
47+
.sourceWord("book")
4848
.targetWord("libro")
4949
.exampleTargetSentence("El libro.")
5050
.build());
5151
flashcards.add(WordFlashcard.builder()
52-
.nativeWord("car")
52+
.sourceWord("car")
5353
.targetWord("coche")
5454
.exampleTargetSentence("El coche.")
5555
.build());

0 commit comments

Comments
 (0)