forked from elimu-ai/webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
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 (C…
…loses elimu-ai#1825)
- Loading branch information
1 parent
e65a7d4
commit 1a03cae
Showing
5 changed files
with
165 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,12 @@ | ||
package ai.elimu.util; | ||
|
||
public class AppConstants { | ||
|
||
public static class READING_LEVEL_CONSTANTS { | ||
public static final String CHAPTER_COUNT_KEY = "chapter_count"; | ||
public static final String PARAGRAPH_COUNT_KEY = "paragraph_count"; | ||
public static final String WORD_COUNT_KEY = "word_count"; | ||
public static final String LEVEL = "LEVEL"; | ||
} | ||
|
||
} |
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,41 @@ | ||
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.AppConstants.READING_LEVEL_CONSTANTS.*; | ||
|
||
public class ReadingLevelUtil { | ||
|
||
public static ReadingLevel predictReadingLevel( | ||
int chapterCount, | ||
int paragraphCount, | ||
int wordCount, | ||
String modelFilePath | ||
) { | ||
|
||
Model model = Model.fromFile(modelFilePath); | ||
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 = LEVEL + 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,59 @@ | ||
package ai.elimu.util.ml; | ||
|
||
import ai.elimu.model.v2.enums.ReadingLevel; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
public class ReadingLevelUtilTest { | ||
|
||
@Test | ||
public void testPredictReadingLevel_Level1() { | ||
|
||
String modelFilePath = "src/test/resources/ai/elimu/util/reading_level/model1.pmml"; | ||
int chapterCount = 5; | ||
int paragraphCount = 20; | ||
int wordCount = 100; | ||
|
||
ReadingLevel result = ReadingLevelUtil.predictReadingLevel(chapterCount, paragraphCount, wordCount, modelFilePath); | ||
assertEquals(ReadingLevel.LEVEL1, result, "Expected ReadingLevel to be LEVEL1, but got: " + result); | ||
|
||
} | ||
|
||
@Test | ||
public void testPredictReadingLevel_Level2() { | ||
|
||
String modelFilePath = "src/test/resources/ai/elimu/util/reading_level/model1.pmml"; | ||
int chapterCount = 12; | ||
int paragraphCount = 22; | ||
int wordCount = 250; | ||
|
||
ReadingLevel result = ReadingLevelUtil.predictReadingLevel(chapterCount, paragraphCount, wordCount, modelFilePath); | ||
assertEquals(ReadingLevel.LEVEL2, result, "Expected ReadingLevel to be LEVEL2, but got: " + result); | ||
|
||
} | ||
|
||
@Test | ||
public void testPredictReadingLevel_Level3() { | ||
|
||
String modelFilePath = "src/test/resources/ai/elimu/util/reading_level/model1.pmml"; | ||
int chapterCount = 12; | ||
int paragraphCount = 25; | ||
int wordCount = 350; | ||
|
||
ReadingLevel result = ReadingLevelUtil.predictReadingLevel(chapterCount, paragraphCount, wordCount, modelFilePath); | ||
assertEquals(ReadingLevel.LEVEL3, result, "Expected ReadingLevel to be LEVEL3, but got: " + result); | ||
|
||
} | ||
|
||
@Test | ||
public void testPredictReadingLevel_InvalidModelFile() { | ||
|
||
assertThrows(IOException.class, () -> { | ||
ReadingLevelUtil.predictReadingLevel(1, 1, 1, "invalidPath"); | ||
}, "Expected IOException when loading an invalid model file path"); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/test/resources/ai/elimu/util/reading_level/model1.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,41 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<PMML version="4.4" xmlns="http://www.dmg.org/PMML-4_4"> | ||
<Header> | ||
<Application name="Decision Tree Regressor" version="1.0"/> | ||
</Header> | ||
<DataDictionary numberOfFields="4"> | ||
<DataField name="chapter_count" optype="continuous" dataType="integer"/> | ||
<DataField name="paragraph_count" optype="continuous" dataType="integer"/> | ||
<DataField name="word_count" optype="continuous" dataType="integer"/> | ||
<DataField name="reading_level" optype="continuous" dataType="double"/> | ||
</DataDictionary> | ||
<TreeModel functionName="regression" algorithmName="DecisionTree" missingValueStrategy="none"> | ||
<MiningSchema> | ||
<MiningField name="chapter_count"/> | ||
<MiningField name="paragraph_count"/> | ||
<MiningField name="word_count"/> | ||
<MiningField name="reading_level" usageType="target"/> | ||
</MiningSchema> | ||
<Node score="1.0"> | ||
<True/> | ||
<Node score="1.0"> | ||
<SimplePredicate field="chapter_count" operator="lessThan" value="10"/> | ||
<Node score="1.0"> | ||
<SimplePredicate field="paragraph_count" operator="lessOrEqual" value="20"/> | ||
</Node> | ||
<Node score="2.0"> | ||
<SimplePredicate field="paragraph_count" operator="greaterThan" value="20"/> | ||
</Node> | ||
</Node> | ||
<Node score="2.0"> | ||
<SimplePredicate field="chapter_count" operator="greaterOrEqual" value="10"/> | ||
<Node score="2.0"> | ||
<SimplePredicate field="word_count" operator="lessThan" value="300"/> | ||
</Node> | ||
<Node score="3.0"> | ||
<SimplePredicate field="word_count" operator="greaterOrEqual" value="300"/> | ||
</Node> | ||
</Node> | ||
</Node> | ||
</TreeModel> | ||
</PMML> |