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

205
LINES

< > BotCompany Repo | #1002689 // SNL Bot (OLD)

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

Libraryless. Click here for Pure Java version (3554L/23K/77K).

!752

answer {
  if (!attn()) ret null;
  
  s = s.trim();
  if (s.startsWith("snl ")) exceptionToUser {
    S input = dropPrefix("snl ", s).trim();
    Explain e = snlParse(input);
    if (e == null) ret "No parse";
    
    //L<S> classes = getClasses(e);
    //ret "Top classes: " + structure(classes);
    
    L<Explain> parts = topParts(e);
    
    L<S> topOperators = getOperators(parts);

    ret "Top parts: " + structure(mapString(parts))
     + ". Top operators: " + structure(topOperators);
  }
}

static L<S> mapString(L<Explain> e) {
  new L<S> l;
  for (Explain ex : e) l.add(ex == null ? "null" : ex.string());
  ret l;
}

static L<Explain> topParts(Explain e) {
  if (e == null) ret litlist();
  L<S> classes = getClasses(e);
  //ret "Classes: " + structure(classes);
  if (classes.contains("juxta")) {
    Explain juxta = descend(e, "juxta");
    L<Explain> parts = scanJuxta(juxta);
    if (allAre(parts, "subword")) // sentences!
      ret topParts(parts);
    else
      ret litlist(juxta); // it's actually one sentence
  } /*else if (classes.contains("subword"))
    ret descend(e, "subword");*/
  else
    ret litlist(e); // just return whatever we have as a top part
}

static L<Explain> topParts(L<Explain> list) {
  new L<Explain> l;
  for (Explain e : list)
    l.addAll(topParts(e));
  ret l;
}

static L<Explain> scanJuxta(Explain e) {
  if (isA(e, "juxta")) {
    Explain juxta = descend(e, "juxta");
    Explain a = juxta.sub(0), b = juxta.sub(1);
    ret concatLists(scanJuxta(a), scanJuxta(b));
  } else
    ret litlist(e);
}

static L<S> getClasses(Explain e) {
  new L<S> l;
  while (e != null) {
    l.add(e.className());
    if (l(e.subs) == 1)
      e = e.sub(0);
    else
      break;
  }
  ret l;
}

static Explain castTo(Explain e, S className) {
  ret descend(e, className);
}

static Explain descend(Explain e, S className) {
  new L<S> l;
  while (e != null) {
    if (eq(e.className(), className))
      ret e;
    if (l(e.subs) == 1)
      e = e.sub(0);
    else
      break;
  }
  ret null;
}

static boolean isA(Explain e, S className) {
  ret getClasses(e).contains(className);
}

static boolean allAre(L<Explain> l, S className) {
  for (Explain e: l)
    if (!isA(e, className))
      ret false;
  ret true;
}

static boolean anyAre(L<Explain> l, S className) {
  for (Explain e: l)
    if (isA(e, className))
      ret true;
  ret false;
}

static L<S> getOperators(L<Explain> parts) {
  new L<S> l;
  for (Explain e : parts)
    l.add(makeOperator(e));
  ret l;
}

static S makeOperatorFromJuxta(L<Explain> parts) {
  new L<S> l;
  int i = 0;
  for (Explain e : parts) {
    //print("Operator part " + (++i) + ": " + e.string() + " / " + structure(getClasses(e)));
    
    if (!isA(e, "subword") && isA(e, "idword"))
      l.add(e.string());
    else
      l.add("*");
  }
  ret join(" ", l);
}

static L<Explain> getJuxtaArgs(L<Explain> parts) {
  new L<Explain> l;
  int i = 0;
  for (Explain e : parts)
    if (!isA(e, "subword") && isA(e, "idword")) {
      // operator part, skip
    } else
      l.add(e);
  ret l;
}

static S makeOperator(Explain e) {
  if (isA(e, "realarrow"))
    ret descend(e, "realarrow").sub(0).string() + " *";
  else if (isA(e, "juxta")) {
    L<Explain> j = scanJuxta(e);
    //print("scanJuxta => " + structure(j));
    ret makeOperatorFromJuxta(j);
  } else
    ret e.string();
}

static Lisp getTree(Explain e) {
  print("getTree " + structure(getClasses(e)));
  
  if (isA(e, "realarrow")) {
    print("realarrow");
    Explain a = castTo(e, "realarrow");
    ret lisp("<", getTree(a.sub(0)), getTree(a.sub(1)));
  }
  
  if (isA(e, "realarrowr")) {
    print("realarrowr");
    Explain a = castTo(e, "realarrowr");
    ret lisp(">", getTree(a.sub(0)), getTree(a.sub(1)));
  }
  
  if (isA(e, "square")) {
    print("square");
    Explain a = castTo(e, "square");
    ret lisp("[]", getTree(a.sub(0)));
  }
  
  if (isA(e, "round")) {
    Explain a = castTo(e, "round");
    ret lisp("()", getTree(a.sub(0)));
  }
  
  if (isA(e, "juxta")) {
    Explain juxta = descend(e, "juxta");
    L<Explain> parts = scanJuxta(juxta);
    
    print("juxta " + e.string());
    
    if (anyAre(parts, "subword")) {
      L<Explain> args = getJuxtaArgs(parts);
      Lisp l = lisp(makeOperatorFromJuxta(parts));
      for (Explain arg : args)
        l.add(removeBrackets(getTree(arg)));
      ret l;
    }
    
    print("  no subwords");

    // no subwords, fall back to simple string
  }
  
  ret lisp(e.string());
}

static Lisp removeBrackets(Lisp l) {
  if (eq(l.head, "[]"))
    ret l.get(0);
  ret l;
}

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: #1002689
Snippet name: SNL Bot (OLD)
Eternal ID of this version: #1002689/1
Text MD5: f35e85111b108114f2333e8a0eea5ba2
Transpilation MD5: 887517a87f61f5c56cea5ffb45dc58eb
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-02-24 21:36:13
Source code size: 4796 bytes / 205 lines
Pitched / IR pitched: No / No
Views / Downloads: 677 / 771
Referenced in: #1002711 - snlToTree
#1002712 - SNL Tree Bot
#3000203 - Answer for stefanreich (>> timing hello bot)
#3000238 - Answer for stefanreich (>> t power bot)
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)