Search in sources :

Example 46 with Sentence

use of nars.entity.Sentence in project opennars by opennars.

the class ConceptMonitor method strongestProjectedEternalizedBelief.

public static Sentence strongestProjectedEternalizedBelief(NAR nar, String st) {
    Concept c = ConceptMonitor.concept(nar, st);
    if (c != null) {
        for (Task t : c.beliefs) {
            Sentence s = t.sentence;
            Sentence projected = s.projection(nar.memory.time(), nar.memory.time());
            return projected;
        }
    }
    return null;
}
Also used : Concept(nars.entity.Concept) Task(nars.entity.Task) Sentence(nars.entity.Sentence)

Example 47 with Sentence

use of nars.entity.Sentence in project opennars by opennars.

the class SupervisedRecognition method main.

public static void main(String[] args) {
    HashMap<String, Integer> map = new HashMap<>();
    // ONES:
    map.put("oxooo" + "\n" + "xxooo" + "\n" + "oxooo" + "\n" + "oxooo" + "\n" + "oxooo" + "\n", 1);
    map.put("oxxoo" + "\n" + "xoxoo" + "\n" + "ooxoo" + "\n" + "ooxoo" + "\n" + "ooxoo" + "\n", 1);
    map.put("oooxo" + "\n" + "ooxxo" + "\n" + "oooxo" + "\n" + "oooxo" + "\n" + "oooxo" + "\n", 1);
    map.put("oooox" + "\n" + "oooxx" + "\n" + "oooox" + "\n" + "oooox" + "\n" + "oooox" + "\n", 1);
    // ZEROS:
    map.put("ooooo" + "\n" + "xxxxx" + "\n" + "xooox" + "\n" + "xooox" + "\n" + "xxxxx" + "\n", 0);
    map.put("xxxxx" + "\n" + "xooox" + "\n" + "xooox" + "\n" + "xooox" + "\n" + "xxxxx" + "\n", 0);
    map.put("ooooo" + "\n" + "ooooo" + "\n" + "xxxxx" + "\n" + "xooox" + "\n" + "xxxxx" + "\n", 0);
    map.put("ooooo" + "\n" + "oxxxx" + "\n" + "oxoox" + "\n" + "oxoox" + "\n" + "oxxxx" + "\n", 0);
    map.put("ooooo" + "\n" + "xxxxx" + "\n" + "xooox" + "\n" + "xooox" + "\n" + "xxxxx" + "\n", 0);
    // training phase:
    NAR nar = new NAR();
    NARSwing.themeInvert();
    new NARSwing(nar);
    nar.param.noiseLevel.set(0);
    for (String example : map.keySet()) {
        int solution = map.get(example);
        inputExample(nar, example, solution);
        nar.cycles(1000);
    }
    // Test phase:
    inputExample(nar, "ooxoo" + "\n" + "ooxoo" + "\n" + "ooxoo" + "\n" + "ooxoo" + "\n" + "ooxoo" + "\n", -1);
    try {
        nar.askNow("<?what --> EXAMPLE>", new AnswerHandler() {

            @Override
            public void onSolution(Sentence belief) {
                System.out.println(belief);
            }
        });
    } catch (Narsese.InvalidInputException ex) {
    }
    nar.cycles(100000);
}
Also used : HashMap(java.util.HashMap) NARSwing(nars.gui.NARSwing) Narsese(nars.io.Narsese) Sentence(nars.entity.Sentence) NAR(nars.main.NAR) AnswerHandler(nars.io.events.AnswerHandler)

Example 48 with Sentence

use of nars.entity.Sentence in project opennars by opennars.

the class NARGraphDisplay method vertex.

