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

138
LINES

< > BotCompany Repo | #1017342 // Intelligent Words v1 [OK]

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

Download Jar. Uses 3874K of libraries. Click here for Pure Java version (9885L/71K).

!7

static Map<S, Set<S>> theSet;
static new LinkedHashSet<Updatable> allObjects;
static long changes;

sclass Updatable {
  void update {}
  
  void setField(S field, O value) {
    if (eq(get(this, field), value)) ret;
    set(this, field, value);
    change();
  }
}

sclass Expectation {
  S ifClass;
  Runnable action;
  
  *() {}
  *(S *ifClass, Runnable *action) {}
}

sclass Word extends Updatable {
  S text; // or null if unknown
  Word prev, next;
  L<Word> constituents; // if group
  new L<Expectation> expectations;
  new L<Expectation> fulfilledExpectations;
  new TreeSet<S> classes;
  new LinkedHashSet<Word> groups; // I am part of
  
  void update {
    // Add direct word classes
    if (text != null)
      if (classes.addAll(reverseLookupInMapToSets(theSet, text))) change();
    
    // Make text for group  
    if (isGroup() && text == null) {
      L<S> l = collect(constituents, 'text);
      if (!anyNull(l)) setField(text := joinWithSpace(l));
    }
    
    // Process expectations
    for (Expectation e : cloneList(expectations)) {
      print("Checking expected class " + e.ifClass);
      if (classes.contains(e.ifClass)) {
        moveElementFromCollectionToCollection(e, expectations, fulfilledExpectations);
        change();
        callF(e.action);
      }
    }
  }
  
  bool isGroup() { ret constituents != null; }
  
  void addExpectation(Expectation e) {
    print("addExpectation " + e);
    expectations.add(e);
    change();
  }
}

static Word makeGroup(Word a, Word b) {
  print("makeGroup " + a.text + " / " + b.text);
  L<Word> list = ll(a, b);
  
  // look for existing group
  for (Word g : a.groups)
    if (eq(g.constituents, list))
      ret g;
      
  // new group
  new Word g;
  allObjects.add(g);
  change();
  g.constituents = list;
  for (Word w : list)
    w.groups.add(g);
  ret g;
}

sclass The extends Word {
  bool expectationSentToNext;
  
  void update {
    super.update();
    if (next != null && !expectationSentToNext) {
      final Word _next = next;
      _next.addExpectation(Expectation("<noun>", r {
        makeGroup(The.this, _next).classes.add("<noun>");
      }));
      set expectationSentToNext;
    }
  }
}

p-exp {
  S sentence = "In the movies Dracula always wears a cape";
  L<S> rawWords = printStruct(words(sentence));
  
  theSet = ai_englishWordCategoriesWithElements();
  
  new L<Word> words;
  for (S w : rawWords)
    words.add(nu(eqic(w, "the") ? The : Word, text := w));
  for (int i = 0; i < l(words)-1; i++)
    linkWords(words.get(i), words.get(i+1));
  //printStruct(first(words));
  
  addAll(allObjects, words);
  long lastChanges;
  do {
    lastChanges = changes;
    print(n2(changes, "change"));
    for (Updatable w : cloneList(allObjects))
      w.update();
  } while (lastChanges != changes);
  
  //for (Word w : words) printStruct(cloneForPrinting(w));
    
  L<Word> groups = [Word w : instancesOf(Word, allObjects) | w.constituents != null];
  print(n2(groups, "group"));
  for (Word g : groups)
    print("Group: " + sfu(cloneForPrinting(g)));
}

svoid linkWords(Word a, Word b) {
  a.next = b;
  b.prev = a;
}

static O cloneForPrinting(Word w) {
  ret cloneWithoutFields(w, 'prev, 'next, 'constituents);
}

svoid change() { ++changes; }

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1017342
Snippet name: Intelligent Words v1 [OK]
Eternal ID of this version: #1017342/30
Text MD5: 0d9a6fb6045ab1a4cf572c53fad6970d
Transpilation MD5: 23e08e68818ed6be567900e82ced4bbb
Author: stefan
Category: javax / gui
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-07-17 22:05:55
Source code size: 3390 bytes / 138 lines
Pitched / IR pitched: No / No
Views / Downloads: 427 / 1077
Version history: 29 change(s)
Referenced in: [show references]