nlcst-search

2018-11-13 admin

nlcst-search是什么

什么是nlcst-search,Search for patterns in an NLCST tree

nlcst-search使用教程帮助文档

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.

Throws

Error — When not given node or patterns.

Parameters
node

Tree to search in (Node).

patterns

Patterns to search for (Array.<string> or Object). If an Object, uses its keys as patterns. 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. Instead of a word, it’s also possible to use a wildcard symbol (*, an asterisk), that matches any word in a pattern (alpha * charlie).

handler

Handler invoked when a match is found (Function).

allowApostrophes

Treated as options.allowApostrophes.

options.allowApostrophes

Passed to nlcst-normalize (boolean, default: false).

options.allowDashes

Passed to nlcst-normalize (boolean, default: false).

options.allowLiterals

Include literal phrases (boolean, default: false).

function handler(nodes, index, parent, pattern)

Handler invoked when a match is found.

Parameters
nodes

List of siblings which match pattern (Array.<Node>).

index

Position at which the match starts in parent (number).

parent

Parent node of nodes (Node).

pattern

The matched pattern (string).

Contribute

See contributing.md in syntax-tree/nlcst for ways to get started.

This organisation has a Code of Conduct. By interacting with this repository, organisation, or community you agree to abide by its terms.

License

MIT © Titus Wormer

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处。

转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]

本文地址:https://www.javascriptcn.com/read-45328.html

文章标题:nlcst-search

回到顶部