-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move StoryBookCreateFromEPubController method to a new Util class (#1919
- Loading branch information
Showing
4 changed files
with
163 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package ai.elimu.util; | ||
|
||
public class ReadingLevelConstants { | ||
|
||
public static class READING_LEVEL_CONSTANTS { | ||
|
||
/** | ||
* Key for the number of chapters in a book | ||
*/ | ||
public static final String CHAPTER_COUNT_KEY = "chapter_count"; | ||
|
||
/** | ||
* Key for the number of paragraphs in a book | ||
*/ | ||
public static final String PARAGRAPH_COUNT_KEY = "paragraph_count"; | ||
|
||
/** | ||
* Key for the total word count in a book | ||
*/ | ||
public static final String WORD_COUNT_KEY = "word_count"; | ||
|
||
/** | ||
* Key for the predicted reading level output | ||
*/ | ||
public static final String READING_LEVEL_KEY = "LEVEL"; | ||
|
||
/** | ||
* Reading level file pmml path key | ||
*/ | ||
public static final String READING_LEVEL_MODEL_FILE_PATH_KEY = "src/main/resources/ai/elimu/web/content/storybook/step2_2_model.pmml"; | ||
} | ||
} |
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,64 @@ | ||
package ai.elimu.util.ml; | ||
|
||
import ai.elimu.model.v2.enums.ReadingLevel; | ||
import org.pmml4s.model.Model; | ||
|
||
import java.util.Arrays; | ||
import java.util.Map; | ||
|
||
import static ai.elimu.util.ReadingLevelConstants.READING_LEVEL_CONSTANTS.*; | ||
|
||
public class ReadingLevelUtil { | ||
|
||
/** | ||
* Predicts the reading level based on chapter count, paragraph count, and word count using a machine learning model. | ||
* | ||
* <p>This method loads a pre-trained machine learning model and predicts the reading level by passing the | ||
* given chapter count, paragraph count, and word count as input features. The model returns a numeric | ||
* prediction, which is then converted into a corresponding {@link ReadingLevel} enum.</p> | ||
* | ||
* @param chapterCount The number of chapters in the text. Must be an integer value. | ||
* @param paragraphCount The number of paragraphs in the text. Must be an integer value. | ||
* @param wordCount The number of words in the text. Must be an integer value. | ||
* | ||
* @return The predicted {@link ReadingLevel} based on the input features. | ||
* | ||
* <p>Example usage:</p> | ||
* <pre> | ||
* {@code | ||
* int chapterCount = 10; | ||
* int paragraphCount = 50; | ||
* int wordCount = 300; | ||
* ReadingLevel readingLevel = PredictionUtils.predictReadingLevel(chapterCount, paragraphCount, wordCount); | ||
* System.out.println("Predicted Reading Level: " + readingLevel); | ||
* } | ||
* </pre> | ||
*/ | ||
public static ReadingLevel predictReadingLevel( | ||
int chapterCount, | ||
int paragraphCount, | ||
int wordCount | ||
) { | ||
|
||
Model model = Model.fromFile(READING_LEVEL_MODEL_FILE_PATH_KEY); | ||
Map<String, Double> features = Map.of( | ||
CHAPTER_COUNT_KEY, (double) chapterCount, | ||
PARAGRAPH_COUNT_KEY, (double) paragraphCount, | ||
WORD_COUNT_KEY, (double) wordCount | ||
); | ||
|
||
Object[] valuesMap = Arrays.stream(model.inputNames()) | ||
.map(features::get) | ||
.toArray(); | ||
|
||
Object[] results = model.predict(valuesMap); | ||
|
||
Object result = results[0]; | ||
Double resultAsDouble = (Double) result; | ||
int resultAsInteger = resultAsDouble.intValue(); | ||
|
||
String readingLevelAsString = READING_LEVEL_KEY + resultAsInteger; | ||
return ReadingLevel.valueOf(readingLevelAsString); | ||
|
||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package ai.elimu.util.ml; | ||
|
||
import ai.elimu.model.v2.enums.ReadingLevel; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class ReadingLevelUtilTest { | ||
|
||
@Test | ||
public void testPredictReadingLevel_Level1() { | ||
|
||
int chapterCount = 12; | ||
int paragraphCount = 18; | ||
int wordCount = 150; | ||
|
||
ReadingLevel result = ReadingLevelUtil.predictReadingLevel(chapterCount, paragraphCount, wordCount); | ||
assertEquals(ReadingLevel.LEVEL1, result, "Expected ReadingLevel to be LEVEL1, but got: " + result); | ||
|
||
} | ||
|
||
@Test | ||
public void testPredictReadingLevel_Level2() { | ||
|
||
int chapterCount = 20; | ||
int paragraphCount = 30; | ||
int wordCount = 300; | ||
|
||
ReadingLevel result = ReadingLevelUtil.predictReadingLevel(chapterCount, paragraphCount, wordCount); | ||
assertEquals(ReadingLevel.LEVEL2, result, "Expected ReadingLevel to be LEVEL2, but got: " + result); | ||
|
||
} | ||
|
||
@Test | ||
public void testPredictReadingLevel_Level3() { | ||
|
||
int chapterCount = 25; | ||
int paragraphCount = 40; | ||
int wordCount = 350; | ||
|
||
ReadingLevel result = ReadingLevelUtil.predictReadingLevel(chapterCount, paragraphCount, wordCount); | ||
assertEquals(ReadingLevel.LEVEL3, result, "Expected ReadingLevel to be LEVEL3, but got: " + result); | ||
|
||
} | ||
|
||
@Test | ||
public void testPredictReadingLevel_Level4() { | ||
|
||
int chapterCount = 15; | ||
int paragraphCount = 45; | ||
int wordCount = 559; | ||
|
||
ReadingLevel result = ReadingLevelUtil.predictReadingLevel(chapterCount, paragraphCount, wordCount); | ||
assertEquals(ReadingLevel.LEVEL4, result, "Expected ReadingLevel to be LEVEL4, but got: " + result); | ||
|
||
} | ||
} |