Pattern match for NLCST.
npm install nlcst-pattern-matchPattern match for NLCST.
Install with npm:
npm install nlcst-pattern-match
patternMatcher.tag is a tagged function.
It is used with template literal.
match text with pattern that is result of tag function.
match cst with pattern that is result of tag function.
You write Pattern of NLCST object in ` patternMatcher.tag${object} .
`jsBob ${{
import { PatternMatcher } from "nlcst-pattern-match";
import { EnglishParser } from "nlcst-parse-english";
const englishParser = new EnglishParser();
const patternMatcher = new PatternMatcher({
parser: englishParser
});
const pattern = patternMatcher.tag
type: "*",
data: {
pos: /^VB/ // verb
}
}} it.;\n${JSON.stringify(result.nodeList)}\n
const text = "Bob does it.";
const results = patternMatcher.match(text, pattern);
assert.equal(results.length, 1, "results should have 1");
const [result] = results;
assert.deepEqual(result.position, {
index: 0,
end: {
column: 13,
line: 1,
offset: 12
},
start: {
column: 1,
line: 1,
offset: 0
}
});
// https://github.com/syntax-tree/nlcst NodeList
assert.deepEqual(
result.nodeList,
[
{
type: "WordNode",
children: [
{
type: "TextNode",
value: "Bob",
position: {
start: { line: 1, column: 1, offset: 0 },
end: { line: 1, column: 4, offset: 3 }
}
}
],
position: {
start: { line: 1, column: 1, offset: 0 },
end: { line: 1, column: 4, offset: 3 }
},
data: { pos: "NNP" }
},
{
type: "WhiteSpaceNode",
value: " ",
position: {
start: { line: 1, column: 4, offset: 3 },
end: { line: 1, column: 5, offset: 4 }
}
},
{
type: "WordNode",
children: [
{
type: "TextNode",
value: "does",
position: {
start: { line: 1, column: 5, offset: 4 },
end: { line: 1, column: 9, offset: 8 }
}
}
],
position: {
start: { line: 1, column: 5, offset: 4 },
end: { line: 1, column: 9, offset: 8 }
},
data: { pos: "VBZ" }
},
{
type: "WhiteSpaceNode",
value: " ",
position: {
start: { line: 1, column: 9, offset: 8 },
end: { line: 1, column: 10, offset: 9 }
}
},
{
type: "WordNode",
children: [
{
type: "TextNode",
value: "it",
position: {
start: { line: 1, column: 10, offset: 9 },
end: { line: 1, column: 12, offset: 11 }
}
}
],
position: {
start: { line: 1, column: 10, offset: 9 },
end: { line: 1, column: 12, offset: 11 }
},
data: { pos: "PRP" }
},
{
type: "PunctuationNode",
value: ".",
position: {
start: { line: 1, column: 12, offset: 11 },
end: { line: 1, column: 13, offset: 12 }
},
data: { pos: "." }
}
],
`
);
assert.strictEqual(result.text, "Bob does it.");
* value is match all value.
`tsThese are ${{
const englishParser = new EnglishParser();
const patternMatcher = new PatternMatcher({
parser: englishParser
});
const pattern = patternMatcher.tag
type: "*", // <= any type is ok
data: {
pos: /^NN/
}
}}.;`
const text = "These are cars. Cool!";
const results = patternMatcher.match(text, pattern);
assert.ok(results.length === 1, "should have 1 result");
You can use NLCST for matching pattern.
Additionally, you can use following special Nodes.
#### PatternNode
PatternNode represent RegExp pattern.
`jsif you want to ${{
const englishParser = new EnglishParser();
const patternMatcher = new PatternMatcher({
parser: englishParser
});
const pattern = patternMatcher.tag
type: "PatternNode",
pattern: /[\w\s]+/
}}.;`
const text = "Click Delete if you want to delete the entire document.";
const results = patternMatcher.match(text, pattern);
See Releases page.
Install devDependencies and Run npm test:
npm i -d && npm test
Pull requests and stars are always welcome.
For bugs and feature requests, please create an issue.
1. Fork it!
2. Create your feature branch: git checkout -b my-new-featuregit commit -am 'Add some feature'
3. Commit your changes: git push origin my-new-feature`
4. Push to the branch:
5. Submit a pull request :D
MIT © azu