Usage

Maven POM

The following dependency includes the Aika software library to an existing maven project.

<dependency>
    <groupId>org.aika-software</groupId>
    <artifactId>aika</artifactId>
    <version>1.4.0</version>
</dependency>

Named Entity Recognition / Entity Resolution example

This example demonstrates the recognition of the words 'jackson cook' as forename and surname. If these words would have occurred independently, they would have been recognized as city or profession based on the example code. The complete code for this example can be found in the class NamedEntityRecognitionTest.
Now, the first step is to create an empty model. The following map is used as a dictionary to look up the individual word input neurons.

				
    Model m = new Model();

    HashMap<String, Neuron> inputNeurons = new HashMap<>();

    for(String word: new String[] {"jackson", "cook"}) {
        Neuron in = m.createNeuron("W-" + word, INPUT);

        inputNeurons.put(word, in);
    }
                
			

These three neurons are just placeholders that will be wired up later on.

				
    Neuron forenameCategory = m.createNeuron("C-forename", INHIBITORY, LIMITED_RECTIFIED_LINEAR_UNIT);
    Neuron surnameCategory = m.createNeuron("C-surname", INHIBITORY, LIMITED_RECTIFIED_LINEAR_UNIT);
    Neuron inhibitingN = m.createNeuron("INHIB", INHIBITORY, LIMITED_RECTIFIED_LINEAR_UNIT);
                
			

The following code snippet is the crucial section of this example. It shows how the first entity neuron is created and connected by its input synapses. The entity neurons represent the concrete meanings of the input words. As can be seen here the individual inputs have a few more properties than we are used to from other neural networks. The reason for this is that Aika not only propagates the input activation value to the next neuron but also the text range, the word position.
This entity neuron has three inputs. The first one simply checks if the word 'cook' occurred in the text. The second one is a positive feedback loop and checks if the previous word was a forename. The last one is a negative feedback loop. It makes sure that only one meaning is assigned to each word.
The other entity neurons are created in a similar fashion which is therefore not shown here.

				
    Neuron cookSurnameEntity = Neuron.init(
            m.createNeuron("E-cook (surname)", EXCITATORY, RECTIFIED_HYPERBOLIC_TANGENT),
            6.0, // adjusts the bias
            new Synapse.Builder() // Requires the word to be recognized
                    .setSynapseId(0)
                    .setNeuron(inputNeurons.get("cook"))
                    .setWeight(10.0)
                    .setRecurrent(false),
            new Synapse.Builder() // The previous word needs to be a forename
                    .setSynapseId(1)
                    .setNeuron(forenameCategory)
                    .setWeight(10.0)
                    .setRecurrent(true), // this input is a positive feedback loop

                    // This neuron may be suppressed by the E-cook (profession) neuron, but there is no
                    // self suppression taking place even though 'E-cook (surname)' is also contained
                    // in the inhibitingN.
            new Synapse.Builder()
                    .setSynapseId(2)
                    .setNeuron(inhibitingN)
                    .setWeight(-100.0)
                    .setRecurrent(true), // this input is a negative feedback loop
            new Relation.Builder()  // references the previous word
                    .setFrom(1)  // From Synpase Id
                    .setTo(0)    // To Synapse Id
                    .setRelation(END_TO_BEGIN_EQUALS),
            new Relation.Builder()
                    .setFrom(2)
                    .setTo(0)
                    .setRelation(OVERLAPS),
            new Relation.Builder()
                    .setFrom(0)
                    .setTo(OUTPUT)
                    .setRelation(EQUALS)
    );
                
			

The category neurons in this example only have input. For a real application there would be a long list of inputs.

				
    Neuron.init(
            forenameCategory,
            0.0,
            new Synapse.Builder() // In this example there is only one forename considered.
                    .setSynapseId(0)
                    .setNeuron(jacksonForenameEntity)
                    .setWeight(1.0),
            new Relation.Builder()
                    .setFrom(0)
                    .setTo(OUTPUT)
                    .setRelation(EQUALS)
    );
    Neuron.init(
            surnameCategory,
            0.0,
            new Synapse.Builder()
                    .setSynapseId(0)
                    .setNeuron(cookSurnameEntity)
                    .setWeight(1.0),
            new Relation.Builder()
                    .setFrom(0)
                    .setTo(OUTPUT)
                    .setRelation(EQUALS)
    );
                
			

