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

114
LINES

< > BotCompany Repo | #1005476 // Parse Sentence Multi [works]

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (5809L/37K/126K).

1  
!752
2  
3  
concepts.
4  
5  
concept WithTime {
6  
  new Ref sentence;
7  
  new Ref time;
8  
}
9  
10  
concept Possession {
11  
  new Ref x; // e.g. "your"
12  
  new Ref y; // e.g. "car"
13  
}
14  
15  
// e.g. someone's girl
16  
concept Possession2 {
17  
  new Ref x; // e.g. "someone"
18  
  new Ref y; // e.g. "girl"
19  
}
20  
21  
concept Sentence {}
22  
23  
Sentence > SPO {
24  
  new Ref s; // subject
25  
  new Ref p; // predicate
26  
  new Ref o; // object
27  
  
28  
  static S _fieldOrder = "s p o";
29  
  
30  
  *() {}
31  
  *(Concept s, Concept p, Concept o) {
32  
    this.s.set(s);
33  
    this.p.set(p);
34  
    this.o.set(o);
35  
  }
36  
}
37  
38  
p {
39  
  testParse("I've been here today");
40  
  testParse("I've been your friend today");
41  
  testParse("I've been his bimbo today");
42  
  testParse("I've been this dude's girl today");
43  
}
44  
45  
svoid testParse(S s) {
46  
  print("** \* s */ **");
47  
  print();
48  
  new L<Concept> l;
49  
  parse(s, listCollector(l));
50  
  for i over l:
51  
    print((i+1) + ". " + structureConcept(l.get(i)));
52  
  
53  
  for (Concept c : iterateOptions(l)) {
54  
    if (properSentence(c)) {
55  
      print("First proper sentence: " + structureConcept(c));
56  
      break;
57  
    }
58  
  }
59  
  print();
60  
}
61  
62  
static Concept parseToOptions(S s) {
63  
  new L<Concept> l;
64  
  parse(s, listCollector(l));
65  
  ret options(l);
66  
}
67  
68  
static L<S> poss = splitAtSpace("my your his her their our");
69  
70  
static void parse(S s, Collector<Concept> out) {
71  
  new Matches m;
72  
  
73  
  if (flexMatchIC("* today", s, m))
74  
    ret if out.add(cnew(WithTime,
75  
      sentence := parseToOptions($1),
76  
      time := concept("today")));
77  
78  
  if (flexMatchStartIC(poss, s, m))
79  
    ret if out.add(cnew(Possession,
80  
      x := cstr($1),
81  
      y := parseToOptions($2)));
82  
83  
  L<S> tok = javaTok(s);
84  
  int i = indexOfEndsWith(tok, "'s");
85  
  if (i >= 0) {
86  
    S left = join(subList(tok, 0, i)) + dropSuffix("'s", tok.get(i));
87  
    S right = join(subList(tok, i+2));
88  
    ret if out.add(cnew(Possession2,
89  
      x := parseToOptions(left), y := parseToOptions(right)));
90  
  }
91  
  
92  
  if (flexMatchIC("I've been *", s, m))
93  
    ret if out.add(
94  
      new SPO(cstr("I"),
95  
      cstr("have been"),
96  
      parseToOptions($1)));
97  
98  
  out.add(cstr(s)); // default
99  
}
100  
101  
static Concept firstOfFirst(Concept c) {
102  
  ret conceptGraphMap_aggressive(c, func(Concept c) {
103  
    c << Options ? first(c/Options) : c
104  
  });
105  
}
106  
107  
sbool properSentence(Concept c) {
108  
  ret c << Sentence
109  
    || c << WithTime && properSentence(c/WithTime.sentence!);
110  
}
111  
112  
static Iterable<Concept> iterateOptions(L<Concept> l) {
113  
  ret mapI(l, func(Concept c) { firstOfFirst(c) });
114  
}

Author comment

Began life as a copy of #1005474

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1005476
Snippet name: Parse Sentence Multi [works]
Eternal ID of this version: #1005476/1
Text MD5: 9cbaee1a74d8b36d7ef925e325633dd1
Transpilation MD5: 5307e4bb7cb2cb0b55a307cf849d6ffe
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-11-21 16:32:16
Source code size: 2517 bytes / 114 lines
Pitched / IR pitched: No / No
Views / Downloads: 616 / 660
Referenced in: [show references]