@Override
public void vertex(AbstractGraphVis<V, E> g, VertexVis<V, E> v) {
    float alpha = 0.9f;
    V o = v.getVertex();
    v.shape = Shape.Ellipse;
    float rad = 1f;
    if (o instanceof Sentence) {
        Sentence kb = (Sentence) o;
        if (kb.truth != null) {
            float confidence = kb.truth.getConfidence();
            alpha = 0.5f + 0.5f * confidence;
        }
    // Term t = ((Sentence) o).content;
    // rad = (float) (Math.log(1 + 2 + confidence));
    } else if (o instanceof Task) {
        Task ta = (Task) o;
        rad = 2.0f + ta.getPriority() * 2.0f;
        alpha = ta.getDurability();
        v.shape = Shape.Rectangle;
    } else if (o instanceof Concept) {
        Concept co = (Concept) o;
        // Term t = co.term;
        rad = (2 + 6 * co.budget.summary());
        if (!co.beliefs.isEmpty()) {
            float confidence = co.beliefs.get(0).sentence.truth.getConfidence();
            alpha = 0.5f + 0.5f * confidence;
        }
    // v.stroke = 5;
    } else if (o instanceof Term) {
        Term t = (Term) o;
    // rad = (float) (Math.log(1 + 2 + t.getComplexity()));
    }
    Object x = o;
    if (!(x instanceof Concept)) {
        if (!(x instanceof Task)) {
            if (x instanceof Concept)
                x = ((Concept) o).getTerm();
            // Video.hashFloat(x.hashCode());
            float hue = 0.0f;
            if (x instanceof Task)
                hue = 0.4f;
            float brightness = 0.33f + 0.66f * rad / 9.0f;
            float saturation = 0.33f + 0.66f * rad / 9.0f;
            // brightness*=brightness;
            // saturation*=saturation;
            v.color = Video.colorHSB(hue, saturation, brightness, 0.25f + (0.75f * alpha));
        } else if (x instanceof Task) {
            float rr = 0.5f, gg = 0.5f, bb = 0.5f, aa = 1.0f;
            Task t = (Task) o;
            if (t.sentence.truth != null) {
                float conf = t.sentence.truth.getConfidence();
                float freq = t.sentence.truth.getFrequency();
                aa = 0.25f + conf * 0.75f;
                float evidence = TruthFunctions.c2w(conf);
                float positive_evidence_in_0_1 = TruthFunctions.w2c(evidence * freq);
                float negative_evidence_in_0_1 = TruthFunctions.w2c(evidence * (1.0f - freq));
                rr = positive_evidence_in_0_1;
                bb = negative_evidence_in_0_1;
                gg = 0.0f;
            }
            Color HSB = new Color(rr, gg, bb, aa);
            float[] hsb = new float[3];
            Color.RGBtoHSB((int) (rr * 255.0f), (int) (gg * 255.0f), (int) (bb * 255.0f), hsb);
            v.color = Video.colorHSB(hsb[0], hsb[1], hsb[2], aa);
        }
    } else if (x instanceof Concept) {
        Concept conc = ((Concept) o);
        float rr = 0.5f, gg = 0.5f, bb = 0.5f, aa = 1.0f;
        if (conc.beliefs.size() > 0) {
            Sentence sent = conc.beliefs.get(0).sentence;
            float conf = sent.truth.getConfidence();
            float freq = sent.truth.getFrequency();
            aa = 0.25f + conf * 0.75f;
            float evidence = TruthFunctions.c2w(conf);
            float positive_evidence_in_0_1 = TruthFunctions.w2c(evidence * freq);
            float negative_evidence_in_0_1 = TruthFunctions.w2c(evidence * (1.0f - freq));
            rr = positive_evidence_in_0_1;
            bb = negative_evidence_in_0_1;
            gg = 0.0f;
        }
        Color HSB = new Color(rr, gg, bb, aa);
        float[] hsb = new float[3];
        Color.RGBtoHSB((int) (rr * 255.0f), (int) (gg * 255.0f), (int) (bb * 255.0f), hsb);
        v.color = Video.colorHSB(hsb[0], hsb[1], hsb[2], aa);
    }
    String label;
    if (o instanceof Concept) {
        label = ((Concept) o).term.toString();
    } else if (o instanceof Task) {
        label = ((Task) o).sentence.toString();
    } else {
        label = o.toString();
    }
    if (label.length() > maxLabelLen) {
        label = label.substring(0, maxLabelLen - 2) + "..";
    }
    v.label = label;
    v.speed = nodeSpeed;
    v.radius = rad * nodeSize;
    v.textColor = defaultTextColor;
    v.textScale = textSize;
}
Also used : Concept(nars.entity.Concept) Task(nars.entity.Task) Color(java.awt.Color) Term(nars.language.Term) Sentence(nars.entity.Sentence)

Example 49 with Sentence

use of nars.entity.Sentence in project opennars by opennars.

the class DefaultGraphizer method onFinish.