The last neuron is the suppressing neuron. It is used as a mediator in order to avoid having to connect all of these neurons with each other. Self suppression does not take place.

				
    Neuron.init(
            inhibitingN,
            0.0,
            new Synapse.Builder().setNeuron(cookProfessionEntity)
                    .setSynapseId(0)
                    .setWeight(1.0),
            new Synapse.Builder()
                    .setSynapseId(1)
                    .setNeuron(cookSurnameEntity)
                    .setWeight(1.0),
            new Synapse.Builder()
                    .setSynapseId(2)
                    .setNeuron(jacksonCityEntity)
                    .setWeight(1.0),
            new Synapse.Builder()
                    .setSynapseId(3)
                    .setNeuron(jacksonForenameEntity)
                    .setWeight(1.0),
            new Relation.Builder()
                    .setFrom(0)
                    .setTo(OUTPUT)
                    .setRelation(EQUALS),
            new Relation.Builder()
                    .setFrom(1)
                    .setTo(OUTPUT)
                    .setRelation(EQUALS),
            new Relation.Builder()
                    .setFrom(2)
                    .setTo(OUTPUT)
                    .setRelation(EQUALS),
            new Relation.Builder()
                    .setFrom(3)
                    .setTo(OUTPUT)
                    .setRelation(EQUALS)
        );
                
			

Now that the model is complete, we can start processing an actual text.

				
    Document doc = m.createDocument("mr. jackson cook was born in new york ");

    int i = 0;
    for(String w: doc.getContent().split(" ")) {
        int j = i + w.length();

        // Feed the individual words as inputs into the network.
        inputNeurons.get(w).addInput(doc, i, j);
        i = j + 1;
    }

    // Search for the best interpretation of this text.
    doc.process();

    System.out.println(doc.activationsToString());

    doc.clearActivations();
                
			

The output of this example looks as following. Each line in the table represents a neuron activation. In this example, slots simply specify the range begin (slot 0) and the range end (slot 1).

				

Activation ID - Neuron Type - Final Decision - Slots (Ranges) | Identity - Neuron Label - Logic Layer - Upper Bound -
Value | Sum | Weight - Input Value | Target Value

0   INPUT      -          - (0:0, 1:4) "mr. " () - W-mr. - V:1.0 UB:1.0 Net:0.0 W:0.0 - IV:1.0
4   INHIBITORY -          - (0:4, 1:12) "jackson " () - C-forename - V:1.0 UB:1.0 Net:1.0 W:0.0
5   INHIBITORY -          - (0:4, 1:12) "jackson " () - INHIB - V:1.0 UB:1.0 Net:1.0 W:0.0
1   INPUT      -          - (0:4, 1:12) "jackson " () - W-jackson - V:1.0 UB:1.0 Net:0.0 W:0.0 - IV:1.0
2   EXCITATORY - SELECTED - (0:4, 1:12) "jackson " () - E-jackson (forename) - V:1.0 UB:1.0 Net:6.0 W:6.0
3   EXCITATORY - EXCLUDED - (0:4, 1:12) "jackson " () - E-jackson (city) - V:0.0 UB:0.0 Net:-95.0 W:0.0
9   INHIBITORY -          - (0:12, 1:17) "cook " () - C-surname - V:1.0 UB:1.0 Net:1.0 W:0.0
10  INHIBITORY -          - (0:12, 1:17) "cook " () - INHIB - V:1.0 UB:1.0 Net:1.0 W:0.0
6   INPUT      -          - (0:12, 1:17) "cook " () - W-cook - V:1.0 UB:1.0 Net:0.0 W:0.0 - IV:1.0
7   EXCITATORY - SELECTED - (0:12, 1:17) "cook " () - E-cook (surname) - V:1.0 UB:1.0 Net:6.0 W:6.0
8   EXCITATORY - EXCLUDED - (0:12, 1:17) "cook " () - E-cook (profession) - V:0.0 UB:0.0 Net:-95.0 W:0.0
11  INPUT      -          - (0:17, 1:21) "was " () - W-was - V:1.0 UB:1.0 Net:0.0 W:0.0 - IV:1.0
12  INPUT      -          - (0:21, 1:26) "born " () - W-born - V:1.0 UB:1.0 Net:0.0 W:0.0 - IV:1.0
13  INPUT      -          - (0:26, 1:29) "in " () - W-in - V:1.0 UB:1.0 Net:0.0 W:0.0 - IV:1.0
14  INPUT      -          - (0:29, 1:33) "new " () - W-new - V:1.0 UB:1.0 Net:0.0 W:0.0 - IV:1.0
15  INPUT      -          - (0:33, 1:38) "york " () - W-york - V:1.0 UB:1.0 Net:0.0 W:0.0 - IV:1.0

 Final SearchNode:6  WeightSum:12.0
                
			


