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

95
LINES

< > BotCompany Repo | #1028593 // EnglishDateParser, attempt 4 [working backup]

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

Libraryless. Click here for Pure Java version (4252L/26K).

sclass EnglishDateParser > DateStructures {
  replace P with ParsedWithTokens.
  
  bool assumeFuture = true; // e.g. for "tuesday" [not used]
  LS tok;
  int maxTokens = 3;
  
  L<P<SomeDate>> longestParses(S s) {
    ret pwt_topDogs(pwt_filterByType SomeDate(allParses(s)));
  }
  
  SomeDate parse(S s) {
    ret getVar(first(longestParses(s)));
  }
  
  ItIt<P> allParses(S s) {
    tok = javaTok(s);
    new L<P> out;
    L<ListAndIndex<S>> codeTokens = asList(lai_codeTokens(tok));
    
    L<P<Int>> numbers = concatMap parseNumber(codeTokens);
    
    // "in <n> days"
    for (P<Int> number : numbers)
      for (P<S> in : print("in", asList(parseFixedToken_before("in", number.start()))))
        for (P<S> days : print("days", asList(parseOneOfFixedTokens(litciset("day", "days"), print(number.remaining())))))
          out.add(pwt_combine(new TodayPlus(number!), in, days));
    
    // "<n> days from now"
    for (P<Int> number : numbers)
      for (P<S> daysFromNow : parseOneOfFixedTokens(litciset("day from now", "days from now"), number.remaining()))
        out.add(pwt_combine(new TodayPlus(number!), number, daysFromNow));
    
    L<P<Int>> years = concatMap parseYear(codeTokens);
    print(+years);
    out.addAll(years);
    
    L<P<Weekday>> weekdays = concatMap parseWeekday(codeTokens);
    out.addAll(weekdays);
    
    L<P<Month>> months = concatMap parseMonthName(codeTokens);
    out.addAll(months);
    
    // "next <weekday>"
    for (P<Weekday> weekday : weekdays)
      for (P<S> next : parseFixedToken_before("next", weekday.start()))
        out.add(pwt_combine(new Weekday(weekday->weekday, new CurrentWeekPlus(1)), next, weekday));

    for (P<Int> year : years)
      for (P<S> slash : parseFixedToken("/", year.remaining()))
        for (P<Int> month : parseMonth(slash.remaining()))
          for (P<S> slash2 : parseFixedToken("/", month.remaining()))
            for (P<Int> day : parseDayOfMonth(slash2.remaining()))
              out.add(pwt_combine(new Day(day!, new Month(month!, new Year(year!))), year, day));
        
    ret itIt(out);
  }
  
  ItIt<P<Int>> parseNumber(ListAndIndex<S> toParse) {
    ret mapI_nonNulls(pwt_followingTokens(1, maxTokens, toParse),
      p -> isInteger(p!) ? p.withValue(parseInt(p!)) : null);
  }
  
  ItIt<P<Int>> parseYear(ListAndIndex<S> toParse) {
    ret filterI(parseNumber(toParse), p -> between(p!, 1900, 2100));
  }
  
  ItIt<P<Int>> parseMonth(ListAndIndex<S> toParse) {
    ret filterI(parseNumber(toParse), p -> between(p!, 1, 12));
  }
  
  ItIt<P<Int>> parseDayOfMonth(ListAndIndex<S> toParse) {
    ret filterI(parseNumber(toParse), p -> between(p!, 1, 31));
  }
  
  ItIt<P<S>> parseFixedToken(S token, ListAndIndex<S> toParse) {
    ret filterI(pwt_followingTokens(1, maxTokens, toParse), p -> eqic(p!, token));
  }
  
  ItIt<P<S>> parseFixedToken_before(S token, ListAndIndex<S> toParse) {
    ret filterI(pwt_precedingTokens(1, maxTokens, toParse), p -> eqic(p!, token));
  }
  
  ItIt<P<S>> parseOneOfFixedTokens(Set<S> tokens, ListAndIndex<S> toParse) {
    ret filterI(pwt_followingTokens(1, maxTokens, toParse), p -> contains(tokens, p!));
  }
  
  ItIt<P<Weekday>> parseWeekday(ListAndIndex<S> toParse) {
    ret pwt_mapI(s -> new Weekday(parseEnglishWeekday(s)), parseOneOfFixedTokens(englishWeekdays_ciSet(), toParse));
  }
  
  ItIt<P<Month>> parseMonthName(ListAndIndex<S> toParse) {
    ret pwt_mapI(s -> new Month(parseEnglishMonthName(s)), parseOneOfFixedTokens(englishMonthNames_ciSet(), toParse));
  }
}

Author comment

Began life as a copy of #1028573

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: #1028593
Snippet name: EnglishDateParser, attempt 4 [working backup]
Eternal ID of this version: #1028593/1
Text MD5: 0b94859eb4f3095d14fa3e03e37542b5
Transpilation MD5: 74d5bb07e6c0e9c509b13180a2397ee2
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-06-28 13:12:35
Source code size: 3620 bytes / 95 lines
Pitched / IR pitched: No / No
Views / Downloads: 73 / 116
Referenced in: [show references]