Search in sources :

Example 1 with AnswerHandler

use of nars.io.events.AnswerHandler in project opennars by opennars.

the class SymRecognizer method resetDetection.

public void resetDetection() {
    clear();
    for (AnswerHandler ans : q) {
        ans.off();
    }
    q = new ArrayList<AnswerHandler>();
    if (nar != null) {
        nar.stop();
        nar.reset();
        if (gui != null)
            gui.mainWindow.setVisible(false);
        nar = null;
        gui = null;
    }
}
Also used : AnswerHandler(nars.io.events.AnswerHandler)

Example 2 with AnswerHandler

use of nars.io.events.AnswerHandler in project opennars by opennars.

the class SymRecognizer method jButton3ActionPerformed.

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
    // GEN-FIRST:event_jButton3ActionPerformed
    resetDetection();
    StringBuilder build = new StringBuilder();
    StringBuilder build2 = new StringBuilder();
    build2.append("<(&|,");
    for (int x = 0; x < SZ; x += 1) {
        for (int y = 0; y < SZ; y += 1) {
            int used_X = x;
            int used_Y = y;
            Color col1 = new Color(canvasIMG.getRGB(x, y));
            float col = ((float) col1.getRed()) / 255.0f;
            if (col > 0.0) {
                float freq = 0.5f + (col - 0.5f);
                if (invar.isSelected()) {
                    build.append("<p[" + String.valueOf(used_X) + "," + String.valueOf(used_Y) + "] --> [on]>. :|: %" + String.valueOf(freq) + "%");
                    build.append("\n");
                    build2.append("<p[" + String.valueOf(used_X) + "," + String.valueOf(used_Y) + "] --> [on]>,");
                } else {
                    build.append("<p_" + String.valueOf(used_X) + "_" + String.valueOf(used_Y) + " --> [on]>. :|: %" + String.valueOf(freq) + "%");
                    build.append("\n");
                    build2.append("<p_" + String.valueOf(used_X) + "_" + String.valueOf(used_Y) + " --> [on]>,");
                }
            }
        }
    }
    String s2 = build2.toString();
    s2 = s2.substring(0, s2.length() - 1);
    s2 = s2 + ")";
    inputPanel.setText(build.toString());
    if (evt == null) {
        String question = "<{?what} --> [observed]>?";
        additional[exid] = s2 + " ==> <{example" + exid + "} --> [observed]>>.";
        inputPanel2.setText(additional[exid] + "\n" + question);
    } else {
        nar = new NAR();
        if (invar1.isSelected()) {
            gui = new NARSwing(nar);
        }
        int u = 0;
        inputPanel2.setText("");
        // for(String s : questions) {
        String s = question;
        {
            if (s != null) {
                AnswerHandler cur = new AnswerHandler() {

                    @Override
                    public void onSolution(Sentence belief) {
                        // System.out.println("solution: " + belief);
                        System.out.println(belief);
                        float howconf = belief.truth.getConfidence();
                        if (howconf > 0.1f) {
                            // only mark if above 0.1 confidence
                            // also mark image:
                            int maxu = Integer.valueOf(belief.getTerm().toString().split("example")[1].split("}")[0]);
                            clear();
                            for (int x = 0; x < SZ * scale_palette; x += 1) {
                                for (int y = 0; y < SZ * scale_palette; y += 1) {
                                    Color col = new Color(canvasIMG.getRGB(x / scale_palette, y / scale_palette));
                                    int k = getK[maxu];
                                    int j = getJ[maxu];
                                    exampleIMG.setRGB(x + k * scale_palette * SZ, y + (3 * j + 1) * scale_palette * SZ, new Color(col.getRed(), 0, 0).getRGB());
                                }
                            }
                            estimate.setIcon(new ImageIcon(fitimage(exampleIMG, estimate.getWidth(), estimate.getHeight())));
                            estimate.repaint();
                        }
                    }
                };
                q.add(cur);
                try {
                    for (int h = 0; h < exid; h++) {
                        inputPanel2.setText(inputPanel2.getText() + additional[h] + "\n");
                        nar.addInput(additional[h]);
                    }
                    inputPanel2.setText(inputPanel2.getText() + s + "\n");
                    nar.ask(s.substring(0, s.length() - 1), cur);
                } catch (Narsese.InvalidInputException ex) {
                    Logger.getLogger(SymRecognizer.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            u++;
        }
        nar.param.noiseLevel.set(0);
        nar.addInput(inputPanel.getText());
        nar.start(0);
    }
}
Also used : NARSwing(nars.gui.NARSwing) Sentence(nars.entity.Sentence) NAR(nars.main.NAR) AnswerHandler(nars.io.events.AnswerHandler)

Example 3 with AnswerHandler

use of nars.io.events.AnswerHandler in project opennars by opennars.

the class SymRecognizerWithVisionChannel method resetDetection.

public void resetDetection() {
    clear();
    for (AnswerHandler ans : q) {
        ans.off();
    }
    q = new ArrayList<AnswerHandler>();
    if (nar != null) {
        nar.stop();
        nar.reset();
        if (gui != null)
            gui.mainWindow.setVisible(false);
        nar = null;
        gui = null;
    }
}
Also used : AnswerHandler(nars.io.events.AnswerHandler)

Example 4 with AnswerHandler

use of nars.io.events.AnswerHandler in project opennars by opennars.

the class SymRecognizerWithVisionChannel method jButton3ActionPerformed.

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
    // GEN-FIRST:event_jButton3ActionPerformed
    resetDetection();
    StringBuilder build = new StringBuilder();
    StringBuilder build2 = new StringBuilder();
    build2.append("<(&|,");
    for (int x = 0; x < SZ; x += 1) {
        for (int y = 0; y < SZ; y += 1) {
            int used_X = x;
            int used_Y = y;
            Color col1 = new Color(canvasIMG.getRGB(x, y));
            float col = ((float) col1.getRed()) / 255.0f;
            if (col > 0.0) {
                float freq = 0.5f + (col - 0.5f);
                if (invar.isSelected()) {
                    build.append("<p[" + String.valueOf(used_X) + "," + String.valueOf(used_Y) + "] --> [on]>. :|: %" + String.valueOf(freq) + "%");
                    build.append("\n");
                    build2.append("<p[" + String.valueOf(used_X) + "," + String.valueOf(used_Y) + "] --> [on]>,");
                } else {
                    build.append("<p_" + String.valueOf(used_X) + "_" + String.valueOf(used_Y) + " --> [on]>. :|: %" + String.valueOf(freq) + "%");
                    build.append("\n");
                    build2.append("<p_" + String.valueOf(used_X) + "_" + String.valueOf(used_Y) + " --> [on]>,");
                }
            }
        }
    }
    String s2 = build2.toString();
    s2 = s2.substring(0, s2.length() - 1);
    s2 = s2 + ")";
    inputPanel.setText(build.toString());
    if (evt == null) {
        String question = "<{?what} --> [observed]>?";
        additional[exid] = s2 + " ==> <{example" + exid + "} --> [observed]>>.";
        inputPanel2.setText(additional[exid] + "\n" + question);
    } else {
        // add vision channel for [on] property
        nar = new NAR();
        // to the nar instance, and nar is also the "next higher" sensory channel
        // to report the results to
        nar.addSensoryChannel("[on]", new SpatialSamplingVisionChannel(nar, nar, SZ, SZ));
        if (invar1.isSelected()) {
            gui = new NARSwing(nar);
        }
        int u = 0;
        inputPanel2.setText("");
        // for(String s : questions) {
        String s = question;
        {
            if (s != null) {
                AnswerHandler cur = new AnswerHandler() {

                    @Override
                    public void onSolution(Sentence belief) {
                        // System.out.println("solution: " + belief);
                        System.out.println(belief);
                        float howconf = belief.truth.getConfidence();
                        if (howconf >= 0.001f) {
                            // only mark if above 0.1 confidence
                            // also mark image:
                            int maxu = Integer.valueOf(belief.getTerm().toString().split("example")[1].split("}")[0]);
                            clear();
                            for (int x = 0; x < SZ * scale_palette; x += 1) {
                                for (int y = 0; y < SZ * scale_palette; y += 1) {
                                    Color col = new Color(canvasIMG.getRGB(x / scale_palette, y / scale_palette));
                                    int k = getK[maxu];
                                    int j = getJ[maxu];
                                    exampleIMG.setRGB(x + k * scale_palette * SZ, y + (3 * j + 1) * scale_palette * SZ, new Color(col.getRed(), 0, 0).getRGB());
                                }
                            }
                            estimate.setIcon(new ImageIcon(fitimage(exampleIMG, estimate.getWidth(), estimate.getHeight())));
                            estimate.repaint();
                        }
                    }
                };
                q.add(cur);
                try {
                    for (int h = 0; h < exid; h++) {
                        inputPanel2.setText(inputPanel2.getText() + additional[h] + "\n");
                        nar.addInput(additional[h]);
                    }
                    inputPanel2.setText(inputPanel2.getText() + s + "\n");
                    nar.ask(s.substring(0, s.length() - 1), cur);
                } catch (Narsese.InvalidInputException ex) {
                    Logger.getLogger(SymRecognizerWithVisionChannel.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            u++;
        }
        nar.param.noiseLevel.set(0);
        nar.addInput(inputPanel.getText());
        nar.start(0);
    }
}
Also used : AnswerHandler(nars.io.events.AnswerHandler) NARSwing(nars.gui.NARSwing) Sentence(nars.entity.Sentence) NAR(nars.main.NAR)

Example 5 with AnswerHandler

use of nars.io.events.AnswerHandler in project opennars by opennars.

the class LanguageGUI method jButton1ActionPerformed.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // GEN-FIRST:event_jButton1ActionPerformed
    String text = this.inputPanel.getText();
    List<String> inputs = new ArrayList<String>();
    this.inputPanel.setText("");
    String[] words = text.split(" ");
    String punctuation = "sentence";
    for (int i = 0; i < words.length; i++) {
        for (int len = words.length; len >= Integer.valueOf(spliceField.getText()); len--) {
            String narsese = "(#,";
            if (i + len > words.length) {
                // this length is not possible
                continue;
            }
            for (int k = i; k < i + len; k++) {
                if (words[k].contains("?")) {
                    punctuation = "question";
                }
                // skip puncutation in words
                words[k] = words[k].replace(".", "").replace("?", "");
                narsese += "\"" + words[k] + "\"" + ",";
            }
            narsese = narsese.substring(0, narsese.length() - 1);
            narsese += ").";
            inputs.add("$1.0;0.99;0.99$" + narsese);
            if (!spliceCheckbox.isSelected()) {
                break;
            }
            break;
        }
        if (!spliceCheckbox.isSelected()) {
            break;
        }
        break;
    }
    languageNAR.stop();
    for (AnswerHandler ans : q) {
        ans.off();
    }
    languageNAR.reset();
    languageNAR.memory.randomNumber.setSeed(seed);
    seed++;
    try {
        HashMap<String, String> wordTypes = new HashMap<String, String>();
        HashSet<String> qWords = new HashSet<String>();
        for (String s : jTextPane1.getText().split("\n")) {
            // if its a word type definition we won't add it directly but only when used in the sentence
            if (s.startsWith("<\"")) {
                String word = s.split("\"")[1];
                wordTypes.put(word, s);
                if (s.contains("QWORD>.")) {
                    qWords.add(word);
                }
            } else {
                languageNAR.addInput(s);
            }
        }
        languageNAR.cycles(100);
        // add word categories of all words that occurred
        for (String word : words) {
            if (wordTypes.containsKey(word)) {
                languageNAR.addInput(wordTypes.get(word));
            }
        }
        languageNAR.cycles(10);
        for (String s : inputs) {
            languageNAR.addInput(s);
            languageNAR.cycles(1);
        }
        boolean isQuestion = punctuation.equals("question");
        String punct = (isQuestion ? "?" : "." + (eventCheck.isSelected() ? " :|:" : ""));
        // Interpretation of Inheritance
        AnswerHandler cur = new AnswerHandler() {

            @Override
            public void onSolution(Sentence belief) {
                // System.out.println("solution: " + belief);
                System.out.println(belief);
                String[] concepts = belief.term.toString().split("\"");
                if (concepts.length < 6 || belief.toString().contains("#")) {
                    // just an example, no " included
                    return;
                }
                String s = considerQWord(concepts[1], qWords).toUpperCase();
                String p = considerQWord(concepts[3], qWords).toUpperCase();
                String v = considerQWord(concepts[5], qWords).toUpperCase();
                String inp = "<(*," + s + "," + p + ") --> " + v + ">" + punct;
                if (v.equals("IS") || v.equals("ARE")) {
                    inp = "<" + s + " --> " + p + ">" + punct;
                }
                reasonerNAR.stop();
                reasonerNAR.addInput(inp + " " + (isQuestion ? "" : belief.truth.toString()));
                reasonerNAR.start(0);
            }
        };
        languageNAR.ask("<(*,(/,REPRESENT,?a,_),(/,REPRESENT,?b,_)) --> (/,REPRESENT,?v,_)>", cur);
        q.add(cur);
        for (String attribute : attributePanel.getText().split(" ")) {
            cur = new AnswerHandler() {

                @Override
                public void onSolution(Sentence belief) {
                    // System.out.println("solution: " + belief);
                    System.out.println(belief);
                    String[] concepts = belief.term.toString().split("\"");
                    if (concepts.length < 2) {
                        // just an example, no " included
                        return;
                    }
                    String s = considerQWord(concepts[1], qWords).toUpperCase();
                    String inp = "<" + s + " --> " + attribute + ">" + punct;
                    reasonerNAR.stop();
                    reasonerNAR.addInput(inp + " " + (isQuestion ? "" : belief.truth.toString()));
                    reasonerNAR.start(0);
                }
            };
            if (!attribute.trim().equals("")) {
                languageNAR.ask("<?what --> " + attribute + ">", cur);
                q.add(cur);
            }
        }
        languageNAR.start(0);
    } catch (Exception ex) {
        Logger.getLogger(LanguageGUI.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) AnswerHandler(nars.io.events.AnswerHandler) Sentence(nars.entity.Sentence) HashSet(java.util.HashSet)

Aggregations

AnswerHandler (nars.io.events.AnswerHandler)7 Sentence (nars.entity.Sentence)5 NARSwing (nars.gui.NARSwing)4 NAR (nars.main.NAR)4 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Narsese (nars.io.Narsese)2 TreeMLData (automenta.vivisect.TreeMLData)1 NWindow (automenta.vivisect.swing.NWindow)1 PCanvas (automenta.vivisect.swing.PCanvas)1 LineChart (automenta.vivisect.timeline.LineChart)1 TimelineVis (automenta.vivisect.timeline.TimelineVis)1 FileNotFoundException (java.io.FileNotFoundException)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 DerivationContext (nars.control.DerivationContext)1 Task (nars.entity.Task)1 TaskImmediateProcess (nars.io.events.Events.TaskImmediateProcess)1 ChangedTextInput (nars.lab.ioutils.ChangedTextInput)1 Term (nars.language.Term)1