Mutual exclusion example

				
    @Test
    public void testMutualExclusion() {
        Model m = new Model();

        // Create the input neurons for the network.
        Neuron inA = m.createNeuron("IN-A", INPUT);
        Neuron inB = m.createNeuron("IN-B", INPUT);
        Neuron inC = m.createNeuron("IN-C", INPUT);

        // Instantiate the inhibitory neuron. Its inputs will be added later on.
        Neuron inhibN = m.createNeuron("INHIB");

        // Create three neurons that might be suppressed by the inhibitory neuron.
        Neuron pA = Neuron.init(
                m.createNeuron("A", EXCITATORY),
                3.0,
                new Synapse.Builder()
                        .setSynapseId(0)
                        .setNeuron(inA)
                        .setWeight(10.0)
                        .setRecurrent(false),
                new Synapse.Builder()
                        .setSynapseId(1)
                        .setNeuron(inhibN)
                        .setWeight(-100.0)
                        .setRecurrent(true),
                new Relation.Builder()
                        .setFrom(1)
                        .setTo(0)
                        .setRelation(EQUALS),
                new Relation.Builder()
                        .setFrom(0)
                        .setTo(OUTPUT)
                        .setRelation(EQUALS)
        );

        Neuron pB = Neuron.init(
                m.createNeuron("B", EXCITATORY),
                5.0,
                new Synapse.Builder()
                        .setSynapseId(0)
                        .setNeuron(inB)
                        .setWeight(10.0)
                        .setRecurrent(false),
                new Synapse.Builder()
                        .setSynapseId(1)
                        .setNeuron(inhibN)
                        .setWeight(-100.0)
                        .setRecurrent(true),
                new Relation.Builder()
                        .setFrom(1)
                        .setTo(0)
                        .setRelation(EQUALS),
                new Relation.Builder()
                        .setFrom(0)
                        .setTo(OUTPUT)
                        .setRelation(EQUALS)
        );

        Neuron pC = Neuron.init(
                m.createNeuron("C", EXCITATORY),
                2.0,
                new Synapse.Builder()
                        .setSynapseId(0)
                        .setNeuron(inC)
                        .setWeight(10.0)
                        .setRecurrent(false),
                new Synapse.Builder()
                        .setSynapseId(1)
                        .setNeuron(inhibN)
                        .setWeight(-100.0)
                        .setRecurrent(true),
                new Relation.Builder()
                        .setFrom(1)
                        .setTo(0)
                        .setRelation(EQUALS),
                new Relation.Builder()
                        .setFrom(0)
                        .setTo(OUTPUT)
                        .setRelation(EQUALS)
        );

        // Finally addInput all the inputs to the suppressing neuron.
        Neuron.init(
                inhibN,
                0.0,
                new Synapse.Builder()
                        .setSynapseId(0)
                        .setNeuron(pA)
                        .setWeight(1.0)
                        .setRecurrent(false),
                new Synapse.Builder()
                        .setSynapseId(1)
                        .setNeuron(pB)
                        .setWeight(1.0)
                        .setRecurrent(false),
                new Synapse.Builder()
                        .setSynapseId(2)
                        .setNeuron(pC)
                        .setWeight(1.0)
                        .setRecurrent(false),
                new Relation.Builder()
                        .setFrom(0)
                        .setTo(OUTPUT)
                        .setRelation(EQUALS),
                new Relation.Builder()
                        .setFrom(1)
                        .setTo(OUTPUT)
                        .setRelation(EQUALS),
                new Relation.Builder()
                        .setFrom(2)
                        .setTo(OUTPUT)
                        .setRelation(EQUALS)
        );

        Neuron outN = Neuron.init(
                m.createNeuron("OUT", EXCITATORY),
                0.0,
                new Synapse.Builder()
                         .setSynapseId(0)
                        .setNeuron(pB)
                        .setWeight(1.0)
                        .setRecurrent(false),
                new Relation.Builder()
                        .setFrom(0)
                        .setTo(OUTPUT)
                        .setRelation(EQUALS)
        );

        // Now that the model is complete, apply it to a document.

        Document doc = m.createDocument("foobar", 0);

        // Add input activations starting at char 0 and ending at char 1
        // These inputs will be immediately propagated through the network.
        inA.addInput(doc, 0, 1);
        inB.addInput(doc, 0, 1);
        inC.addInput(doc, 0, 1);

        // Computes the best interpretation
        doc.process();

        System.out.println(doc.activationsToString());

        Assert.assertEquals(0, pA.getActivations(doc, true).count());
        Assert.assertEquals(1, pB.getActivations(doc, true).count());
        Assert.assertEquals(0, pC.getActivations(doc, true).count());

        Assert.assertEquals(1, outN.getActivations(doc, true).count());
        doc.clearActivations();
    }
                
			
				
 Activation ID - Neuron Type - Final Decision - Slots | Identity - Neuron Label - Upper Bound -
 Value | Net | Weight - Input Value | Target Value

