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

!752

static Map<S, S> types;

p {
  types = new PersistentMap("types");
}

synchronized answer { exceptionToUser {
  if (!tb()) null;
  
  if (match("* is a *", s, m) || match("* is an *", s, m)) {
    /*if (isQuoted(m.get(0)))*/ {
      S word = toLowerCase(m.unq(0));
      S type = toLowerCase(m.unq(1));
      types.put(word, type);
      ret "OK!";
    }
  }
  
  if (match("* is not a *", s, m) || match("* is not an *", s, m)) {
    S word = m.tlc(0);
    S type = m.tlc(1);
    if (eqic(types.get(word), type)) {
      types.remove(word);
      ret "Removed!";
    }
  }

  if (match("type *", s, m) || match("what is *", s, m)) {
    S word = m.tlc(0);
    S type = types.get(word);
    if (nempty(type))
      ret fixSentence(quote(word) + " is a " + type);
  }
  
  if (matchStartSimple("pattern", s, m)) {
    S input = m.rest();
    L<S> tok = /*dropPunctuation*/(nlTok(input));
    new L<S> l;
    for (int i = 1; i < l(tok); i += 2) {
      S word = tok.get(i);
      S type = getType(word);
      if (empty(type)) type = "?";
      l.add(type);
    }
    ret join(" ", l);
  }
  
  if "word types"
    ret structure(asList(asSet(values(types))));
    
  if "word type *"
    ret structure(keysForValue(types, m.tlc(0)));
}}

static S fixformat(S s, O... data) {
  ret fixSentence(format(s, data));
}

static S fixSentence(S s) {
  L<S> tok = nlTok(s);
  for (int i = 1; i+2 < l(tok); i += 2)
    if (eqic(tok.get(i), "a") || eqic(tok.get(i), "an"))
      tok.set(i, aOrAn(tok.get(i+2)));
  ret join(tok);
}

static S aOrAn(S s) {
  if (!empty(s)) {
    if (isVowel(s.charAt(0)))
      ret "an";
    if (eqic(s, "honest")) // etc
      ret "an";
  }
  ret "a";
}

static boolean isVowel(char c) {
  ret containsIgnoreCase("aeiou", c);
}

static S getType(S word) {
  if (isQuoted(word)) ret "quoted";
  if (isInteger(word)) ret "number";
  if (!isExtendedIdentifier(word)) ret "symbol";
  ret types.get(tlc(word));
}

static boolean matchStartSimple(S prefix, S s, Matches m) {
  s = s.trim();
  if (startsWithIgnoreCase(s, prefix + " ")) {
    m.m = new S[] { s.substring(l(prefix) + 1).trim() };
    ret true;
  }
  ret false;
}

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: 668 / 755
Referenced in: #3000380 - Answer for stefanreich (>> list sub bots)
#3000381 - Answer for speedforce (>> list sub bots)
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)