All Projects → RobMcH → CYK-Parser

RobMcH / CYK-Parser

Licence: MIT License
A CYK parser written in Python 3.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to CYK-Parser

Covfefe
A parser for nondeterministic context free languages
Stars: ✭ 49 (+104.17%)
Mutual labels:  parsing, cyk-parser
loquat
Monadic parser combinators for JavaScript / TypeScript
Stars: ✭ 47 (+95.83%)
Mutual labels:  parsing
memology
Memes - why so popular?
Stars: ✭ 32 (+33.33%)
Mutual labels:  parsing
siemstress
Very basic CLI SIEM (Security Information and Event Management system).
Stars: ✭ 24 (+0%)
Mutual labels:  parsing
attach-juxtapose-parser
Code for the paper "Strongly Incremental Constituency Parsing with Graph Neural Networks"
Stars: ✭ 25 (+4.17%)
Mutual labels:  parsing
lua-luaepnf
Extended PEG Notation Format (easy grammars for LPeg)
Stars: ✭ 21 (-12.5%)
Mutual labels:  parsing
cvscan
Your not so typical resume parser
Stars: ✭ 46 (+91.67%)
Mutual labels:  parsing
kolasu
Kotlin Language Support – AST Library
Stars: ✭ 45 (+87.5%)
Mutual labels:  parsing
php-binary
A PHP library for parsing structured binary streams.
Stars: ✭ 30 (+25%)
Mutual labels:  parsing
octet
A library that makes working with bytebuffers painless.
Stars: ✭ 79 (+229.17%)
Mutual labels:  parsing
newspaper3 usage overview
This repository provides usage examples for the Python module Newspaper3k.
Stars: ✭ 78 (+225%)
Mutual labels:  nlp-parsing
bracer
Java library for parsing and evaluating math expressions
Stars: ✭ 18 (-25%)
Mutual labels:  parsing
Ohm-S
A Squeak/Smalltalk implementation of the metaprogramming framework Ohm.
Stars: ✭ 18 (-25%)
Mutual labels:  parsing
http-accept
Parse Accept and Accept-Language HTTP headers in Ruby.
Stars: ✭ 69 (+187.5%)
Mutual labels:  parsing
VirtualBLU
A Virtual Assistant for Windows PC with wicked Qt Graphics.
Stars: ✭ 41 (+70.83%)
Mutual labels:  nlp-parsing
wrangler
Wrangler Transform: A DMD system for transforming Big Data
Stars: ✭ 63 (+162.5%)
Mutual labels:  parsing
JuCC
JuCC - Jadavpur University Compiler Compiler
Stars: ✭ 34 (+41.67%)
Mutual labels:  parsing
tangle-rs
a collection of tools to do tangle in rust
Stars: ✭ 23 (-4.17%)
Mutual labels:  parsing
statham-schema
Statham is a Python Model Parsing Library for JSON Schema.
Stars: ✭ 21 (-12.5%)
Mutual labels:  parsing
copper
An integrated context-aware scanner and parser generator
Stars: ✭ 14 (-41.67%)
Mutual labels:  parsing

CYK-Parser

This is a simple context-free grammar parser written in Python 3. It includes a converter to transform a context-free grammar to chomsky normal form. The converter can't handle epsilon productions, though. For the actual parsing the Cocke-Younger-Kasamai algorithm is used.

The code isn't by any means perfect and isn't supposed to. Feel free to use any piece of the code in your own projects.

Usage

As a standalone program

To run the parser Python 3.6 needs to be installed. The file "GrammarConverter.py" needs to be either in the same directory or within a directory, in which Python looks for modules to include. The program can be run as a module (python3 -m Parser), when in the same directory, or as a normal python script. If the parser is run as a standalone program it expects two arguments. The first one is a file containing the grammar (see below for an example) and the second one a file containing the input sentence.

python3 cyk_parser.py /home/Users/god/grammar.txt /home/Users/god/sentences/pajamas.txt

Using /home/Users/god/grammar.txt as path for the grammar and /home/Users/god/sentences/pajamas.txt as path for the sentence.

When the sentence is contained in the language produced by the grammar, the parser will output the parsed tree in a non-graphical string format.

python3 cyk_parser.py grammar.txt "I shot an elephant in my pajamas"

Using grammar.txt as path for the grammar and sentence.txt as path for the sentence.
The given sentence is contained in the language produced by the given grammar!

Possible parse(s):
[S [NP 'I'][VP [V 'shot'][NP [NP0 [Det 'an'][N 'elephant']][PP [P 'in'][NP [Det 'my'][N 'pajamas']]]]]]
[S [NP 'I'][VP [VP [V 'shot'][NP [Det 'an'][N 'elephant']]][PP [P 'in'][NP [Det 'my'][N 'pajamas']]]]]

When the sentence is not contained in the grammar, the parser won't print out a tree.

python3 cyk_parser.py some_grammar.txt sentence.txt

Using some_grammar.txt as path for the grammar and sentence.txt as path for the sentence.
The given sentence is not contained in the language produced by the given grammar!

As imported module

Alternatively the parser can be imported into other python scripts. The constructor expects a grammar which will be converted to CNF, so any CFG is fine, and some input to parse. Both of the arguments can either be file paths or just a string. Parser objects are callable and expect new input which by default just will be stored in the object and not directly parsed.

Example input

An example for a simple grammar is given below. Just paste it in a text file and run the parser with it and an input sentence file (it is assumed, that the whole input sentence is in the first line of that file) or pass it as a string to the parser.

S -> NP VP
PP -> P NP
NP -> Det N
NP -> Det N PP
NP -> 'I'
VP -> V NP
VP -> VP PP
Det -> 'an'
Det -> 'my'
N -> 'elephant'
N -> 'pajamas'
V -> 'shot'
P -> 'in'

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].