|
23 | 23 | import ai.elimu.util.epub.EPubImageExtractionHelper;
|
24 | 24 | import ai.elimu.util.epub.EPubMetadataExtractionHelper;
|
25 | 25 | import ai.elimu.util.epub.EPubParagraphExtractionHelper;
|
| 26 | +import ai.elimu.util.ml.ReadingLevelUtil; |
26 | 27 | import ai.elimu.web.context.EnvironmentContextLoaderListener;
|
27 | 28 | import org.apache.commons.io.FileUtils;
|
28 | 29 | import org.apache.commons.io.IOUtils;
|
|
52 | 53 | import java.io.InputStream;
|
53 | 54 | import java.net.URI;
|
54 | 55 | import java.net.URL;
|
55 |
| -import java.util.ArrayList; |
56 |
| -import java.util.Arrays; |
57 |
| -import java.util.Calendar; |
58 |
| -import java.util.List; |
59 |
| -import java.util.Map; |
| 56 | +import java.util.*; |
60 | 57 | import java.util.zip.ZipEntry;
|
61 | 58 | import java.util.zip.ZipInputStream;
|
62 | 59 |
|
@@ -542,44 +539,19 @@ private void storeImageContributionEvent(Image image, HttpSession session, HttpS
|
542 | 539 | }
|
543 | 540 |
|
544 | 541 | private ReadingLevel predictReadingLevel(int chapterCount, int paragraphCount, int wordCount) {
|
545 |
| - logger.info("predictReadingLevel"); |
546 | 542 |
|
547 | 543 | // Load the machine learning model (https://github.com/elimu-ai/ml-storybook-reading-level)
|
548 |
| - String modelFilePath = getClass().getResource("step2_2_model.pmml").getFile(); |
549 |
| - logger.info("modelFilePath: " + modelFilePath); |
550 |
| - org.pmml4s.model.Model model = org.pmml4s.model.Model.fromFile(modelFilePath); |
551 |
| - logger.info("model: " + model); |
552 |
| - |
553 |
| - // Prepare values (features) to pass to the model |
554 |
| - Map<String, Double> values = Map.of( |
555 |
| - "chapter_count", Double.valueOf(chapterCount), |
556 |
| - "paragraph_count", Double.valueOf(paragraphCount), |
557 |
| - "word_count", Double.valueOf(wordCount) |
| 544 | + |
| 545 | + String modelFilePath = Objects.requireNonNull(getClass().getResource("step2_2_model.pmml")).getFile(); |
| 546 | + |
| 547 | + logger.info( |
| 548 | + "Predicting reading level for chapter: {}, paragraph: {}, word: {}, modelPath: {} ", |
| 549 | + chapterCount, paragraphCount, wordCount, modelFilePath |
558 | 550 | );
|
559 |
| - logger.info("values: " + values); |
560 |
| - |
561 |
| - // Make prediction |
562 |
| - logger.info("Arrays.toString(model.inputNames()): " + Arrays.toString(model.inputNames())); |
563 |
| - Object[] valuesMap = Arrays.stream(model.inputNames()) |
564 |
| - .map(values::get) |
565 |
| - .toArray(); |
566 |
| - logger.info("valuesMap: " + valuesMap); |
567 |
| - Object[] results = model.predict(valuesMap); |
568 |
| - logger.info("results: " + results); |
569 |
| - logger.info("Arrays.toString(results): " + Arrays.toString(results)); |
570 |
| - Object result = results[0]; |
571 |
| - logger.info("result: " + result); |
572 |
| - logger.info("result.getClass().getSimpleName(): " + result.getClass().getSimpleName()); |
573 |
| - Double resultAsDouble = (Double) result; |
574 |
| - logger.info("resultAsDouble: " + resultAsDouble); |
575 |
| - Integer resultAsInteger = resultAsDouble.intValue(); |
576 |
| - logger.info("resultAsInteger: " + resultAsInteger); |
577 |
| - |
578 |
| - // Convert from number to ReadingLevel enum (e.g. "LEVEL2") |
579 |
| - String readingLevelAsString = "LEVEL" + resultAsInteger; |
580 |
| - logger.info("readingLevelAsString: " + readingLevelAsString); |
581 |
| - ReadingLevel readingLevel = ReadingLevel.valueOf(readingLevelAsString); |
582 |
| - logger.info("readingLevel: " + readingLevel); |
| 551 | + |
| 552 | + ReadingLevel readingLevel = ReadingLevelUtil.predictReadingLevel(chapterCount, paragraphCount, wordCount, modelFilePath); |
| 553 | + logger.info("Predicted reading level: {}", readingLevel); |
| 554 | + |
583 | 555 | return readingLevel;
|
584 | 556 | }
|
585 | 557 | }
|
0 commit comments