# 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.1.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);

            inputNeurons.put(word, in);
        }

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

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

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)"),
                6.0, // adjusts the bias
                RECTIFIED_HYPERBOLIC_TANGENT,
                EXCITATORY,
                new Synapse.Builder() // Requires the word to be recognized
                        .setSynapseId(0)
                        .setNeuron(inputNeurons.get("cook"))
                        .setWeight(10.0)
                        .setBias(-10.0)
                        .setRecurrent(false),
                new Synapse.Builder() // The previous word needs to be a forename
                        .setSynapseId(1)
                        .setNeuron(forenameCategory)
                        .setWeight(10.0)
                        .setBias(-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)
                        .setBias(0.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,
                RECTIFIED_LINEAR_UNIT,
                EXCITATORY,
                new Synapse.Builder() // In this example there is only one forename considered.
                        .setSynapseId(0)
                        .setNeuron(jacksonForenameEntity)
                        .setWeight(1.0)
                        .setBias(0.0),
                new Relation.Builder()
                        .setFrom(0)
                        .setTo(OUTPUT)
                        .setRelation(EQUALS)
        );
        Neuron.init(
                surnameCategory,
                0.0,
                RECTIFIED_LINEAR_UNIT,
                EXCITATORY,
                new Synapse.Builder()
                        .setSynapseId(0)
                        .setNeuron(cookSurnameEntity)
                        .setWeight(1.0)
                        .setBias(0.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,
                RECTIFIED_LINEAR_UNIT,
                INHIBITORY,
                new Synapse.Builder().setNeuron(cookProfessionEntity)
                        .setSynapseId(0)
                        .setWeight(1.0)
                        .setBias(0.0),
                new Synapse.Builder()
                        .setSynapseId(1)
                        .setNeuron(cookSurnameEntity)
                        .setWeight(1.0)
                        .setBias(0.0),
                new Synapse.Builder()
                        .setSynapseId(2)
                        .setNeuron(jacksonCityEntity)
                        .setWeight(1.0)
                        .setBias(0.0),
                new Synapse.Builder()
                        .setSynapseId(3)
                        .setNeuron(jacksonForenameEntity)
                        .setWeight(1.0)
                        .setBias(0.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 - Final Decision - Slots | Identity - Neuron Label - Logic Layer - Upper Bound -
    Value | Sum | Weight - Input Value | Target Value

        ...
    3 - SELECTED - (0:4, 1:12) () - C-forename - OR[] - V:1.0 Net:1.0 W:0.0
    4 - SELECTED - (0:4, 1:12) () - INHIB - OR[] - V:1.0 Net:1.0 W:0.0
    1 - SELECTED - (0:4, 1:12) () - W-jackson - OR[] - V:1.0 Net:0.0 W:0.0 - IV:1.0
    2 - SELECTED - (0:4, 1:12) () - E-jackson (forename) - V:1.0 Net:6.0 W:6.0
    5 - EXCLUDED - (0:4, 1:12) () - E-jackson (city) -
    8 - SELECTED - (0:12, 1:17) () - C-surname - OR[] - V:1.0 Net:1.0 W:0.0
    9 - SELECTED - (0:12, 1:17) () - INHIB - OR[] - V:1.0 Net:1.0 W:0.0
    6 - SELECTED - (0:12, 1:17) () - W-cook - OR[] - V:1.0 Net:0.0 W:0.0 - IV:1.0
    7 - SELECTED - (0:12, 1:17) () - E-cook (surname) - V:1.0 Net:8.0 W:8.0
    10 - EXCLUDED - (0:12, 1:17) () - E-cook (profession) -
        ...

     Final SearchNode:8  WeightSum:13.999

### Mutual exclusion example

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

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

            // 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"),
                    3.0,
                    RECTIFIED_HYPERBOLIC_TANGENT,
                    EXCITATORY,
                    new Synapse.Builder()
                            .setSynapseId(0)
                            .setNeuron(inA)
                            .setWeight(10.0)
                            .setBias(-10.0)
                            .setRecurrent(false),
                    new Synapse.Builder()
                            .setSynapseId(1)
                            .setNeuron(inhibN)
                            .setWeight(-100.0)
                            .setBias(0.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"),
                    5.0,
                    RECTIFIED_HYPERBOLIC_TANGENT,
                    EXCITATORY,
                    new Synapse.Builder()
                            .setSynapseId(0)
                            .setNeuron(inB)
                            .setWeight(10.0)
                            .setBias(-10.0)
                            .setRecurrent(false),
                    new Synapse.Builder()
                            .setSynapseId(1)
                            .setNeuron(inhibN)
                            .setWeight(-100.0)
                            .setBias(0.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"),
                    2.0,
                    RECTIFIED_HYPERBOLIC_TANGENT,
                    EXCITATORY,
                    new Synapse.Builder()
                            .setSynapseId(0)
                            .setNeuron(inC)
                            .setWeight(10.0)
                            .setBias(-10.0)
                            .setRecurrent(false),
                    new Synapse.Builder()
                            .setSynapseId(1)
                            .setNeuron(inhibN)
                            .setWeight(-100.0)
                            .setBias(0.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,
                    RECTIFIED_LINEAR_UNIT,
                    INHIBITORY,
                    new Synapse.Builder()
                            .setSynapseId(0)
                            .setNeuron(pA)
                            .setWeight(1.0)
                            .setBias(0.0)
                            .setRecurrent(false),
                    new Synapse.Builder()
                            .setSynapseId(1)
                            .setNeuron(pB)
                            .setWeight(1.0)
                            .setBias(0.0)
                            .setRecurrent(false),
                    new Synapse.Builder()
                            .setSynapseId(2)
                            .setNeuron(pC)
                            .setWeight(1.0)
                            .setBias(0.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"),
                    0.0,
                    RECTIFIED_HYPERBOLIC_TANGENT,
                    EXCITATORY,
                    new Synapse.Builder()
                             .setSynapseId(0)
                            .setNeuron(pB)
                            .setWeight(1.0)
                            .setBias(0.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 - Final Decision - Slots | Identity - Neuron Label - Upper Bound -
     Value | Net | Weight - Input Value | Target Value

    0 - SELECTED - (0:0, 1:1) () - IN-A - V:1.0 Net:0.0 W:0.0 - IV:1.0
    3 - SELECTED - (0:0, 1:1) () - IN-B - V:1.0 Net:0.0 W:0.0 - IV:1.0
    6 - SELECTED - (0:0, 1:1) () - IN-C - V:1.0 Net:0.0 W:0.0 - IV:1.0
    2 - SELECTED - (0:0, 1:1) () - INHIB - V:1.0 Net:1.0 W:0.0
    1 - EXCLUDED - (0:0, 1:1) () - A -
    4 - SELECTED - (0:0, 1:1) () - B - V:1.0 Net:5.0 W:5.0
    7 - EXCLUDED - (0:0, 1:1) () - C -
    5 - SELECTED - (0:0, 1:1) () - OUT - V: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 + "");

                inputNeurons.put(c, in);
            }

            Neuron pattern = Neuron.init(
                    m.createNeuron("BCDE"),
                    5.0,
                    RECTIFIED_HYPERBOLIC_TANGENT,
                    EXCITATORY,
                    new Synapse.Builder()
                            .setSynapseId(0)
                            .setNeuron(inputNeurons.get('b'))
                            .setWeight(10.0)
                            .setBias(-10.0)
                            .setRecurrent(false),
                    new Synapse.Builder()
                            .setSynapseId(1)
                            .setNeuron(inputNeurons.get('c'))
                            .setWeight(10.0)
                            .setBias(-10.0)
                            .setRecurrent(false),
                    new Synapse.Builder()
                            .setSynapseId(2)
                            .setNeuron(inputNeurons.get('d'))
                            .setWeight(10.0)
                            .setBias(-10.0)
                            .setRecurrent(false),
                    new Synapse.Builder()
                            .setSynapseId(3)
                            .setNeuron(inputNeurons.get('e'))
                            .setWeight(10.0)
                            .setBias(-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 - Final Decision - Slots | Identity - Neuron Label - Upper Bound -
    Value | Net | Weight - Input Value | Target Value

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

     Final SearchNode:1  WeightSum:0.0