Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

95
LINES

< > BotCompany Repo | #1028540 // EnglishDateParser [attempt 1, solves a few cases]

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (5519L/35K).

1  
//import static main.DateStructures.*;
2  
3  
sclass EnglishDateParser > DateStructures {
4  
  bool assumeFuture = true; // e.g. for "tuesday" [not used]
5  
6  
  SomeDate parse(S s) null {
7  
    new ProbabilisticParser1 parser;
8  
9  
    // action that parses a number
10  
    ProbabilisticParser1.Action number = parser.new Any {
11  
      double calcProbabilityForMatchedText(S s) {
12  
        //print("number? " + s);
13  
        ret isInteger(s) ? 100 : 50;
14  
      }
15  
    };
16  
    
17  
    // action that parses a year
18  
    ProbabilisticParser1.Action year = parser.new Any {
19  
      double calcProbabilityForMatchedText(S s) {
20  
        ret !isInteger(s) ? 50
21  
          : between(parseInt(s), 1900, 2100) ? 100 : 75;
22  
      }
23  
    };
24  
25  
    LS tok = javaTok(s);
26  
27  
    // YMD rule
28  
    parser.addState(tok, parser.ruleFromActions(
29  
      year,
30  
      parser.new ConsumeToken("/"),
31  
      number,
32  
      parser.new ConsumeToken("/"),
33  
      number
34  
    )).userObject = func(Matches m) -> SomeDate {
35  
      new Day(parseInt($3), new Month(parseInt($2), new Year(parseInt($1))))
36  
    };
37  
    
38  
    // year rule
39  
    parser.addState(tok, parser.ruleFromActions(
40  
      year
41  
    )).userObject = func(Matches m) -> SomeDate {
42  
      new Year(parseInt($1))
43  
    };
44  
45  
    // weekday rule
46  
    parser.addState(tok, parser.ruleFromActions(
47  
      parser.new ConsumeOneOfTokens(asCISet(englishWeekdays())),
48  
    )).userObject = func(Matches m) -> SomeDate {
49  
      new Weekday(parseEnglishWeekday($1))
50  
    };
51  
    
52  
    // "next <weekday>" rule
53  
    parser.addState(tok, parser.ruleFromActions(
54  
      parser.new ConsumeToken("next"),
55  
      parser.new ConsumeOneOfTokens(asCISet(englishWeekdays())),
56  
    )).userObject = func(Matches m) -> SomeDate {
57  
      new Weekday(parseEnglishWeekday(print("day", $1)), new CurrentWeekPlus(1))
58  
    };
59  
60  
    // "in <n> days"
61  
    parser.addState(tok, parser.ruleFromActions(
62  
      parser.new ConsumeToken("in"),
63  
      number,
64  
      parser.new ConsumeOneOfTokens("day", "days") 
65  
    )).userObject = func(Matches m) -> SomeDate {
66  
      new TodayPlus(parseInt($1))
67  
    };
68  
69  
    // "<n> days from now"
70  
    parser.addState(tok, parser.ruleFromActions(
71  
      number,
72  
      parser.new ConsumeOneOfTokens("day from now", "days from now"),
73  
    )).userObject = func(Matches m) -> SomeDate {
74  
      new TodayPlus(parseInt($1))
75  
    };
76  
    
77  
    // month rule
78  
    parser.addState(tok, parser.ruleFromActions(
79  
      parser.new ConsumeOneOfTokens(asCISet(englishMonthNames())),
80  
    )).userObject = func(Matches m) -> SomeDate {
81  
      new Month(parseEnglishMonthName($1))
82  
    };
83  
    
84  
    // combine month + year
85  
    
86  
    parser.think();
87  
    ProbabilisticParser1.State state = parser.bestDoneState();
88  
    print(state);
89  
    if (state != null) {
90  
      Matches m = parser.stateToMatches(state, action -> !action instanceof ProbabilisticParser1.ConsumeToken);
91  
      print(m);
92  
      ret (SomeDate) callF(state.userObject, m);
93  
    }
94  
  }
95  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1028540
Snippet name: EnglishDateParser [attempt 1, solves a few cases]
Eternal ID of this version: #1028540/36
Text MD5: 11a64170bb282f673eec149fc1fffb6d
Transpilation MD5: 7ded0f442ad324e066448edba6d954ab
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-06-27 21:46:39
Source code size: 2957 bytes / 95 lines
Pitched / IR pitched: No / No
Views / Downloads: 281 / 537
Version history: 35 change(s)
Referenced in: [show references]