@Override
public void onFinish(NARGraph g) {
    if (includeSyntax > 0) {
        for (final Term a : terms.keySet()) {
            if (a instanceof CompoundTerm) {
                CompoundTerm c = (CompoundTerm) a;
                g.addVertex(c.operator());
                g.addEdge(c.operator(), c, new NARGraph.TermType());
                if (includeSyntax - 1 > 0) {
                    recurseTermComponents(g, c, includeSyntax - 1);
                }
            }
        }
    }
    if (includeTermContent) {
        for (final Term a : terms.keySet()) {
            for (final Term b : terms.keySet()) {
                if (a == b) {
                    continue;
                }
                if (a.containsTerm(b)) {
                    g.addVertex(a);
                    g.addVertex(b);
                    g.addEdge(a, b, new NARGraph.TermContent());
                }
                if (b.containsTerm(a)) {
                    g.addVertex(a);
                    g.addVertex(b);
                    g.addEdge(b, a, new NARGraph.TermContent());
                }
            }
        }
    }
    if (includeDerivations && includeBeliefs) {
        for (final Map.Entry<Sentence, Concept> s : sentenceTerms.entrySet()) {
            final Sentence derivedSentence = s.getKey();
            final Concept derived = s.getValue();
            for (final Map.Entry<Sentence, Concept> t : sentenceTerms.entrySet()) {
                if (s == t) {
                    continue;
                }
                final Sentence deriverSentence = t.getKey();
                final Concept deriver = t.getValue();
                if (derived == deriver) {
                    continue;
                }
            }
        }
    }
    if (includeTermLinks) {
        for (Map.Entry<TermLink, Concept> et : termLinks.entrySet()) {
            TermLink t = et.getKey();
            Concept from = et.getValue();
            Concept to = terms.get(t.target);
            if (to != null) {
                g.addEdge(from, to, new NARGraph.TermLinkEdge(t));
            }
        }
    }
    if (includeTaskLinks) {
        for (Map.Entry<TaskLink, Concept> et : taskLinks.entrySet()) {
            TaskLink t = et.getKey();
            if (this.taskPriorityThreshold != null && t.getPriority() > this.taskPriorityThreshold.get()) {
                Concept from = et.getValue();
                if (t.targetTask != null) {
                    Task theTask = t.targetTask;
                    if (!g.containsVertex(theTask)) {
                        g.addVertex(theTask);
                        Term term = theTask.getTerm();
                        if (term != null) {
                            Concept c = terms.get(term);
                            if (c != null) {
                                if (g.containsVertex(c)) {
                                    g.addVertex(c);
                                }
                                g.addEdge(c, theTask, new NARGraph.TermContent());
                            }
                        }
                        onTask(theTask);
                    }
                    g.addEdge(from, t.targetTask, new NARGraph.TaskLinkEdge(t));
                }
            }
        }
    }
}
Also used : CompoundTerm(nars.language.CompoundTerm) Concept(nars.entity.Concept) TermLink(nars.entity.TermLink) Task(nars.entity.Task) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) TaskLink(nars.entity.TaskLink) Map(java.util.Map) HashMap(java.util.HashMap) Sentence(nars.entity.Sentence)

Example 50 with Sentence

use of nars.entity.Sentence in project opennars by opennars.

the class DefaultGraphizer method onConcept.

@Override
public void onConcept(NARGraph g, Concept c) {
    Term t = c.term;
    if (this.filterBox != null && terms.size() < nConcepts.get() * ((double) Parameters.CONCEPT_BAG_SIZE) && c.getPriority() > this.conceptPriorityThreshold.get() && ("".equals(this.filterBox.getText()) || t.toString().contains(this.filterBox.getText()))) {
        g.addVertex(c);
        terms.put(c.term, c);
    } else {
        return;
    }
    if (includeTermLinks) {
        for (TermLink x : c.termLinks) {
            termLinks.put(x, c);
        }
    }
    if (includeTaskLinks) {
        for (TaskLink x : c.taskLinks) {
            if (x.getPriority() > this.taskPriorityThreshold.get()) {
                taskLinks.put(x, c);
            }
        }
    }
    if (includeTermContent) {
        g.addVertex(t);
        g.addEdge(c, c.term, new NARGraph.TermContent());
    }
    if (includeBeliefs) {
        for (final Task beliefT : c.beliefs) {
            Sentence belief = beliefT.sentence;
            onBelief(belief);
            sentenceTerms.put(belief, c);
        // g.addVertex(c);
        // g.addVertex(belief);
        // g.addEdge(belief, c, new SentenceContent());
        // TODO extract to onBelief
        // TODO check if kb.getContent() is never distinct from c.getTerm()
        /*if (c.term.equals(belief.content)) {
                continue;
                }
                addTerm(g, belief.content);
                g.addEdge(term, belief.content, new TermBelief());*/
        }
    }
    if (includeQuestions) {
        for (final Task q : c.getQuestions()) {
            if (c.term.equals(q.getTerm())) {
                continue;
            }
            // TODO extract to onQuestion
            g.addVertex(q);
            // TODO q.getParentBelief()
            // TODO q.getParentTask()
            g.addEdge(c, q, new NARGraph.TermQuestion());
            onQuestion(q);
        }
    }
}
Also used : TermLink(nars.entity.TermLink) Task(nars.entity.Task) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) TaskLink(nars.entity.TaskLink) Sentence(nars.entity.Sentence)

Aggregations

Sentence (nars.entity.Sentence)70 Task (nars.entity.Task)54 Term (nars.language.Term)40 BudgetValue (nars.entity.BudgetValue)39 TruthValue (nars.entity.TruthValue)39 Stamp (nars.entity.Stamp)25 CompoundTerm (nars.language.CompoundTerm)24 Statement (nars.language.Statement)13 Conjunction (nars.language.Conjunction)11 Concept (nars.entity.Concept)10 Implication (nars.language.Implication)10 Inheritance (nars.language.Inheritance)8 Interval (nars.language.Interval)8 ArrayList (java.util.ArrayList)6 Equivalence (nars.language.Equivalence)6 HashMap (java.util.HashMap)5 AnswerHandler (nars.io.events.AnswerHandler)5 NARSwing (nars.gui.NARSwing)4 Narsese (nars.io.Narsese)4 Product (nars.language.Product)4