2525import network .aika .neuron .activation .Activation .Link ;
2626import network .aika .neuron .activation .Candidate ;
2727import network .aika .neuron .range .Position ;
28- import network .aika .neuron .range .Range ;
2928import network .aika .neuron .activation .SearchNode ;
3029import network .aika .lattice .Node .ThreadState ;
3130import network .aika .neuron .activation .*;
@@ -88,22 +87,25 @@ public class Document implements Comparable<Document> {
8887 public TreeMap <Integer , Double > searchNodeWeights = new TreeMap <>();
8988
9089
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 );
9394 if (r != 0 ) return r ;
9495 r = ak1 .node .compareTo (ak2 .node );
9596 if (r != 0 ) return r ;
9697 return Integer .compare (ak1 .actId , ak2 .actId );
9798 });
9899
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 );
101102 if (r != 0 ) return r ;
102103 r = ak1 .node .compareTo (ak2 .node );
103104 if (r != 0 ) return r ;
104105 return Integer .compare (ak1 .actId , ak2 .actId );
105106 });
106107
108+
107109 public TreeMap <Integer , Activation > activationsById = new TreeMap <>();
108110
109111
@@ -112,12 +114,14 @@ public class Document implements Comparable<Document> {
112114
113115
114116 public static class ActKey {
115- Range range ;
117+ int slot ;
118+ Position pos ;
116119 Node node ;
117120 int actId ;
118121
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 ;
121125 this .node = node ;
122126 this .actId = actId ;
123127 }
@@ -134,7 +138,7 @@ public ActKey(Range range, Node node, int actId) {
134138
135139
136140 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 ) );
138142 if (r != 0 ) return r ;
139143 r = act1 .node .compareTo (act2 .node );
140144 if (r != 0 ) return r ;
@@ -188,11 +192,16 @@ public Position lookupFinalPosition(int pos) {
188192 }
189193
190194
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 ) {
193202 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 ()))
196205 );
197206 } else {
198207 return "" ;
@@ -201,12 +210,13 @@ public String getText(Range r) {
201210
202211
203212 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+ }
210220 }
211221 activationsById .put (act .id , act );
212222 }
@@ -225,21 +235,21 @@ public Collection<Activation> getActivations(boolean onlyFinal) {
225235 }
226236
227237
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 ),
231241 fromInclusive ,
232- new Document .ActKey (toKey , Node .MAX_NODE , Integer .MAX_VALUE ),
242+ new Document .ActKey (toSlot , toPos , Node .MAX_NODE , Integer .MAX_VALUE ),
233243 toInclusive
234244 ).values ();
235245 }
236246
237247
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 ),
241251 fromInclusive ,
242- new Document .ActKey (toKey , Node .MAX_NODE , Integer .MAX_VALUE ),
252+ new Document .ActKey (- 1 , toPos , Node .MAX_NODE , Integer .MAX_VALUE ),
243253 toInclusive
244254 ).values ();
245255 }
@@ -452,26 +462,26 @@ public String generateOutputText() {
452462
453463 TreeSet <Position > queue = new TreeSet <>(Comparator .comparingInt (p -> p .id ));
454464
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 ) );
458468 }
459469 }
460470
461471 while (!queue .isEmpty ()) {
462472 Position pos = queue .pollFirst ();
463473
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 ());
469480
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 );
471482
472- queue .add (nextPos );
473- }
474- }
483+ queue .add (nextPos );
484+ });
475485 }
476486 return content .substring (oldLength , length ());
477487 }
@@ -670,10 +680,10 @@ public double process(SearchNode sn) {
670680
671681 public void dumpOscillatingActivations () {
672682 activatedNeurons .stream ()
673- .flatMap (n -> n .getActivations (this , false ). stream () )
683+ .flatMap (n -> n .getActivations (this , false ))
674684 .filter (act -> act .rounds .getLastRound () != null && act .rounds .getLastRound () > MAX_ROUND - 5 )
675685 .forEach (act -> {
676- log .error (act .id + " " + act .range + " " + act .decision + " " + act .rounds );
686+ log .error (act .id + " " + act .slotsToString () + " " + act .decision + " " + act .rounds );
677687 log .error (act .linksToString ());
678688 log .error ("" );
679689 });
0 commit comments