Uses 911K of libraries. Click here for Pure Java version (5068L/26K).
1 | !7 |
2 | |
3 | // Text + questions from https://lingua.com/english/reading/my-day/ |
4 | |
5 | cmodule MyDay > DynPrintLog { |
6 | transient ReadingComprehensionTest test; |
7 | switchable S text = [[ |
8 | |
9 | Text |
10 | ---- |
11 | |
12 | First, I wake up. Then, I get dressed. I walk to school. I do not ride a bike. I do not ride the bus. I like to go to school. It rains. I do not like rain. I eat lunch. I eat a sandwich and an apple. |
13 | |
14 | I play outside. I like to play. I read a book. I like to read books. I walk home. I do not like walking home. My mother cooks soup for dinner. The soup is hot. Then, I go to bed. I do not like to go bed. |
15 | |
16 | |
17 | Question 1: |
18 | What happens first? |
19 | 1 Get dressed 2 Wake up |
20 | 3 Eat lunch 4 Walk to school |
21 | |
22 | Question 2: |
23 | What do I like? |
24 | 1 Books 2 Rain |
25 | 3 Walking home 4 Going to bed |
26 | |
27 | Question 3: |
28 | How do I go to school? |
29 | 1 I ride a bike. 2 I walk. |
30 | 3 I ride the bus. 4 I drive a car. |
31 | |
32 | Question 4: |
33 | What do I eat for dinner? |
34 | 1 Sandwich 2 Pie |
35 | 3 Soup 4 Apple |
36 | |
37 | Question 5: |
38 | What do I not like? |
39 | 1 Going to school 2 Going to bed |
40 | 3 Playing 4 Soup |
41 | |
42 | ]]; |
43 | |
44 | // We help the AI by declaring some "power words" in form of regular expressions |
45 | |
46 | transient LS regexps = ll("(?<!not )like", "not like", "dinner", "school", "bed"); |
47 | |
48 | start-thread { |
49 | test = ai_parseReadingComprehensionTest_v1(text); |
50 | pnl(map_curry renderLineWithMatchingRegexpsIC(regexps, test.sentences)); // to see if the regexps work |
51 | |
52 | for (ReadingComprehensionTest.Question q : test.questions) { |
53 | print_nlBefore(q.question); |
54 | LS matchedInQ = matchingRegexpsIC(regexps, q.question); |
55 | if (empty(matchedInQ)) matchedInQ = ll(""); // match all |
56 | bool firstType = containsWord(q.question, "first"); |
57 | |
58 | new Best<S> best; |
59 | for (S answer : q.answers) { |
60 | LS regexps2 = ll(quoteRegexp(dropPunctuation(answer))); // treat the whole answer as a regexp |
61 | LS regexps2b = matchingRegexpsIC(regexps, answer); // find the regexps that match the answer |
62 | double score = 0; |
63 | if (firstType) { // mode 1: find what appears first in text |
64 | int idx = indexOfEntryContainingAllRegexpsIC(regexps2, test.sentences); |
65 | if (idx < 0) |
66 | idx = indexOfEntryContainingAllRegexpsIC(regexps2b, test.sentences); // try the second set of regexps |
67 | if (idx >= 0) score = -idx; else continue; |
68 | } else { // mode 2: find sentence in text that matches question + answer best |
69 | score = ai_regexpMixScoreIC(regexps2, matchedInQ, test.sentences, quote(answer)); |
70 | if (score == 0) |
71 | score = ai_regexpMixScoreIC(regexps2b, matchedInQ, test.sentences, quote(answer)); // try second set of regexps here too |
72 | } |
73 | best.put(answer, score); |
74 | } |
75 | print(">> " + or(best!, "?")); // print answer |
76 | } |
77 | } |
78 | } |
Began life as a copy of #1023740
download show line numbers debug dex old transpilations
Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1023742 |
Snippet name: | "My Day" [v4, solve a simple reading comprehension test using regexps, OK] |
Eternal ID of this version: | #1023742/37 |
Text MD5: | 5650a3d2ed5b7504258a3eb7b618f544 |
Transpilation MD5: | c3a5d550b0a9e8daee62d50ebe3d83c5 |
Author: | stefan |
Category: | javax |
Type: | JavaX source code (Dynamic Module) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2019-07-08 13:03:55 |
Source code size: | 2948 bytes / 78 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 650 / 994 |
Version history: | 36 change(s) |
Referenced in: | [show references] |