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

1  
sclass EnglishDateParser > DateStructures {
2  
  replace P with ParsedWithTokens.
3  
  
4  
  bool assumeFuture = true; // e.g. for "tuesday" [not used]
5  
  LS tok;
6  
  int maxTokens = 3;
7  
  
8  
  L<P<SomeDate>> longestParses(S s) {
9  
    ret pwt_topDogs(pwt_filterByType SomeDate(allParses(s)));
10  
  }
11  
  
12  
  SomeDate parse(S s) {
13  
    ret getVar(first(longestParses(s)));
14  
  }
15  
  
16  
  ItIt<P> allParses(S s) {
17  
    tok = javaTok(s);
18  
    new L<P> out;
19  
    L<ListAndIndex<S>> codeTokens = asList(lai_codeTokens(tok));
20  
    
21  
    L<P<Int>> numbers = concatMap parseNumber(codeTokens);
22  
    
23  
    // "in <n> days"
24  
    for (P<Int> number : numbers)
25  
      for (P<S> in : print("in", asList(parseFixedToken_before("in", number.start()))))
26  
        for (P<S> days : print("days", asList(parseOneOfFixedTokens(litciset("day", "days"), print(number.remaining())))))
27  
          out.add(pwt_combine(new TodayPlus(number!), in, days));
28  
    
29  
    // "<n> days from now"
30  
    for (P<Int> number : numbers)
31  
      for (P<S> daysFromNow : parseOneOfFixedTokens(litciset("day from now", "days from now"), number.remaining()))
32  
        out.add(pwt_combine(new TodayPlus(number!), number, daysFromNow));
33  
    
34  
    L<P<Int>> years = concatMap parseYear(codeTokens);
35  
    print(+years);
36  
    out.addAll(years);
37  
    
38  
    L<P<Weekday>> weekdays = concatMap parseWeekday(codeTokens);
39  
    out.addAll(weekdays);
40  
    
41  
    L<P<Month>> months = concatMap parseMonthName(codeTokens);
42  
    out.addAll(months);
43  
    
44  
    // "next <weekday>"
45  
    for (P<Weekday> weekday : weekdays)
46  
      for (P<S> next : parseFixedToken_before("next", weekday.start()))
47  
        out.add(pwt_combine(new Weekday(weekday->weekday, new CurrentWeekPlus(1)), next, weekday));
48  
49  
    for (P<Int> year : years)
50  
      for (P<S> slash : parseFixedToken("/", year.remaining()))
51  
        for (P<Int> month : parseMonth(slash.remaining()))
52  
          for (P<S> slash2 : parseFixedToken("/", month.remaining()))
53  
            for (P<Int> day : parseDayOfMonth(slash2.remaining()))
54  
              out.add(pwt_combine(new Day(day!, new Month(month!, new Year(year!))), year, day));
55  
        
56  
    ret itIt(out);
57  
  }
58  
  
59  
  ItIt<P<Int>> parseNumber(ListAndIndex<S> toParse) {
60  
    ret mapI_nonNulls(pwt_followingTokens(1, maxTokens, toParse),
61  
      p -> isInteger(p!) ? p.withValue(parseInt(p!)) : null);
62  
  }
63  
  
64  
  ItIt<P<Int>> parseYear(ListAndIndex<S> toParse) {
65  
    ret filterI(parseNumber(toParse), p -> between(p!, 1900, 2100));
66  
  }
67  
  
68  
  ItIt<P<Int>> parseMonth(ListAndIndex<S> toParse) {
69  
    ret filterI(parseNumber(toParse), p -> between(p!, 1, 12));
70  
  }
71  
  
72  
  ItIt<P<Int>> parseDayOfMonth(ListAndIndex<S> toParse) {
73  
    ret filterI(parseNumber(toParse), p -> between(p!, 1, 31));
74  
  }
75  
  
76  
  ItIt<P<S>> parseFixedToken(S token, ListAndIndex<S> toParse) {
77  
    ret filterI(pwt_followingTokens(1, maxTokens, toParse), p -> eqic(p!, token));
78  
  }
79  
  
80  
  ItIt<P<S>> parseFixedToken_before(S token, ListAndIndex<S> toParse) {
81  
    ret filterI(pwt_precedingTokens(1, maxTokens, toParse), p -> eqic(p!, token));
82  
  }
83  
  
84  
  ItIt<P<S>> parseOneOfFixedTokens(Set<S> tokens, ListAndIndex<S> toParse) {
85  
    ret filterI(pwt_followingTokens(1, maxTokens, toParse), p -> contains(tokens, p!));
86  
  }
87  
  
88  
  ItIt<P<Weekday>> parseWeekday(ListAndIndex<S> toParse) {
89  
    ret pwt_mapI(s -> new Weekday(parseEnglishWeekday(s)), parseOneOfFixedTokens(englishWeekdays_ciSet(), toParse));
90  
  }
91  
  
92  
  ItIt<P<Month>> parseMonthName(ListAndIndex<S> toParse) {
93  
    ret pwt_mapI(s -> new Month(parseEnglishMonthName(s)), parseOneOfFixedTokens(englishMonthNames_ciSet(), toParse));
94  
  }
95  
}

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: 174 / 239
Referenced in: [show references]