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).

//import static main.DateStructures.*;

sclass EnglishDateParser > DateStructures {
  bool assumeFuture = true; // e.g. for "tuesday" [not used]

  SomeDate parse(S s) null {
    new ProbabilisticParser1 parser;

    // action that parses a number
    ProbabilisticParser1.Action number = parser.new Any {
      double calcProbabilityForMatchedText(S s) {
        //print("number? " + s);
        ret isInteger(s) ? 100 : 50;
      }
    };
    
    // action that parses a year
    ProbabilisticParser1.Action year = parser.new Any {
      double calcProbabilityForMatchedText(S s) {
        ret !isInteger(s) ? 50
          : between(parseInt(s), 1900, 2100) ? 100 : 75;
      }
    };

    LS tok = javaTok(s);

    // YMD rule
    parser.addState(tok, parser.ruleFromActions(
      year,
      parser.new ConsumeToken("/"),
      number,
      parser.new ConsumeToken("/"),
      number
    )).userObject = func(Matches m) -> SomeDate {
      new Day(parseInt($3), new Month(parseInt($2), new Year(parseInt($1))))
    };
    
    // year rule
    parser.addState(tok, parser.ruleFromActions(
      year
    )).userObject = func(Matches m) -> SomeDate {
      new Year(parseInt($1))
    };

    // weekday rule
    parser.addState(tok, parser.ruleFromActions(
      parser.new ConsumeOneOfTokens(asCISet(englishWeekdays())),
    )).userObject = func(Matches m) -> SomeDate {
      new Weekday(parseEnglishWeekday($1))
    };
    
    // "next <weekday>" rule
    parser.addState(tok, parser.ruleFromActions(
      parser.new ConsumeToken("next"),
      parser.new ConsumeOneOfTokens(asCISet(englishWeekdays())),
    )).userObject = func(Matches m) -> SomeDate {
      new Weekday(parseEnglishWeekday(print("day", $1)), new CurrentWeekPlus(1))
    };

    // "in <n> days"
    parser.addState(tok, parser.ruleFromActions(
      parser.new ConsumeToken("in"),
      number,
      parser.new ConsumeOneOfTokens("day", "days") 
    )).userObject = func(Matches m) -> SomeDate {
      new TodayPlus(parseInt($1))
    };

    // "<n> days from now"
    parser.addState(tok, parser.ruleFromActions(
      number,
      parser.new ConsumeOneOfTokens("day from now", "days from now"),
    )).userObject = func(Matches m) -> SomeDate {
      new TodayPlus(parseInt($1))
    };
    
    // month rule
    parser.addState(tok, parser.ruleFromActions(
      parser.new ConsumeOneOfTokens(asCISet(englishMonthNames())),
    )).userObject = func(Matches m) -> SomeDate {
      new Month(parseEnglishMonthName($1))
    };
    
    // combine month + year
    
    parser.think();
    ProbabilisticParser1.State state = parser.bestDoneState();
    print(state);
    if (state != null) {
      Matches m = parser.stateToMatches(state, action -> !action instanceof ProbabilisticParser1.ConsumeToken);
      print(m);
      ret (SomeDate) callF(state.userObject, m);
    }
  }
}

download  show line numbers  debug dex   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, 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: 57 / 232
Version history: 35 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1028540 & http://1028540.tinybrain.de