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

156
LINES

< > BotCompany Repo | #1002700 // Translating English to Simplified NL (with first dialog!)

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

Libraryless. Click here for Pure Java version (4164L/27K/90K).

1  
!752
2  
3  
static PersistentMap<S, S> map;
4  
static new HashSet<S> joiners;
5  
static PersistentMap<S, S> splitters;
6  
7  
p {
8  
  map = new PersistentMap("map");
9  
  splitters = new PersistentMap("splitters");
10  
  load("joiners");
11  
}
12  
13  
static S dialog(S snl, S line) {
14  
  new Map<S, Lisp> m;
15  
  Lisp input = snlToTree(line);
16  
  
17  
  if (snlMatch("A < says < i < love < *", input, m))
18  
    ret "oh < that < is < nice";
19  
    
20  
  if (snlMatch("A < says < i < hate < *", input, m))
21  
    ret snlApply("? < why < do < you < hate < *", m);
22  
    
23  
  ret line;
24  
}
25  
26  
synchronized static S answer(S s) {
27  
  new Matches m;
28  
  
29  
  print("1002700 user=" + getUserName() + " attn=" + attn() + ", dedicated=" + dedicated());
30  
  
31  
  if (!attn()) ret null;
32  
  
33  
  if "are we dedicated"
34  
    ret yn(dedicated());
35  
36  
  if "add joiner * *" {
37  
    S j = preSimplify(m.unq(0) + " " + m.unq(1));
38  
    if (joiners.contains(j))
39  
      ret format("Joiner * exists", j);
40  
    joiners.add(j);
41  
    save("joiners");
42  
    ret format("OK, joiner * added", j);
43  
  }
44  
    
45  
  if "add splitter * = * *" {
46  
    S a = preSimplify(m.unq(0));
47  
    S b = preSimplify(m.unq(1) + " " + m.unq(2));
48  
    S old = splitters.get(a);
49  
    if (eq(old, b))
50  
      ret format("Splitter * => * exists", a, b);
51  
    splitters.put(a, b);
52  
    ret format("OK, splitter * => * added" + (old != null ? " (old: " + structure(old) + ")" : ""), a, b);
53  
  }
54  
    
55  
  if (!dedicated()) ret null;
56  
  
57  
  exceptionToUser {
58  
    /*if (isSNL(s))
59  
      ret "SNL";*/
60  
      
61  
    s = preSimplify(s);
62  
    S snl = map.get(s);
63  
    S snlSource = "map";
64  
    if (snl == null) {
65  
      snlSource = "naive";
66  
      snl = naiveSNL(s);
67  
    }
68  
    
69  
    print("1002700: got snl");
70  
    
71  
    S user = getUserName();
72  
    if (user == null)
73  
      user = master() ? "master" : "user";
74  
    //user = user.replaceAll("[0-9]", "");
75  
    S line = snlSimplifyBrackets(user + " < says < [" + snl + "]");
76  
    
77  
    print("1002700: calling dialog, line = " + line + ", user=" + user + ", snl=" + snl);
78  
    S answer = dialog(snl, line);
79  
    print("1002700: answer=" + answer);
80  
    ret answer;
81  
  }
82  
}
83  
84  
//static L<S> keepPunctuation = litlist("*", "<", ">", "[", "]", "?", "!", ":", "@");
85  
86  
static L<S> tokensForSNL(S s) {
87  
  L<S> tok = nlTok(s);
88  
  //ret codeTokensOnly(dropPunctuationExcept(tok, keepPunctuation));
89  
  ret codeTokensOnly(tok);
90  
}
91  
92  
static synchronized S naiveSNL(S s) {
93  
  print("Input: " + s);
94  
    s = preSimplify(s);
95  
  L<S> l = tokensForSNL(s);
96  
  print("Tokens: " + structure(l));
97  
  
98  
  processJoiners(l);
99  
  processSplitters(l);
100  
  addArrows(l);
101  
    
102  
  ret join(" ", l);
103  
}
104  
105  
// just a preprocessing for NL (does not generate SNL)
106  
static S preSimplify(S s) {
107  
  ret join(" ", codeTokensOnly(/*wordTokensToLowerCase*/(nlTok(s))));
108  
}
109  
110  
static void processJoiners(L<S> l) {
111  
  for (int i = 0; i < l(l)-1; i++) {
112  
    S x = l.get(i) + " " + l.get(i+1);
113  
    
114  
    if (joiners.contains(x)) {
115  
      l.set(i, x);
116  
      l.remove(i+1);
117  
      --i;
118  
    }
119  
  }
120  
}
121  
122  
static void processSplitters(L<S> l) {  
123  
  for (int i = 0; i < l(l); i++) {
124  
    S x = splitters.get(l.get(i));
125  
    if (x != null) {
126  
      L<S> tokens = tokensForSNL(x);
127  
      l.remove(i);
128  
      l.addAll(i, tokens);
129  
      i += l(tokens)-1;
130  
    }
131  
  }
132  
}
133  
134  
static void addArrows(L<S> l) {
135  
  for (int i = 0; i < l(l)-1; i++) {
136  
    if (isWord(l.get(i)) && isWord(l.get(i+1)))
137  
      l.add(++i, "<");
138  
  }
139  
}
140  
141  
static boolean isWord(S s) {
142  
  if (empty(s)) ret false;
143  
  if (isQuoted(s)) ret true;
144  
  
145  
  char c = s.charAt(0);
146  
  ret Character.isLetter(c) || c == '\'';
147  
}
148  
149  
static boolean isSNL(S s) {
150  
  L<S> tok = javaTok(s);
151  
  ret tok.contains("<") || tok.contains("[");
152  
}
153  
154  
static S snlSimplifyBrackets(S s) {
155  
  ret snlFromTree(snlToTree(s));
156  
}

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: #1002700
Snippet name: Translating English to Simplified NL (with first dialog!)
Eternal ID of this version: #1002700/1
Text MD5: 52cd59ceffa8113bb2323a105bd1f1d4
Transpilation MD5: eda7753c746b281cfcd4b3e35b39c3a6
Author: stefan
Category: nl bots
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-02-29 20:23:45
Source code size: 3764 bytes / 156 lines
Pitched / IR pitched: No / No
Views / Downloads: 822 / 1802
Referenced in: [show references]