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

97
LINES

< > BotCompany Repo | #1002998 // TB Word Types Bot

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

Libraryless. Click here for Pure Java version (2005L/13K/43K).

1  
!752
2  
3  
static Map<S, S> types;
4  
5  
p {
6  
  types = new PersistentMap("types");
7  
}
8  
9  
synchronized answer { exceptionToUser {
10  
  if (!tb()) null;
11  
  
12  
  if (match("* is a *", s, m) || match("* is an *", s, m)) {
13  
    /*if (isQuoted(m.get(0)))*/ {
14  
      S word = toLowerCase(m.unq(0));
15  
      S type = toLowerCase(m.unq(1));
16  
      types.put(word, type);
17  
      ret "OK!";
18  
    }
19  
  }
20  
  
21  
  if (match("* is not a *", s, m) || match("* is not an *", s, m)) {
22  
    S word = m.tlc(0);
23  
    S type = m.tlc(1);
24  
    if (eqic(types.get(word), type)) {
25  
      types.remove(word);
26  
      ret "Removed!";
27  
    }
28  
  }
29  
30  
  if (match("type *", s, m) || match("what is *", s, m)) {
31  
    S word = m.tlc(0);
32  
    S type = types.get(word);
33  
    if (nempty(type))
34  
      ret fixSentence(quote(word) + " is a " + type);
35  
  }
36  
  
37  
  if (matchStartSimple("pattern", s, m)) {
38  
    S input = m.rest();
39  
    L<S> tok = /*dropPunctuation*/(nlTok(input));
40  
    new L<S> l;
41  
    for (int i = 1; i < l(tok); i += 2) {
42  
      S word = tok.get(i);
43  
      S type = getType(word);
44  
      if (empty(type)) type = "?";
45  
      l.add(type);
46  
    }
47  
    ret join(" ", l);
48  
  }
49  
  
50  
  if "word types"
51  
    ret structure(asList(asSet(values(types))));
52  
    
53  
  if "word type *"
54  
    ret structure(keysForValue(types, m.tlc(0)));
55  
}}
56  
57  
static S fixformat(S s, O... data) {
58  
  ret fixSentence(format(s, data));
59  
}
60  
61  
static S fixSentence(S s) {
62  
  L<S> tok = nlTok(s);
63  
  for (int i = 1; i+2 < l(tok); i += 2)
64  
    if (eqic(tok.get(i), "a") || eqic(tok.get(i), "an"))
65  
      tok.set(i, aOrAn(tok.get(i+2)));
66  
  ret join(tok);
67  
}
68  
69  
static S aOrAn(S s) {
70  
  if (!empty(s)) {
71  
    if (isVowel(s.charAt(0)))
72  
      ret "an";
73  
    if (eqic(s, "honest")) // etc
74  
      ret "an";
75  
  }
76  
  ret "a";
77  
}
78  
79  
static boolean isVowel(char c) {
80  
  ret containsIgnoreCase("aeiou", c);
81  
}
82  
83  
static S getType(S word) {
84  
  if (isQuoted(word)) ret "quoted";
85  
  if (isInteger(word)) ret "number";
86  
  if (!isExtendedIdentifier(word)) ret "symbol";
87  
  ret types.get(tlc(word));
88  
}
89  
90  
static boolean matchStartSimple(S prefix, S s, Matches m) {
91  
  s = s.trim();
92  
  if (startsWithIgnoreCase(s, prefix + " ")) {
93  
    m.m = new S[] { s.substring(l(prefix) + 1).trim() };
94  
    ret true;
95  
  }
96  
  ret false;
97  
}

Author comment

Began life as a copy of #1002991

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: #1002998
Snippet name: TB Word Types Bot
Eternal ID of this version: #1002998/1
Text MD5: 2a28429fd97f76cab18ca88028cb14f0
Transpilation MD5: 8d1f3cac37406c540a0edef806e7af0b
Author: stefan
Category: eleu / nl
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-04-16 18:23:30
Source code size: 2242 bytes / 97 lines
Pitched / IR pitched: No / No
Views / Downloads: 610 / 683
Referenced in: [show references]