NLCST to String Converter

4.0.0 · active · verified Sun Apr 19

nlcst-to-string is a utility designed to serialize NLCST (Natural Language Concrete Syntax Tree) nodes into their plain-text string representation. It is part of the unified ecosystem, focusing specifically on text content extraction from linguistic ASTs. The current stable version is 4.0.0. Releases follow the unified collective's compatibility policy, typically dropping support for unmaintained Node.js versions with new major releases (e.g., v4 requires Node.js 16+). Key differentiators include its tight integration with the NLCST specification, its minimal API (a single `toString` function), and its robust handling of various NLCST node types by prioritizing plain-text fields or recursively serializing children. This package is an ESM-only module since version 3.0.0 and ships with full TypeScript type definitions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to import and use the `toString` function to convert an NLCST node or a list of nodes into a plain-text string, showing its handling of different node types.

import { toString } from 'nlcst-to-string';
import type { Parent, Text } from 'nlcst';

const exampleNode: Parent = {
  type: 'SentenceNode',
  children: [
    { type: 'WordNode', children: [{ type: 'TextNode', value: 'Hello' }] },
    { type: 'PunctuationNode', value: ',' },
    { type: 'SpaceNode', value: ' ' },
    { type: 'WordNode', children: [{ type: 'TextNode', value: 'world' }] },
    { type: 'PunctuationNode', value: '!' }
  ]
};

const complexWordNode: Parent = {
  type: 'WordNode',
  children: [
    { type: 'TextNode', value: 'AT' },
    { type: 'SymbolNode', value: '&' },
    { type: 'TextNode', value: 'T' }
  ]
};

console.log(toString(exampleNode));
console.log(toString(complexWordNode));
// Expected output:
// Hello, world!
// AT&T

view raw JSON →