Warning: session_start(): open(/var/lib/php/sessions/sess_us3mehi7lfjvb8ogbfvp3bfvlf, O_RDWR) failed: No space left on device (28) in /var/www/tb-usercake/models/config.php on line 51
Warning: session_start(): Failed to read session data: files (path: /var/lib/php/sessions) in /var/www/tb-usercake/models/config.php on line 51
!7
static Map> theSet;
static new LinkedHashSet allObjects;
static long changes;
static new MultiMap> groupingsByA;
static new MultiMap subClasses;
static S groupings = [[
+ =
+ =
+ =
=
+ =
+ =
+ =
]];
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
new LinkedHashSet prev;
new LinkedHashSet next;
L constituents; // if group
new L expectations;
new L fulfilledExpectations;
new TreeSet classes;
int classesConvertedToTraits;
new LinkedHashSet groups; // I am part of
new L traits;
void update {
// Add direct word classes
if (text != null)
for (S c : reverseLookupInMapToSets(theSet, text))
addClass(c);
// Make text for group
if (isGroup() && text == null) {
L 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);
}
}
if (l(classes) > classesConvertedToTraits) {
for (fS c : dropFirst(classesConvertedToTraits, classes))
addTraitsForClass(c);
classesConvertedToTraits = l(classes);
}
for (Trait t : iterateListConcurrently(traits))
t.update();
}
bool isGroup() { ret constituents != null; }
void addClass(S c) {
if (!classes.add(c)) ret;
change();
long n;
do {
n = changes;
// optimizable
for (S d : iterateListConcurrently(asList(classes)))
for (S e : subClasses.get(d))
if (classes.add(e)) change();
} while (changes != n);
}
void addExpectation(Expectation e) {
print("addExpectation " + e);
expectations.add(e);
change();
}
void addTraitsForClass(S c) {
for (PairS p : groupingsByA.get(c))
addTrait(LinkWithTo(p.a, p.b));
}
void addTrait(Trait t) {
set(t, w := this);
traits.add(t);
}
}
static Word makeGroup(Word a, Word b) {
print("makeGroup " + a.text + " / " + b.text);
L 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);
for (Word prev : a.prev) prev.next.add(g);
for (Word next : b.next) next.prev.add(g);
ret g;
}
sclass Trait extends Updatable {
Word w;
}
sclass LinkWithTo extends Trait {
S linkWith, linkTo; // classes
int expectationsSentToNext;
*() {}
*(S *linkWith, S *linkTo) {}
void update {
if (l(w.next) > expectationsSentToNext) {
for (final Word next : dropFirst(expectationsSentToNext, w.next))
next.addExpectation(Expectation(linkWith, r {
makeGroup(w, next).addClass(linkTo)
}));
expectationsSentToNext = l(w.next);
}
}
}
p-exp {
S sentence = "In the movies Dracula always wears a cape";
L rawWords = printStruct(words(sentence));
theSet = ai_englishWordCategoriesWithElements();
parseGroupings();
new L words;
for (S w : rawWords)
words.add(nu(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));
for (Word w : words) print(" " + textAndClasses(w));
print();
L groups = [Word w : instancesOf(Word, allObjects) | w.constituents != null];
print();
print(n2(groups, "group"));
for (Word g : groups)
print("Group: " + textAndClasses(g));
}
sS textAndClasses(Word w) {
ret w.text + " (" + joinWithComma(w.classes) + ")";
}
svoid linkWords(Word a, Word b) {
a.next.add(b);
b.prev.add(a);
}
static O cloneForPrinting(Word w) {
ret cloneWithoutFields(w, 'prev, 'next, 'constituents);
}
svoid change() { ++changes; }
svoid parseGroupings() {
for (S s : tlft(groupings)) {
L tok = javaTokWithAngleBracketsC(s);
if (l(tok) == 5)
groupingsByA.put(tok.get(0), pair(tok.get(2), tok.get(4)));
else if (l(tok) == 3)
subClasses.put(tok.get(0), tok.get(2));
}
}