nlcst Literal Node Checker

3.0.0 · active · verified Sun Apr 19

nlcst-is-literal is a focused utility within the `unified` ecosystem, designed to determine if a `WordNode` within an NLCST (Natural Language Concrete Syntax Tree) parent node is semantically considered a "literal." This check typically involves analyzing surrounding punctuation, such as quotes, parentheses, or dashes, that enclose the word. The package is currently stable at version `3.0.0` and follows the `unified` collective's release cadence, typically aligning major versions with Node.js LTS updates and significant architectural shifts like the transition to ESM. It's primarily used by tools that process natural language, such as spell-checkers or linting tools, to correctly identify and potentially exclude words that are meant as examples or explicitly quoted text rather than standard vocabulary, rather than actual words to be evaluated.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to parse a text, visit its word nodes, and use `isLiteral` to identify and log words that are enclosed by delimiters such as quotes, parentheses, or dashes.

import { read } from 'to-vfile';
import { ParseEnglish } from 'parse-english';
import { visit } from 'unist-util-visit';
import { toString } from 'nlcst-to-string';
import { isLiteral } from 'nlcst-is-literal';

async function processDocument() {
  const exampleText = `
    The word “foo” is meant as a literal.
    The word «bar» is meant as a literal.
    The word (baz) is meant as a literal.
    The word, qux, is meant as a literal.
    The word — quux — is meant as a literal.
    This sentence includes a regular word.
  `;

  // Create a mock vfile for the example text
  const file = {
    path: 'example.txt',
    contents: exampleText,
    toString() { return this.contents; }
  };

  const tree = new ParseEnglish().parse(String(file));

  console.log('Literal words identified:');
  visit(tree, 'WordNode', function (node, index, parent) {
    if (isLiteral(parent, index)) {
      console.log(`- ${toString(node)}`);
    }
  });
}

processDocument().catch(console.error);

view raw JSON →