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

79
LINES

< > BotCompany Repo | #1008567 // Group sentence parts v1

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

Libraryless. Click here for Pure Java version (3593L/24K/79K).

1  
!7
2  
3  
p {
4  
  callAnswerFunctionVerbose("tell me what time it is");
5  
  callAnswerFunctionVerbose("Can you tell me what time it is?");
6  
  botSleep();
7  
}
8  
9  
sclass TokConsumer {
10  
  L<S> l;
11  
  int i;
12  
  
13  
  *(L<S> *l) {}
14  
  
15  
  S peek() { ret get(l, i); }
16  
  S next() { ret get(l, i++); }
17  
  
18  
  L<S> remainingTokens() { ret subList(l, i); }
19  
}
20  
21  
sS answer(S s) {
22  
  ret group(s);
23  
}
24  
25  
sS group(S s) {
26  
  s = groupStep(s);
27  
  L<S> tok = joinBrackets(javaTok(s));
28  
  for (int i = 1; i < l(tok); i += 2) {
29  
    S t = tok.get(i);
30  
    if (isRoundBracketed(t))
31  
      tok.set(i, "(" + group(dropFirstAndLast(t)) + ")");
32  
  }
33  
  ret join(tok);
34  
}
35  
36  
sS groupStep(S s) {
37  
  L<S> tok = codeTokens(dropPunctuation(javaTok(s)));
38  
  TokConsumer c = new(tok);
39  
  S verb = consumeVerb(c);
40  
  if (verb != null) {
41  
    S noun = consumeNoun(c);
42  
    if (noun != null)
43  
      ret group(verb, noun, rest(c));
44  
  }
45  
  ret joinWithSpace(tok);
46  
}
47  
48  
static Set<S> verbs = asSet(splitAtSpace("can does will tell would were do"));
49  
static Set<S> nouns = asSet(splitAtSpace("i you he she it we someone me"));
50  
51  
static S consumeVerb(TokConsumer c) {
52  
  ret consumeFromList(verbs, c);
53  
}
54  
55  
static S consumeNoun(TokConsumer c) {
56  
  ret consumeFromList(nouns, c);
57  
}
58  
59  
static S consumeFromList(Set<S> set, TokConsumer c) {
60  
  if (set.contains(toLower(c.peek())))
61  
    ret c.next();
62  
  null;
63  
}
64  
65  
sS group(S... parts) {
66  
  new L<S> l;
67  
  for (S part : parts) {
68  
    L<S> tok = javaTokC(part);
69  
    if (l(tok) > 1)
70  
      l.add("(" + joinWithSpace(tok) + ")");
71  
    else
72  
      l.addAll(tok);
73  
  }
74  
  ret joinWithSpace(l);
75  
}
76  
77  
sS rest(TokConsumer c) {
78  
  ret joinWithSpace(c.remainingTokens());
79  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1008567
Snippet name: Group sentence parts v1
Eternal ID of this version: #1008567/15
Text MD5: 9c7c3895ec48425f1262a282fef224c0
Transpilation MD5: b94c23b1bc35e6e8ac7dbe16e75c4aad
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-05-24 00:00:11
Source code size: 1672 bytes / 79 lines
Pitched / IR pitched: No / No
Views / Downloads: 424 / 515
Version history: 14 change(s)
Referenced in: [show references]