package org.aika.network;

import org.aika.ActivationFunction;
import org.aika.Model;
import org.aika.neuron.INeuron;
import org.aika.neuron.Neuron;
import org.aika.neuron.Synapse;
import org.aika.neuron.activation.Range;
import org.junit.Before;

import java.util.Map;
import java.util.TreeMap;

import static org.aika.ActivationFunction.RECTIFIED_LINEAR_UNIT;
import static org.aika.neuron.INeuron.Type.INHIBITORY;
import static org.aika.neuron.activation.Range.Relation.EQUALS;
import static org.aika.neuron.activation.Range.Relation.NONE;

public class CoreferenceResolutionTest {

    String[][] pronouns = new String[][] {
            {"he", "him", "his"},
            {"she", "her", "hers"}
    };

    String[][] names = new String[][] {
            {"john", "robert", "richard", "mark"},
            {"linda", "lisa", "susan"}
    };


    Model m;

    Neuron maleNameN;
    Neuron malePronounN;
    Neuron femaleNameN;
    Neuron femalePronounN;

    Map<String, Neuron> dictionary = new TreeMap<>();


    @Before
    void init() {
        m = new Model();

        maleNameN = m.createNeuron("C-Male Name");
        Neuron.init(maleNameN, 0.0, RECTIFIED_LINEAR_UNIT, INHIBITORY);

        malePronounN = m.createNeuron("C-Male Pronoun");
        Neuron.init(malePronounN, 0.0, RECTIFIED_LINEAR_UNIT, INHIBITORY);

        femaleNameN = m.createNeuron("C-Female Name");
        Neuron.init(femaleNameN, 0.0, RECTIFIED_LINEAR_UNIT, INHIBITORY);

        femalePronounN = m.createNeuron("C-Female Pronoun");
        Neuron.init(femalePronounN, 0.0, RECTIFIED_LINEAR_UNIT, INHIBITORY);

        addWords(pronouns[0], malePronounN);
        addWords(pronouns[1], femalePronounN);
        addWords(names[0], maleNameN);
        addWords(names[1], femalePronounN);


        Neuron maleCoref = m.createNeuron("Male Coreference");
        Neuron femaleCoref = m.createNeuron("Female Coreference");

        Neuron.init(maleCoref, 5.0, INeuron.Type.EXCITATORY,
                new Synapse.Builder()
                        .setNeuron(malePronounN)
                        .setWeight(10.0)
                        .setBias(-10.0)
                        .setRangeMatch(EQUALS)
                        .setRangeOutput(true, true),
                new Synapse.Builder()
                        .setNeuron(maleNameN)
                        .setWeight(10.0)
                        .setBias(-10.0)
                        .setRangeMatch(NONE)

        );
        Neuron.init(femaleCoref, 5.0, INeuron.Type.EXCITATORY,
                new Synapse.Builder()
                        .setNeuron(femalePronounN)
                        .setWeight(10.0)
                        .setBias(-10.0)
                        .setRangeMatch(EQUALS)
                        .setRangeOutput(true, true),
                new Synapse.Builder()
                        .setNeuron(femaleNameN)
                        .setWeight(10.0)
                        .setBias(-10.0)
                        .setRangeMatch(NONE)

        );
    }


    void addWords(String[] words, Neuron classN) {
        for(String word: words) {
            Neuron wordN = m.createNeuron("W-" + word);

            dictionary.put(word, wordN);

            if(classN != null) {
                classN.addSynapse(
                        new Synapse.Builder()
                                .setNeuron(wordN)
                                .setWeight(1.0)
                                .setRangeMatch(EQUALS)
                                .setRangeOutput(true)
                                .setRelativeRid(0)
                );
            }
        }
    }

}

@@ -36,34 +36,24 @@
 rg.aika.
-ActivationFunction
+Document
 ;%0Aimport
@@ -168,49 +168,8 @@
 se;%0A
-import org.aika.neuron.activation.Range;%0A
 impo
@@ -188,16 +188,39 @@
 .Before;
+%0Aimport org.junit.Test;
 %0A%0Aimport
@@ -509,16 +509,17 @@
 .NONE;%0A%0A
+%0A
 public c
@@ -992,20 +992,27 @@
 @Before%0A
-
     
+public 
 void ini
@@ -3599,10 +3599,688 @@
 %0A    %7D%0A%0A
+%0A    public Document parse(String txt) %7B%0A        Document doc = m.createDocument(txt);%0A%0A        int i = 0;%0A        for(String word: txt.split(%22 %22)) %7B%0A            int j = i + word.length();%0A            Neuron wn = dictionary.get(word);%0A            if(wn != null) %7B%0A                wn.addInput(doc, i, j);%0A            %7D%0A%0A            i = j + 1;%0A        %7D%0A%0A        doc.process();%0A%0A        return doc;%0A    %7D%0A%0A%0A    @Test%0A    public void testCoref() %7B%0A        String txt = %22john and lisa went to a sea park he saw dolphins lisa met richard he also visited the seapark%22;%0A%0A        Document doc = parse(txt);%0A%0A        System.out.println(doc.activationsToString(true, true, true));%0A    %7D%0A%0A
 %7D%0A

test case