25
25
import network .aika .neuron .activation .Activation .Link ;
26
26
import network .aika .neuron .activation .Candidate ;
27
27
import network .aika .neuron .range .Position ;
28
- import network .aika .neuron .range .Range ;
29
28
import network .aika .neuron .activation .SearchNode ;
30
29
import network .aika .lattice .Node .ThreadState ;
31
30
import network .aika .neuron .activation .*;
@@ -88,22 +87,25 @@ public class Document implements Comparable<Document> {
88
87
public TreeMap <Integer , Double > searchNodeWeights = new TreeMap <>();
89
88
90
89
91
- private TreeMap <ActKey , Activation > activationsByRangeBegin = new TreeMap <>((ak1 , ak2 ) -> {
92
- int r = Position .compare (ak1 .range .begin , ak2 .range .begin );
90
+ private TreeMap <ActKey , Activation > activationsBySlotAndPosition = new TreeMap <>((ak1 , ak2 ) -> {
91
+ int r = Integer .compare (ak1 .slot , ak2 .slot );
92
+ if (r != 0 ) return r ;
93
+ r = Position .compare (ak1 .pos , ak2 .pos );
93
94
if (r != 0 ) return r ;
94
95
r = ak1 .node .compareTo (ak2 .node );
95
96
if (r != 0 ) return r ;
96
97
return Integer .compare (ak1 .actId , ak2 .actId );
97
98
});
98
99
99
- private TreeMap <ActKey , Activation > activationsByRangeEnd = new TreeMap <>((ak1 , ak2 ) -> {
100
- int r = Position .compare (ak1 .range . end , ak2 .range . end );
100
+ private TreeMap <ActKey , Activation > activationsByPosition = new TreeMap <>((ak1 , ak2 ) -> {
101
+ int r = Position .compare (ak1 .pos , ak2 .pos );
101
102
if (r != 0 ) return r ;
102
103
r = ak1 .node .compareTo (ak2 .node );
103
104
if (r != 0 ) return r ;
104
105
return Integer .compare (ak1 .actId , ak2 .actId );
105
106
});
106
107
108
+
107
109
public TreeMap <Integer , Activation > activationsById = new TreeMap <>();
108
110
109
111
@@ -112,12 +114,14 @@ public class Document implements Comparable<Document> {
112
114
113
115
114
116
public static class ActKey {
115
- Range range ;
117
+ int slot ;
118
+ Position pos ;
116
119
Node node ;
117
120
int actId ;
118
121
119
- public ActKey (Range range , Node node , int actId ) {
120
- this .range = range ;
122
+ public ActKey (int slot , Position pos , Node node , int actId ) {
123
+ this .slot = slot ;
124
+ this .pos = pos ;
121
125
this .node = node ;
122
126
this .actId = actId ;
123
127
}
@@ -134,7 +138,7 @@ public ActKey(Range range, Node node, int actId) {
134
138
135
139
136
140
public static Comparator <Activation > ACTIVATIONS_OUTPUT_COMPARATOR = (act1 , act2 ) -> {
137
- int r = Range .compare (act1 .range , act2 .range , false );
141
+ int r = Position .compare (act1 .getSlot ( Activation . BEGIN ) , act2 .getSlot ( Activation . BEGIN ) );
138
142
if (r != 0 ) return r ;
139
143
r = act1 .node .compareTo (act2 .node );
140
144
if (r != 0 ) return r ;
@@ -188,11 +192,16 @@ public Position lookupFinalPosition(int pos) {
188
192
}
189
193
190
194
191
- public String getText (Range r ) {
192
- if (r .begin .getFinalPosition () != null && r .end .getFinalPosition () != null ) {
195
+ public String getText (Position begin , Position end ) {
196
+ return getText (begin .getFinalPosition (), end .getFinalPosition ());
197
+ }
198
+
199
+
200
+ public String getText (Integer begin , Integer end ) {
201
+ if (begin != null && end != null ) {
193
202
return content .substring (
194
- Math .max (0 , Math .min (r . begin . getFinalPosition () , length ())),
195
- Math .max (0 , Math .min (r . end . getFinalPosition () , length ()))
203
+ Math .max (0 , Math .min (begin , length ())),
204
+ Math .max (0 , Math .min (end , length ()))
196
205
);
197
206
} else {
198
207
return "" ;
@@ -201,12 +210,13 @@ public String getText(Range r) {
201
210
202
211
203
212
public void addActivation (Activation act ) {
204
- ActKey dak = new ActKey (act .range , act .node , act .id );
205
- if (act .range .begin != null && act .range .begin .getFinalPosition () != null ) {
206
- activationsByRangeBegin .put (dak , act );
207
- }
208
- if (act .range .end != null && act .range .end .getFinalPosition () != null ) {
209
- activationsByRangeEnd .put (dak , act );
213
+ for (Map .Entry <Integer , Position > me : act .slots .entrySet ()) {
214
+ Position pos = me .getValue ();
215
+ if (pos != null && pos .getFinalPosition () != null ) {
216
+ ActKey dak = new ActKey (me .getKey (), pos , act .node , act .id );
217
+ activationsBySlotAndPosition .put (dak , act );
218
+ activationsByPosition .put (dak , act );
219
+ }
210
220
}
211
221
activationsById .put (act .id , act );
212
222
}
@@ -225,21 +235,21 @@ public Collection<Activation> getActivations(boolean onlyFinal) {
225
235
}
226
236
227
237
228
- public Collection <Activation > getActivationsByRangeBegin ( Range fromKey , boolean fromInclusive , Range toKey , boolean toInclusive ) {
229
- return activationsByRangeBegin .subMap (
230
- new Document .ActKey (fromKey , Node .MIN_NODE , Integer .MIN_VALUE ),
238
+ public Collection <Activation > getActivationsByPosition ( int fromSlot , Position fromPos , boolean fromInclusive , int toSlot , Position toPos , boolean toInclusive ) {
239
+ return activationsBySlotAndPosition .subMap (
240
+ new Document .ActKey (fromSlot , fromPos , Node .MIN_NODE , Integer .MIN_VALUE ),
231
241
fromInclusive ,
232
- new Document .ActKey (toKey , Node .MAX_NODE , Integer .MAX_VALUE ),
242
+ new Document .ActKey (toSlot , toPos , Node .MAX_NODE , Integer .MAX_VALUE ),
233
243
toInclusive
234
244
).values ();
235
245
}
236
246
237
247
238
- public Collection <Activation > getActivationByRangeEnd ( Range fromKey , boolean fromInclusive , Range toKey , boolean toInclusive ) {
239
- return activationsByRangeEnd .subMap (
240
- new Document .ActKey (fromKey , Node .MIN_NODE , Integer .MIN_VALUE ),
248
+ public Collection <Activation > getActivationsByPosition ( Position fromPos , boolean fromInclusive , Position toPos , boolean toInclusive ) {
249
+ return activationsByPosition .subMap (
250
+ new Document .ActKey (- 1 , fromPos , Node .MIN_NODE , Integer .MIN_VALUE ),
241
251
fromInclusive ,
242
- new Document .ActKey (toKey , Node .MAX_NODE , Integer .MAX_VALUE ),
252
+ new Document .ActKey (- 1 , toPos , Node .MAX_NODE , Integer .MAX_VALUE ),
243
253
toInclusive
244
254
).values ();
245
255
}
@@ -452,26 +462,26 @@ public String generateOutputText() {
452
462
453
463
TreeSet <Position > queue = new TreeSet <>(Comparator .comparingInt (p -> p .id ));
454
464
455
- for (Activation act : activationsByRangeBegin .values ()) {
456
- if (act .range . begin . getFinalPosition () != null && act .range . end .getFinalPosition () == null ) {
457
- queue .add (act .range . begin );
465
+ for (Activation act : activationsById .values ()) {
466
+ if (act .getINeuron (). getOutputText () != null && act . getSlot ( Activation . BEGIN ). getFinalPosition () != null && act .getSlot ( Activation . END ) .getFinalPosition () == null ) {
467
+ queue .add (act .getSlot ( Activation . BEGIN ) );
458
468
}
459
469
}
460
470
461
471
while (!queue .isEmpty ()) {
462
472
Position pos = queue .pollFirst ();
463
473
464
- for (Activation act : pos .beginActivations ) {
465
- if (act .getINeuron ().outputText != null && act .isFinalActivation ()) {
466
- String outText = act .getINeuron ().outputText ;
467
- Position nextPos = act .range .end ;
468
- nextPos .setFinalPosition (pos .getFinalPosition () + outText .length ());
474
+ pos .getActivations (Activation .BEGIN )
475
+ .filter (act -> act .getINeuron ().getOutputText () != null && act .isFinalActivation ())
476
+ .forEach (act -> {
477
+ String outText = act .getINeuron ().getOutputText ();
478
+ Position nextPos = act .getSlot (Activation .END );
479
+ nextPos .setFinalPosition (pos .getFinalPosition () + outText .length ());
469
480
470
- content .replace (act .range . begin .getFinalPosition (), act .range . end .getFinalPosition (), outText );
481
+ content .replace (act .getSlot ( Activation . BEGIN ) .getFinalPosition (), act .getSlot ( Activation . END ) .getFinalPosition (), outText );
471
482
472
- queue .add (nextPos );
473
- }
474
- }
483
+ queue .add (nextPos );
484
+ });
475
485
}
476
486
return content .substring (oldLength , length ());
477
487
}
@@ -670,10 +680,10 @@ public double process(SearchNode sn) {
670
680
671
681
public void dumpOscillatingActivations () {
672
682
activatedNeurons .stream ()
673
- .flatMap (n -> n .getActivations (this , false ). stream () )
683
+ .flatMap (n -> n .getActivations (this , false ))
674
684
.filter (act -> act .rounds .getLastRound () != null && act .rounds .getLastRound () > MAX_ROUND - 5 )
675
685
.forEach (act -> {
676
- log .error (act .id + " " + act .range + " " + act .decision + " " + act .rounds );
686
+ log .error (act .id + " " + act .slotsToString () + " " + act .decision + " " + act .rounds );
677
687
log .error (act .linksToString ());
678
688
log .error ("" );
679
689
});
0 commit comments