Giter VIP home page Giter VIP logo

node-semgrex's Introduction

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'));
    }
}

node-semgrex's People

Contributors

koorchik avatar

Stargazers

Vitaliy Stoliarov avatar  avatar lawlite avatar  avatar  avatar Nicolas RUFF avatar Smrutiranjan Sahu avatar Pratheek Rebala avatar Gerardo Bort avatar

Watchers

 avatar James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.