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

67
LINES

< > BotCompany Repo | #1011074 // ai_tripelize [getting better]

JavaX fragment (include)

static Triple<S> ai_tripelize(S s) {
  T3<S> t = ai_parseTriple(s);
  if (t != null) ret t;
  
  S x = "";
  if (endsWith(s, "?")) x = " (?)"; // statement with question mark, e.g. "Al Gore invented the Internet?"
  t = ai_tripelize_impl(dropCertainPunctuationAtEnd(s, ":;,?!."));
  if (t == null) null;
  ai_fixTriple(t);
  //print("After fix: " + t);
  if (nempty(x) && t != null && !endsWith(t.a, "?") && !endsWith(t.b, "?") && !endsWith(t.c, "?")) t.b += x;
  if (endsWith(s, "!") && eq(t.b, ".")) t.b = "!";
  ret t;
}
  
static Triple<S> ai_tripelize_impl(S s) {
  L<S> original = ai_tripelize_prepare(s);
  if (l(original) <= 1) null; // no tokens
  original = simpleSpaces(original);
  L<S> tok = original;
  S a = get(tok, 1);
  
  T3<S> triple;
  ret optional triple = ai_tripelize_dash(s, original);
  ret optional triple = ai_tripelize_exclamCommand(s, original);
  ret optional triple = ai_tripelize_howLongEtc(s, original);

  if (ai_questionWord_1(a)) { tok = subList(tok, 2); a = get(tok, 1); }
  S questionWord = trimJoinSubList(original, 0, indexOfSubList(original, tok));
  
  if (ai_yesNoQuestionWord_1(a) && neqicOneOf(a, "am", "are", "is")) {
    L<S> tok2 = subList(tok, 2);
    int i = indexOfFirstVerb(tok2);
    S v = get(tok2, i);
    if (v != null) {
      S middle = trimJoinSubList(tok2, 0, i);
      S rest = trimJoinSubList(tok2, i+1);
      v = ai_tripelize_helperVerb(a, v);
      ret triple(joinWithSpace2(questionWord, middle), v, rest);
    }
  }
  
  // sentence starting with verb - instruction or question?
  if (isVerb(a)) {
    if (ai_yesNoQuestionWord_1(a)) {
      // question
      tok = subList(tok, 2);
      int i = ai_splitTwoNounPhrases(tok);
      //int i = ai_endOfNounPhrase(tok);
      ret triple(
        joinWithSpace2(questionWord, trimJoinSubList(tok, 0, i)),
        a + "?",
        trimJoinSubList(tok, i));
    } else {
      // instruction
      ret triple(a, ".", join(subList(tok, 2)));
    }
  }
  
  int i = ai_endOfNounPhrase(tok);
  int j = indexOfFirstVerb(tok, i);
  if (j < 0) ret triple(s, ".", ".");
  ret triple(
    joinWithSpace2(questionWord, trimJoinSubList(tok, 0, j)),
    tok.get(j),
    trimJoinSubList(tok, j+2));
}

Author comment

Began life as a copy of #1011058

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1011074
Snippet name: ai_tripelize [getting better]
Eternal ID of this version: #1011074/41
Text MD5: 49dc2b7937602c412488dfee07e711f2
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-12-30 18:23:18
Source code size: 2265 bytes / 67 lines
Pitched / IR pitched: No / No
Views / Downloads: 504 / 577
Version history: 40 change(s)
Referenced in: [show references]