0   INPUT      -          - (0:0, 1:1) "f" () - IN-A - V:1.0 UB:1.0 Net:0.0 W:0.0 - IV:1.0
3   INPUT      -          - (0:0, 1:1) "f" () - IN-B - V:1.0 UB:1.0 Net:0.0 W:0.0 - IV:1.0
6   INPUT      -          - (0:0, 1:1) "f" () - IN-C - V:1.0 UB:1.0 Net:0.0 W:0.0 - IV:1.0
2   INHIBITORY -          - (0:0, 1:1) "f" () - INHIB - V:1.0 UB:1.0 Net:1.0 W:0.0
1   EXCITATORY - EXCLUDED - (0:0, 1:1) "f" () - A - V:0.0 UB:0.0 Net:-97.0 W:0.0
4   EXCITATORY - SELECTED - (0:0, 1:1) "f" () - B - V:1.0 UB:1.0 Net:5.0 W:5.0
7   EXCITATORY - EXCLUDED - (0:0, 1:1) "f" () - C - V:0.0 UB:0.0 Net:-98.0 W:0.0
5   INHIBITORY -          - (0:0, 1:1) "f" () - OUT - V:0.762 UB:0.762 Net:1.0 W:0.0

Final SearchNode:10  WeightSum:5.0
				
			


Pattern matching example

				
    @Test
    public void testPatternMatching() {
        Model m = new Model();

        Map<Character, Neuron> inputNeurons = new HashMap<>();

        // Create an input neuron for every letter in this example.
        for(char c: new char[] {'a', 'b', 'c', 'd', 'e', 'f'}) {
            Neuron in = m.createNeuron(c + "", INPUT);

            inputNeurons.put(c, in);
        }

        Neuron pattern = Neuron.init(
                m.createNeuron("BCDE", EXCITATORY),
                5.0,
                new Synapse.Builder()
                        .setSynapseId(0)
                        .setNeuron(inputNeurons.get('b'))
                        .setWeight(10.0)
                        .setRecurrent(false),
                new Synapse.Builder()
                        .setSynapseId(1)
                        .setNeuron(inputNeurons.get('c'))
                        .setWeight(10.0)
                        .setRecurrent(false),
                new Synapse.Builder()
                        .setSynapseId(2)
                        .setNeuron(inputNeurons.get('d'))
                        .setWeight(10.0)
                        .setRecurrent(false),
                new Synapse.Builder()
                        .setSynapseId(3)
                        .setNeuron(inputNeurons.get('e'))
                        .setWeight(10.0)
                        .setRecurrent(false),
                new Relation.Builder()
                        .setFrom(0)
                        .setTo(1)
                        .setRelation(END_TO_BEGIN_EQUALS),
                new Relation.Builder()
                        .setFrom(1)
                        .setTo(2)
                        .setRelation(END_TO_BEGIN_EQUALS),
                new Relation.Builder()
                        .setFrom(2)
                        .setTo(3)
                        .setRelation(END_TO_BEGIN_EQUALS),
                new Relation.Builder()
                        .setFrom(0)
                        .setTo(OUTPUT)
                        .setRelation(BEGIN_EQUALS),
                new Relation.Builder()
                        .setFrom(3)
                        .setTo(OUTPUT)
                        .setRelation(END_EQUALS)
        );


        // Create a simple text document.
        Document doc = m.createDocument("a b c d e ");

        // Then add the characters
        for(int i = 0; i < doc.length(); i++) {
            char c = doc.getContent().charAt(i);
            if(c != ' ') {
                inputNeurons.get(c).addInput(doc, i, i + 2);
            }
        }

        // Computes the best interpretation
        doc.process();

        Assert.assertEquals(1, pattern.getActivations(doc, false).count());


        System.out.println("All activations:");
        System.out.println(doc.activationsToString());
        System.out.println();

        doc.clearActivations();
    }
				
			
				
Activation ID - Neuron Type - Final Decision - Slots | Identity - Neuron Label - Upper Bound -
Value | Net | Weight - Input Value | Target Value

0   INPUT      -          - (0:0, 1:2) "a " () - a - V:1.0 UB:1.0 Net:0.0 W:0.0 - IV:1.0
1   INPUT      -          - (0:2, 1:4) "b " () - b - V:1.0 UB:1.0 Net:0.0 W:0.0 - IV:1.0
5   EXCITATORY - SELECTED - (0:2, 1:10) "b c d e " () - BCDE - V:1.0 UB:1.0 Net:5.0 W:0.0
2   INPUT      -          - (0:4, 1:6) "c " () - c - V:1.0 UB:1.0 Net:0.0 W:0.0 - IV:1.0
3   INPUT      -          - (0:6, 1:8) "d " () - d - V:1.0 UB:1.0 Net:0.0 W:0.0 - IV:1.0
4   INPUT      -          - (0:8, 1:10) "e " () - e - V:1.0 UB:1.0 Net:0.0 W:0.0 - IV:1.0

 Final SearchNode:1  WeightSum:0.0