Giter Site home page Giter Site logo

youyou22222 / cky-parser-for-context-free-grammar Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 22 KB

A CKY Parsing Algorithm for Context Free Grammar. Specifically, the Context Free Grammar is first to be converted to Chomsky Norm Form and then Apply CKY Algotrithm to the input sentence. The result is synstactic tree structure of the input sentence.

C++ 97.82% Makefile 2.18%
nlp syntactic-parser cky-parser cky

cky-parser-for-context-free-grammar's Introduction

CKY-Parsing-for-Context-Free-Grammar

A CKY Parsing Algorithm for Context Free Grammar. Specifically, the Context Free Grammar is first to be converted to Chomsky Norm Form and then Apply CKY Algotrithm to the input sentence. The result is synstactic tree structure of the input sentence.

Examples:

#include "cyk.h"
#include"chomsky_norm_form.h"
#include <iostream>

int main(){
       std::string cfg_s = "S -> NP VP | Aux NP VP | VP\n"
                         "NP -> Pronoun | Proper-Noun | Det Nominal\n" 
                         "Nominal -> Noun | Nominal apple Noun book | Nominal PP\n" 
                         "VP -> Verb | Verb finsh NP | Verb NP PP | Verb PP | VP PP\n" 
                         "PP -> Preposition NP\n" 
                         "Det -> that | this | a | the\n" 
                         "Noun -> book | flight | meal | money\n" 
                         "Verb -> book | include | prefer\n" 
                         "Pronoun -> I | she | me\n" 
                         "Proper-Noun -> Houston | TWA\n" 
                         "Aux -> does\n" 
                         "Preposition -> from | to | on | near | through"; 
        
        //Buld context free grammar from strings
        Grammar grammar(cfg_s);
        // convert context free grammar to Chomsky Norm Form
        grammar.convert_to_cnf();
        auto cnf = grammar.get_cnf();

        RuleVector rules;
        for(auto it = cnf.begin(); it != cnf.end(); ++it){

            if(it ->symbols.size() == 2){

                rules.push_back(new Rule(it ->head,it ->symbols[0], it->symbols[1]));
            }else{
                rules.push_back(new Rule(it ->head,it ->symbols[0]));
            }
        }
        
        // input sentence "book the flight through Houston"
        StringVector sents {"book", "the", "flight", "through", "Houston"};
        //apply cyk algorithm
        std::vector<BTreeNode*> parses = cyk(sents, rules);
        // print
        for(auto tree : parses){
                std::cout << tree->string_repr() << std::endl;
        }

        //remember to destroy the rules
        for(auto rule : rules){
            if(rule)
                delete rule;
        }
        return 0;

For this test example:

  1. Compile: make
  2. Excute: ./main
  3. Out: ('S', '('VERB_NP', '('Verb', ''book'')', '('NP', '('Det', ''the'')', '('Nominal', ''flight'')')')', '('PP', '('Preposition', ''through'')', '('NP', ''Houston'')')')

cky-parser-for-context-free-grammar's People

Contributors

youyou22222 avatar

Watchers

 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.