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

75
LINES

< > BotCompany Repo | #1002036 // Huge English Dictionary Bot

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

Libraryless. Click here for Pure Java version (6187L/43K/143K).

!7

static CondensedStrings2 words;

p {
  File zip = loadLibrary("#1002028"); // words.zip
  byte[] data = loadBinaryFromZip(zip, "words.txt");
  S text = fromUTF8(data);
  //print("UTF expansion: " + (l(data)-l(text))); // if 0, then strings can be extra-condensed.
  data = null;
  L<S> lines = toLines(text);
  //print("Sorted? " + yn(isSortedList(lines)));
  //print("Percentage sorted: " + formatDouble(percentageSorted(lines), 3));
  //print("Unsorted indexes: " + struct(unsortedIndexes(lines)));
  time { sort(lines); }
  //print("Unsorted indexes: " + struct(unsortedIndexes(lines)));
  text = null;
  words = new CondensedStrings2(lines);
  print(l(words) + " words loaded!");
  //printMemoryInfo();
  makeBot();
}

answer {
  if (match("random english words", s))
    ret randomWords(10);
  if (match("* random english words", s, m))
    ret randomWords(parseInt(m.get(0)));
  if (match("1 random english word", s) || match("random english word", s))
    ret randomWords(1);
  if (match("how many english words do you know?", s, m))
    ret format("I know * english words. Even funny ones such as ", words.size()) + quote(randomWord());
  if (match("is * an english word?", s, m))
    ret isWordQuick(m.unq(0)) ? " Yes." : "Not in my dictionary...";
  if "English words *-*" {
    int i = parseInt(m.unq(0));
    int j = parseInt(m.unq(1));
    ret structure(subList(words, i-1, min(i+50, j-1)));
  }
  if "number of characters in english dictionary"
    ret str(words.totalSize());
  if "number of different characters in english dictionary"
    ret str(countDifferentChars());
}

static int countDifferentChars() {
  new HashSet<Character> chars;
  for (S s : words)
    for (int i = 0; i < l(s); i++)
      chars.add(s.charAt(i));
  ret l(chars);
}

static S randomWord() {
  ret words.get(random(words.size()));
}

static S randomWords(int n) {
  n = min(n, 100);
  new L<S> l;
  for (int i = 0; i < n; i++)
    l.add(randomWord());
  ret join(" ", l) + (random(2) == 0 ? "?" : "!");
}

static bool isWord(S word) {
  ret words.contains(toLower(word));
}

// assumes list is sorted (which we did)
static bool isWordQuick(S word) {
  word = toLower(word);
  int i = Collections.binarySearch(words, word);
  ret i >= 0;
}

Author comment

Began life as a copy of #1002030

download  show line numbers  debug dex  old transpilations   

Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1002036
Snippet name: Huge English Dictionary Bot
Eternal ID of this version: #1002036/5
Text MD5: b7a3a86c5cac3c707232cc111019983d
Transpilation MD5: f3020440daa182822668ef3180f0abbc
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-04-07 14:59:29
Source code size: 2309 bytes / 75 lines
Pitched / IR pitched: No / No
Views / Downloads: 734 / 2466
Version history: 4 change(s)
Referenced in: [show references]