Tutorial 02

Previous Table of contents Next

Description

Tutorial02 extends the previous one by specifying a 2nd parameter to 'defineSentence', an object of type Evaluator. Evaluator is an interface with a single function evaluate(), which is called upon parsing the corresponding sentence.

For details, see also

The code


<!doctype html>
<html>
  <body>
    <!-- The HTML element that will hold the editor -->
    <div id="nls-container"></div>

    <!-- The only javascript file needed for nlScript -->
    <script src="https://cdn.jsdelivr.net/npm/@nlscript/nlscript@0.3.0/dist/umd/nlScript.js"></script>

    <!-- Load the library for the actual processing -->
    <script src="preprocessing.js"></script>

    <script>
      // Create an instance of the preprocessing backend.
      let preprocessing = new Preprocessing("output");
      
      let parser = new nlScript.Parser();
      parser.defineSentence(
        "Apply Gaussian blurring with a standard deviation of {stddev:float} pixel(s).",

        // The function specified here will be called upon parsing the sentence above
        pn => {

          // The argument given to evaluate(), a ParsedNode, can be used to
          // evaluate the value of the sentence's variables, here 'stddev'.
          // They are accessed by name.
          let stdDev = pn.evaluate("stddev");

          // Do the ctual blurring, using the processing backend.
          preprocessing.gaussianBlur(stdDev);

          // Update the output image
          preprocessing.show("output");
          return undefined;
        });
      
      new nlScript.ACEditor(parser, document.getElementById("nls-container"));
    </script>
  </body>
</html>

Demo

The result