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

1  
!7
2  
3  
static Map<S, Set<S>> theSet;
4  
static new LinkedHashSet<Updatable> allObjects;
5  
static long changes;
6  
7  
sclass Updatable {
8  
  void update {}
9  
  
10  
  void setField(S field, O value) {
11  
    if (eq(get(this, field), value)) ret;
12  
    set(this, field, value);
13  
    change();
14  
  }
15  
}
16  
17  
sclass Expectation {
18  
  S ifClass;
19  
  Runnable action;
20  
  
21  
  *() {}
22  
  *(S *ifClass, Runnable *action) {}
23  
}
24  
25  
sclass Word extends Updatable {
26  
  S text; // or null if unknown
27  
  Word prev, next;
28  
  L<Word> constituents; // if group
29  
  new L<Expectation> expectations;
30  
  new L<Expectation> fulfilledExpectations;
31  
  new TreeSet<S> classes;
32  
  new LinkedHashSet<Word> groups; // I am part of
33  
  
34  
  void update {
35  
    // Add direct word classes
36  
    if (text != null)
37  
      if (classes.addAll(reverseLookupInMapToSets(theSet, text))) change();
38  
    
39  
    // Make text for group  
40  
    if (isGroup() && text == null) {
41  
      L<S> l = collect(constituents, 'text);
42  
      if (!anyNull(l)) setField(text := joinWithSpace(l));
43  
    }
44  
    
45  
    // Process expectations
46  
    for (Expectation e : cloneList(expectations)) {
47  
      print("Checking expected class " + e.ifClass);
48  
      if (classes.contains(e.ifClass)) {
49  
        moveElementFromCollectionToCollection(e, expectations, fulfilledExpectations);
50  
        change();
51  
        callF(e.action);
52  
      }
53  
    }
54  
  }
55  
  
56  
  bool isGroup() { ret constituents != null; }
57  
  
58  
  void addExpectation(Expectation e) {
59  
    print("addExpectation " + e);
60  
    expectations.add(e);
61  
    change();
62  
  }
63  
}
64  
65  
static Word makeGroup(Word a, Word b) {
66  
  print("makeGroup " + a.text + " / " + b.text);
67  
  L<Word> list = ll(a, b);
68  
  
69  
  // look for existing group
70  
  for (Word g : a.groups)
71  
    if (eq(g.constituents, list))
72  
      ret g;
73  
      
74  
  // new group
75  
  new Word g;
76  
  allObjects.add(g);
77  
  change();
78  
  g.constituents = list;
79  
  for (Word w : list)
80  
    w.groups.add(g);
81  
  ret g;
82  
}
83  
84  
sclass The extends Word {
85  
  bool expectationSentToNext;
86  
  
87  
  void update {
88  
    super.update();
89  
    if (next != null && !expectationSentToNext) {
90  
      final Word _next = next;
91  
      _next.addExpectation(Expectation("<noun>", r {
92  
        makeGroup(The.this, _next).classes.add("<noun>");
93  
      }));
94  
      set expectationSentToNext;
95  
    }
96  
  }
97  
}
98  
99  
p-exp {
100  
  S sentence = "In the movies Dracula always wears a cape";
101  
  L<S> rawWords = printStruct(words(sentence));
102  
  
103  
  theSet = ai_englishWordCategoriesWithElements();
104  
  
105  
  new L<Word> words;
106  
  for (S w : rawWords)
107  
    words.add(nu(eqic(w, "the") ? The : Word, text := w));
108  
  for (int i = 0; i < l(words)-1; i++)
109  
    linkWords(words.get(i), words.get(i+1));
110  
  //printStruct(first(words));
111  
  
112  
  addAll(allObjects, words);
113  
  long lastChanges;
114  
  do {
115  
    lastChanges = changes;
116  
    print(n2(changes, "change"));
117  
    for (Updatable w : cloneList(allObjects))
118  
      w.update();
119  
  } while (lastChanges != changes);
120  
  
121  
  //for (Word w : words) printStruct(cloneForPrinting(w));
122  
    
123  
  L<Word> groups = [Word w : instancesOf(Word, allObjects) | w.constituents != null];
124  
  print(n2(groups, "group"));
125  
  for (Word g : groups)
126  
    print("Group: " + sfu(cloneForPrinting(g)));
127  
}
128  
129  
svoid linkWords(Word a, Word b) {
130  
  a.next = b;
131  
  b.prev = a;
132  
}
133  
134  
static O cloneForPrinting(Word w) {
135  
  ret cloneWithoutFields(w, 'prev, 'next, 'constituents);
136  
}
137  
138  
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: 428 / 1078
Version history: 29 change(s)
Referenced in: [show references]