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

1  
!752
2  
3  
answer {
4  
  if (!attn()) ret null;
5  
  
6  
  s = s.trim();
7  
  if (s.startsWith("snl ")) exceptionToUser {
8  
    S input = dropPrefix("snl ", s).trim();
9  
    Explain e = snlParse(input);
10  
    if (e == null) ret "No parse";
11  
    
12  
    //L<S> classes = getClasses(e);
13  
    //ret "Top classes: " + structure(classes);
14  
    
15  
    L<Explain> parts = topParts(e);
16  
    
17  
    L<S> topOperators = getOperators(parts);
18  
19  
    ret "Top parts: " + structure(mapString(parts))
20  
     + ". Top operators: " + structure(topOperators);
21  
  }
22  
}
23  
24  
static L<S> mapString(L<Explain> e) {
25  
  new L<S> l;
26  
  for (Explain ex : e) l.add(ex == null ? "null" : ex.string());
27  
  ret l;
28  
}
29  
30  
static L<Explain> topParts(Explain e) {
31  
  if (e == null) ret litlist();
32  
  L<S> classes = getClasses(e);
33  
  //ret "Classes: " + structure(classes);
34  
  if (classes.contains("juxta")) {
35  
    Explain juxta = descend(e, "juxta");
36  
    L<Explain> parts = scanJuxta(juxta);
37  
    if (allAre(parts, "subword")) // sentences!
38  
      ret topParts(parts);
39  
    else
40  
      ret litlist(juxta); // it's actually one sentence
41  
  } /*else if (classes.contains("subword"))
42  
    ret descend(e, "subword");*/
43  
  else
44  
    ret litlist(e); // just return whatever we have as a top part
45  
}
46  
47  
static L<Explain> topParts(L<Explain> list) {
48  
  new L<Explain> l;
49  
  for (Explain e : list)
50  
    l.addAll(topParts(e));
51  
  ret l;
52  
}
53  
54  
static L<Explain> scanJuxta(Explain e) {
55  
  if (isA(e, "juxta")) {
56  
    Explain juxta = descend(e, "juxta");
57  
    Explain a = juxta.sub(0), b = juxta.sub(1);
58  
    ret concatLists(scanJuxta(a), scanJuxta(b));
59  
  } else
60  
    ret litlist(e);
61  
}
62  
63  
static L<S> getClasses(Explain e) {
64  
  new L<S> l;
65  
  while (e != null) {
66  
    l.add(e.className());
67  
    if (l(e.subs) == 1)
68  
      e = e.sub(0);
69  
    else
70  
      break;
71  
  }
72  
  ret l;
73  
}
74  
75  
static Explain castTo(Explain e, S className) {
76  
  ret descend(e, className);
77  
}
78  
79  
static Explain descend(Explain e, S className) {
80  
  new L<S> l;
81  
  while (e != null) {
82  
    if (eq(e.className(), className))
83  
      ret e;
84  
    if (l(e.subs) == 1)
85  
      e = e.sub(0);
86  
    else
87  
      break;
88  
  }
89  
  ret null;
90  
}
91  
92  
static boolean isA(Explain e, S className) {
93  
  ret getClasses(e).contains(className);
94  
}
95  
96  
static boolean allAre(L<Explain> l, S className) {
97  
  for (Explain e: l)
98  
    if (!isA(e, className))
99  
      ret false;
100  
  ret true;
101  
}
102  
103  
static boolean anyAre(L<Explain> l, S className) {
104  
  for (Explain e: l)
105  
    if (isA(e, className))
106  
      ret true;
107  
  ret false;
108  
}
109  
110  
static L<S> getOperators(L<Explain> parts) {
111  
  new L<S> l;
112  
  for (Explain e : parts)
113  
    l.add(makeOperator(e));
114  
  ret l;
115  
}
116  
117  
static S makeOperatorFromJuxta(L<Explain> parts) {
118  
  new L<S> l;
119  
  int i = 0;
120  
  for (Explain e : parts) {
121  
    //print("Operator part " + (++i) + ": " + e.string() + " / " + structure(getClasses(e)));
122  
    
123  
    if (!isA(e, "subword") && isA(e, "idword"))
124  
      l.add(e.string());
125  
    else
126  
      l.add("*");
127  
  }
128  
  ret join(" ", l);
129  
}
130  
131  
static L<Explain> getJuxtaArgs(L<Explain> parts) {
132  
  new L<Explain> l;
133  
  int i = 0;
134  
  for (Explain e : parts)
135  
    if (!isA(e, "subword") && isA(e, "idword")) {
136  
      // operator part, skip
137  
    } else
138  
      l.add(e);
139  
  ret l;
140  
}
141  
142  
static S makeOperator(Explain e) {
143  
  if (isA(e, "realarrow"))
144  
    ret descend(e, "realarrow").sub(0).string() + " *";
145  
  else if (isA(e, "juxta")) {
146  
    L<Explain> j = scanJuxta(e);
147  
    //print("scanJuxta => " + structure(j));
148  
    ret makeOperatorFromJuxta(j);
149  
  } else
150  
    ret e.string();
151  
}
152  
153  
static Lisp getTree(Explain e) {
154  
  print("getTree " + structure(getClasses(e)));
155  
  
156  
  if (isA(e, "realarrow")) {
157  
    print("realarrow");
158  
    Explain a = castTo(e, "realarrow");
159  
    ret lisp("<", getTree(a.sub(0)), getTree(a.sub(1)));
160  
  }
161  
  
162  
  if (isA(e, "realarrowr")) {
163  
    print("realarrowr");
164  
    Explain a = castTo(e, "realarrowr");
165  
    ret lisp(">", getTree(a.sub(0)), getTree(a.sub(1)));
166  
  }
167  
  
168  
  if (isA(e, "square")) {
169  
    print("square");
170  
    Explain a = castTo(e, "square");
171  
    ret lisp("[]", getTree(a.sub(0)));
172  
  }
173  
  
174  
  if (isA(e, "round")) {
175  
    Explain a = castTo(e, "round");
176  
    ret lisp("()", getTree(a.sub(0)));
177  
  }
178  
  
179  
  if (isA(e, "juxta")) {
180  
    Explain juxta = descend(e, "juxta");
181  
    L<Explain> parts = scanJuxta(juxta);
182  
    
183  
    print("juxta " + e.string());
184  
    
185  
    if (anyAre(parts, "subword")) {
186  
      L<Explain> args = getJuxtaArgs(parts);
187  
      Lisp l = lisp(makeOperatorFromJuxta(parts));
188  
      for (Explain arg : args)
189  
        l.add(removeBrackets(getTree(arg)));
190  
      ret l;
191  
    }
192  
    
193  
    print("  no subwords");
194  
195  
    // no subwords, fall back to simple string
196  
  }
197  
  
198  
  ret lisp(e.string());
199  
}
200  
201  
static Lisp removeBrackets(Lisp l) {
202  
  if (eq(l.head, "[]"))
203  
    ret l.get(0);
204  
  ret l;
205  
}

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