Skip to content

syntax-tree/nlcst-search

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

nlcst-search Build Status Coverage Status

Search for patterns in an NLCST tree.

Installation

npm:

npm install nlcst-search

Usage

var search = require('nlcst-search');
var toString = require('nlcst-to-string');

var tree = {
  type: 'SentenceNode',
  children: [
    {
      type: 'WordNode',
      children: [
        {type: 'TextNode', value: 'Don'},
        {type: 'PunctuationNode', value: '’'},
        {type: 'TextNode', value: 't'}
      ]
    },
    {type: 'WhiteSpaceNode', value: ' '},
    {
      type: 'WordNode',
      children: [
        {type: 'TextNode', value: 'do'}
      ]
    },
    {type: 'WhiteSpaceNode', value: ' '},
    {
      type: 'WordNode',
      children: [
        {type: 'TextNode', value: 'Block'},
        {type: 'PunctuationNode', value: '-'},
        {type: 'TextNode', value: 'level'}
      ]
    }
  ]
};


search(tree, ['dont'], function (nodes) {
  console.log(toString(nodes));
});
// Don’t

search(tree, ['do blocklevel'], function (nodes) {
  console.log(toString(nodes));
});
// do Block-level

API

search(node, patterns, handler[, allowApostrophes|options])

Search for patterns in an NLCST tree.

Parameters
  • node (Node) — Tree to search in
  • patterns (Array.<string> or Object) — Patterns to search for. If an Object, uses its keys. Each pattern is a space-delimited list of words, where each word is normalized to remove casing, apostrophes, and dashes. Spaces in a pattern mean zero or more white space nodes in the tree
  • handler (Function) — Handler invoked when a match is found
  • allowApostrophes (boolean, default: false) — Configuration for nlcst-normalize)
  • options (Object) — Configuration:
    • allowApostrophes (boolean, default: false) — Configuration for nlcst-normalize)
    • allowDashes (boolean, default: false) — Configuration for nlcst-normalize)
    • allowLiterals (boolean, default: false) — Include literal phrases
Throws

Error — When not given node or patterns.

function handler(nodes, index, parent, pattern)

Handler invoked when a match is found.

Parameters
  • nodes (Array.<Node>) — List of siblings which match pattern
  • index (number) — Position at which the match starts in parent
  • parent (Node) — Parent node of nodes
  • pattern (string) — The matched pattern

License

MIT © Titus Wormer

About

utility to search for patterns in an nlcst tree

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Contributors 3

  •  
  •  
  •