npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

semgrex

v2.0.0

Published

NodeJs wrapper for Stanford NLP Semgrex

Downloads

18

Readme

NodeJs wrapper for Stanford NLP Semgrex

Build Status npm version Known Vulnerabilities

What is Semgrex?

Semgrex is a utility for matching patterns in dependency trees, based on tree relationships and regular expression matches on nodes.

Semgrex is part of StafordNLP which is written in Java. So, this module uses Java Native Interface(JNI). You will need JRE to run this module.

This modules fully supports "Semgrex Matcher API".

Documentation

How to convert sentence to Dependecy Tree?

You should use syntax parser for your language. You can start with https://cloud.google.com/natural-language/ for parsing sentences.

Syntax details

  1. Use 'idx' field to match node by 'id'.
  2. Use 'tag' field to match node by 'upostag'.
  3. Use 'word' field to match node by 'form'.
  4. Use 'lemma' field to match node by 'lemma'.
const matcher = new SemgrexMatcher({
    nodes,
    pattern: '{tag:NOUN} <<rcmod {lemma:do}'
});

Complex example

For more examples see "examples" foler

const { SemgrexMatcher } = require('semgrex');

// "Kiev, which is one of the largest cities in Europe, is the capital of Ukraine." as a dependency tree nodes

const nodes = [
    {
        id: 1,
        form: 'Kiev',
        lemma: 'Kiev',
        upostag: 'NOUN',
        xpostag: '_',
        feat: '_',
        head: 13,
        deprel: 'nsubj',
        deps: '_',
        misc: '_'
    },
    {
        id: 2,
        form: ',',
        lemma: ',',
        upostag: 'PUNCT',
        xpostag: '_',
        feat: '_',
        head: 1,
        deprel: 'p',
        deps: '_',
        misc: '_'
    },
    {
        id: 3,
        form: 'which',
        lemma: 'which',
        upostag: 'DET',
        xpostag: '_',
        feat: '_',
        head: 4,
        deprel: 'nsubj',
        deps: '_',
        misc: '_'
    },
    {
        id: 4,
        form: 'is',
        lemma: 'be',
        upostag: 'VERB',
        xpostag: '_',
        feat: '_',
        head: 1,
        deprel: 'rcmod',
        deps: '_',
        misc: '_'
    },
    {
        id: 5,
        form: 'one',
        lemma: 'one',
        upostag: 'NUM',
        xpostag: '_',
        feat: '_',
        head: 4,
        deprel: 'attr',
        deps: '_',
        misc: '_'
    },
    {
        id: 6,
        form: 'of',
        lemma: 'of',
        upostag: 'ADP',
        xpostag: '_',
        feat: '_',
        head: 5,
        deprel: 'prep',
        deps: '_',
        misc: '_'
    },
    {
        id: 7,
        form: 'the',
        lemma: 'the',
        upostag: 'DET',
        xpostag: '_',
        feat: '_',
        head: 9,
        deprel: 'det',
        deps: '_',
        misc: '_'
    },
    {
        id: 8,
        form: 'largest',
        lemma: 'large',
        upostag: 'ADJ',
        xpostag: '_',
        feat: '_',
        head: 9,
        deprel: 'amod',
        deps: '_',
        misc: '_'
    },
    {
        id: 9,
        form: 'cities',
        lemma: 'city',
        upostag: 'NOUN',
        xpostag: '_',
        feat: '_',
        head: 6,
        deprel: 'pobj',
        deps: '_',
        misc: '_'
    },
    {
        id: 10,
        form: 'in',
        lemma: 'in',
        upostag: 'ADP',
        xpostag: '_',
        feat: '_',
        head: 9,
        deprel: 'prep',
        deps: '_',
        misc: '_'
    },
    {
        id: 11,
        form: 'Europe',
        lemma: 'Europe',
        upostag: 'NOUN',
        xpostag: '_',
        feat: '_',
        head: 10,
        deprel: 'pobj',
        deps: '_',
        misc: '_'
    },
    {
        id: 12,
        form: ',',
        lemma: ',',
        upostag: 'PUNCT',
        xpostag: '_',
        feat: '_',
        head: 1,
        deprel: 'p',
        deps: '_',
        misc: '_'
    },
    {
        id: 13,
        form: 'is',
        lemma: 'be',
        upostag: 'VERB',
        xpostag: '_',
        feat: '_',
        head: 0,
        deprel: 'root',
        deps: '_',
        misc: '_'
    },
    {
        id: 14,
        form: 'the',
        lemma: 'the',
        upostag: 'DET',
        xpostag: '_',
        feat: '_',
        head: 15,
        deprel: 'det',
        deps: '_',
        misc: '_'
    },
    {
        id: 15,
        form: 'capital',
        lemma: 'capital',
        upostag: 'NOUN',
        xpostag: '_',
        feat: '_',
        head: 13,
        deprel: 'attr',
        deps: '_',
        misc: '_'
    },
    {
        id: 16,
        form: 'of',
        lemma: 'of',
        upostag: 'ADP',
        xpostag: '_',
        feat: '_',
        head: 15,
        deprel: 'prep',
        deps: '_',
        misc: '_'
    },
    {
        id: 17,
        form: 'Ukraine',
        lemma: 'Ukraine',
        upostag: 'NOUN',
        xpostag: '_',
        feat: '_',
        head: 16,
        deprel: 'pobj',
        deps: '_',
        misc: '_'
    },
    {
        id: 18,
        form: '.',
        lemma: '.',
        upostag: 'PUNCT',
        xpostag: '_',
        feat: '_',
        head: 13,
        deprel: 'p',
        deps: '_',
        misc: '_'
    }
];

main()
    .then(() => {
        process.exit();
    })
    .catch(error => {
        console.log(error);
    });

async function main() {
    const matcher = new SemgrexMatcher({
        nodes,
        pattern: '{}=A <rcmod=rel {}=B'
    });

    while (await matcher.find()) {
        console.log('MATCH=', await matcher.getMatch());
        console.log('NODE A=', await matcher.getNode('A'));
        console.log('NODE B=', await matcher.getNode('B'));

        console.log('NODE NAMES', await matcher.getNodeNames());
        console.log('RELS NAMES', await matcher.getRelationNames());
        console.log('REL STRING', await matcher.getRelnString('rel'));
    }
}