Search in sources :

Example 1 with TaskBuilder

use of nars.task.TaskBuilder in project narchy by automenta.

the class Twenglish method parseSentence.

@NotNull
protected Collection<TaskBuilder> parseSentence(String source, @NotNull NAR n, @NotNull List<Span> s) {
    LinkedList<Term> t = new LinkedList();
    Span last = null;
    for (Span c : s) {
        t.add(spanToTerm(c));
        last = c;
    }
    if (t.isEmpty())
        return Collections.emptyList();
    // Atomic sentenceType = FRAGMENT;
    // if ((last!=null) && ("punct".equals(last.pattern))) {
    // switch (last.content) {
    // case ".": sentenceType = JUDGMENT; break;
    // case "?": sentenceType = QUESTION; break;
    // //case "@": sentenceType = QUEST; break;
    // case "!": sentenceType = GOAL; break;
    // }
    // }
    // if (!"words".equals(sentenceType.toString()))
    // t.removeLast(); //remove the punctuation, it will be redundant
    List<TaskBuilder> tt = new ArrayList();
    // 1. add the logical structure of the sequence of terms
    if (inputProduct) {
        Term tokens = $.p(t.toArray(new Term[t.size()]));
        // Term q =
        // $.image(2,
        // $.the(source),
        // sentenceType,
        // tokens
        // )
        Term q = $.func("hear", Atomic.the(source), tokens);
        // n.task(q + ". %0.95|0.95%");
        TaskBuilder newtask = new TaskBuilder(q, BELIEF, 1f, n).present(n);
        // TODO non-string construct
        tt.add(newtask);
    }
    return tt;
}
Also used : TaskBuilder(nars.task.TaskBuilder) Term(nars.term.Term) Span(jcog.io.Twokenize.Span) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with TaskBuilder

use of nars.task.TaskBuilder in project narchy by automenta.

the class Twenglish method parse.

/**
 * returns a list of all tasks that it was able to parse for the input
 */
@NotNull
public List<TaskBuilder> parse(String source, @NotNull NAR n, String s) {
    List<TaskBuilder> results = $.newArrayList();
    List<Span> tokens = Twokenize.twokenize(s);
    List<List<Span>> sentences = $.newArrayList();
    List<Span> currentSentence = $.newArrayList(tokens.size());
    for (Span p : tokens) {
        currentSentence.add(p);
        if ("punct".equals(p.pattern)) {
            switch(p.content) {
                case ".":
                case "?":
                case "!":
                    if (!currentSentence.isEmpty()) {
                        sentences.add(currentSentence);
                        currentSentence = $.newArrayList();
                        break;
                    }
            }
        }
    }
    if (!currentSentence.isEmpty())
        sentences.add(currentSentence);
    for (List<Span> x : sentences) {
        Collection<TaskBuilder> ss = parseSentence(source, n, x);
        if (ss != null)
            results.addAll(ss);
    }
    if (!results.isEmpty()) {
    // if (!languageBooted) {
    // 
    // 
    // results.add(0, n.task(new StringBuilder(
    // "<{word,pronoun,qpronoun,prepos,conjunc} -]- symbol>.").toString()));
    // results.add(0, n.task(new StringBuilder(
    // "$0.90;0.90$ <(*,<$a-->[$d]>,<is-->[verb]>,<$b-->[$d]>) =/> <$a <-> $b>>.").toString()));
    // 
    // languageBooted = true;
    // }
    }
    return results;
}
Also used : TaskBuilder(nars.task.TaskBuilder) Span(jcog.io.Twokenize.Span) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with TaskBuilder

use of nars.task.TaskBuilder in project narchy by automenta.

the class Narsese method decodeTask.

/**
 * returns null if the Task is invalid (ex: invalid term)
 */
static Task decodeTask(NAR m, Object[] x) {
    if (x.length == 1 && x[0] instanceof Task) {
        return (Task) x[0];
    }
    Term content = ((Term) x[1]).normalize();
    /*if (!(content instanceof Compound)) {
                throw new NarseseException("Task term unnormalizable: " + contentRaw);
                //return Command.task($.func("log", content));
            } else */
    Object px = x[2];
    byte punct = px instanceof Byte ? (Byte) x[2] : (byte) (((Character) x[2]).charValue());
    Truth t = (Truth) x[3];
    if (t != null && !Float.isFinite(t.conf()))
        t = $.t(t.freq(), m.confDefault(punct));
    if (t == null && (punct == BELIEF || punct == GOAL)) {
        t = $.t(1, m.confDefault(punct));
    }
    Task y = Task.tryTask(content, punct, t, (C, tr) -> {
        // TODO construct directly and remove TaskBuilder
        TaskBuilder ttt = new TaskBuilder(C, punct, tr).time(// creation time
        m.time(), Tense.getRelativeOccurrence((Tense) x[4], m));
        if (x[0] == null)
            /* do not set, Memory will apply defaults */
            ttt.priSet(m.priDefault(punct));
        else
            ttt.priSet((Float) x[0]);
        return ttt.apply(m).log(NARSESE_TASK_TAG);
    });
    if (y == null) {
        throw new InvalidTaskException(content, "input: " + Arrays.toString(x));
    }
    return y;
}
Also used : TaskBuilder(nars.task.TaskBuilder) Tense(nars.time.Tense) InvalidTaskException(nars.task.util.InvalidTaskException) Term(nars.term.Term) Truth(nars.truth.Truth)

Example 4 with TaskBuilder

use of nars.task.TaskBuilder in project narchy by automenta.

the class NQuadsRDF method inputRaw.

public static Task inputRaw(@NotNull NAR nar, @Nullable Atom subject, @NotNull Atom predicate, @NotNull Term object) {
    if (subject == null)
        return null;
    if (predicatesIgnored.contains(predicate))
        return null;
    try {
        Term term = /*$.inst*/
        $.inh($.p(subject, object), predicate);
        if (term == null)
            throw new NullPointerException();
        Task t = new TaskBuilder(term, BELIEF, $.t(1f, nar.confDefault(BELIEF))).apply(nar);
        return t;
    } catch (Exception e) {
        logger.error("rdf({}) to task: {}", new Term[] { subject, object, predicate }, e);
        return null;
    }
}
Also used : TaskBuilder(nars.task.TaskBuilder) Task(nars.Task) Term(nars.term.Term)

Aggregations

TaskBuilder (nars.task.TaskBuilder)4 Term (nars.term.Term)3 Span (jcog.io.Twokenize.Span)2 NotNull (org.jetbrains.annotations.NotNull)2 Task (nars.Task)1 InvalidTaskException (nars.task.util.InvalidTaskException)1 Tense (nars.time.Tense)1 Truth (nars.truth.Truth)1